View source
<?php
namespace Drupal\Tests\apigee_edge\Unit\Command\Util;
use Apigee\Edge\ClientInterface as ApigeeClientInterface;
use Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliService;
use Drupal\apigee_edge\Command\Util\ApigeeEdgeManagementCliServiceInterface;
use Drupal\Tests\UnitTestCase;
use Drush\Utils\StringUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Exception\TransferException;
use GuzzleHttp\Psr7\Response;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Console\Style\StyleInterface;
class ApigeeEdgeManagementCliServiceTest extends UnitTestCase {
protected $baseUrl = 'http://api.apigee.com';
protected $email = 'noreply@apigee.com';
protected $password = 'secret';
protected $org = 'org1';
protected $roleName = 'drupal_connect_role';
protected $httpClient;
protected function setUp() {
parent::setUp();
$this->httpClient = $this
->prophesize(Client::class);
}
public function testCreateEdgeRoleForDrupalCustomRoleAndBaseUrl() {
$io = $this
->prophesize(StyleInterface::class);
$io
->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))
->shouldBeCalledTimes(1);
$io
->success(Argument::containingString('Role ' . $this->roleName . ' is configured.'))
->shouldBeCalledTimes(1);
$io
->text(Argument::containingString('Role ' . $this->roleName . ' does not exist. Creating role.'))
->shouldBeCalledTimes(1);
$io
->text(Argument::containingString('Setting permissions on role ' . $this->roleName . '.'))
->shouldBeCalledTimes(1);
$io
->text(Argument::containingString('/'))
->shouldBeCalledTimes(12);
$response_org = $this
->prophesize(Response::class);
$response_org
->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org), Argument::type('array'))
->shouldBeCalledTimes(1)
->willReturn($response_org
->reveal());
$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($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
->willThrow($exception);
$this->httpClient
->post(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles'), Argument::type('array'))
->shouldBeCalledTimes(1);
$this->httpClient
->post(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName . '/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, $this->baseUrl, $this->roleName, FALSE);
}
public function testCreateEdgeRoleForDrupalDefaultRoleAndBaseUrl() {
$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);
$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());
$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);
$this->httpClient
->post(Argument::exact(ApigeeClientInterface::EDGE_ENDPOINT . '/o/' . $this->org . '/userroles'), Argument::type('array'))
->shouldBeCalledTimes(1);
$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);
}
public function testCreateEdgeRoleForDrupalWhenRoleExistsTestWithForceFlag() {
$io = $this
->prophesize(StyleInterface::class);
$io
->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))
->shouldBeCalledTimes(1);
$io
->text(Argument::containingString('Setting permissions on role ' . $this->roleName . '.'))
->shouldBeCalledTimes(1);
$io
->text(Argument::containingString('/'))
->shouldBeCalledTimes(12);
$io
->success(Argument::containingString('Role ' . $this->roleName . ' is configured.'))
->shouldBeCalledTimes(1);
$response_org = $this
->prophesize(Response::class);
$response_org
->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org), Argument::type('array'))
->shouldBeCalledTimes(1)
->willReturn($response_org
->reveal());
$response_user_role = $this
->prophesize(Response::class);
$response_user_role
->getBody()
->willReturn('{ "name": "' . $this->roleName . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
->willReturn($response_user_role
->reveal());
$this->httpClient
->post(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles'), Argument::type('array'))
->shouldNotBeCalled();
$this->httpClient
->post(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName . '/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, $this->baseUrl, $this->roleName, TRUE);
}
public function testCreateEdgeRoleForDrupalWhenRoleExistsTestNoForceFlag() {
$io = $this
->prophesize(StyleInterface::class);
$io
->success(Argument::exact('Connected to Edge org ' . $this->org . '.'))
->shouldBeCalledTimes(1);
$io
->error(Argument::containingString('Role ' . $this->roleName . ' already exists.'))
->shouldBeCalledTimes(1);
$io
->note(Argument::containingString('Run with --force option'))
->shouldBeCalled();
$response_org = $this
->prophesize(Response::class);
$response_org
->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org), Argument::type('array'))
->shouldBeCalledTimes(1)
->willReturn($response_org
->reveal());
$response_user_role = $this
->prophesize(Response::class);
$response_user_role
->getBody()
->willReturn('{ "name": "' . $this->roleName . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
->willReturn($response_user_role
->reveal());
$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, $this->baseUrl, $this->roleName, FALSE);
}
public function testIsValidEdgeCredentialsBadEndpoint() {
$body = "<h1>not json</h1>";
$response = $this
->prophesize(Response::class);
$response
->getBody()
->shouldBeCalledTimes(1)
->willReturn($body);
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::containingString('Unable to parse response from GET'))
->shouldBeCalledTimes(1);
$this->httpClient
->get(Argument::type('string'), Argument::type('array'))
->willReturn($response
->reveal());
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$is_valid_creds = $apigee_edge_management_cli_service
->isValidEdgeCredentials($io
->reveal(), [
$this,
'mockDt',
], $this->org, $this->email, $this->password, $this->baseUrl);
$this
->assertEquals(FALSE, $is_valid_creds, 'Credentials are not valid, should return false.');
}
public function testIsValidEdgeCredentialsUnauthorized() {
$request_role = $this
->prophesize(RequestInterface::class);
$response_role = $this
->prophesize(Response::class);
$response_role
->getStatusCode()
->willReturn(403);
$exception = new ClientException('Unauthorized', $request_role
->reveal(), $response_role
->reveal());
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org), Argument::type('array'))
->willThrow($exception)
->shouldBeCalledTimes(1);
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::containingString('Error connecting to Apigee Edge'))
->shouldBeCalledTimes(1);
$io
->note(Argument::containingString('may not have the orgadmin role for Apigee Edge org'))
->shouldBeCalledTimes(1);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$is_valid_creds = $apigee_edge_management_cli_service
->isValidEdgeCredentials($io
->reveal(), [
$this,
'mockDt',
], $this->org, $this->email, $this->password, $this->baseUrl);
$this
->assertEquals(FALSE, $is_valid_creds, 'Credentials are not valid, should return false.');
}
public function testIsValidEdgeCredentialsValid() {
$response_org = $this
->prophesize(Response::class);
$response_org
->getBody()
->shouldBeCalledTimes(1)
->willReturn('{ "name": "' . $this->org . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org), Argument::type('array'))
->shouldBeCalledTimes(1)
->willReturn($response_org
->reveal());
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::type('string'))
->shouldNotBeCalled();
$io
->section(Argument::type('string'))
->shouldNotBeCalled();
$io
->text(Argument::type('string'))
->shouldNotBeCalled();
$io
->success(Argument::type('string'))
->shouldBeCalled();
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$is_valid_creds = $apigee_edge_management_cli_service
->isValidEdgeCredentials($io
->reveal(), [
$this,
'mockDt',
], $this->org, $this->email, $this->password, $this->baseUrl);
$this
->assertEquals(TRUE, $is_valid_creds, 'Credentials are not valid, should return false.');
}
public function testDoesRoleExistTrue() {
$response_user_role = $this
->prophesize(Response::class);
$response_user_role
->getBody()
->willReturn('{ "name": "' . $this->roleName . '" }');
$this->httpClient
->get(Argument::exact($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
->shouldBeCalledTimes(1)
->willReturn($response_user_role
->reveal());
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$does_role_exist = $apigee_edge_management_cli_service
->doesRoleExist($this->org, $this->email, $this->password, $this->baseUrl, $this->roleName);
$this
->assertEquals(TRUE, $does_role_exist, 'Method doesRoleExist() should return true when role exists.');
}
public function testDoesRoleExistNotTrue() {
$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($this->baseUrl . '/o/' . $this->org . '/userroles/' . $this->roleName), Argument::type('array'))
->willThrow($exception);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$does_role_exist = $apigee_edge_management_cli_service
->doesRoleExist($this->org, $this->email, $this->password, $this->baseUrl, $this->roleName);
$this
->assertEquals(FALSE, $does_role_exist, 'Method doesRoleExist() should return false when role exists.');
}
public function testDoesRoleExistServerErrorThrown() {
$request = $this
->prophesize(RequestInterface::class);
$response = $this
->prophesize(Response::class);
$response
->getStatusCode()
->willReturn(500);
$exception = new ServerException('Server error.', $request
->reveal(), $response
->reveal());
$this
->expectException(ServerException::class);
$this->httpClient
->get(Argument::type('string'), Argument::type('array'))
->willThrow($exception);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$apigee_edge_management_cli_service
->doesRoleExist($this->org, $this->email, $this->password, $this->baseUrl, $this->roleName);
}
public function testHandleHttpClientExceptions0Code() {
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::containingString('Error connecting to Apigee Edge'))
->shouldBeCalledTimes(1);
$io
->note(Argument::containingString('Your system may not be able to connect'))
->shouldBeCalledTimes(1);
$exception = $this
->prophesize(TransferException::class);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$apigee_edge_management_cli_service
->handleHttpClientExceptions($exception
->reveal(), $io
->reveal(), [
$this,
'mockDt',
], 'http://api.apigee.com/test', $this->org, $this->email);
}
public function testHandleHttpClientExceptions401Code() {
$request = $this
->prophesize(RequestInterface::class);
$response = $this
->prophesize(Response::class);
$response
->getStatusCode()
->willReturn(401);
$exception = new ClientException('Unauthorized', $request
->reveal(), $response
->reveal());
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::containingString('Error connecting to Apigee Edge'))
->shouldBeCalledTimes(1);
$io
->note(Argument::exact('Your username or password is invalid.'))
->shouldBeCalledTimes(1);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$apigee_edge_management_cli_service
->handleHttpClientExceptions($exception, $io
->reveal(), [
$this,
'mockDt',
], 'http://api.apigee.com/test', $this->org, $this->email);
}
public function testHandleHttpClientExceptions403Code() {
$request = $this
->prophesize(RequestInterface::class);
$response = $this
->prophesize(Response::class);
$response
->getStatusCode()
->willReturn(403);
$exception = new ClientException('Forbidden', $request
->reveal(), $response
->reveal());
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::containingString('Error connecting to Apigee Edge'))
->shouldBeCalledTimes(1);
$io
->note(Argument::containingString('User ' . $this->email . ' may not have the orgadmin role'))
->shouldBeCalledTimes(1);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$apigee_edge_management_cli_service
->handleHttpClientExceptions($exception, $io
->reveal(), [
$this,
'mockDt',
], 'http://api.apigee.com/test', $this->org, $this->email);
}
public function testHandleHttpClientExceptions302Code() {
$request = $this
->prophesize(RequestInterface::class);
$response = $this
->prophesize(Response::class);
$response
->getStatusCode()
->willReturn(302);
$exception = new ClientException('Forbidden', $request
->reveal(), $response
->reveal());
$io = $this
->prophesize(StyleInterface::class);
$io
->error(Argument::containingString('Error connecting to Apigee Edge'))
->shouldBeCalledTimes(1);
$io
->note(Argument::containingString('the url ' . $this->baseUrl . '/test' . ' does not seem to be a valid Apigee Edge endpoint.'))
->shouldBeCalledTimes(1);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$apigee_edge_management_cli_service
->handleHttpClientExceptions($exception, $io
->reveal(), [
$this,
'mockDt',
], $this->baseUrl . '/test', $this->org, $this->email);
}
public function testSetDefaultPermissions() {
$this->httpClient
->post(Argument::type('string'), Argument::type('array'))
->shouldBeCalledTimes(12);
$apigee_edge_management_cli_service = new ApigeeEdgeManagementCliService($this->httpClient
->reveal());
$apigee_edge_management_cli_service_reflection = new \ReflectionClass($apigee_edge_management_cli_service);
$method_set_default_permissions = $apigee_edge_management_cli_service_reflection
->getMethod('setDefaultPermissions');
$method_set_default_permissions
->setAccessible(TRUE);
$io = $this
->prophesize(StyleInterface::class);
$args = [
$io
->reveal(),
[
$this,
'mockDt',
],
$this->org,
$this->email,
$this->password,
$this->baseUrl,
$this->roleName,
];
$method_set_default_permissions
->invokeArgs($apigee_edge_management_cli_service, $args);
}
public function mockDt(string $message, array $context = []) : string {
return StringUtils::interpolate($message, $context);
}
}