You are here

public function PasswordFunctionalTest::testInvalidPasswordGrant in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/PasswordFunctionalTest.php \Drupal\Tests\simple_oauth\Functional\PasswordFunctionalTest::testInvalidPasswordGrant()
  2. 8.3 tests/src/Functional/PasswordFunctionalTest.php \Drupal\Tests\simple_oauth\Functional\PasswordFunctionalTest::testInvalidPasswordGrant()
  3. 5.x tests/src/Functional/PasswordFunctionalTest.php \Drupal\Tests\simple_oauth\Functional\PasswordFunctionalTest::testInvalidPasswordGrant()

Test invalid Password grant.

File

tests/src/Functional/PasswordFunctionalTest.php, line 110

Class

PasswordFunctionalTest
@group simple_oauth

Namespace

Drupal\Tests\simple_oauth\Functional

Code

public function testInvalidPasswordGrant() {
  $valid_payload = [
    'grant_type' => 'password',
    'client_id' => $this->client
      ->uuid(),
    'client_secret' => $this->clientSecret,
    'username' => $this->user
      ->getAccountName(),
    'password' => $this->user->pass_raw,
    'scope' => $this->scope,
  ];
  $data = [
    'grant_type' => [
      'error' => 'invalid_grant',
      'code' => 400,
    ],
    'client_id' => [
      'error' => 'invalid_client',
      'code' => 401,
    ],
    'client_secret' => [
      'error' => 'invalid_client',
      'code' => 401,
    ],
    'username' => [
      'error' => 'invalid_credentials',
      'code' => 401,
    ],
    'password' => [
      'error' => 'invalid_credentials',
      'code' => 401,
    ],
  ];
  foreach ($data as $key => $value) {
    $invalid_payload = $valid_payload;
    $invalid_payload[$key] = $this
      ->getRandomGenerator()
      ->string();
    $response = $this
      ->post($this->url, $invalid_payload);
    $parsed_response = Json::decode((string) $response
      ->getBody());
    $this
      ->assertSame($value['error'], $parsed_response['error'], sprintf('Correct error code %s for %s.', $value['error'], $key));
    $this
      ->assertSame($value['code'], $response
      ->getStatusCode(), sprintf('Correct status code %d for %s.', $value['code'], $key));
  }
}