You are here

public function ApigeeEdgeManagementCliServiceTest::testCreateEdgeRoleForDrupalDefaultRoleAndBaseUrl in Apigee Edge 8

Pass null role name to test using default role name.

File

tests/src/Unit/Command/Util/ApigeeEdgeManagementCliServiceTest.php, line 141

Class

ApigeeEdgeManagementCliServiceTest
Test ApigeeEdgeManagementCliService.

Namespace

Drupal\Tests\apigee_edge\Unit\Command\Util

Code

public function testCreateEdgeRoleForDrupalDefaultRoleAndBaseUrl() {

  // Output to user should show role created and permissions set.
  $io = $this
    ->prophesize(StyleInterface::class);
  $io
    ->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))
    ->shouldBeCalledTimes(1);
  $io
    ->success(Argument::containingString('Role ' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . ' is configured.'))
    ->shouldBeCalledTimes(1);
  $io
    ->text(Argument::containingString('Role ' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . ' does not exist'))
    ->shouldBeCalledTimes(1);
  $io
    ->text(Argument::containingString('Setting permissions on role ' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . '.'))
    ->shouldBeCalledTimes(1);
  $io
    ->text(Argument::containingString('/'))
    ->shouldBeCalledTimes(12);

  // Org should exist.
  $response_org = $this
    ->prophesize(Response::class);
  $response_org
    ->getBody()
    ->shouldBeCalledTimes(1)
    ->willReturn('{ "name": "' . $this->org . '" }');
  $this->httpClient
    ->get(Argument::exact(ApigeeClientInterface::EDGE_ENDPOINT . '/o/' . $this->org), Argument::type('array'))
    ->shouldBeCalledTimes(1)
    ->willReturn($response_org
    ->reveal());

  // The role should not exist yet in system.
  $request_role = $this
    ->prophesize(RequestInterface::class);
  $response_role = $this
    ->prophesize(Response::class);
  $response_role
    ->getStatusCode()
    ->willReturn(404);
  $exception = new ClientException('Forbidden', $request_role
    ->reveal(), $response_role
    ->reveal());
  $this->httpClient
    ->get(Argument::exact(ApigeeClientInterface::EDGE_ENDPOINT . '/o/' . $this->org . '/userroles/' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME), Argument::type('array'))
    ->willThrow($exception);

  // The role should be created.
  $this->httpClient
    ->post(Argument::exact(ApigeeClientInterface::EDGE_ENDPOINT . '/o/' . $this->org . '/userroles'), Argument::type('array'))
    ->shouldBeCalledTimes(1);

  // The permissions should be set.
  $this->httpClient
    ->post(Argument::exact(ApigeeClientInterface::EDGE_ENDPOINT . '/o/' . $this->org . '/userroles/' . ApigeeEdgeManagementCliServiceInterface::DEFAULT_ROLE_NAME . '/permissions'), Argument::type('array'))
    ->shouldBeCalledTimes(12);
  $apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
    ->reveal());
  $apigee_edge_management_cli_service
    ->createEdgeRoleForDrupal($io
    ->reveal(), [
    $this,
    'mockDt',
  ], $this->org, $this->email, $this->password, NULL, NULL, FALSE);
}