View source
<?php
namespace Drupal\Tests\apigee_edge\Kernel;
use Drupal\apigee_edge\Exception\OauthTokenStorageException;
use Drupal\apigee_edge\OauthTokenFileStorage;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\File\FileSystemInterface;
use Drupal\KernelTests\KernelTestBase;
class OauthTokenFileStorageTest extends KernelTestBase {
protected static $mock_api_client_ready = TRUE;
private const CUSTOM_TOKEN_DIR = 'oauth/token/dir';
protected static $modules = [
'system',
'apigee_edge',
'key',
];
private $testTokenData = [];
public function setUp() {
parent::setUp();
$this->testTokenData = [
'access_token' => mb_strtolower($this
->randomMachineName(32)),
'token_type' => 'bearer',
'expires_in' => 300,
'refresh_token' => mb_strtolower($this
->randomMachineName(32)),
'scope' => 'create',
];
}
private function tokenStorage(bool $rebuild = FALSE) : OauthTokenFileStorage {
$config = $this
->config('apigee_edge.auth');
$config
->set('oauth_token_storage_location', $this
->tokenDirectoryUri())
->save();
if ($rebuild) {
$this->container
->get('kernel')
->rebuildContainer();
}
return $this->container
->get('apigee_edge.authentication.oauth_token_storage');
}
private function tokenDirectoryUri() : string {
return $this->vfsRoot
->url() . '/' . static::CUSTOM_TOKEN_DIR;
}
private function tokenFileUri() : string {
return $this
->tokenDirectoryUri() . '/oauth.dat';
}
public function testCheckRequirements() {
$storage = $this->container
->get('apigee_edge.authentication.oauth_token_storage');
try {
$storage
->checkRequirements();
} catch (OauthTokenStorageException $exception) {
$this
->assertEquals('Unable to save token data to private filesystem because it has not been configured yet.', $exception
->getMessage());
}
$this
->setSetting('file_private_path', 'vfs://private');
try {
$storage
->checkRequirements();
} catch (OauthTokenStorageException $exception) {
$this
->assertEquals(sprintf('Unable to set up %s directory for token file.', OauthTokenFileStorage::DEFAULT_DIRECTORY), $exception
->getMessage());
}
$storage = $this
->tokenStorage(TRUE);
$storage
->checkRequirements();
$this
->assertTrue(file_exists($this->vfsRoot
->getChild(static::CUSTOM_TOKEN_DIR)
->url()));
}
public function testSaveToken() {
$storage = $this
->tokenStorage();
$current_time = time();
$storage
->saveToken($this->testTokenData);
$stored_token = unserialize(base64_decode(file_get_contents($this
->tokenFileUri())));
$this
->assertSame($this->testTokenData['access_token'], $stored_token['access_token']);
$this
->assertSame($this->testTokenData['token_type'], $stored_token['token_type']);
$this
->assertSame($this->testTokenData['refresh_token'], $stored_token['refresh_token']);
$this
->assertSame($this->testTokenData['scope'], $stored_token['scope']);
$this
->assertLessThan(2, abs($this->testTokenData['expires_in'] + $current_time - $stored_token['expires']));
}
public function testStaticCaching() {
$storage = $this
->tokenStorage();
$storage
->saveToken($this->testTokenData);
$access_token = $storage
->getAccessToken();
$stored_token = unserialize(base64_decode(file_get_contents($this
->tokenFileUri())));
$stored_token['access_token'] = mb_strtolower($this
->randomMachineName(32));
\Drupal::service('file_system')
->saveData(base64_encode(serialize($stored_token)), $this
->tokenFileUri(), FileSystemInterface::EXISTS_REPLACE);
$this
->assertSame($access_token, $storage
->getAccessToken());
}
public function testGetters() {
$storage = $this
->tokenStorage();
$current_time = time();
$storage
->saveToken($this->testTokenData);
$this
->assertSame($this->testTokenData['access_token'], $storage
->getAccessToken());
$this
->assertSame($this->testTokenData['token_type'], $storage
->getTokenType());
$this
->assertSame($this->testTokenData['refresh_token'], $storage
->getRefreshToken());
$this
->assertSame($this->testTokenData['scope'], $storage
->getScope());
$this
->assertLessThan(2, abs($this->testTokenData['expires_in'] + $current_time - $storage
->getExpires()));
$this
->assertFalse($storage
->hasExpired());
$storage
->markExpired();
$this
->assertTrue($storage
->hasExpired());
}
public function testCacheClear() {
$storage = $this
->tokenStorage();
$storage
->saveToken($this->testTokenData);
$this
->assertNotEmpty($storage
->getAccessToken());
drupal_flush_all_caches();
$this
->assertEmpty($storage
->getAccessToken());
}
public function testTokenFileRemovalOnUninstall() {
$storage = $this
->tokenStorage();
$storage
->saveToken($this->testTokenData);
$this
->assertTrue(file_exists($this
->tokenFileUri()), 'Oauth token data file should not exist after the module got uninstalled.');
$installer = $this->container
->get('module_installer');
$installer
->uninstall([
'apigee_edge',
]);
$this
->assertFalse(file_exists($this
->tokenFileUri()), 'Oauth token data file should not exist after the module got uninstalled.');
}
public function register(ContainerBuilder $container) {
parent::register($container);
$container
->register('stream_wrapper.private', 'Drupal\\Core\\StreamWrapper\\PrivateStream')
->addTag('stream_wrapper', [
'scheme' => 'private',
]);
}
}