PublicShareMiddlewareTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\AppFramework\Middleware\PublicShare;
  24. use OC\AppFramework\Middleware\PublicShare\Exceptions\NeedAuthenticationException;
  25. use OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware;
  26. use OCP\AppFramework\AuthPublicShareController;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\NotFoundResponse;
  29. use OCP\AppFramework\Http\RedirectResponse;
  30. use OCP\AppFramework\PublicShareController;
  31. use OCP\Files\NotFoundException;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IURLGenerator;
  36. use OCP\Security\Bruteforce\IThrottler;
  37. class PublicShareMiddlewareTest extends \Test\TestCase {
  38. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
  39. private $request;
  40. /** @var ISession|\PHPUnit\Framework\MockObject\MockObject */
  41. private $session;
  42. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  43. private $config;
  44. /** @var IThrottler|\PHPUnit\Framework\MockObject\MockObject */
  45. private $throttler;
  46. /** @var PublicShareMiddleware */
  47. private $middleware;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->request = $this->createMock(IRequest::class);
  51. $this->session = $this->createMock(ISession::class);
  52. $this->config = $this->createMock(IConfig::class);
  53. $this->throttler = $this->createMock(IThrottler::class);
  54. $this->middleware = new PublicShareMiddleware(
  55. $this->request,
  56. $this->session,
  57. $this->config,
  58. $this->throttler
  59. );
  60. }
  61. public function testBeforeControllerNoPublicShareController() {
  62. $controller = $this->createMock(Controller::class);
  63. $this->middleware->beforeController($controller, 'method');
  64. $this->assertTrue(true);
  65. }
  66. public function dataShareApi() {
  67. return [
  68. ['no', 'no',],
  69. ['no', 'yes',],
  70. ['yes', 'no',],
  71. ];
  72. }
  73. /**
  74. * @dataProvider dataShareApi
  75. */
  76. public function testBeforeControllerShareApiDisabled(string $shareApi, string $shareLinks) {
  77. $controller = $this->createMock(PublicShareController::class);
  78. $this->config->method('getAppValue')
  79. ->willReturnMap([
  80. ['core', 'shareapi_enabled', 'yes', $shareApi],
  81. ['core', 'shareapi_allow_links', 'yes', $shareLinks],
  82. ]);
  83. $this->expectException(NotFoundException::class);
  84. $this->middleware->beforeController($controller, 'mehod');
  85. }
  86. public function testBeforeControllerNoTokenParam() {
  87. $controller = $this->createMock(PublicShareController::class);
  88. $this->config->method('getAppValue')
  89. ->willReturnMap([
  90. ['core', 'shareapi_enabled', 'yes', 'yes'],
  91. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  92. ]);
  93. $this->expectException(NotFoundException::class);
  94. $this->middleware->beforeController($controller, 'mehod');
  95. }
  96. public function testBeforeControllerInvalidToken() {
  97. $controller = $this->createMock(PublicShareController::class);
  98. $this->config->method('getAppValue')
  99. ->willReturnMap([
  100. ['core', 'shareapi_enabled', 'yes', 'yes'],
  101. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  102. ]);
  103. $this->request->method('getParam')
  104. ->with('token', null)
  105. ->willReturn('myToken');
  106. $controller->method('isValidToken')
  107. ->willReturn(false);
  108. $controller->expects($this->once())
  109. ->method('shareNotFound');
  110. $this->expectException(NotFoundException::class);
  111. $this->middleware->beforeController($controller, 'mehod');
  112. }
  113. public function testBeforeControllerValidTokenNotAuthenticated() {
  114. $controller = $this->getMockBuilder(PublicShareController::class)
  115. ->setConstructorArgs(['app', $this->request, $this->session])
  116. ->getMock();
  117. $this->config->method('getAppValue')
  118. ->willReturnMap([
  119. ['core', 'shareapi_enabled', 'yes', 'yes'],
  120. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  121. ]);
  122. $this->request->method('getParam')
  123. ->with('token', null)
  124. ->willReturn('myToken');
  125. $controller->method('isValidToken')
  126. ->willReturn(true);
  127. $controller->method('isPasswordProtected')
  128. ->willReturn(true);
  129. $this->expectException(NotFoundException::class);
  130. $this->middleware->beforeController($controller, 'mehod');
  131. }
  132. public function testBeforeControllerValidTokenAuthenticateMethod() {
  133. $controller = $this->getMockBuilder(PublicShareController::class)
  134. ->setConstructorArgs(['app', $this->request, $this->session])
  135. ->getMock();
  136. $this->config->method('getAppValue')
  137. ->willReturnMap([
  138. ['core', 'shareapi_enabled', 'yes', 'yes'],
  139. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  140. ]);
  141. $this->request->method('getParam')
  142. ->with('token', null)
  143. ->willReturn('myToken');
  144. $controller->method('isValidToken')
  145. ->willReturn(true);
  146. $controller->method('isPasswordProtected')
  147. ->willReturn(true);
  148. $this->middleware->beforeController($controller, 'authenticate');
  149. $this->assertTrue(true);
  150. }
  151. public function testBeforeControllerValidTokenShowAuthenticateMethod() {
  152. $controller = $this->getMockBuilder(PublicShareController::class)
  153. ->setConstructorArgs(['app', $this->request, $this->session])
  154. ->getMock();
  155. $this->config->method('getAppValue')
  156. ->willReturnMap([
  157. ['core', 'shareapi_enabled', 'yes', 'yes'],
  158. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  159. ]);
  160. $this->request->method('getParam')
  161. ->with('token', null)
  162. ->willReturn('myToken');
  163. $controller->method('isValidToken')
  164. ->willReturn(true);
  165. $controller->method('isPasswordProtected')
  166. ->willReturn(true);
  167. $this->middleware->beforeController($controller, 'showAuthenticate');
  168. $this->assertTrue(true);
  169. }
  170. public function testBeforeControllerAuthPublicShareController() {
  171. $controller = $this->getMockBuilder(AuthPublicShareController::class)
  172. ->setConstructorArgs(['app', $this->request, $this->session, $this->createMock(IURLGenerator::class)])
  173. ->getMock();
  174. $this->config->method('getAppValue')
  175. ->willReturnMap([
  176. ['core', 'shareapi_enabled', 'yes', 'yes'],
  177. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  178. ]);
  179. $this->request->method('getParam')
  180. ->with('token', null)
  181. ->willReturn('myToken');
  182. $controller->method('isValidToken')
  183. ->willReturn(true);
  184. $controller->method('isPasswordProtected')
  185. ->willReturn(true);
  186. $this->session->expects($this->once())
  187. ->method('set')
  188. ->with('public_link_authenticate_redirect', '[]');
  189. $this->expectException(NeedAuthenticationException::class);
  190. $this->middleware->beforeController($controller, 'method');
  191. }
  192. public function testAfterExceptionNoPublicShareController() {
  193. $controller = $this->createMock(Controller::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 testAfterExceptionPublicShareControllerNotFoundException() {
  202. $controller = $this->createMock(PublicShareController::class);
  203. $exception = new NotFoundException();
  204. $result = $this->middleware->afterException($controller, 'method', $exception);
  205. $this->assertInstanceOf(NotFoundResponse::class, $result);
  206. }
  207. public function testAfterExceptionPublicShareController() {
  208. $controller = $this->createMock(PublicShareController::class);
  209. $exception = new \Exception();
  210. try {
  211. $this->middleware->afterException($controller, 'method', $exception);
  212. } catch (\Exception $e) {
  213. $this->assertEquals($exception, $e);
  214. }
  215. }
  216. public function testAfterExceptionAuthPublicShareController() {
  217. $controller = $this->getMockBuilder(AuthPublicShareController::class)
  218. ->setConstructorArgs([
  219. 'app',
  220. $this->request,
  221. $this->session,
  222. $this->createMock(IURLGenerator::class),
  223. ])->getMock();
  224. $controller->setToken('token');
  225. $exception = new NeedAuthenticationException();
  226. $this->request->method('getParam')
  227. ->with('_route')
  228. ->willReturn('my.route');
  229. $result = $this->middleware->afterException($controller, 'method', $exception);
  230. $this->assertInstanceOf(RedirectResponse::class, $result);
  231. }
  232. }