AuthServiceTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\Backend\Model\Session as BackendSession;
  7. use Magento\Framework\Exception\SessionException;
  8. use Magento\Framework\Session\SessionManagerInterface;
  9. use Magento\Framework\Session\Storage;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Temando\Shipping\Rest\Exception\AdapterException;
  12. use Temando\Shipping\Rest\Response\DataObject\Session;
  13. /**
  14. * Temando Session Handling Test
  15. *
  16. * @package Temando\Shipping\Test\Integration
  17. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. * @link http://www.temando.com/
  20. */
  21. class AuthServiceTest extends \PHPUnit\Framework\TestCase
  22. {
  23. /**
  24. * @var BackendSession|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $storageMock;
  27. /**
  28. * @var SessionManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $sessionManager;
  31. /**
  32. * @return string[]
  33. */
  34. public function invalidCredentialsDataProvider()
  35. {
  36. return [
  37. 'no_credentials' => [null, null],
  38. 'no_account_id' => ['23', null],
  39. 'no_bearer_token' => [null, '808'],
  40. ];
  41. }
  42. protected function setUp()
  43. {
  44. parent::setUp();
  45. $this->storageMock = $this->getMockBuilder(Storage::class)
  46. ->setMethods(['getData', 'setData'])
  47. ->getMock();
  48. $this->sessionManager = Bootstrap::getObjectManager()->create(
  49. SessionManagerInterface::class,
  50. ['storage' => $this->storageMock]
  51. );
  52. }
  53. /**
  54. * Assert token being requested from API if there is no expiry date available.
  55. *
  56. * @test
  57. */
  58. public function sessionTokenExpiryDateUnavailable()
  59. {
  60. $currentTokenExpiry = null;
  61. $newSessionToken = 'foo';
  62. $newSessionTokenExpiry = '2038';
  63. $newSessionResponseAttributes = new \Temando\Shipping\Rest\Response\Fields\SessionAttributes();
  64. $newSessionResponseAttributes->setSessionToken($newSessionToken);
  65. $newSessionResponseAttributes->setExpiry($newSessionTokenExpiry);
  66. $newSessionResponse = new Session();
  67. $newSessionResponse->setAttributes($newSessionResponseAttributes);
  68. $this->storageMock->expects($this->once())
  69. ->method('getData')
  70. ->with(Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, null)
  71. ->willReturn($currentTokenExpiry);
  72. $this->storageMock->expects($this->exactly(2))
  73. ->method('setData')
  74. ->withConsecutive(
  75. [Authentication::DATA_KEY_SESSION_TOKEN, $newSessionToken],
  76. [Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, $newSessionTokenExpiry]
  77. );
  78. // $storageMock = $this->getMockBuilder(Storage::class)
  79. // ->setMethods(['getData', 'setData'])
  80. // ->getMock();
  81. // $storageMock->expects($this->once())
  82. // ->method('getData')
  83. // ->with(Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, null)
  84. // ->willReturn($currentTokenExpiry);
  85. // $storageMock->expects($this->exactly(2))
  86. // ->method('setData')
  87. // ->withConsecutive(
  88. // [Authentication::DATA_KEY_SESSION_TOKEN, $newSessionToken],
  89. // [Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, $newSessionTokenExpiry]
  90. // );
  91. // $session = Bootstrap::getObjectManager()->create(Session::class, ['storage' => $storageMock]);
  92. $adapterMock = $this->getMockBuilder(AuthAdapter::class)
  93. ->setMethods(['startSession'])
  94. ->disableOriginalConstructor()
  95. ->getMock();
  96. $adapterMock->expects($this->once())
  97. ->method('startSession')
  98. ->willReturn($newSessionResponse);
  99. /** @var Authentication $auth */
  100. $auth = Bootstrap::getObjectManager()->create(Authentication::class, [
  101. 'session' => $this->sessionManager,
  102. 'apiAdapter' => $adapterMock,
  103. ]);
  104. $auth->connect('foo', 'bar');
  105. }
  106. /**
  107. * Assert AuthenticationException being thrown when API returns error.
  108. *
  109. * @test
  110. * @expectedException \Magento\Framework\Exception\AuthenticationException
  111. */
  112. public function sessionTokenRefreshFails()
  113. {
  114. $currentTokenExpiry = '1999-01-19T03:03:33.000Z';
  115. $exceptionMessage = 'error foo';
  116. $this->storageMock->expects($this->once())
  117. ->method('getData')
  118. ->with(Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, null)
  119. ->willReturn($currentTokenExpiry);
  120. $this->storageMock->expects($this->never())
  121. ->method('setData');
  122. $adapterMock = $this->getMockBuilder(AuthAdapter::class)
  123. ->setMethods(['startSession'])
  124. ->disableOriginalConstructor()
  125. ->getMock();
  126. $adapterMock->expects($this->once())
  127. ->method('startSession')
  128. ->willThrowException(new AdapterException($exceptionMessage));
  129. /** @var Authentication $auth */
  130. $auth = Bootstrap::getObjectManager()->create(Authentication::class, [
  131. 'session' => $this->sessionManager,
  132. 'apiAdapter' => $adapterMock,
  133. ]);
  134. $auth->connect('foo', 'bar');
  135. }
  136. /**
  137. * @test
  138. */
  139. public function sessionTokenExpired()
  140. {
  141. $currentTokenExpiry = '1999-01-19T03:03:33.000Z';
  142. $newSessionToken = 'foo';
  143. $newSessionTokenExpiry = '2038';
  144. $newSessionResponseAttributes = new \Temando\Shipping\Rest\Response\Fields\SessionAttributes();
  145. $newSessionResponseAttributes->setSessionToken($newSessionToken);
  146. $newSessionResponseAttributes->setExpiry($newSessionTokenExpiry);
  147. $newSessionResponse = new Session();
  148. $newSessionResponse->setAttributes($newSessionResponseAttributes);
  149. $this->storageMock->expects($this->once())
  150. ->method('getData')
  151. ->with(Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, null)
  152. ->willReturn($currentTokenExpiry);
  153. $this->storageMock->expects($this->exactly(2))
  154. ->method('setData')
  155. ->withConsecutive(
  156. [Authentication::DATA_KEY_SESSION_TOKEN, $newSessionToken],
  157. [Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, $newSessionTokenExpiry]
  158. );
  159. $adapterMock = $this->getMockBuilder(AuthAdapter::class)
  160. ->setMethods(['startSession'])
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $adapterMock->expects($this->once())
  164. ->method('startSession')
  165. ->willReturn($newSessionResponse);
  166. /** @var Authentication $auth */
  167. $auth = Bootstrap::getObjectManager()->create(Authentication::class, [
  168. 'session' => $this->sessionManager,
  169. 'apiAdapter' => $adapterMock,
  170. ]);
  171. $auth->connect('foo', 'bar');
  172. }
  173. /**
  174. * @test
  175. */
  176. public function sessionTokenValid()
  177. {
  178. $currentTokenExpiry = '2038-01-19T03:03:33.000Z';
  179. $this->storageMock->expects($this->once())
  180. ->method('getData')
  181. ->with(Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, null)
  182. ->willReturn($currentTokenExpiry);
  183. $this->storageMock->expects($this->never())
  184. ->method('setData');
  185. $adapterMock = $this->getMockBuilder(AuthAdapter::class)
  186. ->setMethods(['startSession'])
  187. ->disableOriginalConstructor()
  188. ->getMock();
  189. $adapterMock->expects($this->never())
  190. ->method('startSession');
  191. /** @var Authentication $auth */
  192. $auth = Bootstrap::getObjectManager()->create(Authentication::class, [
  193. 'session' => $this->sessionManager,
  194. 'apiAdapter' => $adapterMock,
  195. ]);
  196. $auth->connect('foo', 'bar');
  197. }
  198. /**
  199. * @test
  200. * @dataProvider invalidCredentialsDataProvider
  201. * @expectedException \Magento\Framework\Exception\InputException
  202. *
  203. * @param string $bearerToken
  204. * @param string $accountId
  205. */
  206. public function credentialsMissing($bearerToken, $accountId)
  207. {
  208. $currentTokenExpiry = '1999-01-19T03:03:33.000Z';
  209. $this->storageMock->expects($this->once())
  210. ->method('getData')
  211. ->with(Authentication::DATA_KEY_SESSION_TOKEN_EXPIRY, null)
  212. ->willReturn($currentTokenExpiry);
  213. $this->storageMock->expects($this->never())
  214. ->method('setData');
  215. /** @var Authentication $auth */
  216. $auth = Bootstrap::getObjectManager()->create(Authentication::class, [
  217. 'session' => $this->sessionManager,
  218. ]);
  219. $auth->connect($bearerToken, $accountId);
  220. }
  221. /**
  222. * @test
  223. * @magentoAppArea adminhtml
  224. */
  225. public function disconnect()
  226. {
  227. $currentToken = 'abcde';
  228. $currentTokenExpiry = '1999-01-19T03:03:33.000Z';
  229. /** @var SessionManagerInterface $adminSession */
  230. $adminSession = Bootstrap::getObjectManager()->get(SessionManagerInterface::class);
  231. $adminSession->setData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN, $currentToken);
  232. $adminSession->setData(AuthenticationInterface::DATA_KEY_SESSION_TOKEN_EXPIRY, $currentTokenExpiry);
  233. $adapterMock = $this->getMockBuilder(AuthAdapter::class)
  234. ->setMethods(['endSession'])
  235. ->disableOriginalConstructor()
  236. ->getMock();
  237. $adapterMock->expects($this->once())
  238. ->method('endSession');
  239. /** @var Authentication $auth */
  240. $auth = Bootstrap::getObjectManager()->create(Authentication::class, [
  241. 'session' => $adminSession,
  242. 'apiAdapter' => $adapterMock,
  243. ]);
  244. // before disconnect
  245. $this->assertEquals($currentToken, $auth->getSessionToken());
  246. $this->assertEquals($currentTokenExpiry, $auth->getSessionTokenExpiry());
  247. $auth->disconnect();
  248. // after disconnect
  249. $this->assertEmpty($auth->getSessionToken());
  250. $this->assertEmpty($auth->getSessionTokenExpiry());
  251. }
  252. }