You are here

public function ConsumerClientIdTest::testValidClientId in farmOS 2.x

Test a valid Password grant using the consumer.client_id field.

File

modules/core/api/tests/src/Functional/ConsumerClientIdTest.php, line 17

Class

ConsumerClientIdTest
Tests using the consumer.client_id field.

Namespace

Drupal\Tests\farm_api\Functional

Code

public function testValidClientId() {

  // 1. Test the valid request using a UUID.
  // Using the consumer.client_id instead of UUID should be optional.
  $valid_payload = [
    'grant_type' => 'password',
    'client_id' => $this->client
      ->uuid(),
    'username' => $this->user
      ->getAccountName(),
    'password' => $this->user->pass_raw,
    'scope' => $this->scope,
  ];
  $response = $this
    ->post($this->url, $valid_payload);
  $this
    ->assertValidTokenResponse($response, TRUE);

  // Repeat the request but pass an obtained access token as a header in
  // order to check the authentication in parallel, which will precede
  // the creation of a new token.
  $parsed = Json::decode((string) $response
    ->getBody());
  $response = $this
    ->post($this->url, $valid_payload, [
    'headers' => [
      'Authorization' => 'Bearer ' . $parsed['access_token'],
    ],
  ]);
  $this
    ->assertValidTokenResponse($response, TRUE);

  // 2. Test the valid request using the consumer.client_id field.
  $payload_client_id = $valid_payload;
  $payload_client_id['client_id'] = $this->client
    ->get('client_id')->value;
  $response = $this
    ->post($this->url, $payload_client_id);
  $this
    ->assertValidTokenResponse($response, TRUE);

  // 3. Test the valid request without scopes.
  $payload_no_scope = $valid_payload;
  unset($payload_no_scope['scope']);
  $response = $this
    ->post($this->url, $payload_no_scope);
  $this
    ->assertValidTokenResponse($response, TRUE);

  // 4. Test valid request using HTTP Basic Auth.
  $payload_no_client = $valid_payload;
  unset($payload_no_client['client_id']);
  $response = $this
    ->post($this->url, $payload_no_scope, [
    'auth' => [
      $this->client
        ->get('client_id')->value,
      '',
    ],
  ]);
  $this
    ->assertValidTokenResponse($response, TRUE);
}