You are here

public function OAuth2ServerTestCase::testScopes in OAuth2 Server 7

Tests scopes.

File

tests/oauth2_server.test, line 372
OAuth2 tests.

Class

OAuth2ServerTestCase
Test basic API.

Code

public function testScopes() {

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

  // A non-existent scope.
  $result = $this
    ->passwordGrantRequest('invalid_scope');
  $response = json_decode($result->data);
  $error = isset($response->error) && $response->error == 'invalid_scope';
  $this
    ->assertTrue($error, 'Invalid scope correctly detected.');

  // A scope forbidden by oauth2_server_scope_access.
  // @see oauth2_server_test_entity_query_alter()
  $result = $this
    ->passwordGrantRequest('forbidden');
  $response = json_decode($result->data);
  $error = isset($response->error) && $response->error == 'invalid_scope';
  $this
    ->assertTrue($error, 'Inaccessible scope correctly detected.');

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