You are here

public function OauthPasswordTest::testInvalidPasswordGrant in farmOS 2.x

Test an invalid Password grant.

File

modules/core/login/tests/src/Functional/OauthPasswordTest.php, line 59

Class

OauthPasswordTest
Tests using an email with OAuth Password Grant.

Namespace

Druapl\tests\farm_login\Functional

Code

public function testInvalidPasswordGrant() {
  $valid_payload = [
    'grant_type' => 'password',
    'client_id' => $this->client
      ->get('client_id')->value,
    'client_secret' => $this->clientSecret,
    'username' => $this->user
      ->getAccountName(),
    'password' => $this->user->pass_raw,
    'scope' => $this->scope,
  ];

  // 1. Test the password grant with an invalid username.
  $invalid_payload = $valid_payload;
  $invalid_payload['username'] = $this
    ->getRandomGenerator()
    ->string();
  $response = $this
    ->post($this->url, $invalid_payload);
  $parsed_response = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame('invalid_credentials', $parsed_response['error']);
  $this
    ->assertSame(401, $response
    ->getStatusCode());

  // 2. Test the password grant with an invalid password.
  $invalid_payload = $valid_payload;
  $invalid_payload['password'] = $this
    ->getRandomGenerator()
    ->string();
  $response = $this
    ->post($this->url, $invalid_payload);
  $parsed_response = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame('invalid_credentials', $parsed_response['error']);
  $this
    ->assertSame(401, $response
    ->getStatusCode());
}