RequestValidatorTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Test\Unit\Controller\Rest;
  7. class RequestValidatorTest extends \PHPUnit\Framework\TestCase
  8. {
  9. const SERVICE_METHOD = 'testMethod';
  10. const SERVICE_ID = 'Magento\Webapi\Controller\Rest\TestService';
  11. /**
  12. * @var \Magento\Webapi\Controller\Rest\RequestValidator
  13. */
  14. private $requestValidator;
  15. /**
  16. * @var \Magento\Framework\Webapi\Rest\Request|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $requestMock;
  19. /** @var \Magento\Store\Model\StoreManagerInterface |\PHPUnit_Framework_MockObject_MockObject */
  20. private $storeManagerMock;
  21. /** @var \Magento\Store\Api\Data\StoreInterface |\PHPUnit_Framework_MockObject_MockObject */
  22. private $storeMock;
  23. /**
  24. * @var \Magento\Framework\Webapi\Authorization|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $authorizationMock;
  27. /**
  28. * @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Webapi\Controller\Rest\Router\Route
  29. */
  30. private $routeMock;
  31. protected function setUp()
  32. {
  33. $this->requestMock = $this->getMockBuilder(\Magento\Framework\Webapi\Rest\Request::class)
  34. ->setMethods(
  35. [
  36. 'isSecure',
  37. 'getRequestData',
  38. 'getParams',
  39. 'getParam',
  40. 'getRequestedServices',
  41. 'getPathInfo',
  42. 'getHttpHost',
  43. 'getMethod',
  44. ]
  45. )->disableOriginalConstructor()->getMock();
  46. $this->requestMock->expects($this->any())
  47. ->method('getHttpHost')
  48. ->willReturn('testHostName.com');
  49. $routerMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router::class)->setMethods(['match'])
  50. ->disableOriginalConstructor()->getMock();
  51. $this->routeMock = $this->getMockBuilder(\Magento\Webapi\Controller\Rest\Router\Route::class)
  52. ->setMethods(['isSecure', 'getServiceMethod', 'getServiceClass', 'getAclResources', 'getParameters'])
  53. ->disableOriginalConstructor()->getMock();
  54. $this->authorizationMock = $this->getMockBuilder(\Magento\Framework\Webapi\Authorization::class)
  55. ->disableOriginalConstructor()->getMock();
  56. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  57. $this->storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
  58. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  59. $this->storeManagerMock->expects($this->any())->method('getStore')->willReturn($this->storeMock);
  60. $this->requestValidator =
  61. $objectManager->getObject(
  62. \Magento\Webapi\Controller\Rest\RequestValidator::class,
  63. [
  64. 'request' => $this->requestMock,
  65. 'router' => $routerMock,
  66. 'authorization' => $this->authorizationMock,
  67. 'storeManager' => $this->storeManagerMock
  68. ]
  69. );
  70. // Set default expectations used by all tests
  71. $this->routeMock->expects($this->any())->method('getServiceClass')->will($this->returnValue(self::SERVICE_ID));
  72. $this->routeMock->expects($this->any())->method('getServiceMethod')
  73. ->will($this->returnValue(self::SERVICE_METHOD));
  74. $routerMock->expects($this->any())->method('match')->will($this->returnValue($this->routeMock));
  75. parent::setUp();
  76. }
  77. /**
  78. * Test Secure Request and Secure route combinations
  79. *
  80. * @dataProvider dataProviderSecureRequestSecureRoute
  81. */
  82. public function testSecureRouteAndRequest($isSecureRoute, $isSecureRequest)
  83. {
  84. $this->routeMock->expects($this->any())->method('isSecure')->will($this->returnValue($isSecureRoute));
  85. $this->routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1']));
  86. $this->requestMock->expects($this->any())->method('getRequestData')->will($this->returnValue([]));
  87. $this->requestMock->expects($this->any())->method('isSecure')->will($this->returnValue($isSecureRequest));
  88. $this->authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
  89. $this->requestValidator->validate();
  90. }
  91. /**
  92. * Data provider for testSecureRouteAndRequest.
  93. *
  94. * @return array
  95. */
  96. public function dataProviderSecureRequestSecureRoute()
  97. {
  98. // Each array contains return type for isSecure method of route and request objects.
  99. return [[true, true], [false, true], [false, false]];
  100. }
  101. /**
  102. * Test insecure request for a secure route
  103. *
  104. * @expectedException \Magento\Framework\Webapi\Exception
  105. * @expectedExceptionMessage Operation allowed only in HTTPS
  106. */
  107. public function testInSecureRequestOverSecureRoute()
  108. {
  109. $this->routeMock->expects($this->any())->method('isSecure')->will($this->returnValue(true));
  110. $this->routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['1']));
  111. $this->requestMock->expects($this->any())->method('isSecure')->will($this->returnValue(false));
  112. $this->authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(true));
  113. $this->requestValidator->validate();
  114. }
  115. /**
  116. * @expectedException \Magento\Framework\Exception\AuthorizationException
  117. * @expectedExceptionMessage The consumer isn't authorized to access 5, 6.
  118. */
  119. public function testAuthorizationFailed()
  120. {
  121. $this->authorizationMock->expects($this->once())->method('isAllowed')->will($this->returnValue(false));
  122. $this->routeMock->expects($this->any())->method('getAclResources')->will($this->returnValue(['5', '6']));
  123. $this->requestValidator->validate();
  124. }
  125. }