summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_sharing/tests/middleware/sharingcheckmiddleware.php')
-rw-r--r--apps/files_sharing/tests/middleware/sharingcheckmiddleware.php126
1 files changed, 121 insertions, 5 deletions
diff --git a/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php b/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php
index a43b11c81b6..05cb7497680 100644
--- a/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php
+++ b/apps/files_sharing/tests/middleware/sharingcheckmiddleware.php
@@ -25,6 +25,9 @@
namespace OCA\Files_Sharing\Middleware;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\Files\NotFoundException;
+use OCP\AppFramework\Utility\IControllerMethodReflector;
+use OCA\Files_Sharing\Exceptions\S2SException;
+use OCP\AppFramework\Http\JSONResponse;
/**
* @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
@@ -39,6 +42,8 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
private $sharingCheckMiddleware;
/** @var \OCP\AppFramework\Controller */
private $controllerMock;
+ /** @var IControllerMethodReflector */
+ private $reflector;
protected function setUp() {
$this->config = $this->getMockBuilder('\OCP\IConfig')
@@ -47,8 +52,14 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
->disableOriginalConstructor()->getMock();
$this->controllerMock = $this->getMockBuilder('\OCP\AppFramework\Controller')
->disableOriginalConstructor()->getMock();
+ $this->reflector = $this->getMockBuilder('\OCP\AppFramework\Utility\IControllerMethodReflector')
+ ->disableOriginalConstructor()->getMock();
- $this->sharingCheckMiddleware = new SharingCheckMiddleware('files_sharing', $this->config, $this->appManager);
+ $this->sharingCheckMiddleware = new SharingCheckMiddleware(
+ 'files_sharing',
+ $this->config,
+ $this->appManager,
+ $this->reflector);
}
public function testIsSharingEnabledWithAppEnabled() {
@@ -114,7 +125,93 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
$this->assertFalse(self::invokePrivate($this->sharingCheckMiddleware, 'isLinkSharingEnabled'));
}
- public function testBeforeControllerWithSharingEnabled() {
+ public 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;
+ }
+
+ /**
+ * @dataProvider externalSharesChecksDataProvider
+ */
+ public function testExternalSharesChecks($annotations, $config, $expectedResult) {
+ $this->reflector
+ ->expects($this->atLeastOnce())
+ ->method('hasAnnotation')
+ ->will($this->returnValueMap($annotations));
+
+ $this->config
+ ->method('getAppValue')
+ ->will($this->returnValueMap($config));
+
+ $this->assertEquals($expectedResult, self::invokePrivate($this->sharingCheckMiddleware, 'externalSharesChecks'));
+ }
+
+ /**
+ * @dataProvider externalSharesChecksDataProvider
+ */
+ public function testBeforeControllerWithExternalShareControllerWithSharingEnabled($annotations, $config, $noException) {
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->will($this->returnValue(true));
+
+ $this->reflector
+ ->expects($this->atLeastOnce())
+ ->method('hasAnnotation')
+ ->will($this->returnValueMap($annotations));
+
+ $this->config
+ ->method('getAppValue')
+ ->will($this->returnValueMap($config));
+
+ $controller = $this->getMockBuilder('\OCA\Files_Sharing\Controllers\ExternalSharesController')
+ ->disableOriginalConstructor()->getMock();
+
+ $exceptionThrown = false;
+
+ try {
+ $this->sharingCheckMiddleware->beforeController($controller, 'myMethod');
+ } catch (\OCA\Files_Sharing\Exceptions\S2SException $exception) {
+ $exceptionThrown = true;
+ }
+
+ $this->assertNotEquals($noException, $exceptionThrown);
+ }
+
+ public function testBeforeControllerWithShareControllerWithSharingEnabled() {
$this->appManager
->expects($this->once())
->method('isEnabledForUser')
@@ -141,14 +238,14 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
/**
* @expectedException \OCP\Files\NotFoundException
- * @expectedExceptionMessage Sharing is disabled.
+ * @expectedExceptionMessage Link sharing is disabled
*/
- public function testBeforeControllerWithSharingDisabled() {
+ public function testBeforeControllerWithShareControllerWithSharingEnabledAPIDisabled() {
$this->appManager
->expects($this->once())
->method('isEnabledForUser')
->with('files_sharing')
- ->will($this->returnValue(false));
+ ->will($this->returnValue(true));
$controller = $this->getMockBuilder('\OCA\Files_Sharing\Controllers\ShareController')
->disableOriginalConstructor()->getMock();
@@ -157,6 +254,20 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
}
/**
+ * @expectedException \OCP\Files\NotFoundException
+ * @expectedExceptionMessage Sharing is disabled.
+ */
+ public function testBeforeControllerWithSharingDisabled() {
+ $this->appManager
+ ->expects($this->once())
+ ->method('isEnabledForUser')
+ ->with('files_sharing')
+ ->will($this->returnValue(false));
+
+ $this->sharingCheckMiddleware->beforeController($this->controllerMock, 'myMethod');
+ }
+
+ /**
* @expectedException \Exception
* @expectedExceptionMessage My Exception message
*/
@@ -168,4 +279,9 @@ class SharingCheckMiddlewareTest extends \Test\TestCase {
$this->assertEquals(new NotFoundResponse(), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new NotFoundException('My Exception message')));
}
+ public function testAfterExceptionWithS2SException() {
+ $this->assertEquals(new JSONResponse('My Exception message', 405), $this->sharingCheckMiddleware->afterException($this->controllerMock, 'myMethod', new S2SException('My Exception message')));
+ }
+
+
}