You are here

public function AuthCodeFunctionalTest::testAuthCodeGrant in Simple OAuth (OAuth2) & OpenID Connect 5.x

Same name and namespace in other branches
  1. 8.4 tests/src/Functional/AuthCodeFunctionalTest.php \Drupal\Tests\simple_oauth\Functional\AuthCodeFunctionalTest::testAuthCodeGrant()

Test the valid AuthCode grant.

File

tests/src/Functional/AuthCodeFunctionalTest.php, line 85

Class

AuthCodeFunctionalTest
The auth code test.

Namespace

Drupal\Tests\simple_oauth\Functional

Code

public function testAuthCodeGrant() {
  $valid_params = [
    'response_type' => 'code',
    'client_id' => $this->client
      ->uuid(),
    'client_secret' => $this->clientSecret,
    'redirect_uri' => Url::fromRoute('oauth2_token.test_token', [], [
      'absolute' => TRUE,
    ])
      ->toString(),
  ];

  // 1. Anonymous request invites the user to log in.
  $this
    ->drupalGet($this->authorizeUrl
    ->toString(), [
    'query' => $valid_params,
  ]);
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->buttonExists('Log in');

  // 2. Log the user in and try again.
  $this
    ->drupalLogin($this->user);
  $this
    ->drupalGet($this->authorizeUrl
    ->toString(), [
    'query' => $valid_params,
  ]);
  $this
    ->assertGrantForm();

  // 3. Grant access by submitting the form and get the token back.
  $this
    ->drupalPostForm($this->authorizeUrl, [], 'Grant', [
    'query' => $valid_params,
  ]);

  // Store the code for the second part of the flow.
  $code = $this
    ->getAndValidateCodeFromResponse();

  // 4. Send the code to get the access token.
  $response = $this
    ->postGrantedCodeWithScopes($code, $this->scope);
  $this
    ->assertValidTokenResponse($response, TRUE);
}