View source
<?php
namespace Drupal\Tests\oauth2_server\Functional;
use Drupal\Tests\BrowserTestBase;
class OAuth2ServerStorageTest extends BrowserTestBase {
protected $defaultTheme = 'stable';
public static $modules = [
'oauth2_server',
];
protected $clientId = 'test_client';
protected $clientSecret = 'test_secret';
protected $storage;
protected $client;
protected $redirectUri;
public function setUp() {
parent::setUp();
$this->redirectUri = $this
->buildUrl('authorized', [
'absolute' => TRUE,
]);
$server = $this->container
->get('entity_type.manager')
->getStorage('oauth2_server')
->create([
'server_id' => 'test_server',
'name' => 'Test Server',
'settings' => [
'default_scope' => '',
'allow_implicit' => TRUE,
'grant_types' => [
'authorization_code' => 'authorization_code',
'client_credentials' => 'client_credentials',
'refresh_token' => 'refresh_token',
'password' => 'password',
],
'always_issue_new_refresh_token' => TRUE,
'advanced_settings' => [
'require_exact_redirect_uri' => TRUE,
],
],
]);
$server
->save();
$this->client = $this->container
->get('entity_type.manager')
->getStorage('oauth2_server_client')
->create([
'client_id' => $this->clientId,
'server_id' => $server
->id(),
'name' => 'Test client',
'unhashed_client_secret' => $this->clientSecret,
'redirect_uri' => $this->redirectUri,
'automatic_authorization' => TRUE,
]);
$this->client
->save();
$this->storage = $this->container
->get('oauth2_server.storage');
}
public function testCheckClientCredentials() {
$result = $this->storage
->checkClientCredentials('fakeclient', 'testpass');
$this
->assertFalse($result, 'Invalid client credentials correctly detected.');
$result = $this->storage
->checkClientCredentials($this->clientId, 'invalidcredentials');
$this
->assertFalse($result, 'Invalid client_secret correctly detected.');
$result = $this->storage
->checkClientCredentials($this->clientId, $this->clientSecret);
$this
->assertTrue($result, 'Valid client credentials correctly detected.');
$result = $this->storage
->checkClientCredentials($this->clientId, '');
$this
->assertFalse($result, 'Empty client secret not accepted.');
$this->client->client_secret = '';
$this->client
->save();
$result = $this->storage
->checkClientCredentials($this->clientId, '');
$this
->assertTrue($result, 'Empty client secret accepted if none required.');
$result = $this->storage
->checkClientCredentials($this->clientId, NULL);
$this
->assertTrue($result, 'Null client secret accepted if none required.');
}
public function testGetClientDetails() {
$details = $this->storage
->getClientDetails('fakeclient');
$this
->assertFalse($details, 'Invalid client_id correctly detected.');
$details = $this->storage
->getClientDetails($this->clientId);
$this
->assertNotNull($details, 'Client details successfully returned.');
$this
->assertArrayHasKey('client_id', $details, 'The "client_id" value is present in the client details.');
$this
->assertArrayHasKey('client_secret', $details, 'The "client_secret" value is present in the client details.');
$this
->assertArrayHasKey('redirect_uri', $details, 'The "redirect_uri" value is present in the client details.');
}
public function testAccessToken() {
$user = $this
->drupalCreateUser([
'use oauth2 server',
]);
$token = (bool) $this->storage
->getAccessToken('newtoken');
$this
->assertFalse($token, 'Trying to load a nonexistent token is unsuccessful.');
$expires = time() + 20;
$success = (bool) $this->storage
->setAccessToken('newtoken', $this->clientId, $user
->id(), $expires);
$this
->assertTrue($success, 'A new access token has been successfully created.');
$token = $this->storage
->getAccessToken('newtoken');
$this
->assertTrue((bool) $token, 'An access token was successfully returned.');
$this
->assertArrayHasKey('access_token', $token, 'The "access_token" value is present in the token array.');
$this
->assertArrayHasKey('client_id', $token, 'The "client_id" value is present in the token array.');
$this
->assertArrayHasKey('user_id', $token, 'The "user_id" value is present in the token array.');
$this
->assertArrayHasKey('expires', $token, 'The "expires" value is present in the token array.');
$this
->assertEqual($token['access_token'], 'newtoken', 'The "access_token" key has the expected value.');
$this
->assertEqual($token['client_id'], $this->clientId, 'The "client_id" key has the expected value.');
$this
->assertEqual($token['user_id'], $user
->id(), 'The "user_id" key has the expected value.');
$this
->assertEqual($token['expires'], $expires, 'The "expires" key has the expected value.');
$expires = time() + 42;
$success = (bool) $this->storage
->setAccessToken('newtoken', $this->clientId, $user
->id(), $expires);
$this
->assertTrue($success, 'The access token was successfully updated.');
$token = $this->storage
->getAccessToken('newtoken');
$this
->assertTrue((bool) $token, 'An access token was successfully returned.');
$this
->assertEqual($token['expires'], $expires, 'The expires timestamp matches the new value.');
}
public function testSetRefreshToken() {
$user = $this
->drupalCreateUser([
'use oauth2 server',
]);
$token = (bool) $this->storage
->getRefreshToken('refreshtoken');
$this
->assertFalse($token, 'Trying to load a nonexistent token is unsuccessful.');
$expires = time() + 20;
$success = (bool) $this->storage
->setRefreshToken('refreshtoken', $this->clientId, $user
->id(), $expires);
$this
->assertTrue($success, 'A new refresh token has been successfully created.');
$token = $this->storage
->getRefreshToken('refreshtoken');
$this
->assertTrue((bool) $token, 'A refresh token was successfully returned.');
$this
->assertArrayHasKey('refresh_token', $token, 'The "refresh_token" value is present in the token array.');
$this
->assertArrayHasKey('client_id', $token, 'The "client_id" value is present in the token array.');
$this
->assertArrayHasKey('user_id', $token, 'The "user_id" value is present in the token array.');
$this
->assertArrayHasKey('expires', $token, 'The "expires" value is present in the token array.');
$this
->assertEqual($token['refresh_token'], 'refreshtoken', 'The "refresh_token" key has the expected value.');
$this
->assertEqual($token['client_id'], $this->clientId, 'The "client_id" key has the expected value.');
$this
->assertEqual($token['user_id'], $user
->id(), 'The "user_id" key has the expected value.');
$this
->assertEqual($token['expires'], $expires, 'The "expires" key has the expected value.');
}
public function testAuthorizationCode() {
$user = $this
->drupalCreateUser([
'use oauth2 server',
]);
$code = (bool) $this->storage
->getAuthorizationCode('newcode');
$this
->assertFalse($code, 'Trying to load a nonexistent authorization code is unsuccessful.');
$expires = time() + 20;
$success = (bool) $this->storage
->setAuthorizationCode('newcode', $this->clientId, $user
->id(), 'http://example.com', $expires);
$this
->assertTrue($success, 'A new authorization code was successfully created.');
$code = $this->storage
->getAuthorizationCode('newcode');
$this
->assertTrue((bool) $code, 'An authorization code was successfully returned.');
$this
->assertArrayHasKey('authorization_code', $code, 'The "authorization_code" value is present in the code array.');
$this
->assertArrayHasKey('client_id', $code, 'The "client_id" value is present in the code array.');
$this
->assertArrayHasKey('user_id', $code, 'The "user_id" value is present in the code array.');
$this
->assertArrayHasKey('redirect_uri', $code, 'The "redirect_uri" value is present in the code array.');
$this
->assertArrayHasKey('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->clientId, 'The "client_id" key has the expected value.');
$this
->assertEqual($code['user_id'], $user
->id(), '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.');
$expires = time() + 42;
$success = (bool) $this->storage
->setAuthorizationCode('newcode', $this->clientId, $user
->id(), 'http://example.org', $expires);
$this
->assertTrue($success, 'The authorization code was successfully updated.');
$code = $this->storage
->getAuthorizationCode('newcode');
$this
->assertTrue((bool) $code, 'An authorization code was successfully returned.');
$this
->assertEqual($code['expires'], $expires, 'The expires timestamp matches the new value.');
}
public function testCheckUserCredentials() {
$user = $this
->drupalCreateUser([
'use oauth2 server',
]);
$result = $this->storage
->checkUserCredentials($user->name->value, $user->pass_raw);
$this
->assertTrue($result, 'Valid user credentials correctly detected.');
$result = $this->storage
->checkUserCredentials('fakeusername', $user->pass_raw);
$this
->assertFalse($result, 'Invalid username correctly detected.');
$result = $this->storage
->checkUserCredentials($user->name->value, 'fakepass');
$this
->assertFalse($result, 'Invalid password correctly detected');
}
}