You are here

public function OAuth2ServerTest::testImplicitFlow in OAuth2 Server 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/OAuth2ServerTest.php \Drupal\Tests\oauth2_server\Functional\OAuth2ServerTest::testImplicitFlow()

Tests the implicit flow.

File

tests/src/Functional/OAuth2ServerTest.php, line 201

Class

OAuth2ServerTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

public function testImplicitFlow() {
  $user = $this
    ->drupalCreateUser([
    'use oauth2 server',
  ]);
  $this
    ->drupalLogin($user);
  $response = $this
    ->authorizationCodeRequest('token');
  $this
    ->assertEqual($response
    ->getStatusCode(), 302, 'The implicit flow request completed successfully');
  $parameters = $this
    ->getRedirectParams($response, '#');
  $this
    ->assertTokenResponse($parameters, FALSE);

  // We have received an access token. Verify it.
  // See http://drupal.org/node/1958718.
  if (!empty($parameters['access_token'])) {
    $verification_url = $this
      ->buildUrl(new Url('oauth2_server.tokens', [
      'oauth2_server_token' => $parameters['access_token'],
    ]));
    $response = $this
      ->httpGetRequest($verification_url);
    $verification_response = json_decode($response
      ->getBody());
    $this
      ->assertEqual($response
      ->getStatusCode(), 200, 'The provided access token was successfully verified.');
    $this
      ->verbose($verification_response->scope);
    $this
      ->verbose(urldecode($parameters['scope']));
    $this
      ->assertEqual($verification_response->scope, urldecode($parameters['scope']), 'The provided scope matches the scope of the verified access token.');
  }
}