OauthServiceTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. <?php
  2. /**
  3. * Test for \Magento\Integration\Model\OauthService
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Integration\Test\Unit\Model;
  9. use Magento\Integration\Model\Oauth\Token;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class OauthServiceTest extends \PHPUnit\Framework\TestCase
  14. {
  15. const VALUE_CONSUMER_ID = 1;
  16. const VALUE_CONSUMER_KEY = 'asdfghjklaqwerfdtyuiomnbgfdhbsoi';
  17. const VALUE_TOKEN_TYPE = 'access';
  18. /** @var \Magento\Integration\Model\Oauth\ConsumerFactory|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $_consumerFactory;
  20. /** @var \Magento\Integration\Model\Oauth\Token\Provider|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $_tokenProviderMock;
  22. /** @var \Magento\Integration\Model\Oauth\Consumer|\PHPUnit_Framework_MockObject_MockObject */
  23. private $_consumerMock;
  24. /** @var \Magento\Integration\Model\Integration|\PHPUnit_Framework_MockObject_MockObject */
  25. private $_emptyConsumerMock;
  26. /**
  27. * @var \Magento\Integration\Model\Oauth\Token|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $_tokenMock;
  30. /** @var \Magento\Integration\Model\OauthService */
  31. private $_service;
  32. /** @var array */
  33. private $_consumerData;
  34. /**
  35. * @var \Magento\Integration\Model\Oauth\TokenFactory|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $_tokenFactoryMock;
  38. /**
  39. * @return void
  40. */
  41. protected function setUp()
  42. {
  43. $this->_consumerFactory = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['create'])
  46. ->getMock();
  47. $this->_tokenProviderMock = $this->getMockBuilder(
  48. \Magento\Integration\Model\Oauth\Token\Provider::class
  49. )->disableOriginalConstructor()->getMock();
  50. $this->_tokenMock = $this->getMockBuilder(
  51. \Magento\Integration\Model\Oauth\Token::class
  52. )->disableOriginalConstructor()->setMethods(
  53. ['createVerifierToken', 'getType', '__wakeup', 'delete']
  54. )->getMock();
  55. $this->_tokenFactoryMock = $this->createPartialMock(
  56. \Magento\Integration\Model\Oauth\TokenFactory::class,
  57. ['create']
  58. );
  59. $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
  60. $this->_consumerMock = $this->getMockBuilder(
  61. \Magento\Integration\Model\Oauth\Consumer::class
  62. )->disableOriginalConstructor()->setMethods(
  63. ['getData', 'getId', 'load', 'save', 'delete', '__wakeup']
  64. )->getMock();
  65. $this->_consumerData = [
  66. 'entity_id' => self::VALUE_CONSUMER_ID,
  67. 'key' => self::VALUE_CONSUMER_KEY,
  68. 'secret' => 'iuyytrfdsdfbnnhbmkkjlkjl',
  69. 'created_at' => '',
  70. 'updated_at' => '',
  71. 'callback_url' => '',
  72. 'rejected_callback_url' => ''
  73. ];
  74. $this->_consumerFactory->expects(
  75. $this->any()
  76. )->method(
  77. 'create'
  78. )->will(
  79. $this->returnValue($this->_consumerMock)
  80. );
  81. $this->_service = new \Magento\Integration\Model\OauthService(
  82. $this->createMock(\Magento\Store\Model\StoreManagerInterface::class),
  83. $this->_consumerFactory,
  84. $this->_tokenFactoryMock,
  85. $this->createMock(\Magento\Integration\Helper\Oauth\Data::class),
  86. $this->createMock(\Magento\Framework\HTTP\ZendClient::class),
  87. $this->createMock(\Psr\Log\LoggerInterface::class),
  88. $this->createMock(\Magento\Framework\Oauth\Helper\Oauth::class),
  89. $this->_tokenProviderMock
  90. );
  91. $this->_emptyConsumerMock = $this->getMockBuilder(
  92. \Magento\Integration\Model\Integration::class
  93. )->disableOriginalConstructor()->setMethods(
  94. ['getData', 'load', 'getId', 'save', 'delete', '__wakeup']
  95. )->getMock();
  96. $this->_emptyConsumerMock->expects($this->any())->method('getId')->will($this->returnValue(null));
  97. }
  98. /**
  99. * @return void
  100. */
  101. public function testDelete()
  102. {
  103. $this->_consumerMock->expects(
  104. $this->once()
  105. )->method(
  106. 'getId'
  107. )->will(
  108. $this->returnValue(self::VALUE_CONSUMER_ID)
  109. );
  110. $this->_consumerMock->expects(
  111. $this->once()
  112. )->method(
  113. 'load'
  114. )->with(
  115. self::VALUE_CONSUMER_ID
  116. )->will(
  117. $this->returnValue($this->_consumerMock)
  118. );
  119. $this->_consumerMock->expects($this->once())->method('delete')->will($this->returnValue($this->_consumerMock));
  120. $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
  121. $consumerData = $this->_service->deleteConsumer(self::VALUE_CONSUMER_ID);
  122. $this->assertEquals($this->_consumerData['entity_id'], $consumerData['entity_id']);
  123. }
  124. /**
  125. * @return void
  126. * @expectedException \Magento\Framework\Exception\IntegrationException
  127. * @expectedExceptionMessage A consumer with ID "1" doesn't exist. Verify the ID and try again.
  128. */
  129. public function testDeleteException()
  130. {
  131. $this->_consumerMock->expects($this->any())->method('getId')->will($this->returnValue(null));
  132. $this->_consumerMock->expects($this->once())->method('load')->will($this->returnSelf());
  133. $this->_consumerMock->expects($this->never())->method('delete');
  134. $this->_service->deleteConsumer(self::VALUE_CONSUMER_ID);
  135. }
  136. /**
  137. * @return void
  138. */
  139. public function testCreateAccessTokenAndClearExisting()
  140. {
  141. $this->_consumerMock->expects(
  142. $this->any()
  143. )->method(
  144. 'load'
  145. )->with(
  146. self::VALUE_CONSUMER_ID
  147. )->will(
  148. $this->returnValue($this->_consumerMock)
  149. );
  150. $this->_tokenProviderMock->expects(
  151. $this->any()
  152. )->method(
  153. 'getIntegrationTokenByConsumerId'
  154. )->will(
  155. $this->returnValue($this->_tokenMock)
  156. );
  157. $this->_tokenProviderMock->expects($this->any())->method('createRequestToken')->with($this->_consumerMock);
  158. $this->_tokenProviderMock->expects($this->any())->method('getAccessToken')->with($this->_consumerMock);
  159. $this->_tokenFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->_tokenMock));
  160. $this->_tokenMock->expects($this->once())->method('delete');
  161. $this->_tokenMock->expects($this->once())->method('createVerifierToken')->with(self::VALUE_CONSUMER_ID);
  162. $this->_tokenProviderMock->expects($this->once())->method('createRequestToken')->with($this->_consumerMock);
  163. $this->_tokenProviderMock->expects($this->once())->method('getAccessToken')->with($this->_consumerMock);
  164. $this->assertTrue($this->_service->createAccessToken(self::VALUE_CONSUMER_ID, true));
  165. }
  166. /**
  167. * @return void
  168. */
  169. public function testCreateAccessTokenWithoutClearingExisting()
  170. {
  171. $this->_consumerMock->expects(
  172. $this->any()
  173. )->method(
  174. 'load'
  175. )->with(
  176. self::VALUE_CONSUMER_ID
  177. )->will(
  178. $this->returnValue($this->_consumerMock)
  179. );
  180. $this->_tokenProviderMock->expects(
  181. $this->any()
  182. )->method(
  183. 'getIntegrationTokenByConsumerId'
  184. )->will(
  185. $this->returnValue($this->_tokenMock)
  186. );
  187. $this->_tokenMock->expects($this->never())->method('delete');
  188. $this->assertFalse($this->_service->createAccessToken(self::VALUE_CONSUMER_ID, false));
  189. }
  190. /**
  191. * @return void
  192. */
  193. public function testCreateAccessTokenInvalidConsumerId()
  194. {
  195. $this->_consumerMock->expects(
  196. $this->any()
  197. )->method(
  198. 'load'
  199. )->with(
  200. 0
  201. )->will(
  202. $this->returnValue($this->_consumerMock)
  203. );
  204. $this->_tokenProviderMock->expects(
  205. $this->any()
  206. )->method(
  207. 'getIntegrationTokenByConsumerId'
  208. )->will(
  209. $this->throwException(
  210. new \Magento\Framework\Oauth\Exception(
  211. __('A token with consumer ID 0 does not exist')
  212. )
  213. )
  214. );
  215. $this->_tokenMock->expects($this->never())->method('delete');
  216. $this->_tokenFactoryMock->expects(
  217. $this->once()
  218. )->method(
  219. 'create'
  220. )->will(
  221. $this->returnValue($this->_tokenMock)
  222. );
  223. $this->_tokenMock->expects($this->once())->method('createVerifierToken');
  224. $this->_tokenProviderMock->expects($this->once())->method('createRequestToken');
  225. $this->_tokenProviderMock->expects($this->once())->method('getAccessToken');
  226. $this->assertTrue($this->_service->createAccessToken(0, false));
  227. }
  228. /**
  229. * @return void
  230. */
  231. public function testLoadConsumer()
  232. {
  233. $this->_consumerMock->expects(
  234. $this->once()
  235. )->method(
  236. 'load'
  237. )->with(
  238. self::VALUE_CONSUMER_ID
  239. )->will(
  240. $this->returnValue($this->_consumerMock)
  241. );
  242. $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
  243. $consumer = $this->_service->loadConsumer(self::VALUE_CONSUMER_ID);
  244. $consumerData = $consumer->getData();
  245. $this->assertEquals($this->_consumerData['entity_id'], $consumerData['entity_id']);
  246. }
  247. /**
  248. * @return void
  249. * @expectedException \Magento\Framework\Oauth\Exception
  250. */
  251. public function testLoadConsumerException()
  252. {
  253. $this->_consumerMock->expects(
  254. $this->once()
  255. )->method(
  256. 'load'
  257. )->will(
  258. $this->throwException(
  259. new \Magento\Framework\Oauth\Exception(
  260. __(
  261. "The oAuth consumer account couldn't be loaded due to an unexpected error. "
  262. . "Please try again later."
  263. )
  264. )
  265. )
  266. );
  267. $this->_service->loadConsumer(self::VALUE_CONSUMER_ID);
  268. $this->expectExceptionMessage(
  269. "The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later."
  270. );
  271. }
  272. /**
  273. * @return void
  274. */
  275. public function testLoadConsumerByKey()
  276. {
  277. $this->_consumerMock->expects(
  278. $this->once()
  279. )->method(
  280. 'load'
  281. )->with(
  282. self::VALUE_CONSUMER_KEY,
  283. 'key'
  284. )->will(
  285. $this->returnValue($this->_consumerMock)
  286. );
  287. $this->_consumerMock->expects($this->any())->method('getData')->will($this->returnValue($this->_consumerData));
  288. $consumer = $this->_service->loadConsumerByKey(self::VALUE_CONSUMER_KEY);
  289. $consumerData = $consumer->getData();
  290. $this->assertEquals($this->_consumerData['key'], $consumerData['key']);
  291. }
  292. /**
  293. * @return void
  294. * @expectedException \Magento\Framework\Oauth\Exception
  295. */
  296. public function testLoadConsumerByKeyException()
  297. {
  298. $this->_consumerMock->expects(
  299. $this->once()
  300. )->method(
  301. 'load'
  302. )->will(
  303. $this->throwException(
  304. new \Magento\Framework\Oauth\Exception(
  305. __(
  306. "The oAuth consumer account couldn't be loaded due to an unexpected error. "
  307. . "Please try again later."
  308. )
  309. )
  310. )
  311. );
  312. $this->_service->loadConsumerByKey(self::VALUE_CONSUMER_KEY);
  313. $this->expectExceptionMessage(
  314. "The oAuth consumer account couldn't be loaded due to an unexpected error. Please try again later."
  315. );
  316. }
  317. /**
  318. * @return void
  319. */
  320. public function testDeleteToken()
  321. {
  322. $this->_consumerMock->expects(
  323. $this->any()
  324. )->method(
  325. 'load'
  326. )->with(
  327. self::VALUE_CONSUMER_ID
  328. )->will(
  329. $this->returnValue($this->_consumerMock)
  330. );
  331. $this->_tokenProviderMock->expects(
  332. $this->any()
  333. )->method(
  334. 'getIntegrationTokenByConsumerId'
  335. )->will(
  336. $this->returnValue($this->_tokenMock)
  337. );
  338. $this->_tokenMock->expects($this->once())->method('delete');
  339. $this->assertTrue($this->_service->deleteIntegrationToken(self::VALUE_CONSUMER_ID));
  340. }
  341. /**
  342. * @return void
  343. */
  344. public function testDeleteTokenNegative()
  345. {
  346. $this->_consumerMock->expects(
  347. $this->any()
  348. )->method(
  349. 'load'
  350. )->with(
  351. self::VALUE_CONSUMER_ID
  352. )->will(
  353. $this->returnValue($this->_consumerMock)
  354. );
  355. $this->_tokenProviderMock->expects(
  356. $this->any()
  357. )->method(
  358. 'getIntegrationTokenByConsumerId'
  359. )->will(
  360. $this->returnValue($this->_tokenMock)
  361. );
  362. $this->_tokenMock->expects($this->never())->method('delete');
  363. $this->assertFalse($this->_service->deleteIntegrationToken(null));
  364. }
  365. /**
  366. * @return void
  367. */
  368. public function testGetAccessTokenNoAccess()
  369. {
  370. $this->_consumerMock->expects(
  371. $this->any()
  372. )->method(
  373. 'load'
  374. )->with(
  375. self::VALUE_CONSUMER_ID
  376. )->will(
  377. $this->returnValue($this->_consumerMock)
  378. );
  379. $this->_tokenProviderMock->expects(
  380. $this->any()
  381. )->method(
  382. 'getIntegrationTokenByConsumerId'
  383. )->will(
  384. $this->returnValue($this->_tokenMock)
  385. );
  386. $this->assertFalse($this->_service->getAccessToken(self::VALUE_CONSUMER_ID), false);
  387. }
  388. /**
  389. * @return void
  390. */
  391. public function testGetAccessSuccess()
  392. {
  393. $this->_consumerMock->expects(
  394. $this->any()
  395. )->method(
  396. 'load'
  397. )->with(
  398. self::VALUE_CONSUMER_ID
  399. )->will(
  400. $this->returnValue($this->_consumerMock)
  401. );
  402. $this->_tokenMock->expects($this->once())->method('getType')->will($this->returnValue(Token::TYPE_ACCESS));
  403. $this->_tokenProviderMock->expects(
  404. $this->any()
  405. )->method(
  406. 'getIntegrationTokenByConsumerId'
  407. )->will(
  408. $this->returnValue($this->_tokenMock)
  409. );
  410. $this->assertEquals($this->_service->getAccessToken(self::VALUE_CONSUMER_ID), $this->_tokenMock);
  411. }
  412. }