You are here

public function ClientCredentialsFunctionalTest::testMissingClientCredentialsGrant in Simple OAuth (OAuth2) & OpenID Connect 8.4

Same name and namespace in other branches
  1. 5.x tests/src/Functional/ClientCredentialsFunctionalTest.php \Drupal\Tests\simple_oauth\Functional\ClientCredentialsFunctionalTest::testMissingClientCredentialsGrant()

Test invalid ClientCredentials grant.

File

tests/src/Functional/ClientCredentialsFunctionalTest.php, line 39

Class

ClientCredentialsFunctionalTest
The client credentials test.

Namespace

Drupal\Tests\simple_oauth\Functional

Code

public function testMissingClientCredentialsGrant() {
  $valid_payload = [
    'grant_type' => 'client_credentials',
    'client_id' => $this->client
      ->uuid(),
    'client_secret' => $this->clientSecret,
    'scope' => $this->scope,
  ];
  $data = [
    'grant_type' => [
      'error' => 'invalid_grant',
      'code' => 400,
    ],
    'client_id' => [
      'error' => 'invalid_request',
      'code' => 400,
    ],
    'client_secret' => [
      'error' => 'invalid_client',
      'code' => 401,
    ],
  ];
  foreach ($data as $key => $value) {
    $invalid_payload = $valid_payload;
    unset($invalid_payload[$key]);
    $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));
  }
}