View source
<?php
namespace Drupal\Tests\apigee_edge\Kernel\Entity\ListBuilder;
use Apigee\Edge\Api\Management\Entity\App;
use Apigee\Edge\Api\Management\Entity\AppCredentialInterface;
use Drupal\apigee_edge\Entity\ApiProduct;
use Drupal\apigee_edge\Entity\AppInterface;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
use Drupal\Component\Utility\Html;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\apigee_edge\Kernel\ApigeeEdgeKernelTestTrait;
use Drupal\Tests\apigee_edge\Traits\CredsUtilsTrait;
use Drupal\Tests\apigee_mock_api_client\Traits\ApigeeMockApiClientHelperTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\User;
use Symfony\Component\HttpFoundation\Request;
class AppListBuilderTest extends KernelTestBase {
use ApigeeMockApiClientHelperTrait, ApigeeEdgeKernelTestTrait, UserCreationTrait, CredsUtilsTrait;
protected static $mock_api_client_ready = TRUE;
const ENTITY_TYPE = 'developer_app';
protected static $modules = [
'system',
'apigee_edge',
'apigee_mock_api_client',
'key',
'user',
'options',
];
protected $account;
protected $developer;
protected $approvedAppWithApprovedCredential;
protected $approvedAppWithOneRevokedCredential;
protected $approvedAppWithAllRevokedCredential;
protected $revokedAppWithRevokedCredential;
protected $approvedAppWithExpiredCredential;
protected $revokedAppWithExpiredCredential;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$this
->installConfig([
'apigee_edge',
]);
$this
->apigeeTestHelperSetup();
$this
->addOrganizationMatchedResponse();
$this->account = User::create([
'mail' => $this
->randomMachineName() . '@example.com',
'name' => $this
->randomMachineName(),
'first_name' => $this
->getRandomGenerator()
->word(16),
'last_name' => $this
->getRandomGenerator()
->word(16),
]);
$this->account
->save();
$this
->queueDeveloperResponse($this->account);
$this->developer = Developer::load($this->account
->getEmail());
$this->approvedAppWithApprovedCredential = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->approvedAppWithApprovedCredential
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->approvedAppWithApprovedCredential);
$this->approvedAppWithApprovedCredential
->save();
$this->approvedAppWithOneRevokedCredential = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->approvedAppWithOneRevokedCredential
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->approvedAppWithOneRevokedCredential);
$this->approvedAppWithOneRevokedCredential
->save();
$this->approvedAppWithAllRevokedCredential = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->approvedAppWithAllRevokedCredential
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->approvedAppWithAllRevokedCredential);
$this->approvedAppWithAllRevokedCredential
->save();
$this->revokedAppWithRevokedCredential = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_REVOKED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->revokedAppWithRevokedCredential
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->revokedAppWithRevokedCredential);
$this->revokedAppWithRevokedCredential
->save();
$this->approvedAppWithExpiredCredential = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->approvedAppWithExpiredCredential
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->approvedAppWithExpiredCredential);
$this->approvedAppWithExpiredCredential
->save();
$this->revokedAppWithExpiredCredential = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_REVOKED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->revokedAppWithExpiredCredential
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->revokedAppWithExpiredCredential);
$this->revokedAppWithExpiredCredential
->save();
}
protected function tearDown() {
$this->stack
->reset();
try {
if ($this->account) {
$this
->queueDeveloperResponse($this->account);
$developer = \Drupal::entityTypeManager()
->getStorage('developer')
->create([
'email' => $this->account
->getEmail(),
]);
$developer
->delete();
}
if ($this->approvedAppWithApprovedCredential) {
$this->approvedAppWithApprovedCredential
->delete();
}
if ($this->approvedAppWithOneRevokedCredential) {
$this->approvedAppWithOneRevokedCredential
->delete();
}
if ($this->approvedAppWithAllRevokedCredential) {
$this->approvedAppWithAllRevokedCredential
->delete();
}
if ($this->revokedAppWithRevokedCredential) {
$this->revokedAppWithRevokedCredential
->delete();
}
if ($this->approvedAppWithExpiredCredential) {
$this->approvedAppWithExpiredCredential
->delete();
}
if ($this->revokedAppWithExpiredCredential) {
$this->revokedAppWithExpiredCredential
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testAppWarnings() {
$entity_type_manager = $this->container
->get('entity_type.manager');
if ($this->integration_enabled) {
$this->apiProduct = ApiProduct::create([
'name' => $this
->randomMachineName(),
'displayName' => $this
->randomMachineName(),
'approvalType' => ApiProduct::APPROVAL_TYPE_AUTO,
]);
$this->apiProduct
->save();
$this
->operationOnCredential($this->approvedAppWithOneRevokedCredential, 'revoke', 0);
$this
->operationOnCredential($this->approvedAppWithOneRevokedCredential, 'generate');
$this
->operationOnCredential($this->approvedAppWithAllRevokedCredential, 'revoke', 0);
$this
->operationOnCredential($this->revokedAppWithRevokedCredential, 'revoke', 0);
$this
->operationOnCredential($this->revokedAppWithRevokedCredential, 'generate');
$this
->operationOnCredential($this->approvedAppWithExpiredCredential, 'delete', 0);
$this
->operationOnCredential($this->approvedAppWithExpiredCredential, 'generate', 0, 5 * 1000);
$this
->operationOnCredential($this->revokedAppWithExpiredCredential, 'delete', 0);
$this
->operationOnCredential($this->revokedAppWithExpiredCredential, 'generate', 0, 5 * 1000);
sleep(6);
$request = Request::create('/', 'GET');
$this->container
->get('http_kernel')
->handle($request);
}
else {
$approved_credential = [
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
'expiresAt' => ($this->container
->get('datetime.time')
->getRequestTime() + 24 * 60 * 60) * 1000,
];
$revoked_credential = [
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_REVOKED,
'expiresAt' => ($this->container
->get('datetime.time')
->getRequestTime() + 24 * 60 * 60) * 1000,
];
$expired_credential = [
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
'expiresAt' => ($this->container
->get('datetime.time')
->getRequestTime() - 24 * 60 * 60) * 1000,
];
$this->stack
->queueMockResponse([
'get_developer_apps_with_credentials' => [
'apps' => [
$this->approvedAppWithApprovedCredential,
$this->approvedAppWithOneRevokedCredential,
$this->revokedAppWithRevokedCredential,
$this->approvedAppWithExpiredCredential,
$this->revokedAppWithExpiredCredential,
],
'credentials' => [
$this->approvedAppWithApprovedCredential
->id() => [
$approved_credential,
],
$this->approvedAppWithOneRevokedCredential
->id() => [
$approved_credential,
$revoked_credential,
],
$this->approvedAppWithAllRevokedCredential
->id() => [
$revoked_credential,
],
$this->revokedAppWithRevokedCredential
->id() => [
$approved_credential,
$revoked_credential,
],
$this->approvedAppWithExpiredCredential
->id() => [
$expired_credential,
],
$this->revokedAppWithExpiredCredential
->id() => [
$expired_credential,
],
],
],
]);
}
$build = $entity_type_manager
->getListBuilder(static::ENTITY_TYPE)
->render();
$this
->assertEmpty($build['table']['#rows'][$this
->getStatusRowKey($this->approvedAppWithApprovedCredential)]['data']);
$this
->assertEmpty($build['table']['#rows'][$this
->getStatusRowKey($this->approvedAppWithOneRevokedCredential)]['data']);
$warnings = $build['table']['#rows'][$this
->getStatusRowKey($this->approvedAppWithAllRevokedCredential)]['data'];
$this
->assertCount(1, $warnings);
$this
->assertEqual('No valid credentials associated with this app.', (string) $warnings['info']['data']['#items'][0]);
$this
->assertEmpty($build['table']['#rows'][$this
->getStatusRowKey($this->revokedAppWithRevokedCredential)]['data']);
$warnings = $build['table']['#rows'][$this
->getStatusRowKey($this->approvedAppWithExpiredCredential)]['data'];
$this
->assertCount(1, $warnings);
$this
->assertEqual('At least one of the credentials associated with this app is expired.', (string) $warnings['info']['data']['#items'][0]);
$this
->assertEmpty($build['table']['#rows'][$this
->getStatusRowKey($this->revokedAppWithExpiredCredential)]['data']);
}
protected function getStatusRowKey(AppInterface $app, $key = "warning") : string {
return Html::getId($app
->getAppId()) . '-' . $key;
}
}