PublicShareMiddlewareTest.php 8.0 KB

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