DeveloperAppCredentialEventTest.php in Apigee Edge 8
File
tests/src/Functional/DeveloperAppCredentialEventTest.php
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\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
use Drupal\apigee_edge\Event\AppCredentialCreateEvent;
use Drupal\apigee_edge\Event\AppCredentialDeleteEvent;
use Drupal\apigee_edge_test_app_keys\EventSubscriber\CreateDeleteAppKey;
class DeveloperAppCredentialEventTest extends ApigeeEdgeFunctionalTestBase {
protected $account;
protected $developer;
protected $developerApp;
protected $apiProduct;
protected function setUp() {
parent::setUp();
$this
->installExtraModules([
'apigee_edge_test_app_keys',
]);
$this->account = $this
->createAccount();
$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->developerApp
->save();
$this->apiProduct = ApiProduct::create([
'name' => $this
->randomMachineName(),
'displayName' => $this
->randomMachineName(),
'approvalType' => ApiProduct::APPROVAL_TYPE_AUTO,
]);
$this->apiProduct
->save();
}
protected function tearDown() {
try {
if ($this->developer !== NULL) {
$this->developer
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
try {
if ($this->apiProduct !== NULL) {
$this->apiProduct
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
parent::tearDown();
}
public function testAppCredentialEvents() {
$validateCredential = function (AppCredentialInterface $credential) {
$prefix = apigee_edge_test_app_keys_get_prefix();
$this
->assertStringStartsWith($prefix, $credential
->getConsumerKey());
$this
->assertStringStartsWith($prefix, $credential
->getConsumerSecret());
};
$credentials = $this->developerApp
->getCredentials();
$credential = reset($credentials);
$validateCredential($credential);
$credentials = $this->developerApp
->getCredentials();
$credential = reset($credentials);
$dacc = $this->container
->get('apigee_edge.controller.developer_app_credential_factory')
->developerAppCredentialController($this->developerApp
->getDeveloperId(), $this->developerApp
->getName());
$dacc
->delete($credential
->getConsumerKey());
$dacc
->generate([
$this->apiProduct
->id(),
], $this->developerApp
->getAttributes(), (string) $this->developerApp
->getCallbackUrl(), $this->developerApp
->getScopes(), 60 * 60 * 1000);
$credentials = $this->developerApp
->getCredentials();
$credential = reset($credentials);
$validateCredential($credential);
$state = $this->container
->get('state');
$dacc
->delete($credential
->id());
$this
->assertNotNull($state
->get(CreateDeleteAppKey::generateStateKey(AppCredentialDeleteEvent::APP_TYPE_DEVELOPER, $this->developerApp
->getDeveloperId(), $this->developerApp
->getName(), $credential
->id())));
$credential_key = $this
->randomMachineName();
$dacc
->create($credential_key, $this
->randomMachineName());
$this
->assertNotNull($state
->get(CreateDeleteAppKey::generateStateKey(AppCredentialCreateEvent::APP_TYPE_DEVELOPER, $this->developerApp
->getDeveloperId(), $this->developerApp
->getName(), $credential
->id())));
}
}