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\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
class DeveloperAppApiKeysPermissionTest extends ApigeeEdgeFunctionalTestBase {
protected static $mock_api_client_ready = TRUE;
protected $consumer_key;
protected $account;
protected $admin;
protected $developer;
protected $developerApp;
protected $apiProduct;
protected function setUp() {
parent::setUp();
$this
->addOrganizationMatchedResponse();
$this->account = $this
->createAccount([
'add_api_key own developer_app',
]);
$this->admin = $this
->createAccount([
'add_api_key any developer_app',
'revoke_api_key any developer_app',
'delete_api_key any developer_app',
'view any developer_app',
'update any 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();
if ($keys = $this->developerApp
->getCredentials()) {
$credential = reset($keys);
$this->consumer_key = $credential
->getConsumerKey();
$this->apiProduct = $this
->createProduct();
$appCredentialController = \Drupal::service('apigee_edge.controller.developer_app_credential_factory')
->developerAppCredentialController($this->developerApp
->getAppOwner(), $this->developerApp
->getName());
$appCredentialController
->addProducts($this->consumer_key, [
$this->apiProduct
->getName(),
]);
}
}
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();
}
if ($this->apiProduct) {
$this->apiProduct
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testPermissions() {
if (empty($this->consumer_key)) {
$this->consumer_key = $this
->randomMachineName(32);
$credentials = [
[
"consumerKey" => $this->consumer_key,
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
"apiProducts" => [
[
"name" => $this
->randomMachineName(),
],
],
],
];
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
}
$this
->drupalLogin($this->account);
$add_url = $this->developerApp
->toUrl('add-api-key-form');
$this
->drupalGet($add_url);
$this
->assertSession()
->pageTextContains('Add key');
$revoke_url = $this->developerApp
->toUrl('revoke-api-key-form')
->setRouteParameter('consumer_key', $this->consumer_key);
$this
->drupalGet($revoke_url);
$this
->assertSession()
->pageTextContains('Access denied');
$delete_url = $this->developerApp
->toUrl('delete-api-key-form')
->setRouteParameter('consumer_key', $this->consumer_key);
$this
->drupalGet($delete_url);
$this
->assertSession()
->pageTextContains('Access denied');
$this
->drupalLogin($this->admin);
$add_url = $this->developerApp
->toUrl('add-api-key-form');
$this
->drupalGet($add_url);
$this
->assertSession()
->pageTextContains('Add key');
if (!$this->integration_enabled) {
$this->stack
->queueMockResponse([
'api-product' => [
'product' => [
'name' => $credentials[0]['apiProducts'][0]['name'],
],
],
]);
}
$this
->submitForm([], 'Confirm');
$this
->assertSession()
->pageTextContains('New API key added');
if (!$this->integration_enabled) {
$credentials[] = [
"consumerKey" => $this
->randomMachineName(32),
"consumerSecret" => $this
->randomMachineName(),
"status" => AppCredentialInterface::STATUS_APPROVED,
];
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
$this
->queueDeveloperAppResponse($this->developerApp, 200, $credentials);
}
$revoke_url = $this->developerApp
->toUrl('revoke-api-key-form')
->setRouteParameter('consumer_key', $this->consumer_key);
$this
->drupalGet($revoke_url);
$this
->assertSession()
->pageTextContains('Are you sure that you want to revoke the API key ' . $this->consumer_key . '?');
$delete_url = $this->developerApp
->toUrl('delete-api-key-form')
->setRouteParameter('consumer_key', $this->consumer_key);
$this
->drupalGet($delete_url);
$this
->assertSession()
->pageTextContains('Are you sure that you want to delete the API key ' . $this->consumer_key . '?');
}
}