]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add OCSShareAPIMiddleware tests
authorRoeland Jago Douma <roeland@famdouma.nl>
Fri, 22 Jul 2016 12:44:16 +0000 (14:44 +0200)
committerRoeland Jago Douma <roeland@famdouma.nl>
Fri, 5 Aug 2016 12:17:32 +0000 (14:17 +0200)
apps/files_sharing/lib/Middleware/OCSShareAPIMiddleware.php
apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php [new file with mode: 0644]

index b42cf6b00ada90d571cb78eb75a6160ef44e4b67..04a6545c9a7f4eb0dfac37dcb1e4f06664326b2c 100644 (file)
@@ -29,7 +29,6 @@ class OCSShareAPIMiddleware extends Middleware {
         */
        public function beforeController($controller, $methodName) {
                if ($controller instanceof Share20OCS) {
-                       /** @var Share20OCS $controller */
                        if (!$this->shareManager->shareApiEnabled()) {
                                throw new OCSNotFoundException($this->l->t('Share API is disabled'));
                        }
diff --git a/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php b/apps/files_sharing/tests/Middleware/OCSShareAPIMiddlewareTest.php
new file mode 100644 (file)
index 0000000..6a24603
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+
+namespace OCA\Files_Sharing\Tests\Middleware;
+
+use OCA\Files_Sharing\Middleware\OCSShareAPIMiddleware;
+use OCP\AppFramework\Controller;
+use OCP\AppFramework\OCS\OCSNotFoundException;
+use OCP\IL10N;
+use OCP\Share\IManager;
+
+/**
+ * @package OCA\Files_Sharing\Middleware\SharingCheckMiddleware
+ */
+class OCSShareAPIMiddlewareTest extends \Test\TestCase {
+
+       /** @var IManager */
+       private $shareManager;
+       /** @var IL10N */
+       private $l;
+       /** @var OCSShareAPIMiddleware */
+       private $middleware;
+
+       public function setUp() {
+               $this->shareManager = $this->getMockBuilder('OCP\Share\IManager')->getMock();
+               $this->l = $this->getMockBuilder('OCP\IL10N')->getMock();
+
+               $this->l->method('t')->will($this->returnArgument(0));
+
+               $this->middleware = new OCSShareAPIMiddleware($this->shareManager, $this->l);
+       }
+
+       public function dataBeforeController() {
+               return [
+                       [
+                               $this->getMockBuilder('OCP\AppFramework\Controller')->disableOriginalConstructor()->getMock(),
+                               false,
+                               false
+                       ],
+                       [
+                               $this->getMockBuilder('OCP\AppFramework\Controller')->disableOriginalConstructor()->getMock(),
+                               true,
+                               false
+                       ],
+                       [
+                               $this->getMockBuilder('OCP\AppFramework\OCSController')->disableOriginalConstructor()->getMock(),
+                               false,
+                               false
+                       ],
+                       [
+                               $this->getMockBuilder('OCP\AppFramework\OCSController')->disableOriginalConstructor()->getMock(),
+                               true,
+                               false
+                       ],
+                       [
+                               $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')->disableOriginalConstructor()->getMock(),
+                               false,
+                               true
+                       ],
+                       [
+                               $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')->disableOriginalConstructor()->getMock(),
+                               true,
+                               false
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataBeforeController
+        *
+        * @param Controller $controller
+        * @param bool $enabled
+        * @param bool $exception
+        */
+       public function testBeforeController(Controller $controller, $enabled, $exception) {
+               $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->getMockBuilder('OCP\AppFramework\Controller')->disableOriginalConstructor()->getMock(),
+                       ],
+                       [
+                               $this->getMockBuilder('OCP\AppFramework\OCSController')->disableOriginalConstructor()->getMock(),
+                       ],
+                       [
+                               $this->getMockBuilder('OCA\Files_Sharing\API\Share20OCS')->disableOriginalConstructor()->getMock(),
+                       ],
+               ];
+       }
+
+       /**
+        * @dataProvider dataAfterController
+        *
+        * @param Controller $controller
+        * @param bool $called
+        */
+       public function testAfterController(Controller $controller) {
+               if ($controller instanceof OCA\Files_Sharing\API\Share20OCS) {
+                       $controller->expects($this->once())->method('cleanup');
+               }
+
+               $response = $this->getMockBuilder('OCP\AppFramework\Http\Response')
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $this->middleware->afterController($controller, 'foo', $response);
+       }
+}