SharingCheckMiddlewareTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Middleware;
  8. use OCA\Files_Sharing\Controller\ExternalSharesController;
  9. use OCA\Files_Sharing\Controller\ShareController;
  10. use OCA\Files_Sharing\Exceptions\S2SException;
  11. use OCP\App\IAppManager;
  12. use OCP\AppFramework\Controller;
  13. use OCP\AppFramework\Http\JSONResponse;
  14. use OCP\AppFramework\Http\NotFoundResponse;
  15. use OCP\AppFramework\Utility\IControllerMethodReflector;
  16. use OCP\Files\NotFoundException;
  17. use OCP\IConfig;
  18. use OCP\IRequest;
  19. use OCP\Share\IManager;
  20. use OCP\Share\IShare;
  21. /**
  22. * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
  23. */
  24. class SharingCheckMiddlewareTest extends \Test\TestCase {
  25. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  26. private $config;
  27. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  28. private $appManager;
  29. /** @var SharingCheckMiddleware */
  30. private $sharingCheckMiddleware;
  31. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */
  32. private $controllerMock;
  33. /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  34. private $reflector;
  35. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  36. private $shareManager;
  37. /** @var IRequest | \PHPUnit\Framework\MockObject\MockObject */
  38. private $request;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->config = $this->createMock(IConfig::class);
  42. $this->appManager = $this->createMock(IAppManager::class);
  43. $this->controllerMock = $this->createMock(Controller::class);
  44. $this->reflector = $this->createMock(IControllerMethodReflector::class);
  45. $this->shareManager = $this->createMock(IManager::class);
  46. $this->request = $this->createMock(IRequest::class);
  47. $this->sharingCheckMiddleware = new SharingCheckMiddleware(
  48. 'files_sharing',
  49. $this->config,
  50. $this->appManager,
  51. $this->reflector,
  52. $this->shareManager,
  53. $this->request);
  54. }
  55. public function testIsSharingEnabledWithAppEnabled(): void {
  56. $this->appManager
  57. ->expects($this->once())
  58. ->method('isEnabledForUser')
  59. ->with('files_sharing')
  60. ->willReturn(true);
  61. $this->assertTrue(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
  62. }
  63. public function testIsSharingEnabledWithAppDisabled(): void {
  64. $this->appManager
  65. ->expects($this->once())
  66. ->method('isEnabledForUser')
  67. ->with('files_sharing')
  68. ->willReturn(false);
  69. $this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
  70. }
  71. public function externalSharesChecksDataProvider() {
  72. $data = [];
  73. foreach ([false, true] as $annIn) {
  74. foreach ([false, true] as $annOut) {
  75. foreach ([false, true] as $confIn) {
  76. foreach ([false, true] as $confOut) {
  77. $res = true;
  78. if (!$annIn && !$confIn) {
  79. $res = false;
  80. } elseif (!$annOut && !$confOut) {
  81. $res = false;
  82. }
  83. $d = [
  84. [
  85. ['NoIncomingFederatedSharingRequired', $annIn],
  86. ['NoOutgoingFederatedSharingRequired', $annOut],
  87. ],
  88. [
  89. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', $confIn ? 'yes' : 'no'],
  90. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', $confOut ? 'yes' : 'no'],
  91. ],
  92. $res
  93. ];
  94. $data[] = $d;
  95. }
  96. }
  97. }
  98. }
  99. return $data;
  100. }
  101. /**
  102. * @dataProvider externalSharesChecksDataProvider
  103. */
  104. public function testExternalSharesChecks($annotations, $config, $expectedResult): void {
  105. $this->reflector
  106. ->expects($this->atLeastOnce())
  107. ->method('hasAnnotation')
  108. ->willReturnMap($annotations);
  109. $this->config
  110. ->method('getAppValue')
  111. ->willReturnMap($config);
  112. $this->assertEquals($expectedResult, self::invokePrivate($this->sharingCheckMiddleware, 'externalSharesChecks'));
  113. }
  114. /**
  115. * @dataProvider externalSharesChecksDataProvider
  116. */
  117. public function testBeforeControllerWithExternalShareControllerWithSharingEnabled($annotations, $config, $noException): void {
  118. $this->appManager
  119. ->expects($this->once())
  120. ->method('isEnabledForUser')
  121. ->with('files_sharing')
  122. ->willReturn(true);
  123. $this->reflector
  124. ->expects($this->atLeastOnce())
  125. ->method('hasAnnotation')
  126. ->willReturnMap($annotations);
  127. $this->config
  128. ->method('getAppValue')
  129. ->willReturnMap($config);
  130. $controller = $this->createMock(ExternalSharesController::class);
  131. $exceptionThrown = false;
  132. try {
  133. $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
  134. } catch (S2SException $exception) {
  135. $exceptionThrown = true;
  136. }
  137. $this->assertNotEquals($noException, $exceptionThrown);
  138. }
  139. public function testBeforeControllerWithShareControllerWithSharingEnabled(): void {
  140. $share = $this->createMock(IShare::class);
  141. $this->appManager
  142. ->expects($this->once())
  143. ->method('isEnabledForUser')
  144. ->with('files_sharing')
  145. ->willReturn(true);
  146. $controller = $this->createMock(ShareController::class);
  147. $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
  148. }
  149. public function testBeforeControllerWithSharingDisabled(): void {
  150. $this->expectException(\OCP\Files\NotFoundException::class);
  151. $this->expectExceptionMessage('Sharing is disabled.');
  152. $this->appManager
  153. ->expects($this->once())
  154. ->method('isEnabledForUser')
  155. ->with('files_sharing')
  156. ->willReturn(false);
  157. $this->sharingCheckMiddleware->beforeController($this->controllerMock, 'myMethod');
  158. }
  159. public function testAfterExceptionWithRegularException(): void {
  160. $this->expectException(\Exception::class);
  161. $this->expectExceptionMessage('My Exception message');
  162. $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new \Exception('My Exception message'));
  163. }
  164. public function testAfterExceptionWithNotFoundException(): void {
  165. $this->assertEquals(new NotFoundResponse(), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new NotFoundException('My Exception message')));
  166. }
  167. public function testAfterExceptionWithS2SException(): void {
  168. $this->assertEquals(new JSONResponse('My Exception message', 405), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new S2SException('My Exception message')));
  169. }
  170. }