PublicShareMiddlewareTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\AppFramework\Middleware\PublicShare;
  7. use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
  8. use OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware;
  9. use OCP\AppFramework\AuthPublicShareController;
  10. use OCP\AppFramework\Controller;
  11. use OCP\AppFramework\Http\NotFoundResponse;
  12. use OCP\AppFramework\Http\RedirectResponse;
  13. use OCP\AppFramework\PublicShareController;
  14. use OCP\Files\NotFoundException;
  15. use OCP\IConfig;
  16. use OCP\IRequest;
  17. use OCP\ISession;
  18. use OCP\IURLGenerator;
  19. use OCP\Security\Bruteforce\IThrottler;
  20. class PublicShareMiddlewareTest extends \Test\TestCase {
  21. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  22. private $request;
  23. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  24. private $session;
  25. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  26. private $config;
  27. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  28. private $throttler;
  29. /** @var PublicShareMiddleware */
  30. private $middleware;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->request = $this->createMock(IRequest::class);
  34. $this->session = $this->createMock(ISession::class);
  35. $this->config = $this->createMock(IConfig::class);
  36. $this->throttler = $this->createMock(IThrottler::class);
  37. $this->middleware = new PublicShareMiddleware(
  38. $this->request,
  39. $this->session,
  40. $this->config,
  41. $this->throttler
  42. );
  43. }
  44. public function testBeforeControllerNoPublicShareController() {
  45. $controller = $this->createMock(Controller::class);
  46. $this->middleware->beforeController($controller, 'method');
  47. $this->assertTrue(true);
  48. }
  49. public function dataShareApi() {
  50. return [
  51. ['no', 'no',],
  52. ['no', 'yes',],
  53. ['yes', 'no',],
  54. ];
  55. }
  56. /**
  57. * @dataProvider dataShareApi
  58. */
  59. public function testBeforeControllerShareApiDisabled(string $shareApi, string $shareLinks) {
  60. $controller = $this->createMock(PublicShareController::class);
  61. $this->config->method('getAppValue')
  62. ->willReturnMap([
  63. ['core', 'shareapi_enabled', 'yes', $shareApi],
  64. ['core', 'shareapi_allow_links', 'yes', $shareLinks],
  65. ]);
  66. $this->expectException(NotFoundException::class);
  67. $this->middleware->beforeController($controller, 'mehod');
  68. }
  69. public function testBeforeControllerNoTokenParam() {
  70. $controller = $this->createMock(PublicShareController::class);
  71. $this->config->method('getAppValue')
  72. ->willReturnMap([
  73. ['core', 'shareapi_enabled', 'yes', 'yes'],
  74. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  75. ]);
  76. $this->expectException(NotFoundException::class);
  77. $this->middleware->beforeController($controller, 'mehod');
  78. }
  79. public function testBeforeControllerInvalidToken() {
  80. $controller = $this->createMock(PublicShareController::class);
  81. $this->config->method('getAppValue')
  82. ->willReturnMap([
  83. ['core', 'shareapi_enabled', 'yes', 'yes'],
  84. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  85. ]);
  86. $this->request->method('getParam')
  87. ->with('token', null)
  88. ->willReturn('myToken');
  89. $controller->method('isValidToken')
  90. ->willReturn(false);
  91. $controller->expects($this->once())
  92. ->method('shareNotFound');
  93. $this->expectException(NotFoundException::class);
  94. $this->middleware->beforeController($controller, 'mehod');
  95. }
  96. public function testBeforeControllerValidTokenNotAuthenticated() {
  97. $controller = $this->getMockBuilder(PublicShareController::class)
  98. ->setConstructorArgs(['app', $this->request, $this->session])
  99. ->getMock();
  100. $this->config->method('getAppValue')
  101. ->willReturnMap([
  102. ['core', 'shareapi_enabled', 'yes', 'yes'],
  103. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  104. ]);
  105. $this->request->method('getParam')
  106. ->with('token', null)
  107. ->willReturn('myToken');
  108. $controller->method('isValidToken')
  109. ->willReturn(true);
  110. $controller->method('isPasswordProtected')
  111. ->willReturn(true);
  112. $this->expectException(NotFoundException::class);
  113. $this->middleware->beforeController($controller, 'mehod');
  114. }
  115. public function testBeforeControllerValidTokenAuthenticateMethod() {
  116. $controller = $this->getMockBuilder(PublicShareController::class)
  117. ->setConstructorArgs(['app', $this->request, $this->session])
  118. ->getMock();
  119. $this->config->method('getAppValue')
  120. ->willReturnMap([
  121. ['core', 'shareapi_enabled', 'yes', 'yes'],
  122. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  123. ]);
  124. $this->request->method('getParam')
  125. ->with('token', null)
  126. ->willReturn('myToken');
  127. $controller->method('isValidToken')
  128. ->willReturn(true);
  129. $controller->method('isPasswordProtected')
  130. ->willReturn(true);
  131. $this->middleware->beforeController($controller, 'authenticate');
  132. $this->assertTrue(true);
  133. }
  134. public function testBeforeControllerValidTokenShowAuthenticateMethod() {
  135. $controller = $this->getMockBuilder(PublicShareController::class)
  136. ->setConstructorArgs(['app', $this->request, $this->session])
  137. ->getMock();
  138. $this->config->method('getAppValue')
  139. ->willReturnMap([
  140. ['core', 'shareapi_enabled', 'yes', 'yes'],
  141. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  142. ]);
  143. $this->request->method('getParam')
  144. ->with('token', null)
  145. ->willReturn('myToken');
  146. $controller->method('isValidToken')
  147. ->willReturn(true);
  148. $controller->method('isPasswordProtected')
  149. ->willReturn(true);
  150. $this->middleware->beforeController($controller, 'showAuthenticate');
  151. $this->assertTrue(true);
  152. }
  153. public function testBeforeControllerAuthPublicShareController() {
  154. $controller = $this->getMockBuilder(AuthPublicShareController::class)
  155. ->setConstructorArgs(['app', $this->request, $this->session, $this->createMock(IURLGenerator::class)])
  156. ->getMock();
  157. $this->config->method('getAppValue')
  158. ->willReturnMap([
  159. ['core', 'shareapi_enabled', 'yes', 'yes'],
  160. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  161. ]);
  162. $this->request->method('getParam')
  163. ->with('token', null)
  164. ->willReturn('myToken');
  165. $controller->method('isValidToken')
  166. ->willReturn(true);
  167. $controller->method('isPasswordProtected')
  168. ->willReturn(true);
  169. $this->session->expects($this->once())
  170. ->method('set')
  171. ->with('public_link_authenticate_redirect', '[]');
  172. $this->expectException(NeedAuthenticationException::class);
  173. $this->middleware->beforeController($controller, 'method');
  174. }
  175. public function testAfterExceptionNoPublicShareController() {
  176. $controller = $this->createMock(Controller::class);
  177. $exception = new \Exception();
  178. try {
  179. $this->middleware->afterException($controller, 'method', $exception);
  180. } catch (\Exception $e) {
  181. $this->assertEquals($exception, $e);
  182. }
  183. }
  184. public function testAfterExceptionPublicShareControllerNotFoundException() {
  185. $controller = $this->createMock(PublicShareController::class);
  186. $exception = new NotFoundException();
  187. $result = $this->middleware->afterException($controller, 'method', $exception);
  188. $this->assertInstanceOf(NotFoundResponse::class, $result);
  189. }
  190. public function testAfterExceptionPublicShareController() {
  191. $controller = $this->createMock(PublicShareController::class);
  192. $exception = new \Exception();
  193. try {
  194. $this->middleware->afterException($controller, 'method', $exception);
  195. } catch (\Exception $e) {
  196. $this->assertEquals($exception, $e);
  197. }
  198. }
  199. public function testAfterExceptionAuthPublicShareController() {
  200. $controller = $this->getMockBuilder(AuthPublicShareController::class)
  201. ->setConstructorArgs([
  202. 'app',
  203. $this->request,
  204. $this->session,
  205. $this->createMock(IURLGenerator::class),
  206. ])->getMock();
  207. $controller->setToken('token');
  208. $exception = new NeedAuthenticationException();
  209. $this->request->method('getParam')
  210. ->with('_route')
  211. ->willReturn('my.route');
  212. $result = $this->middleware->afterException($controller, 'method', $exception);
  213. $this->assertInstanceOf(RedirectResponse::class, $result);
  214. }
  215. }