You are here

public function DeveloperAppCredentialEventTest::testAppCredentialEvents in Apigee Edge 8

Developer app keys, credential event test.

File

tests/src/Functional/DeveloperAppCredentialEventTest.php, line 120

Class

DeveloperAppCredentialEventTest
Developer app keys, credential event test.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

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);

  // Override (default) app key when an app is created.
  $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());

  // Override app key on generate.
  $dacc
    ->generate([
    $this->apiProduct
      ->id(),
  ], $this->developerApp
    ->getAttributes(), (string) $this->developerApp
    ->getCallbackUrl(), $this->developerApp
    ->getScopes(), 60 * 60 * 1000);

  // Also test that related caches got invalidated, this is the reason why
  // we retrieve the credentials from the app instead of use the return
  // value of the function above.
  $credentials = $this->developerApp
    ->getCredentials();
  $credential = reset($credentials);
  $validateCredential($credential);

  // Delete app key event.
  $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())));

  // Create (additional) app key event.
  $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())));
}