AuthAdapterTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Rest;
  6. use Magento\TestFramework\Helper\Bootstrap;
  7. use Psr\Log\NullLogger;
  8. use Temando\Shipping\Rest\Adapter as RestAdapter;
  9. use Temando\Shipping\Rest\Request\AuthRequestInterface;
  10. use Temando\Shipping\Rest\Request\ItemRequestInterface;
  11. use Temando\Shipping\Rest\Request\ListRequestInterface;
  12. use Temando\Shipping\Rest\Response\DataObject\Session;
  13. use Temando\Shipping\Test\Integration\Provider\RestResponseProvider;
  14. use Temando\Shipping\Webservice\HttpClient;
  15. use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
  16. /**
  17. * AuthAdapterTest
  18. *
  19. * @magentoAppIsolation enabled
  20. *
  21. * @package Temando\Shipping\Test
  22. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link http://www.temando.com/
  25. */
  26. class AuthAdapterTest extends \PHPUnit\Framework\TestCase
  27. {
  28. /**
  29. * Delegate provisioning of test data to separate class
  30. * @return string[]
  31. */
  32. public function startSessionResponseDataProvider()
  33. {
  34. return RestResponseProvider::startSessionResponseDataProvider();
  35. }
  36. /**
  37. * Delegate provisioning of test data to separate class
  38. * @return string[]
  39. */
  40. public function startSessionValidationErrorResponseDataProvider()
  41. {
  42. return RestResponseProvider::startSessionValidationErrorResponseDataProvider();
  43. }
  44. /**
  45. * Delegate provisioning of test data to separate class
  46. * @return string[]
  47. */
  48. public function startSessionInvalidCredentialsResponseDataProvider()
  49. {
  50. return RestResponseProvider::startSessionBadRequestResponseDataProvider();
  51. }
  52. /**
  53. * @test
  54. *
  55. * @dataProvider startSessionResponseDataProvider
  56. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  57. * @magentoConfigFixture default/carriers/temando/session_endpoint https://api.temando.io/v1/
  58. *
  59. * @param string $jsonResponse
  60. */
  61. public function startSession($jsonResponse)
  62. {
  63. $testResponse = new \Zend\Http\Response();
  64. $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_200);
  65. $testResponse->setContent($jsonResponse);
  66. $testAdapter = new \Zend\Http\Client\Adapter\Test();
  67. $testAdapter->setResponse($testResponse);
  68. $zendClient = new \Zend\Http\Client();
  69. $zendClient->setAdapter($testAdapter);
  70. $httpClient = Bootstrap::getObjectManager()->create(HttpClient::class, ['client' => $zendClient]);
  71. $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  72. ->setMethods(['create'])
  73. ->disableOriginalConstructor()
  74. ->getMock();
  75. $clientFactoryMock
  76. ->expects($this->once())
  77. ->method('create')
  78. ->willReturn($httpClient);
  79. $restClient = Bootstrap::getObjectManager()->create(RestClient::class, [
  80. 'httpClientFactory' => $clientFactoryMock,
  81. ]);
  82. /** @var AuthRequestInterface $request */
  83. $request = Bootstrap::getObjectManager()->create(AuthRequestInterface::class, [
  84. 'accountId' => 'foo',
  85. 'bearerToken' => 'bar',
  86. 'scope' => AuthenticationInterface::AUTH_SCOPE_ADMIN,
  87. ]);
  88. /** @var AuthAdapter $adapter */
  89. $adapter = Bootstrap::getObjectManager()->create(AuthAdapter::class, [
  90. 'restClient' => $restClient,
  91. ]);
  92. $session = $adapter->startSession($request);
  93. $this->assertInstanceOf(Session::class, $session);
  94. $this->assertNotEmpty($session->getAttributes()->getSessionToken());
  95. $this->assertNotEmpty($session->getAttributes()->getExpiry());
  96. }
  97. /**
  98. * @test
  99. * @expectedException \Temando\Shipping\Rest\Exception\AdapterException
  100. *
  101. * @dataProvider startSessionValidationErrorResponseDataProvider
  102. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  103. * @magentoConfigFixture default/carriers/temando/session_endpoint https://api.temando.io/v1/
  104. *
  105. * @param string $jsonResponse
  106. */
  107. public function invalidAccountIdThrowsException($jsonResponse)
  108. {
  109. $testResponse = new \Zend\Http\Response();
  110. $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_422);
  111. $testResponse->setContent($jsonResponse);
  112. $testAdapter = new \Zend\Http\Client\Adapter\Test();
  113. $testAdapter->setResponse($testResponse);
  114. $zendClient = new \Zend\Http\Client();
  115. $zendClient->setAdapter($testAdapter);
  116. $httpClient = Bootstrap::getObjectManager()->create(HttpClient::class, ['client' => $zendClient]);
  117. $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  118. ->setMethods(['create'])
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $clientFactoryMock
  122. ->expects($this->once())
  123. ->method('create')
  124. ->willReturn($httpClient);
  125. $restClient = Bootstrap::getObjectManager()->create(RestClient::class, [
  126. 'httpClientFactory' => $clientFactoryMock,
  127. ]);
  128. /** @var AuthRequestInterface $request */
  129. $request = Bootstrap::getObjectManager()->create(AuthRequestInterface::class, [
  130. 'accountId' => 'foo',
  131. 'bearerToken' => 'bar',
  132. 'scope' => AuthenticationInterface::AUTH_SCOPE_ADMIN,
  133. ]);
  134. /** @var AuthAdapter $adapter */
  135. $adapter = Bootstrap::getObjectManager()->create(AuthAdapter::class, [
  136. 'restClient' => $restClient,
  137. ]);
  138. $adapter->startSession($request);
  139. }
  140. /**
  141. * @test
  142. * @expectedException \Temando\Shipping\Rest\Exception\AdapterException
  143. *
  144. * @dataProvider startSessionInvalidCredentialsResponseDataProvider
  145. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  146. * @magentoConfigFixture default/carriers/temando/session_endpoint https://api.temando.io/v1/
  147. *
  148. * @param string $jsonResponse
  149. */
  150. public function invalidBearerTokenThrowsException($jsonResponse)
  151. {
  152. $testResponse = new \Zend\Http\Response();
  153. $testResponse->setStatusCode(\Zend\Http\Response::STATUS_CODE_400);
  154. $testResponse->setContent($jsonResponse);
  155. $testAdapter = new \Zend\Http\Client\Adapter\Test();
  156. $testAdapter->setResponse($testResponse);
  157. $zendClient = new \Zend\Http\Client();
  158. $zendClient->setAdapter($testAdapter);
  159. $httpClient = Bootstrap::getObjectManager()->create(HttpClient::class, ['client' => $zendClient]);
  160. $clientFactoryMock = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  161. ->setMethods(['create'])
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $clientFactoryMock
  165. ->expects($this->once())
  166. ->method('create')
  167. ->willReturn($httpClient);
  168. $restClient = Bootstrap::getObjectManager()->create(RestClient::class, [
  169. 'httpClientFactory' => $clientFactoryMock,
  170. ]);
  171. /** @var AuthRequestInterface $request */
  172. $request = Bootstrap::getObjectManager()->create(AuthRequestInterface::class, [
  173. 'accountId' => 'foo',
  174. 'bearerToken' => 'bar',
  175. 'scope' => AuthenticationInterface::AUTH_SCOPE_ADMIN,
  176. ]);
  177. /** @var AuthAdapter $adapter */
  178. $adapter = Bootstrap::getObjectManager()->create(AuthAdapter::class, [
  179. 'restClient' => $restClient,
  180. ]);
  181. $adapter->startSession($request);
  182. }
  183. /**
  184. * @test
  185. *
  186. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  187. * @magentoConfigFixture default/carriers/temando/session_endpoint https://api.temando.io/v1/
  188. */
  189. public function missingAuthCredentialsReturnsEmptyList()
  190. {
  191. /** @var ListRequestInterface $request */
  192. $request = Bootstrap::getObjectManager()->create(ListRequestInterface::class, ['offset' => 0, 'limit' => 20]);
  193. /** @var RestAdapter $adapter */
  194. $adapter = Bootstrap::getObjectManager()->create(RestAdapter::class, [
  195. 'logger' => new NullLogger(),
  196. ]);
  197. $this->assertEmpty($adapter->getCarrierIntegrations($request));
  198. }
  199. /**
  200. * @test
  201. * @expectedException \Temando\Shipping\Rest\Exception\AdapterException
  202. *
  203. * @magentoConfigFixture default/carriers/temando/logging_enabled 0
  204. * @magentoConfigFixture default/carriers/temando/session_endpoint https://api.temando.io/v1/
  205. */
  206. public function missingAuthCredentialsThrowsException()
  207. {
  208. /** @var ItemRequestInterface $request */
  209. $request = Bootstrap::getObjectManager()->create(ItemRequestInterface::class, ['entityId' => 'foo']);
  210. /** @var RestAdapter $adapter */
  211. $adapter = Bootstrap::getObjectManager()->create(RestAdapter::class);
  212. $adapter->getCompletion($request);
  213. }
  214. }