You are here

public function AppListBuilderTest::testAppWarnings in Apigee Edge 8

Test app warnings.

File

tests/src/Kernel/Entity/ListBuilder/AppListBuilderTest.php, line 266

Class

AppListBuilderTest
Tests the AppListBuilder.

Namespace

Drupal\Tests\apigee_edge\Kernel\Entity\ListBuilder

Code

public function testAppWarnings() {

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  if ($this->integration_enabled) {
    $this->apiProduct = ApiProduct::create([
      'name' => $this
        ->randomMachineName(),
      'displayName' => $this
        ->randomMachineName(),
      'approvalType' => ApiProduct::APPROVAL_TYPE_AUTO,
    ]);
    $this->apiProduct
      ->save();

    // Revoke old credential and create a new valid one.
    $this
      ->operationOnCredential($this->approvedAppWithOneRevokedCredential, 'revoke', 0);
    $this
      ->operationOnCredential($this->approvedAppWithOneRevokedCredential, 'generate');

    // Revoke old credential.
    $this
      ->operationOnCredential($this->approvedAppWithAllRevokedCredential, 'revoke', 0);

    // Revoke old credential.
    $this
      ->operationOnCredential($this->revokedAppWithRevokedCredential, 'revoke', 0);
    $this
      ->operationOnCredential($this->revokedAppWithRevokedCredential, 'generate');

    // Create a new cred that will expire in 5 seconds, delete old.
    $this
      ->operationOnCredential($this->approvedAppWithExpiredCredential, 'delete', 0);
    $this
      ->operationOnCredential($this->approvedAppWithExpiredCredential, 'generate', 0, 5 * 1000);

    // Create a new cred that will expire in 5 seconds, delete old.
    $this
      ->operationOnCredential($this->revokedAppWithExpiredCredential, 'delete', 0);
    $this
      ->operationOnCredential($this->revokedAppWithExpiredCredential, 'generate', 0, 5 * 1000);

    // Wait a bit and reset "request time" to make sure credentials
    // are considered expired.
    sleep(6);
    $request = Request::create('/', 'GET');
    $this->container
      ->get('http_kernel')
      ->handle($request);
  }
  else {
    $approved_credential = [
      "consumerKey" => $this
        ->randomMachineName(),
      "consumerSecret" => $this
        ->randomMachineName(),
      "status" => AppCredentialInterface::STATUS_APPROVED,
      'expiresAt' => ($this->container
        ->get('datetime.time')
        ->getRequestTime() + 24 * 60 * 60) * 1000,
    ];
    $revoked_credential = [
      "consumerKey" => $this
        ->randomMachineName(),
      "consumerSecret" => $this
        ->randomMachineName(),
      "status" => AppCredentialInterface::STATUS_REVOKED,
      'expiresAt' => ($this->container
        ->get('datetime.time')
        ->getRequestTime() + 24 * 60 * 60) * 1000,
    ];
    $expired_credential = [
      "consumerKey" => $this
        ->randomMachineName(),
      "consumerSecret" => $this
        ->randomMachineName(),
      "status" => AppCredentialInterface::STATUS_APPROVED,
      'expiresAt' => ($this->container
        ->get('datetime.time')
        ->getRequestTime() - 24 * 60 * 60) * 1000,
    ];
    $this->stack
      ->queueMockResponse([
      'get_developer_apps_with_credentials' => [
        'apps' => [
          $this->approvedAppWithApprovedCredential,
          $this->approvedAppWithOneRevokedCredential,
          $this->revokedAppWithRevokedCredential,
          $this->approvedAppWithExpiredCredential,
          $this->revokedAppWithExpiredCredential,
        ],
        'credentials' => [
          $this->approvedAppWithApprovedCredential
            ->id() => [
            $approved_credential,
          ],
          $this->approvedAppWithOneRevokedCredential
            ->id() => [
            $approved_credential,
            $revoked_credential,
          ],
          $this->approvedAppWithAllRevokedCredential
            ->id() => [
            $revoked_credential,
          ],
          $this->revokedAppWithRevokedCredential
            ->id() => [
            $approved_credential,
            $revoked_credential,
          ],
          $this->approvedAppWithExpiredCredential
            ->id() => [
            $expired_credential,
          ],
          $this->revokedAppWithExpiredCredential
            ->id() => [
            $expired_credential,
          ],
        ],
      ],
    ]);
  }
  $build = $entity_type_manager
    ->getListBuilder(static::ENTITY_TYPE)
    ->render();

  // No warnings for approved app.
  $this
    ->assertEmpty($build['table']['#rows'][$this
    ->getStatusRowKey($this->approvedAppWithApprovedCredential)]['data']);

  // No warnings to approved app with one revoked credentials.
  $this
    ->assertEmpty($build['table']['#rows'][$this
    ->getStatusRowKey($this->approvedAppWithOneRevokedCredential)]['data']);

  // One warning for approved app with all credentials revoked.
  $warnings = $build['table']['#rows'][$this
    ->getStatusRowKey($this->approvedAppWithAllRevokedCredential)]['data'];
  $this
    ->assertCount(1, $warnings);
  $this
    ->assertEqual('No valid credentials associated with this app.', (string) $warnings['info']['data']['#items'][0]);

  // No warnings to revoked app with revoked credentials.
  $this
    ->assertEmpty($build['table']['#rows'][$this
    ->getStatusRowKey($this->revokedAppWithRevokedCredential)]['data']);

  // One warning for approved app with expired credentials.
  $warnings = $build['table']['#rows'][$this
    ->getStatusRowKey($this->approvedAppWithExpiredCredential)]['data'];
  $this
    ->assertCount(1, $warnings);
  $this
    ->assertEqual('At least one of the credentials associated with this app is expired.', (string) $warnings['info']['data']['#items'][0]);

  // No warnings for revoked app with expired credentials.
  // Note: \Drupal\apigee_edge\Entity\AppWarningsChecker::getWarnings will
  // return warnings for this but it is not shown in the UI.
  $this
    ->assertEmpty($build['table']['#rows'][$this
    ->getStatusRowKey($this->revokedAppWithExpiredCredential)]['data']);
}