View source
<?php
namespace Drupal\Tests\simple_oauth\Functional;
use Drupal\Core\Url;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class ImplicitFunctionalTest extends TokenBearerFunctionalTestBase {
protected $authorizeUrl;
protected $redirectUri;
public static $modules = [
'simple_oauth_test',
];
protected function setUp() {
parent::setUp();
$this->redirectUri = Url::fromRoute('oauth2_token.test_token', [], [
'absolute' => TRUE,
])
->toString();
$this->client
->set('redirect', $this->redirectUri);
$this->client
->save();
$this->authorizeUrl = Url::fromRoute('oauth2_token.authorize');
$this
->grantPermissions(Role::load(RoleInterface::AUTHENTICATED_ID), [
'grant simple_oauth codes',
]);
}
public function testImplicitGrant() {
$valid_params = [
'response_type' => 'token',
'client_id' => $this->client
->uuid(),
'client_secret' => $this->clientSecret,
];
$this
->drupalGet($this->authorizeUrl
->toString(), [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->buttonExists('Log in');
$this
->drupalLogin($this->user);
$this
->drupalGet($this->authorizeUrl
->toString(), [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->statusCodeEquals(500);
$this
->config('simple_oauth.settings')
->set('use_implicit', TRUE)
->save();
$this
->drupalGet($this->authorizeUrl
->toString(), [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->statusCodeEquals(200);
$assert_session
->titleEquals('Grant Access to Client | Drupal');
$assert_session
->buttonExists('Grant');
$assert_session
->responseContains('Permissions');
$this
->drupalPostForm($this->authorizeUrl, [], 'Grant', [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->statusCodeEquals(200);
$assert_session
->addressMatches('/\\/oauth\\/test#access_token=.*&token_type=Bearer&expires_in=\\d*/');
}
public function testValidClientImplicitGrant() {
$this->client
->set('third_party', FALSE);
$this->client
->save();
$valid_params = [
'response_type' => 'token',
'client_id' => $this->client
->uuid(),
'client_secret' => $this->clientSecret,
];
$this
->drupalGet($this->authorizeUrl
->toString(), [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->buttonExists('Log in');
$this
->drupalLogin($this->user);
$this
->drupalGet($this->authorizeUrl
->toString(), [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->responseContains('Fatal error. Unable to get the authorization server.');
$this
->config('simple_oauth.settings')
->set('use_implicit', TRUE)
->save();
$this
->drupalGet($this->authorizeUrl
->toString(), [
'query' => $valid_params,
]);
$assert_session = $this
->assertSession();
$assert_session
->statusCodeEquals(200);
$assert_session
->addressMatches('/\\/oauth\\/test#access_token=.*&token_type=Bearer&expires_in=\\d*/');
}
}