TokenTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Test\Unit\Model\Oauth;
  7. use Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory;
  8. use Magento\Integration\Model\Oauth\Token;
  9. use Magento\Framework\Oauth\Helper\Oauth as OauthHelper;
  10. use Magento\Authorization\Model\UserContextInterface;
  11. use Magento\Framework\TestFramework\Unit\Matcher\MethodInvokedAtIndex;
  12. /**
  13. * Unit test for \Magento\Integration\Model\Oauth\Nonce
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class TokenTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Integration\Model\Oauth\Token
  20. */
  21. protected $tokenModel;
  22. /**
  23. * @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $contextMock;
  26. /**
  27. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $registryMock;
  30. /**
  31. * @var KeyLengthFactory|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $keyLengthFactoryMock;
  34. /**
  35. * @var \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $validatorKeyLengthMock;
  38. /**
  39. * @var \Magento\Framework\Url\Validator|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $validatorMock;
  42. /**
  43. * @var \Magento\Integration\Model\Oauth\ConsumerFactory|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $consumerFactoryMock;
  46. /**
  47. * @var \Magento\Integration\Helper\Oauth\Data|\PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $oauthDataMock;
  50. /**
  51. * @var \Magento\Framework\Oauth\Helper\Oauth|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $oauthHelperMock;
  54. /**
  55. * @var \Magento\Framework\Model\ResourceModel\AbstractResource|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $resourceMock;
  58. protected function setUp()
  59. {
  60. $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
  61. ->setMethods(['getEventDispatcher'])
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
  65. ->disableOriginalConstructor()
  66. ->getMock();
  67. $this->validatorKeyLengthMock = $this->getMockBuilder(
  68. \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLength::class
  69. )
  70. ->setMethods(['isValid', 'setLength', 'setName', 'getMessages'])
  71. ->disableOriginalConstructor()
  72. ->getMock();
  73. $this->keyLengthFactoryMock = $this->getMockBuilder(
  74. \Magento\Integration\Model\Oauth\Consumer\Validator\KeyLengthFactory::class
  75. )
  76. ->setMethods(['create'])
  77. ->disableOriginalConstructor()
  78. ->getMock();
  79. $this->validatorMock = $this->getMockBuilder(\Magento\Framework\Url\Validator::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $this->consumerFactoryMock = $this->getMockBuilder(\Magento\Integration\Model\Oauth\ConsumerFactory::class)
  83. ->setMethods(['create'])
  84. ->disableOriginalConstructor()
  85. ->getMock();
  86. $this->oauthDataMock = $this->getMockBuilder(\Magento\Integration\Helper\Oauth\Data::class)
  87. ->setMethods(['isCleanupProbability', 'getCleanupExpirationPeriod'])
  88. ->disableOriginalConstructor()
  89. ->getMock();
  90. $this->oauthHelperMock = $this->getMockBuilder(\Magento\Framework\Oauth\Helper\Oauth::class)
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $this->resourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
  94. ->setMethods(
  95. [
  96. 'getIdFieldName',
  97. 'deleteOldEntries',
  98. '_construct',
  99. 'getConnection',
  100. 'selectTokenByType',
  101. 'save',
  102. 'selectTokenByConsumerIdAndUserType',
  103. 'selectTokenByAdminId',
  104. 'selectTokenByCustomerId',
  105. 'load'
  106. ]
  107. )
  108. ->disableOriginalConstructor()
  109. ->getMockForAbstractClass();
  110. $this->resourceMock->expects($this->any())
  111. ->method('getIdFieldName')
  112. ->willReturn('id');
  113. $eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
  114. ->setMethods(['dispatch'])
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $this->contextMock->expects($this->once())
  118. ->method('getEventDispatcher')
  119. ->willReturn($eventManagerMock);
  120. $this->tokenModel = new \Magento\Integration\Model\Oauth\Token(
  121. $this->contextMock,
  122. $this->registryMock,
  123. $this->keyLengthFactoryMock,
  124. $this->validatorMock,
  125. $this->consumerFactoryMock,
  126. $this->oauthDataMock,
  127. $this->oauthHelperMock,
  128. $this->resourceMock
  129. );
  130. }
  131. public function testAfterSave()
  132. {
  133. $this->oauthDataMock->expects($this->once())->method('isCleanupProbability')->willReturn(true);
  134. $this->oauthDataMock->expects($this->once())->method('getCleanupExpirationPeriod')->willReturn(30);
  135. $this->resourceMock->expects($this->once())->method('deleteOldEntries')->with(30);
  136. $this->assertEquals($this->tokenModel, $this->tokenModel->afterSave());
  137. }
  138. public function testAfterSaveNoCleanupProbability()
  139. {
  140. $this->oauthDataMock->expects($this->once())->method('isCleanupProbability')->willReturn(false);
  141. $this->oauthDataMock->expects($this->never())->method('getCleanupExpirationPeriod');
  142. $this->resourceMock->expects($this->never())->method('deleteOldEntries');
  143. $this->assertEquals($this->tokenModel, $this->tokenModel->afterSave());
  144. }
  145. public function testCreateVerifierToken()
  146. {
  147. $consumerId = 1;
  148. $this->resourceMock->expects($this->once())
  149. ->method('selectTokenByType')
  150. ->with($consumerId, Token::TYPE_VERIFIER)
  151. ->willReturn(['id' => 123]);
  152. $this->oauthHelperMock->expects($this->never())->method('generateToken');
  153. $this->oauthHelperMock->expects($this->never())->method('generateTokenSecret');
  154. $this->oauthHelperMock->expects($this->never())->method('generateVerifier');
  155. $this->validatorMock->expects($this->never())->method('isValid');
  156. $this->keyLengthFactoryMock->expects($this->never())->method('create');
  157. $this->resourceMock->expects($this->never())->method('save');
  158. $this->assertEquals($this->tokenModel, $this->tokenModel->createVerifierToken($consumerId));
  159. }
  160. public function testCreateVerifierTokenIfNoTokenId()
  161. {
  162. $consumerId = 1;
  163. $secret = 'secret';
  164. $token = 'token';
  165. $verifier = 'verifier';
  166. $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
  167. $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
  168. $this->oauthHelperMock->expects($this->once())->method('generateVerifier')->willReturn($verifier);
  169. $this->resourceMock->expects($this->once())
  170. ->method('selectTokenByType')
  171. ->with($consumerId, Token::TYPE_VERIFIER)
  172. ->willReturn([]);
  173. $this->tokenModel->setCallbackUrl(OauthHelper::CALLBACK_ESTABLISHED);
  174. $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
  175. $this->validatorKeyLengthMock
  176. );
  177. $this->validatorKeyLengthMock->expects($this->exactly(3))->method('setLength');
  178. $this->validatorKeyLengthMock->expects($this->exactly(3))->method('setName');
  179. $this->validatorKeyLengthMock->expects($this->exactly(3))->method('isValid')->willReturn(true);
  180. $this->resourceMock->expects($this->once())->method('save');
  181. $this->assertEquals($this->tokenModel, $this->tokenModel->createVerifierToken($consumerId));
  182. }
  183. /**
  184. * @expectedException \Magento\Framework\Oauth\Exception
  185. * @expectedExceptionMessage Cannot convert to access token due to token is not request type
  186. */
  187. public function testConvertToAccessIfIsNotRequestType()
  188. {
  189. $this->tokenModel->setType('isNotRequestType');
  190. $this->tokenModel->convertToAccess();
  191. }
  192. public function testConvertToAccess()
  193. {
  194. $token = 'token';
  195. $secret = 'secret';
  196. $this->tokenModel->setType(Token::TYPE_REQUEST);
  197. $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
  198. $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
  199. $this->resourceMock->expects($this->once())->method('save');
  200. $result = $this->tokenModel->convertToAccess();
  201. $this->assertEquals($this->tokenModel, $result);
  202. $this->assertEquals($token, $result->getToken());
  203. $this->assertEquals($secret, $result->getSecret());
  204. $this->assertEquals(UserContextInterface::USER_TYPE_INTEGRATION, $result->getUserType());
  205. }
  206. public function testCreateAdminToken()
  207. {
  208. $userId = 1;
  209. $token = 'token';
  210. $secret = 'secret';
  211. $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
  212. $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
  213. $this->resourceMock->expects($this->once())->method('save');
  214. $result = $this->tokenModel->createAdminToken($userId);
  215. $this->assertEquals($this->tokenModel, $result);
  216. $this->assertEquals($token, $result->getToken());
  217. $this->assertEquals($secret, $result->getSecret());
  218. $this->assertEquals($userId, $result->getAdminId());
  219. $this->assertEquals(UserContextInterface::USER_TYPE_ADMIN, $result->getUserType());
  220. }
  221. public function testCreateCustomerToken()
  222. {
  223. $userId = 1;
  224. $token = 'token';
  225. $secret = 'secret';
  226. $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
  227. $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
  228. $this->resourceMock->expects($this->once())->method('save');
  229. $result = $this->tokenModel->createCustomerToken($userId);
  230. $this->assertEquals($this->tokenModel, $result);
  231. $this->assertEquals($token, $result->getToken());
  232. $this->assertEquals($secret, $result->getSecret());
  233. $this->assertEquals($userId, $result->getCustomerId());
  234. $this->assertNotEquals($userId, $result->getAdminId());
  235. $this->assertEquals(UserContextInterface::USER_TYPE_CUSTOMER, $result->getUserType());
  236. }
  237. public function testCreateRequestToken()
  238. {
  239. $entityId = 1;
  240. $callbackUrl = OauthHelper::CALLBACK_ESTABLISHED;
  241. $token = 'token';
  242. $secret = 'secret';
  243. $this->oauthHelperMock->expects($this->once())->method('generateTokenSecret')->willReturn($secret);
  244. $this->oauthHelperMock->expects($this->once())->method('generateToken')->willReturn($token);
  245. $this->tokenModel->setCallbackUrl($callbackUrl);
  246. $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
  247. $this->validatorKeyLengthMock
  248. );
  249. $this->validatorKeyLengthMock->expects($this->exactly(2))->method('setLength');
  250. $this->validatorKeyLengthMock->expects($this->exactly(2))->method('setName');
  251. $this->validatorKeyLengthMock->expects($this->exactly(2))->method('isValid')->willReturn(true);
  252. $this->resourceMock->expects($this->once())->method('save');
  253. $actualToken = $this->tokenModel->createRequestToken($entityId, $callbackUrl);
  254. $this->assertEquals($this->tokenModel, $actualToken);
  255. $this->assertEquals($this->tokenModel->getSecret(), $actualToken->getSecret());
  256. $this->assertEquals($this->tokenModel->getToken(), $actualToken->getToken());
  257. }
  258. public function testToString()
  259. {
  260. $token = 'token';
  261. $secret = 'secret';
  262. $expectedResponse = "oauth_token={$token}&oauth_token_secret={$secret}";
  263. $this->tokenModel->setToken($token)->setSecret($secret);
  264. $this->assertEquals($expectedResponse, sprintf($this->tokenModel));
  265. }
  266. public function testBeforeSave()
  267. {
  268. $this->assertEquals($this->tokenModel, $this->tokenModel->beforeSave());
  269. }
  270. public function testGetVerifier()
  271. {
  272. $verifier = 'testVerifier';
  273. $this->tokenModel->setData('verifier', $verifier);
  274. $this->assertEquals($verifier, $this->tokenModel->getVerifier());
  275. }
  276. public function testLoadByConsumerIdAndUserType()
  277. {
  278. $consumerId = 1;
  279. $userType = 1;
  280. $tokenData = 'testToken';
  281. $data = ['token' => $tokenData];
  282. $this->resourceMock->expects($this->once())->method('selectTokenByConsumerIdAndUserType')->willReturn($data);
  283. $actualToken = $this->tokenModel->loadByConsumerIdAndUserType($consumerId, $userType);
  284. $this->assertEquals($this->tokenModel, $actualToken);
  285. $this->assertEquals($tokenData, $actualToken->getToken());
  286. }
  287. public function testLoadByAdminId()
  288. {
  289. $adminId = 1;
  290. $tokenData = 'testToken';
  291. $data = ['token' => $tokenData];
  292. $this->resourceMock->expects($this->once())->method('selectTokenByAdminId')->willReturn($data);
  293. $actualToken = $this->tokenModel->loadByAdminId($adminId);
  294. $this->assertEquals($this->tokenModel, $actualToken);
  295. $this->assertEquals($tokenData, $actualToken->getToken());
  296. }
  297. public function testLoadByCustomerId()
  298. {
  299. $customerId = 1;
  300. $tokenData = 'testToken';
  301. $data = ['token' => $tokenData];
  302. $this->resourceMock->expects($this->once())->method('selectTokenByCustomerId')->willReturn($data);
  303. $actualToken = $this->tokenModel->loadByCustomerId($customerId);
  304. $this->assertEquals($this->tokenModel, $actualToken);
  305. $this->assertEquals($tokenData, $actualToken->getToken());
  306. }
  307. public function testLoad()
  308. {
  309. $token = 'testToken';
  310. $this->resourceMock->expects($this->once())->method('load');
  311. $actualToken = $this->tokenModel->loadByToken($token);
  312. $this->assertEquals($this->tokenModel, $actualToken);
  313. }
  314. public function testValidateIfNotCallbackEstablishedAndNotValid()
  315. {
  316. $exceptionMessage = 'exceptionMessage';
  317. $this->tokenModel->setCallbackUrl('notCallbackEstablished');
  318. $this->validatorMock->expects($this->once())->method('isValid')->willReturn(false);
  319. $this->validatorMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
  320. $this->expectException(\Magento\Framework\Oauth\Exception::class);
  321. $this->expectExceptionMessage($exceptionMessage);
  322. $this->tokenModel->validate();
  323. }
  324. public function testValidateIfSecretNotValid()
  325. {
  326. $exceptionMessage = 'exceptionMessage';
  327. $this->tokenModel->setCallbackUrl('notCallbackEstablished');
  328. $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
  329. $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
  330. $this->validatorKeyLengthMock
  331. );
  332. $this->validatorKeyLengthMock->expects($this->once())->method('isValid')->willReturn(false);
  333. $this->validatorKeyLengthMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
  334. $this->expectException(\Magento\Framework\Oauth\Exception::class);
  335. $this->expectExceptionMessage($exceptionMessage);
  336. $this->tokenModel->validate();
  337. }
  338. public function testValidateIfTokenNotValid()
  339. {
  340. $exceptionMessage = 'exceptionMessage';
  341. $token = 'token';
  342. $secret = 'secret';
  343. $this->tokenModel->setCallbackUrl('notCallbackEstablished');
  344. $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
  345. $this->keyLengthFactoryMock->expects($this->once())
  346. ->method('create')
  347. ->willReturn($this->validatorKeyLengthMock);
  348. $this->tokenModel->setSecret($secret);
  349. $this->tokenModel->setToken($token);
  350. $this->validatorKeyLengthMock->expects($this->exactly(2))->method('isValid')->willReturnMap(
  351. [
  352. [$secret, true],
  353. [$token, false]
  354. ]
  355. );
  356. $this->validatorKeyLengthMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
  357. $this->expectException(\Magento\Framework\Oauth\Exception::class);
  358. $this->expectExceptionMessage($exceptionMessage);
  359. $this->tokenModel->validate();
  360. }
  361. public function testValidateIfVerifierNotValid()
  362. {
  363. $exceptionMessage = 'exceptionMessage';
  364. $secret = 'secret';
  365. $token = 'token';
  366. $verifier = 'isSetAndNotValid';
  367. $this->tokenModel->setCallbackUrl('notCallbackEstablished');
  368. $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
  369. $this->keyLengthFactoryMock->expects($this->once())
  370. ->method('create')
  371. ->willReturn($this->validatorKeyLengthMock);
  372. $this->tokenModel->setSecret($secret);
  373. $this->tokenModel->setToken($token);
  374. $this->tokenModel->setData('verifier', $verifier);
  375. $this->validatorKeyLengthMock->expects($this->exactly(3))->method('isValid')->willReturnMap(
  376. [
  377. [$secret, true],
  378. [$token, true],
  379. [$verifier, false],
  380. ]
  381. );
  382. $this->validatorKeyLengthMock->expects($this->once())->method('getMessages')->willReturn([$exceptionMessage]);
  383. $this->expectException(\Magento\Framework\Oauth\Exception::class);
  384. $this->expectExceptionMessage($exceptionMessage);
  385. $this->tokenModel->validate();
  386. }
  387. public function testValidateIfVerifierIsNotSet()
  388. {
  389. $token = 'token';
  390. $secret = 'secret';
  391. $verifier = null;
  392. $this->tokenModel->setCallbackUrl('notCallbackEstablished');
  393. $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
  394. $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
  395. $this->validatorKeyLengthMock
  396. );
  397. $this->tokenModel->setSecret($secret);
  398. $this->tokenModel->setToken($token);
  399. $this->tokenModel->setData('verifier', $verifier);
  400. $this->validatorKeyLengthMock->expects($this->exactly(2))->method('isValid')->willReturnMap(
  401. [
  402. [$secret, true],
  403. [$token, true],
  404. ]
  405. );
  406. $this->assertTrue($this->tokenModel->validate());
  407. }
  408. public function testValidate()
  409. {
  410. $token = 'token';
  411. $secret = 'secret';
  412. $verifier = 'verifier';
  413. $this->tokenModel->setCallbackUrl('notCallbackEstablished');
  414. $this->validatorMock->expects($this->once())->method('isValid')->willReturn(true);
  415. $this->keyLengthFactoryMock->expects($this->once())->method('create')->willReturn(
  416. $this->validatorKeyLengthMock
  417. );
  418. $this->tokenModel->setSecret($secret);
  419. $this->tokenModel->setToken($token);
  420. $this->tokenModel->setData('verifier', $verifier);
  421. $this->validatorKeyLengthMock->expects($this->exactly(3))->method('isValid')->willReturnMap(
  422. [
  423. [$secret, true],
  424. [$token, true],
  425. [$verifier, true],
  426. ]
  427. );
  428. $this->assertTrue($this->tokenModel->validate());
  429. }
  430. }