1
0

OCSMiddlewareTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Middleware;
  7. use OC\AppFramework\Middleware\OCSMiddleware;
  8. use OC\AppFramework\OCS\BaseResponse;
  9. use OC\AppFramework\OCS\V1Response;
  10. use OC\AppFramework\OCS\V2Response;
  11. use OCP\AppFramework\Controller;
  12. use OCP\AppFramework\Http;
  13. use OCP\AppFramework\OCS\OCSBadRequestException;
  14. use OCP\AppFramework\OCS\OCSException;
  15. use OCP\AppFramework\OCS\OCSForbiddenException;
  16. use OCP\AppFramework\OCS\OCSNotFoundException;
  17. use OCP\AppFramework\OCSController;
  18. use OCP\IRequest;
  19. class OCSMiddlewareTest extends \Test\TestCase {
  20. /**
  21. * @var IRequest
  22. */
  23. private $request;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->request = $this->getMockBuilder(IRequest::class)
  27. ->getMock();
  28. }
  29. public function dataAfterException() {
  30. $OCSController = $this->getMockBuilder(OCSController::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $controller = $this->getMockBuilder(Controller::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. return [
  37. [$OCSController, new \Exception(), true],
  38. [$OCSController, new OCSException(), false, '', Http::STATUS_INTERNAL_SERVER_ERROR],
  39. [$OCSController, new OCSException('foo'), false, 'foo', Http::STATUS_INTERNAL_SERVER_ERROR],
  40. [$OCSController, new OCSException('foo', Http::STATUS_IM_A_TEAPOT), false, 'foo', Http::STATUS_IM_A_TEAPOT],
  41. [$OCSController, new OCSBadRequestException(), false, '', Http::STATUS_BAD_REQUEST],
  42. [$OCSController, new OCSBadRequestException('foo'), false, 'foo', Http::STATUS_BAD_REQUEST],
  43. [$OCSController, new OCSForbiddenException(), false, '', Http::STATUS_FORBIDDEN],
  44. [$OCSController, new OCSForbiddenException('foo'), false, 'foo', Http::STATUS_FORBIDDEN],
  45. [$OCSController, new OCSNotFoundException(), false, '', Http::STATUS_NOT_FOUND],
  46. [$OCSController, new OCSNotFoundException('foo'), false, 'foo', Http::STATUS_NOT_FOUND],
  47. [$controller, new \Exception(), true],
  48. [$controller, new OCSException(), true],
  49. [$controller, new OCSException('foo'), true],
  50. [$controller, new OCSException('foo', Http::STATUS_IM_A_TEAPOT), true],
  51. [$controller, new OCSBadRequestException(), true],
  52. [$controller, new OCSBadRequestException('foo'), true],
  53. [$controller, new OCSForbiddenException(), true],
  54. [$controller, new OCSForbiddenException('foo'), true],
  55. [$controller, new OCSNotFoundException(), true],
  56. [$controller, new OCSNotFoundException('foo'), true],
  57. ];
  58. }
  59. /**
  60. * @dataProvider dataAfterException
  61. *
  62. * @param Controller $controller
  63. * @param \Exception $exception
  64. * @param bool $forward
  65. * @param string $message
  66. * @param int $code
  67. */
  68. public function testAfterExceptionOCSv1($controller, $exception, $forward, $message = '', $code = 0) {
  69. $this->request
  70. ->method('getScriptName')
  71. ->willReturn('/ocs/v1.php');
  72. $OCSMiddleware = new OCSMiddleware($this->request);
  73. $OCSMiddleware->beforeController($controller, 'method');
  74. if ($forward) {
  75. $this->expectException(get_class($exception));
  76. $this->expectExceptionMessage($exception->getMessage());
  77. }
  78. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  79. $this->assertInstanceOf(V1Response::class, $result);
  80. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  81. if ($exception->getCode() === 0) {
  82. $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  83. } else {
  84. $this->assertSame($code, $result->getOCSStatus());
  85. }
  86. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  87. }
  88. /**
  89. * @dataProvider dataAfterException
  90. *
  91. * @param Controller $controller
  92. * @param \Exception $exception
  93. * @param bool $forward
  94. * @param string $message
  95. * @param int $code
  96. */
  97. public function testAfterExceptionOCSv2($controller, $exception, $forward, $message = '', $code = 0) {
  98. $this->request
  99. ->method('getScriptName')
  100. ->willReturn('/ocs/v2.php');
  101. $OCSMiddleware = new OCSMiddleware($this->request);
  102. $OCSMiddleware->beforeController($controller, 'method');
  103. if ($forward) {
  104. $this->expectException(get_class($exception));
  105. $this->expectExceptionMessage($exception->getMessage());
  106. }
  107. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  108. $this->assertInstanceOf(V2Response::class, $result);
  109. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  110. if ($exception->getCode() === 0) {
  111. $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  112. } else {
  113. $this->assertSame($code, $result->getOCSStatus());
  114. }
  115. $this->assertSame($code, $result->getStatus());
  116. }
  117. /**
  118. * @dataProvider dataAfterException
  119. *
  120. * @param Controller $controller
  121. * @param \Exception $exception
  122. * @param bool $forward
  123. * @param string $message
  124. * @param int $code
  125. */
  126. public function testAfterExceptionOCSv2SubFolder($controller, $exception, $forward, $message = '', $code = 0) {
  127. $this->request
  128. ->method('getScriptName')
  129. ->willReturn('/mysubfolder/ocs/v2.php');
  130. $OCSMiddleware = new OCSMiddleware($this->request);
  131. $OCSMiddleware->beforeController($controller, 'method');
  132. if ($forward) {
  133. $this->expectException(get_class($exception));
  134. $this->expectExceptionMessage($exception->getMessage());
  135. }
  136. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  137. $this->assertInstanceOf(V2Response::class, $result);
  138. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  139. if ($exception->getCode() === 0) {
  140. $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  141. } else {
  142. $this->assertSame($code, $result->getOCSStatus());
  143. }
  144. $this->assertSame($code, $result->getStatus());
  145. }
  146. public function dataAfterController() {
  147. $OCSController = $this->getMockBuilder(OCSController::class)
  148. ->disableOriginalConstructor()
  149. ->getMock();
  150. $controller = $this->getMockBuilder(Controller::class)
  151. ->disableOriginalConstructor()
  152. ->getMock();
  153. return [
  154. [$OCSController, new Http\Response(), false],
  155. [$OCSController, new Http\JSONResponse(), false],
  156. [$OCSController, new Http\JSONResponse(['message' => 'foo']), false],
  157. [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), true, OCSController::RESPOND_UNAUTHORISED],
  158. [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), true],
  159. [$controller, new Http\Response(), false],
  160. [$controller, new Http\JSONResponse(), false],
  161. [$controller, new Http\JSONResponse(['message' => 'foo']), false],
  162. [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), false],
  163. [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), false],
  164. ];
  165. }
  166. /**
  167. * @dataProvider dataAfterController
  168. *
  169. * @param Controller $controller
  170. * @param Http\Response $response
  171. * @param bool $converted
  172. * @param int $convertedOCSStatus
  173. */
  174. public function testAfterController($controller, $response, $converted, $convertedOCSStatus = 0) {
  175. $OCSMiddleware = new OCSMiddleware($this->request);
  176. $newResponse = $OCSMiddleware->afterController($controller, 'foo', $response);
  177. if ($converted === false) {
  178. $this->assertSame($response, $newResponse);
  179. } else {
  180. $this->assertInstanceOf(BaseResponse::class, $newResponse);
  181. $this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'statusMessage'));
  182. if ($convertedOCSStatus) {
  183. $this->assertSame($convertedOCSStatus, $newResponse->getOCSStatus());
  184. } else {
  185. $this->assertSame($response->getStatus(), $newResponse->getOCSStatus());
  186. }
  187. $this->assertSame($response->getStatus(), $newResponse->getStatus());
  188. }
  189. }
  190. }