OauthUserContextTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Test\Unit\Model\Authorization;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. /**
  9. * Tests \Magento\Webapi\Model\Authorization\OauthUserContext
  10. */
  11. class OauthUserContextTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var \Magento\Webapi\Model\Authorization\OauthUserContext
  19. */
  20. protected $oauthUserContext;
  21. /**
  22. * @var \Magento\Framework\Webapi\Request
  23. */
  24. protected $request;
  25. /**
  26. * @var \Magento\Framework\Oauth\Helper\Request
  27. */
  28. protected $oauthRequestHelper;
  29. /**
  30. * @var \Magento\Integration\Api\IntegrationServiceInterface
  31. */
  32. protected $integrationService;
  33. /**
  34. * @var \Magento\Framework\Oauth\Oauth
  35. */
  36. protected $oauthService;
  37. protected function setUp()
  38. {
  39. $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  40. $this->request = $this->getMockBuilder(\Magento\Framework\Webapi\Request::class)
  41. ->disableOriginalConstructor()
  42. ->setMethods(['getConsumerId'])
  43. ->getMock();
  44. $this->integrationService = $this->getMockBuilder(\Magento\Integration\Api\IntegrationServiceInterface::class)
  45. ->disableOriginalConstructor()
  46. ->setMethods(
  47. [
  48. 'findByName',
  49. 'update',
  50. 'create',
  51. 'get',
  52. 'findByConsumerId',
  53. 'findActiveIntegrationByConsumerId',
  54. 'delete',
  55. 'getSelectedResources'
  56. ]
  57. )
  58. ->getMock();
  59. $this->oauthRequestHelper = $this->getMockBuilder(\Magento\Framework\Oauth\Helper\Request::class)
  60. ->disableOriginalConstructor()
  61. ->setMethods(['prepareRequest', 'getRequestUrl'])
  62. ->getMock();
  63. $this->oauthService = $this->getMockBuilder(\Magento\Framework\Oauth\Oauth::class)
  64. ->disableOriginalConstructor()
  65. ->setMethods(['validateAccessTokenRequest'])
  66. ->getMock();
  67. $this->oauthUserContext = $this->objectManager->getObject(
  68. \Magento\Webapi\Model\Authorization\OauthUserContext::class,
  69. [
  70. 'request' => $this->request,
  71. 'integrationService' => $this->integrationService,
  72. 'oauthService' => $this->oauthService,
  73. 'oauthHelper' => $this->oauthRequestHelper
  74. ]
  75. );
  76. }
  77. public function testGetUserType()
  78. {
  79. $this->assertEquals(UserContextInterface::USER_TYPE_INTEGRATION, $this->oauthUserContext->getUserType());
  80. }
  81. public function testGetUserIdExist()
  82. {
  83. $integrationId = 12345;
  84. $this->setupUserId($integrationId, ['oauth_token' => 'asdcfsdvanskdcalkdsjcfljldk']);
  85. $this->assertEquals($integrationId, $this->oauthUserContext->getUserId());
  86. }
  87. public function testGetUserIdDoesNotExist()
  88. {
  89. $integrationId = null;
  90. $this->setupUserId($integrationId, ['oauth_token' => 'asdcfsdvanskdcalkdsjcfljldk']);
  91. $this->assertEquals($integrationId, $this->oauthUserContext->getUserId());
  92. }
  93. public function testGetUserIdNoOauthInformation()
  94. {
  95. $integrationId = 12345;
  96. $this->setupUserId($integrationId, []);
  97. $this->assertEquals(null, $this->oauthUserContext->getUserId());
  98. }
  99. /**
  100. * @param int|null $integrationId
  101. * @param array $oauthRequest
  102. * @return void
  103. */
  104. public function setupUserId($integrationId, $oauthRequest)
  105. {
  106. $integration = $this->getMockBuilder(\Magento\Integration\Model\Integration::class)
  107. ->disableOriginalConstructor()
  108. ->setMethods(['getId', '__wakeup'])
  109. ->getMock();
  110. $this->integrationService->expects($this->any())
  111. ->method('findActiveIntegrationByConsumerId')
  112. ->will($this->returnValue($integration));
  113. $this->oauthRequestHelper->expects($this->once())
  114. ->method('prepareRequest')
  115. ->will($this->returnValue($oauthRequest));
  116. $this->oauthService->expects($this->any())
  117. ->method('validateAccessTokenRequest')
  118. ->will($this->returnValue(1));
  119. $integration->expects($this->any())
  120. ->method('getId')
  121. ->will($this->returnValue($integrationId));
  122. }
  123. }