AdminLoginObserverTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Observer;
  6. use Magento\Framework\Message\Manager as MessageManager;
  7. use Magento\Framework\Session\SessionManagerInterface;
  8. use Magento\TestFramework\Helper\Bootstrap;
  9. use Temando\Shipping\Rest\AuthenticationInterface;
  10. use Temando\Shipping\Webservice\HttpClientInterfaceFactory;
  11. use Temando\Shipping\Model\Shipping\Carrier;
  12. use Temando\Shipping\Rest\AuthAdapter as RestAdapter;
  13. use Temando\Shipping\Rest\Authentication;
  14. use Temando\Shipping\Rest\RestClient;
  15. use Temando\Shipping\Test\Integration\Provider\RestResponseProvider;
  16. use Temando\Shipping\Webservice\Exception\HttpResponseException;
  17. use Temando\Shipping\Webservice\HttpClient;
  18. /**
  19. * AdminLoginObserverTest
  20. *
  21. * @package Temando\Shipping\Test\Integration
  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 AdminLoginObserverTest extends \PHPUnit\Framework\TestCase
  27. {
  28. /**
  29. * @var \Magento\Framework\Event\Invoker\InvokerDefault
  30. */
  31. private $invoker;
  32. /**
  33. * @var \Magento\Framework\Event\Observer
  34. */
  35. private $observer;
  36. /**
  37. * @var MessageManager|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $messageManager;
  40. /**
  41. * @var HttpClientInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  42. */
  43. private $httpClientFactory;
  44. /**
  45. * @var HttpClient|\PHPUnit_Framework_MockObject_MockObject
  46. */
  47. private $httpClient;
  48. /**
  49. * Delegate provisioning of test data to separate class
  50. * @return string[]
  51. */
  52. public function startSessionSuccessResponseDataProvider()
  53. {
  54. return RestResponseProvider::startSessionResponseDataProvider();
  55. }
  56. /**
  57. * Delegate provisioning of test data to separate class
  58. * @return string[]
  59. */
  60. public function startSessionFailureResponseDataProvider()
  61. {
  62. return RestResponseProvider::startSessionValidationErrorResponseDataProvider();
  63. }
  64. /**
  65. * Init object manager
  66. */
  67. public function setUp()
  68. {
  69. parent::setUp();
  70. /** @var SessionManagerInterface $adminSession */
  71. $adminSession = Bootstrap::getObjectManager()->get(SessionManagerInterface::class);
  72. $adminSession->setData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY, '1999-01-19T03:03:33.000Z');
  73. /** @var \Magento\TestFramework\ObjectManager $objectManager */
  74. $objectManager = Bootstrap::getObjectManager();
  75. $this->invoker = $objectManager->get(\Magento\Framework\Event\InvokerInterface::class);
  76. $this->observer = $objectManager->get(\Magento\Framework\Event\Observer::class);
  77. $carrierMock = $this->getMockBuilder(Carrier::class)
  78. ->setMethods(['getConfigFlag'])
  79. ->disableOriginalConstructor()
  80. ->getMock();
  81. $carrierMock->expects($this->once())
  82. ->method('getConfigFlag')
  83. ->with('active')
  84. ->willReturn(($this->getName(false) !== 'carrierIsNotActive'));
  85. $objectManager->addSharedInstance($carrierMock, Carrier::class);
  86. // prepare the http connection to be mocked
  87. $this->httpClient = $this->getMockBuilder(HttpClient::class)
  88. ->setMethods(['send'])
  89. ->setConstructorArgs(['client' => new \Zend\Http\Client()])
  90. ->getMock();
  91. $this->httpClientFactory = $this->getMockBuilder(HttpClientInterfaceFactory::class)
  92. ->setMethods(['create'])
  93. ->disableOriginalConstructor()
  94. ->getMock();
  95. $this->messageManager = $this->getMockBuilder(MessageManager::class)
  96. ->setMethods(['addWarningMessage', 'addExceptionMessage'])
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $objectManager->addSharedInstance($this->messageManager, MessageManager::class);
  100. $restClient = $objectManager->create(RestClient::class, [
  101. 'httpClientFactory' => $this->httpClientFactory,
  102. ]);
  103. $objectManager->addSharedInstance($restClient, RestClient::class);
  104. }
  105. protected function tearDown()
  106. {
  107. /** @var \Magento\TestFramework\ObjectManager $objectManager */
  108. $objectManager = Bootstrap::getObjectManager();
  109. /** @var SessionManagerInterface $adminSession */
  110. $adminSession = $objectManager->get(SessionManagerInterface::class);
  111. $adminSession->unsetData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY);
  112. $objectManager->removeSharedInstance(AdminLoginObserver::class);
  113. $objectManager->removeSharedInstance(Authentication::class);
  114. $objectManager->removeSharedInstance(RestAdapter::class);
  115. parent::tearDown();
  116. }
  117. /**
  118. * @test
  119. * @magentoAppArea adminhtml
  120. * @magentoConfigFixture default/carriers/temando/account_id accountId
  121. * @magentoConfigFixture default/carriers/temando/bearer_token bearerToken
  122. */
  123. public function carrierIsNotActive()
  124. {
  125. $this->httpClient
  126. ->expects($this->never())
  127. ->method('send');
  128. $this->httpClientFactory
  129. ->expects($this->never())
  130. ->method('create');
  131. $this->messageManager
  132. ->expects($this->never())
  133. ->method('addWarningMessage');
  134. $this->messageManager
  135. ->expects($this->never())
  136. ->method('addExceptionMessage');
  137. $config = [
  138. 'instance' => AdminLoginObserver::class,
  139. 'name' => 'temando_admin_login',
  140. ];
  141. $this->invoker->dispatch($config, $this->observer);
  142. }
  143. /**
  144. * @test
  145. * @magentoAppArea adminhtml
  146. */
  147. public function carrierIsActiveButCredentialsAreMissing()
  148. {
  149. $this->httpClient
  150. ->expects($this->never())
  151. ->method('send');
  152. $this->httpClientFactory
  153. ->expects($this->never())
  154. ->method('create');
  155. $this->messageManager
  156. ->expects($this->once())
  157. ->method('addWarningMessage');
  158. $this->messageManager
  159. ->expects($this->never())
  160. ->method('addExceptionMessage');
  161. $config = [
  162. 'instance' => AdminLoginObserver::class,
  163. 'name' => 'temando_admin_login',
  164. ];
  165. $this->invoker->dispatch($config, $this->observer);
  166. }
  167. /**
  168. * @test
  169. * @magentoAppArea adminhtml
  170. * @magentoConfigFixture default/carriers/temando/bearer_token foo
  171. * @magentoConfigFixture default/carriers/temando/account_id bar
  172. * @dataProvider startSessionSuccessResponseDataProvider
  173. *
  174. * @param string $jsonResponse
  175. */
  176. public function sessionRefreshSuccess($jsonResponse)
  177. {
  178. $this->httpClient
  179. ->expects($this->once())
  180. ->method('send')
  181. ->willReturn($jsonResponse);
  182. $this->httpClientFactory
  183. ->expects($this->once())
  184. ->method('create')
  185. ->willReturn($this->httpClient);
  186. $this->messageManager
  187. ->expects($this->never())
  188. ->method('addWarningMessage');
  189. $this->messageManager
  190. ->expects($this->never())
  191. ->method('addExceptionMessage');
  192. $config = [
  193. 'instance' => AdminLoginObserver::class,
  194. 'name' => 'temando_admin_login',
  195. ];
  196. $this->invoker->dispatch($config, $this->observer);
  197. }
  198. /**
  199. * @test
  200. * @magentoAppArea adminhtml
  201. * @magentoConfigFixture default/carriers/temando/bearer_token foo
  202. * @magentoConfigFixture default/carriers/temando/account_id bar
  203. * @dataProvider startSessionFailureResponseDataProvider
  204. *
  205. * @param string $jsonResponse
  206. */
  207. public function sessionRefreshFailure($jsonResponse)
  208. {
  209. /** @var SessionManagerInterface $adminSession */
  210. $adminSession = Bootstrap::getObjectManager()->get(SessionManagerInterface::class);
  211. $adminSession->setData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY, '1999-01-19T03:03:33.000Z');
  212. $httpException = new HttpResponseException($jsonResponse);
  213. $this->httpClient
  214. ->expects($this->once())
  215. ->method('send')
  216. ->willThrowException($httpException);
  217. $this->httpClientFactory
  218. ->expects($this->once())
  219. ->method('create')
  220. ->willReturn($this->httpClient);
  221. $this->messageManager
  222. ->expects($this->never())
  223. ->method('addWarningMessage');
  224. $this->messageManager
  225. ->expects($this->once())
  226. ->method('addExceptionMessage');
  227. $config = [
  228. 'instance' => AdminLoginObserver::class,
  229. 'name' => 'temando_admin_login',
  230. ];
  231. $this->invoker->dispatch($config, $this->observer);
  232. }
  233. }