OCSMiddlewareTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. try {
  91. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  92. $this->assertFalse($forward);
  93. $this->assertInstanceOf(V1Response::class, $result);
  94. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  95. if ($exception->getCode() === 0) {
  96. $this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  97. } else {
  98. $this->assertSame($code, $result->getOCSStatus());
  99. }
  100. if ($exception instanceof OCSForbiddenException) {
  101. $this->assertSame(Http::STATUS_UNAUTHORIZED, $result->getStatus());
  102. } else {
  103. $this->assertSame(Http::STATUS_OK, $result->getStatus());
  104. }
  105. } catch (\Exception $e) {
  106. $this->assertTrue($forward);
  107. $this->assertEquals($exception, $e);
  108. }
  109. }
  110. /**
  111. * @dataProvider dataAfterException
  112. *
  113. * @param Controller $controller
  114. * @param \Exception $exception
  115. * @param bool $forward
  116. * @param string $message
  117. * @param int $code
  118. */
  119. public function testAfterExceptionOCSv2($controller, $exception, $forward, $message = '', $code = 0) {
  120. $this->request
  121. ->method('getScriptName')
  122. ->willReturn('/ocs/v2.php');
  123. $OCSMiddleware = new OCSMiddleware($this->request);
  124. $OCSMiddleware->beforeController($controller, 'method');
  125. try {
  126. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  127. $this->assertFalse($forward);
  128. $this->assertInstanceOf(V2Response::class, $result);
  129. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  130. if ($exception->getCode() === 0) {
  131. $this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  132. } else {
  133. $this->assertSame($code, $result->getOCSStatus());
  134. }
  135. $this->assertSame($code, $result->getStatus());
  136. } catch (\Exception $e) {
  137. $this->assertTrue($forward);
  138. $this->assertEquals($exception, $e);
  139. }
  140. }
  141. /**
  142. * @dataProvider dataAfterException
  143. *
  144. * @param Controller $controller
  145. * @param \Exception $exception
  146. * @param bool $forward
  147. * @param string $message
  148. * @param int $code
  149. */
  150. public function testAfterExceptionOCSv2SubFolder($controller, $exception, $forward, $message = '', $code = 0) {
  151. $this->request
  152. ->method('getScriptName')
  153. ->willReturn('/mysubfolder/ocs/v2.php');
  154. $OCSMiddleware = new OCSMiddleware($this->request);
  155. $OCSMiddleware->beforeController($controller, 'method');
  156. try {
  157. $result = $OCSMiddleware->afterException($controller, 'method', $exception);
  158. $this->assertFalse($forward);
  159. $this->assertInstanceOf(V2Response::class, $result);
  160. $this->assertSame($message, $this->invokePrivate($result, 'statusMessage'));
  161. if ($exception->getCode() === 0) {
  162. $this->assertSame(\OCP\API::RESPOND_UNKNOWN_ERROR, $result->getOCSStatus());
  163. } else {
  164. $this->assertSame($code, $result->getOCSStatus());
  165. }
  166. $this->assertSame($code, $result->getStatus());
  167. } catch (\Exception $e) {
  168. $this->assertTrue($forward);
  169. $this->assertEquals($exception, $e);
  170. }
  171. }
  172. public function dataAfterController() {
  173. $OCSController = $this->getMockBuilder(OCSController::class)
  174. ->disableOriginalConstructor()
  175. ->getMock();
  176. $controller = $this->getMockBuilder(Controller::class)
  177. ->disableOriginalConstructor()
  178. ->getMock();
  179. return [
  180. [$OCSController, new Http\Response(), false],
  181. [$OCSController, new Http\JSONResponse(), false],
  182. [$OCSController, new Http\JSONResponse(['message' => 'foo']), false],
  183. [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), true],
  184. [$OCSController, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), true],
  185. [$controller, new Http\Response(), false],
  186. [$controller, new Http\JSONResponse(), false],
  187. [$controller, new Http\JSONResponse(['message' => 'foo']), false],
  188. [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_UNAUTHORIZED), false],
  189. [$controller, new Http\JSONResponse(['message' => 'foo'], Http::STATUS_FORBIDDEN), false],
  190. ];
  191. }
  192. /**
  193. * @dataProvider dataAfterController
  194. *
  195. * @param Controller $controller
  196. * @param Http\Response $response
  197. * @param bool $converted
  198. */
  199. public function testAfterController($controller, $response, $converted) {
  200. $OCSMiddleware = new OCSMiddleware($this->request);
  201. $newResponse = $OCSMiddleware->afterController($controller, 'foo', $response);
  202. if ($converted === false) {
  203. $this->assertSame($response, $newResponse);
  204. } else {
  205. $this->assertInstanceOf(BaseResponse::class, $newResponse);
  206. /** @var Http\OCSResponse $newResponse */
  207. $this->assertSame($response->getData()['message'], $this->invokePrivate($newResponse, 'statusMessage'));
  208. $this->assertSame(\OCP\API::RESPOND_UNAUTHORISED, $newResponse->getOCSStatus());
  209. $this->assertSame(Http::STATUS_UNAUTHORIZED, $newResponse->getStatus());
  210. }
  211. }
  212. }