1
0

SharingCheckMiddlewareTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Files_Sharing\Middleware;
  28. use OCA\Files_Sharing\Controller\ExternalSharesController;
  29. use OCA\Files_Sharing\Controller\ShareController;
  30. use OCA\Files_Sharing\Exceptions\S2SException;
  31. use OCP\App\IAppManager;
  32. use OCP\AppFramework\Controller;
  33. use OCP\AppFramework\Http\JSONResponse;
  34. use OCP\AppFramework\Http\NotFoundResponse;
  35. use OCP\AppFramework\Utility\IControllerMethodReflector;
  36. use OCP\Files\NotFoundException;
  37. use OCP\IConfig;
  38. use OCP\IRequest;
  39. use OCP\Share\IManager;
  40. use OCP\Share\IShare;
  41. /**
  42. * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
  43. */
  44. class SharingCheckMiddlewareTest extends \Test\TestCase {
  45. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  46. private $config;
  47. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  48. private $appManager;
  49. /** @var SharingCheckMiddleware */
  50. private $sharingCheckMiddleware;
  51. /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */
  52. private $controllerMock;
  53. /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
  54. private $reflector;
  55. /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
  56. private $shareManager;
  57. /** @var IRequest | \PHPUnit\Framework\MockObject\MockObject */
  58. private $request;
  59. protected function setUp(): void {
  60. parent::setUp();
  61. $this->config = $this->createMock(IConfig::class);
  62. $this->appManager = $this->createMock(IAppManager::class);
  63. $this->controllerMock = $this->createMock(Controller::class);
  64. $this->reflector = $this->createMock(IControllerMethodReflector::class);
  65. $this->shareManager = $this->createMock(IManager::class);
  66. $this->request = $this->createMock(IRequest::class);
  67. $this->sharingCheckMiddleware = new SharingCheckMiddleware(
  68. 'files_sharing',
  69. $this->config,
  70. $this->appManager,
  71. $this->reflector,
  72. $this->shareManager,
  73. $this->request);
  74. }
  75. public function testIsSharingEnabledWithAppEnabled() {
  76. $this->appManager
  77. ->expects($this->once())
  78. ->method('isEnabledForUser')
  79. ->with('files_sharing')
  80. ->willReturn(true);
  81. $this->assertTrue(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
  82. }
  83. public function testIsSharingEnabledWithAppDisabled() {
  84. $this->appManager
  85. ->expects($this->once())
  86. ->method('isEnabledForUser')
  87. ->with('files_sharing')
  88. ->willReturn(false);
  89. $this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
  90. }
  91. public function externalSharesChecksDataProvider() {
  92. $data = [];
  93. foreach ([false, true] as $annIn) {
  94. foreach ([false, true] as $annOut) {
  95. foreach ([false, true] as $confIn) {
  96. foreach ([false, true] as $confOut) {
  97. $res = true;
  98. if (!$annIn && !$confIn) {
  99. $res = false;
  100. } elseif (!$annOut && !$confOut) {
  101. $res = false;
  102. }
  103. $d = [
  104. [
  105. ['NoIncomingFederatedSharingRequired', $annIn],
  106. ['NoOutgoingFederatedSharingRequired', $annOut],
  107. ],
  108. [
  109. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', $confIn ? 'yes' : 'no'],
  110. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', $confOut ? 'yes' : 'no'],
  111. ],
  112. $res
  113. ];
  114. $data[] = $d;
  115. }
  116. }
  117. }
  118. }
  119. return $data;
  120. }
  121. /**
  122. * @dataProvider externalSharesChecksDataProvider
  123. */
  124. public function testExternalSharesChecks($annotations, $config, $expectedResult) {
  125. $this->reflector
  126. ->expects($this->atLeastOnce())
  127. ->method('hasAnnotation')
  128. ->willReturnMap($annotations);
  129. $this->config
  130. ->method('getAppValue')
  131. ->willReturnMap($config);
  132. $this->assertEquals($expectedResult, self::invokePrivate($this->sharingCheckMiddleware, 'externalSharesChecks'));
  133. }
  134. /**
  135. * @dataProvider externalSharesChecksDataProvider
  136. */
  137. public function testBeforeControllerWithExternalShareControllerWithSharingEnabled($annotations, $config, $noException) {
  138. $this->appManager
  139. ->expects($this->once())
  140. ->method('isEnabledForUser')
  141. ->with('files_sharing')
  142. ->willReturn(true);
  143. $this->reflector
  144. ->expects($this->atLeastOnce())
  145. ->method('hasAnnotation')
  146. ->willReturnMap($annotations);
  147. $this->config
  148. ->method('getAppValue')
  149. ->willReturnMap($config);
  150. $controller = $this->createMock(ExternalSharesController::class);
  151. $exceptionThrown = false;
  152. try {
  153. $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
  154. } catch (S2SException $exception) {
  155. $exceptionThrown = true;
  156. }
  157. $this->assertNotEquals($noException, $exceptionThrown);
  158. }
  159. public function testBeforeControllerWithShareControllerWithSharingEnabled() {
  160. $share = $this->createMock(IShare::class);
  161. $this->appManager
  162. ->expects($this->once())
  163. ->method('isEnabledForUser')
  164. ->with('files_sharing')
  165. ->willReturn(true);
  166. $controller = $this->createMock(ShareController::class);
  167. $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
  168. }
  169. public function testBeforeControllerWithSharingDisabled() {
  170. $this->expectException(\OCP\Files\NotFoundException::class);
  171. $this->expectExceptionMessage('Sharing is disabled.');
  172. $this->appManager
  173. ->expects($this->once())
  174. ->method('isEnabledForUser')
  175. ->with('files_sharing')
  176. ->willReturn(false);
  177. $this->sharingCheckMiddleware->beforeController($this->controllerMock, 'myMethod');
  178. }
  179. public function testAfterExceptionWithRegularException() {
  180. $this->expectException(\Exception::class);
  181. $this->expectExceptionMessage('My Exception message');
  182. $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new \Exception('My Exception message'));
  183. }
  184. public function testAfterExceptionWithNotFoundException() {
  185. $this->assertEquals(new NotFoundResponse(), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new NotFoundException('My Exception message')));
  186. }
  187. public function testAfterExceptionWithS2SException() {
  188. $this->assertEquals(new JSONResponse('My Exception message', 405), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new S2SException('My Exception message')));
  189. }
  190. }