View source
<?php
namespace Drupal\Tests\apigee_edge\Functional;
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\Controller\AppCredentialControllerInterface;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
class DeveloperAppApiKeyTest extends ApigeeEdgeFunctionalTestBase {
protected $consumer_key;
protected static $mock_api_client_ready = TRUE;
protected $account;
protected $developer;
protected $developerApp;
protected $apiProducts;
protected $appCredentialController;
protected function setUp() {
parent::setUp();
$this->appCredentialController = \Drupal::service('apigee_edge.controller.developer_app_credential_factory');
$this->account = $this
->createAccount([
'add_api_key own developer_app',
'revoke_api_key own developer_app',
'delete_api_key own developer_app',
]);
$this
->queueDeveloperResponse($this->account);
$this->developer = Developer::load($this->account
->getEmail());
$this->developerApp = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => App::STATUS_APPROVED,
'developerId' => $this->developer
->getDeveloperId(),
]);
$this->developerApp
->setOwner($this->account);
$this
->queueDeveloperAppResponse($this->developerApp);
$this->developerApp
->save();
$productOne = ApiProduct::create([
'name' => 'api_one',
'displayName' => 'API One',
'approvalType' => ApiProduct::APPROVAL_TYPE_AUTO,
]);
$this->stack
->queueMockResponse([
'api_product' => [
'product' => $productOne,
],
]);
$productOne
->save();
$productTwo = ApiProduct::create([
'name' => 'api_two',
'displayName' => 'API Two',
'approvalType' => ApiProduct::APPROVAL_TYPE_AUTO,
]);
$this->stack
->queueMockResponse([
'api_product' => [
'product' => $productTwo,
],
]);
$productTwo
->save();
$this->apiProducts = [
$productOne,
$productTwo,
];
if ($keys = $this->developerApp
->getCredentials()) {
$credential = reset($keys);
$this->consumer_key = $credential
->getConsumerKey();
}
$this
->addOrganizationMatchedResponse();
$this
->drupalLogin($this->account);
}
protected function tearDown() {
$this->stack
->reset();
try {
if ($this->account) {
$developer = \Drupal::entityTypeManager()
->getStorage('developer')
->create([
'email' => $this->account
->getEmail(),
]);
$developer
->delete();
}
if ($this->developerApp) {
$this->developerApp
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
try {
if ($this->apiProducts !== NULL) {
foreach ($this->apiProducts as $product) {
$product
->delete();
}
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testAppApiKeyOperations() {
if (empty($this->consumer_key)) {
$this->consumer_key = $this
->randomMachineName(32);
$credentials = [
[
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
[
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
[
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_REVOKED,
],
];
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
}
else {
$credController = $this->appCredentialController
->developerAppCredentialController($this->developerApp
->getAppOwner(), $this->developerApp
->getName());
$credController
->create($this
->randomMachineName(), $this
->randomMachineName());
$credController
->create($this
->randomMachineName(), $this
->randomMachineName());
}
$this
->drupalGet($this->developerApp
->toUrl('canonical-by-developer'));
$this
->assertSession()
->elementContains('css', '.app-credential:first-child .dropbutton', 'Revoke');
$this
->assertSession()
->elementContains('css', '.app-credential:first-child .dropbutton', 'Delete');
}
public function testAppApiKeyAddSingle() {
$credentials = [];
if (empty($this->consumer_key)) {
$this->consumer_key = $this
->randomMachineName(32);
$credentials = [
[
"apiProducts" => [
$this->apiProducts[0],
],
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
];
}
else {
$credController = $this->appCredentialController
->developerAppCredentialController($this->developerApp
->getAppOwner(), $this->developerApp
->getName());
$credController
->addProducts($this->consumer_key, [
$this->apiProducts[0]
->getName(),
]);
}
$path = $this->developerApp
->toUrl('add-api-key-form');
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Add key');
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this->stack
->queueMockResponse([
'api_product' => [
'product' => $this->apiProducts[0],
],
]);
$this
->drupalPostForm(NULL, [
'expiry' => 'date',
'expiry_date' => "07/20/2030",
], 'Confirm');
$this
->assertSession()
->pageTextContains('New API key added to ' . $this->developerApp
->getName() . '.');
$this
->assertSession()
->elementContains('css', '.app-credential .api-product-list-row', 'API One');
}
public function testAppApiKeyAddMultiple() {
$credentials = [];
if (empty($this->consumer_key)) {
$this->consumer_key = $this
->randomMachineName(32);
$credentials = [
[
"apiProducts" => [
$this->apiProducts[0],
],
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
"issuedAt" => 1594973277149,
],
[
"apiProducts" => [
$this->apiProducts[1],
],
"consumerKey" => $this
->randomMachineName(32),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
"issuedAt" => 1594973277300,
],
];
}
else {
$credController = $this->appCredentialController
->developerAppCredentialController($this->developerApp
->getAppOwner(), $this->developerApp
->getName());
$credController
->addProducts($this->consumer_key, [
$this->apiProducts[0]
->getName(),
]);
$credController
->generate([
$this->apiProducts[1]
->getName(),
], $this->developerApp
->getAttributes(), '');
}
$this
->queueDeveloperAppResponse($this->developerApp);
$path = $this->developerApp
->toUrl('add-api-key-form');
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Add key');
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this->stack
->queueMockResponse([
'api_product' => [
'product' => $this->apiProducts[0],
],
]);
$this->stack
->queueMockResponse([
'api_product' => [
'product' => $this->apiProducts[1],
],
]);
$this
->drupalPostForm(NULL, [
'expiry' => 'date',
'expiry_date' => "07/20/2030",
], 'Confirm');
$this
->assertSession()
->pageTextContains('New API key added to ' . $this->developerApp
->getName() . '.');
$this
->assertSession()
->elementContains('css', '.app-credential:last-child .api-product-list-row', 'API Two');
}
public function testAppApiKeyRevoke() {
$credentials = [];
if (empty($this->consumer_key)) {
$this->consumer_key = $this
->randomMachineName(32);
$credentials = [
[
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
[
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
];
}
else {
$credController = $this->appCredentialController
->developerAppCredentialController($this->developerApp
->getAppOwner(), $this->developerApp
->getName());
$credController
->create($this
->randomMachineName(), $this
->randomMachineName());
}
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$path = $this->developerApp
->toUrl('revoke-api-key-form')
->setRouteParameter('consumer_key', $this->consumer_key);
$this
->drupalGet($path);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this->stack
->queueMockResponse('no_content');
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->drupalPostForm(NULL, [], 'Revoke');
$this
->assertSession()
->pageTextContains('API key ' . $this->consumer_key . ' revoked from ' . $this->developerApp
->getName() . '.');
$credentials = [
[
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
];
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Access denied');
}
public function testAppApiKeyDelete() {
$credentials = [];
if (empty($this->consumer_key)) {
$this->consumer_key = $this
->randomMachineName(32);
$credentials = [
[
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
[
"consumerKey" => $this
->randomMachineName(),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
];
}
else {
$credController = $this->appCredentialController
->developerAppCredentialController($this->developerApp
->getAppOwner(), $this->developerApp
->getName());
$credController
->create($this
->randomMachineName(), $this
->randomMachineName());
}
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$path = $this->developerApp
->toUrl('delete-api-key-form')
->setRouteParameter('consumer_key', $this->consumer_key);
$this
->drupalGet($path);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
unset($credentials[0]);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this->stack
->queueMockResponse('no_content');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->assertSession()
->pageTextContains('API key ' . $this->consumer_key . ' deleted from ' . $this->developerApp
->getName() . '.');
$credentials = [
[
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
],
];
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->drupalGet($path);
$this
->assertSession()
->pageTextContains('Access denied');
}
}