You are here

public function OAuth2ServerStorageTestCase::testAuthorizationCode in OAuth2 Server 7

File

tests/oauth2_server.test, line 1034
OAuth2 tests.

Class

OAuth2ServerStorageTestCase
Test \Drupal\oauth2_server\Storage.

Code

public function testAuthorizationCode() {
  $user = $this
    ->drupalCreateUser(array(
    'use oauth2 server',
  ));
  $code = $this->storage
    ->getAuthorizationCode('newcode');
  $this
    ->assertFalse($code, 'Trying to load a nonexistent authorization code is unsuccessful.');
  $expires = time() + 20;
  $success = $this->storage
    ->setAuthorizationCode('newcode', $this->client_key, $user->uid, 'http://example.com', $expires);
  $this
    ->assertTrue($success, 'A new authorization code was successfully created.');

  // Verify the return format of getAuthorizationCode().
  $code = $this->storage
    ->getAuthorizationCode('newcode');
  $this
    ->assertTrue($code, 'An authorization code was successfully returned.');
  $this
    ->assertTrue(array_key_exists('authorization_code', $code), 'The "authorization_code" value is present in the code array.');
  $this
    ->assertTrue(array_key_exists('client_id', $code), 'The "client_id" value is present in the code array.');
  $this
    ->assertTrue(array_key_exists('user_id', $code), 'The "user_id" value is present in the code array.');
  $this
    ->assertTrue(array_key_exists('redirect_uri', $code), 'The "redirect_uri" value is present in the code array.');
  $this
    ->assertTrue(array_key_exists('expires', $code), 'The "expires" value is present in the code array.');
  $this
    ->assertEqual($code['authorization_code'], 'newcode', 'The "authorization_code" key has the expected value.');
  $this
    ->assertEqual($code['client_id'], $this->client_key, 'The "client_id" key has the expected value.');
  $this
    ->assertEqual($code['user_id'], $user->uid, 'The "user_id" key has the expected value.');
  $this
    ->assertEqual($code['redirect_uri'], 'http://example.com', 'The "redirect_uri" key has the expected value.');
  $this
    ->assertEqual($code['expires'], $expires, 'The "expires" key has the expected value.');

  // Change an existing code
  $expires = time() + 42;
  $success = $this->storage
    ->setAuthorizationCode('newcode', $this->client_key, $user->uid, 'http://example.org', $expires);
  $this
    ->assertTrue($success, 'The authorization code was successfully updated.');
  $code = $this->storage
    ->getAuthorizationCode('newcode');
  $this
    ->assertTrue($code, 'An authorization code was successfully returned.');
  $this
    ->assertEqual($code['expires'], $expires, 'The expires timestamp matches the new value.');
}