OCSMiddlewareTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. *
  4. * @author Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Middleware;
  23. use OC\AppFramework\Middleware\OCSMiddleware;
  24. use OC\AppFramework\OCS\BaseResponse;
  25. use OC\AppFramework\OCS\V1Response;
  26. use OC\AppFramework\OCS\V2Response;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\OCS\OCSBadRequestException;
  30. use OCP\AppFramework\OCS\OCSException;
  31. use OCP\AppFramework\OCS\OCSForbiddenException;
  32. use OCP\AppFramework\OCS\OCSNotFoundException;
  33. use OCP\AppFramework\OCSController;
  34. use OCP\IRequest;
  35. class OCSMiddlewareTest extends \Test\TestCase {
  36. /**
  37. * @var IRequest
  38. */
  39. private $request;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->request = $this->getMockBuilder(IRequest::class)
  43. ->getMock();
  44. }
  45. public function dataAfterException() {
  46. $OCSController = $this->getMockBuilder(OCSController::class)
  47. ->disableOriginalConstructor()
  48. ->getMock();
  49. $controller = $this->getMockBuilder(Controller::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. return [
  53. [$OCSController, new \Exception(), true],
  54. [$OCSController, new OCSException(), false, '', Http::STATUS_INTERNAL_SERVER_ERROR],
  55. [$OCSController, new OCSException('foo'), false, 'foo', Http::STATUS_INTERNAL_SERVER_ERROR],
  56. [$OCSController, new OCSException('foo', Http::STATUS_IM_A_TEAPOT), false, 'foo', Http::STATUS_IM_A_TEAPOT],
  57. [$OCSController, new OCSBadRequestException(), false, '', Http::STATUS_BAD_REQUEST],
  58. [$OCSController, new OCSBadRequestException('foo'), false, 'foo', Http::STATUS_BAD_REQUEST],
  59. [$OCSController, new OCSForbiddenException(), false, '', Http::STATUS_FORBIDDEN],
  60. [$OCSController, new OCSForbiddenException('foo'), false, 'foo', Http::STATUS_FORBIDDEN],
  61. [$OCSController, new OCSNotFoundException(), false, '', Http::STATUS_NOT_FOUND],
  62. [$OCSController, new OCSNotFoundException('foo'), false, 'foo', Http::STATUS_NOT_FOUND],
  63. [$controller, new \Exception(), true],
  64. [$controller, new OCSException(), true],
  65. [$controller, new OCSException('foo'), true],
  66. [$controller, new OCSException('foo', Http::STATUS_IM_A_TEAPOT), true],
  67. [$controller, new OCSBadRequestException(), true],
  68. [$controller, new OCSBadRequestException('foo'), true],
  69. [$controller, new OCSForbiddenException(), true],
  70. [$controller, new OCSForbiddenException('foo'), true],
  71. [$controller, new OCSNotFoundException(), true],
  72. [$controller, new OCSNotFoundException('foo'), true],
  73. ];
  74. }
  75. /**
  76. * @dataProvider dataAfterException
  77. *
  78. * @param Controller $controller
  79. * @param \Exception $exception
  80. * @param bool $forward
  81. * @param string $message
  82. * @param int $code
  83. */
  84. public function testAfterExceptionOCSv1($controller, $exception, $forward, $message = '', $code = 0) {
  85. $this->request
  86. ->method('getScriptName')
  87. ->willReturn('/ocs/v1.php');
  88. $OCSMiddleware = new OCSMiddleware($this->request);
  89. $OCSMiddleware->beforeController($controller, 'method');
  90. if ($forward) {
  91. $this->expectException(get_class($exception));
  92. $this->expectExceptionMessage($exception->getMessage());
  93. }
  94. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  95. $this->assertInstanceOf(V1Response::class, $result);
  96. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  97. if ($exception->getCode() === 0) {
  98. $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  99. } else {
  100. $this->assertSame($code, $result->getOCSStatus());
  101. }
  102. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  103. }
  104. /**
  105. * @dataProvider dataAfterException
  106. *
  107. * @param Controller $controller
  108. * @param \Exception $exception
  109. * @param bool $forward
  110. * @param string $message
  111. * @param int $code
  112. */
  113. public function testAfterExceptionOCSv2($controller, $exception, $forward, $message = '', $code = 0) {
  114. $this->request
  115. ->method('getScriptName')
  116. ->willReturn('/ocs/v2.php');
  117. $OCSMiddleware = new OCSMiddleware($this->request);
  118. $OCSMiddleware->beforeController($controller, 'method');
  119. if ($forward) {
  120. $this->expectException(get_class($exception));
  121. $this->expectExceptionMessage($exception->getMessage());
  122. }
  123. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  124. $this->assertInstanceOf(V2Response::class, $result);
  125. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  126. if ($exception->getCode() === 0) {
  127. $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  128. } else {
  129. $this->assertSame($code, $result->getOCSStatus());
  130. }
  131. $this->assertSame($code, $result->getStatus());
  132. }
  133. /**
  134. * @dataProvider dataAfterException
  135. *
  136. * @param Controller $controller
  137. * @param \Exception $exception
  138. * @param bool $forward
  139. * @param string $message
  140. * @param int $code
  141. */
  142. public function testAfterExceptionOCSv2SubFolder($controller, $exception, $forward, $message = '', $code = 0) {
  143. $this->request
  144. ->method('getScriptName')
  145. ->willReturn('/mysubfolder/ocs/v2.php');
  146. $OCSMiddleware = new OCSMiddleware($this->request);
  147. $OCSMiddleware->beforeController($controller, 'method');
  148. if ($forward) {
  149. $this->expectException(get_class($exception));
  150. $this->expectExceptionMessage($exception->getMessage());
  151. }
  152. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  153. $this->assertInstanceOf(V2Response::class, $result);
  154. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  155. if ($exception->getCode() === 0) {
  156. $this->assertSame(\OCP\AppFramework\OCSController::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  157. } else {
  158. $this->assertSame($code, $result->getOCSStatus());
  159. }
  160. $this->assertSame($code, $result->getStatus());
  161. }
  162. public function dataAfterController() {
  163. $OCSController = $this->getMockBuilder(OCSController::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $controller = $this->getMockBuilder(Controller::class)
  167. ->disableOriginalConstructor()
  168. ->getMock();
  169. return [
  170. [$OCSController, new Http\Response(), false],
  171. [$OCSController, new Http\JSONResponse(), false],
  172. [$OCSController, new Http\JSONResponse(['message' => 'foo']), false],
  173. [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), true, OCSController::RESPOND_UNAUTHORISED],
  174. [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), true],
  175. [$controller, new Http\Response(), false],
  176. [$controller, new Http\JSONResponse(), false],
  177. [$controller, new Http\JSONResponse(['message' => 'foo']), false],
  178. [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), false],
  179. [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), false],
  180. ];
  181. }
  182. /**
  183. * @dataProvider dataAfterController
  184. *
  185. * @param Controller $controller
  186. * @param Http\Response $response
  187. * @param bool $converted
  188. * @param int $convertedOCSStatus
  189. */
  190. public function testAfterController($controller, $response, $converted, $convertedOCSStatus = 0) {
  191. $OCSMiddleware = new OCSMiddleware($this->request);
  192. $newResponse = $OCSMiddleware->afterController($controller, 'foo', $response);
  193. if ($converted === false) {
  194. $this->assertSame($response, $newResponse);
  195. } else {
  196. $this->assertInstanceOf(BaseResponse::class, $newResponse);
  197. $this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'statusMessage'));
  198. if ($convertedOCSStatus) {
  199. $this->assertSame($convertedOCSStatus, $newResponse->getOCSStatus());
  200. } else {
  201. $this->assertSame($response->getStatus(), $newResponse->getOCSStatus());
  202. }
  203. $this->assertSame($response->getStatus(), $newResponse->getStatus());
  204. }
  205. }
  206. }