aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/tests/Middleware
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_sharing/tests/Middleware')
-rw-r--r--apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php124
-rw-r--r--apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php140
-rw-r--r--apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php205
3 files changed, 469 insertions, 0 deletions
diff --git a/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php
new file mode 100644
index 00000000000..efc6b3f7f7f
--- /dev/null
+++ b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php
@@ -0,0 +1,124 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Sharing\Tests\Middleware;
+
+use OCA\Files_Sharing\Controller\ShareAPIController;
+use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\OCS\OCSNotFoundException;
+use OCP\AppFramework\OCSController;
+use OCP\IL10N;
+use OCP\Share\IManager;
+
+/**
+ * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
+ */
+class OCSShareAPIMiddlewareTest extends \Test\TestCase {
+
+ /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
+ private $shareManager;
+ /** @var IL10N */
+ private $l;
+ /** @var OCSShareAPIMiddleware */
+ private $middleware;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->shareManager = $this->createMock(IManager::class);
+ $this->l = $this->createMock(IL10N::class);
+
+ $this->l->method('t')->willReturnArgument(0);
+
+ $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l);
+ }
+
+ public function dataBeforeController() {
+ return [
+ [
+ $this->createMock(Controller::class),
+ false,
+ false
+ ],
+ [
+ $this->createMock(Controller::class),
+ true,
+ false
+ ],
+ [
+ $this->createMock(OCSController::class),
+ false,
+ false
+ ],
+ [
+ $this->createMock(OCSController::class),
+ true,
+ false
+ ],
+ [
+ $this->createMock(ShareAPIController::class),
+ false,
+ true
+ ],
+ [
+ $this->createMock(ShareAPIController::class),
+ true,
+ false
+ ],
+ ];
+ }
+
+ /**
+ *
+ * @param Controller $controller
+ * @param bool $enabled
+ * @param bool $exception
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataBeforeController')]
+ public function testBeforeController(Controller $controller, $enabled, $exception): void {
+ $this->shareManager->method('shareApiEnabled')->willReturn($enabled);
+
+ try {
+ $this->middleware->beforeController($controller, 'foo');
+ $this->assertFalse($exception);
+ } catch (OCSNotFoundException $e) {
+ $this->assertTrue($exception);
+ }
+ }
+
+ public function dataAfterController() {
+ return [
+ [
+ $this->createMock(Controller::class),
+ ],
+ [
+ $this->createMock(OCSController::class),
+ ],
+ [
+ $this->createMock(ShareAPIController::class),
+ ],
+ ];
+ }
+
+ /**
+ *
+ * @param Controller $controller
+ * @param bool $called
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataAfterController')]
+ public function testAfterController(Controller $controller): void {
+ if ($controller instanceof ShareAPIController) {
+ $controller->expects($this->once())->method('cleanup');
+ }
+
+ $response = $this->getMockBuilder('OCP\AppFramework\Http\Response')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->middleware->afterController($controller, 'foo', $response);
+ $this->addToAssertionCount(1);
+ }
+}
diff --git a/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php b/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php
new file mode 100644
index 00000000000..631b6a0f51c
--- /dev/null
+++ b/apps/files_sharing/tests/Middleware/ShareInfoMiddlewareTest.php
@@ -0,0 +1,140 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_Sharing\Tests\Middleware;
+
+use OCA\Files_Sharing\Controller\ShareInfoController;
+use OCA\Files_Sharing\Exceptions\S2SException;
+use OCA\Files_Sharing\Middleware\ShareInfoMiddleware;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\Response;
+use OCP\Share\IManager as ShareManager;
+use Test\TestCase;
+
+class ShareInfoMiddlewareTest extends TestCase {
+
+ /** @var ShareManager|\PHPUnit\Framework\MockObject\MockObject */
+ private $shareManager;
+
+ /** @var ShareInfoMiddleware */
+ private $middleware;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->shareManager = $this->createMock(ShareManager::class);
+ $this->middleware = new ShareInfoMiddleware($this->shareManager);
+ }
+
+ public function testBeforeControllerNoShareInfo(): void {
+ $this->shareManager->expects($this->never())
+ ->method($this->anything());
+
+ $this->middleware->beforeController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo');
+ }
+
+ public function testBeforeControllerShareInfoNoS2s(): void {
+ $this->shareManager->expects($this->once())
+ ->method('outgoingServer2ServerSharesAllowed')
+ ->willReturn(false);
+
+ $this->expectException(S2SException::class);
+ $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
+ }
+
+ public function testBeforeControllerShareInfo(): void {
+ $this->shareManager->expects($this->once())
+ ->method('outgoingServer2ServerSharesAllowed')
+ ->willReturn(true);
+
+ $this->middleware->beforeController($this->createMock(ShareInfoController::class), 'foo');
+ }
+
+ public function testAfterExceptionNoShareInfo(): void {
+ $exeption = new \Exception();
+
+ try {
+ $this->middleware->afterException($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $exeption);
+ $this->fail();
+ } catch (\Exception $e) {
+ $this->assertSame($exeption, $e);
+ }
+ }
+
+
+ public function testAfterExceptionNoS2S(): void {
+ $exeption = new \Exception();
+
+ try {
+ $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', $exeption);
+ $this->fail();
+ } catch (\Exception $e) {
+ $this->assertSame($exeption, $e);
+ }
+ }
+
+ public function testAfterExceptionS2S(): void {
+ $expected = new JSONResponse([], Http::STATUS_NOT_FOUND);
+
+ $this->assertEquals(
+ $expected,
+ $this->middleware->afterException($this->createMock(ShareInfoController::class), 'foo', new S2SException())
+ );
+ }
+
+ public function testAfterControllerNoShareInfo(): void {
+ $response = $this->createMock(Response::class);
+
+ $this->assertEquals(
+ $response,
+ $this->middleware->afterController($this->createMock(ShareInfoMiddlewareTestController::class), 'foo', $response)
+ );
+ }
+
+ public function testAfterControllerNoJSON(): void {
+ $response = $this->createMock(Response::class);
+
+ $this->assertEquals(
+ $response,
+ $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
+ );
+ }
+
+ public function testAfterControllerJSONok(): void {
+ $data = ['foo' => 'bar'];
+ $response = new JSONResponse($data);
+
+ $expected = new JSONResponse([
+ 'data' => $data,
+ 'status' => 'success',
+ ]);
+
+ $this->assertEquals(
+ $expected,
+ $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
+ );
+ }
+
+ public function testAfterControllerJSONerror(): void {
+ $data = ['foo' => 'bar'];
+ $response = new JSONResponse($data, Http::STATUS_FORBIDDEN);
+
+ $expected = new JSONResponse([
+ 'data' => $data,
+ 'status' => 'error',
+ ], Http::STATUS_FORBIDDEN);
+
+ $this->assertEquals(
+ $expected,
+ $this->middleware->afterController($this->createMock(ShareInfoController::class), 'foo', $response)
+ );
+ }
+}
+
+class ShareInfoMiddlewareTestController extends Controller {
+}
diff --git a/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php b/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php
new file mode 100644
index 00000000000..3d86007a54c
--- /dev/null
+++ b/apps/files_sharing/tests/Middleware/SharingCheckMiddlewareTest.php
@@ -0,0 +1,205 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace OCA\Files_Sharing\Middleware;
+
+use OCA\Files_Sharing\Controller\ExternalSharesController;
+use OCA\Files_Sharing\Controller\ShareController;
+use OCA\Files_Sharing\Exceptions\S2SException;
+use OCP\App\IAppManager;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http\JSONResponse;
+use OCP\AppFramework\Http\NotFoundResponse;
+use OCP\AppFramework\Utility\IControllerMethodReflector;
+use OCP\Files\NotFoundException;
+use OCP\IConfig;
+use OCP\IRequest;
+use OCP\Share\IManager;
+use OCP\Share\IShare;
+
+/**
+ * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
+ */
+class SharingCheckMiddlewareTest extends \Test\TestCase {
+
+ /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
+ private $config;
+ /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
+ private $appManager;
+ /** @var SharingCheckMiddleware */
+ private $sharingCheckMiddleware;
+ /** @var Controller|\PHPUnit\Framework\MockObject\MockObject */
+ private $controllerMock;
+ /** @var IControllerMethodReflector|\PHPUnit\Framework\MockObject\MockObject */
+ private $reflector;
+ /** @var IManager | \PHPUnit\Framework\MockObject\MockObject */
+ private $shareManager;
+ /** @var IRequest | \PHPUnit\Framework\MockObject\MockObject */
+ private $request;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->config = $this->createMock(IConfig::class);
+ $this->appManager = $this->createMock(IAppManager::class);
+ $this->controllerMock = $this->createMock(Controller::class);
+ $this->reflector = $this->createMock(IControllerMethodReflector::class);
+ $this->shareManager = $this->createMock(IManager::class);
+ $this->request = $this->createMock(IRequest::class);
+
+ $this->sharingCheckMiddleware = new SharingCheckMiddleware(
+ 'files_sharing',
+ $this->config,
+ $this->appManager,
+ $this->reflector,
+ $this->shareManager,
+ $this->request);
+ }
+
+ public function testIsSharingEnabledWithAppEnabled(): void {
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->willReturn(true);
+
+ $this->assertTrue(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
+ }
+
+ public function testIsSharingEnabledWithAppDisabled(): void {
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->willReturn(false);
+
+ $this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isSharingEnabled'));
+ }
+
+ public static function externalSharesChecksDataProvider() {
+ $data = [];
+
+ foreach ([false, true] as $annIn) {
+ foreach ([false, true] as $annOut) {
+ foreach ([false, true] as $confIn) {
+ foreach ([false, true] as $confOut) {
+ $res = true;
+ if (!$annIn && !$confIn) {
+ $res = false;
+ } elseif (!$annOut && !$confOut) {
+ $res = false;
+ }
+
+ $d = [
+ [
+ ['NoIncomingFederatedSharingRequired', $annIn],
+ ['NoOutgoingFederatedSharingRequired', $annOut],
+ ],
+ [
+ ['files_sharing', 'incoming_server2server_share_enabled', 'yes', $confIn ? 'yes' : 'no'],
+ ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', $confOut ? 'yes' : 'no'],
+ ],
+ $res
+ ];
+
+ $data[] = $d;
+ }
+ }
+ }
+ }
+
+ return $data;
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('externalSharesChecksDataProvider')]
+ public function testExternalSharesChecks($annotations, $config, $expectedResult): void {
+ $this->reflector
+ ->expects($this->atLeastOnce())
+ ->method('hasAnnotation')
+ ->willReturnMap($annotations);
+
+ $this->config
+ ->method('getAppValue')
+ ->willReturnMap($config);
+
+ $this->assertEquals($expectedResult, self::invokePrivate($this->sharingCheckMiddleware, 'externalSharesChecks'));
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('externalSharesChecksDataProvider')]
+ public function testBeforeControllerWithExternalShareControllerWithSharingEnabled($annotations, $config, $noException): void {
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->willReturn(true);
+
+ $this->reflector
+ ->expects($this->atLeastOnce())
+ ->method('hasAnnotation')
+ ->willReturnMap($annotations);
+
+ $this->config
+ ->method('getAppValue')
+ ->willReturnMap($config);
+
+ $controller = $this->createMock(ExternalSharesController::class);
+
+ $exceptionThrown = false;
+
+ try {
+ $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
+ } catch (S2SException $exception) {
+ $exceptionThrown = true;
+ }
+
+ $this->assertNotEquals($noException, $exceptionThrown);
+ }
+
+ public function testBeforeControllerWithShareControllerWithSharingEnabled(): void {
+ $share = $this->createMock(IShare::class);
+
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->willReturn(true);
+
+ $controller = $this->createMock(ShareController::class);
+
+ $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
+ }
+
+
+ public function testBeforeControllerWithSharingDisabled(): void {
+ $this->expectException(NotFoundException::class);
+ $this->expectExceptionMessage('Sharing is disabled.');
+
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->willReturn(false);
+
+ $this->sharingCheckMiddleware->beforeController($this->controllerMock, 'myMethod');
+ }
+
+
+ public function testAfterExceptionWithRegularException(): void {
+ $this->expectException(\Exception::class);
+ $this->expectExceptionMessage('My Exception message');
+
+ $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new \Exception('My Exception message'));
+ }
+
+ public function testAfterExceptionWithNotFoundException(): void {
+ $this->assertEquals(new NotFoundResponse(), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new NotFoundException('My Exception message')));
+ }
+
+ public function testAfterExceptionWithS2SException(): void {
+ $this->assertEquals(new JSONResponse('My Exception message', 405), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new S2SException('My Exception message')));
+ }
+}