You are here

public function DeveloperAppApiKeysPermissionTest::testPermissions in Apigee Edge 8

Tests permissions for API key routes.

File

tests/src/Functional/DeveloperAppApiKeysPermissionTest.php, line 161

Class

DeveloperAppApiKeysPermissionTest
Tests Api Keys permissions for developer_app.

Namespace

Drupal\Tests\apigee_edge\Functional

Code

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 API key.
  $add_url = $this->developerApp
    ->toUrl('add-api-key-form');
  $this
    ->drupalGet($add_url);
  $this
    ->assertSession()
    ->pageTextContains('Add key');

  // Revoke API 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 API key.
  $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 API key.
  $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 API key.
  $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 API 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 . '?');
}