You are here

public function OAuth2ServerTest::testScopes in OAuth2 Server 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Functional/OAuth2ServerTest.php \Drupal\Tests\oauth2_server\Functional\OAuth2ServerTest::testScopes()

Tests scopes.

File

tests/src/Functional/OAuth2ServerTest.php, line 334

Class

OAuth2ServerTest
The OAuth2 Server admin test case.

Namespace

Drupal\Tests\oauth2_server\Functional

Code

public function testScopes() {

  // The default scope returned by oauth2_server_default_scope().
  $response = $this
    ->passwordGrantRequest();
  $payload = json_decode($response
    ->getBody());
  $this
    ->assertEqual($payload->scope, 'admin basic', 'The correct default scope was returned.');

  // A non-existent scope.
  try {
    $this
      ->passwordGrantRequest('invalid_scope');
  } catch (ClientException $e) {
    if ($e
      ->hasResponse()) {
      $this
        ->assertEqual($e
        ->getResponse()
        ->getStatusCode(), 400, 'Invalid scope correctly detected.');
    }
  }

  // A scope forbidden by oauth2_server_scope_access.
  // @see oauth2_server_test_entity_query_alter()
  try {
    $this
      ->passwordGrantRequest('forbidden');
  } catch (ClientException $e) {
    if ($e
      ->hasResponse()) {
      $this
        ->assertEqual($e
        ->getResponse()
        ->getStatusCode(), 400, 'Inaccessible scope correctly detected.');
    }
  }

  // A specific requested scope.
  $response = $this
    ->passwordGrantRequest('admin');
  $payload = json_decode($response
    ->getBody());
  $this
    ->assertEqual($payload->scope, 'admin', 'The correct scope was returned.');
}