1
0

SharingCheckMiddlewareTest.php 9.7 KB

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