summaryrefslogtreecommitdiff
path: root/tests/units/Middleware/AuthenticationMiddlewareTest.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-05-15 20:56:01 -0400
committerFrederic Guillot <fred@kanboard.net>2016-05-15 20:56:01 -0400
commit4eaab1f6da1a56376a840231f7b8a10469308f97 (patch)
tree8f96f9f7562c69b19a2faf53923b5ba9cdb51397 /tests/units/Middleware/AuthenticationMiddlewareTest.php
parent8a6f02735b628033a3284d06a9f633bd260e19ef (diff)
Added unit tests for middleware
Diffstat (limited to 'tests/units/Middleware/AuthenticationMiddlewareTest.php')
-rw-r--r--tests/units/Middleware/AuthenticationMiddlewareTest.php141
1 files changed, 141 insertions, 0 deletions
diff --git a/tests/units/Middleware/AuthenticationMiddlewareTest.php b/tests/units/Middleware/AuthenticationMiddlewareTest.php
new file mode 100644
index 00000000..b6819adc
--- /dev/null
+++ b/tests/units/Middleware/AuthenticationMiddlewareTest.php
@@ -0,0 +1,141 @@
+<?php
+
+use Kanboard\Middleware\AuthenticationMiddleware;
+
+require_once __DIR__.'/../Base.php';
+
+class AuthenticationMiddlewareTest extends Base
+{
+ /**
+ * @var AuthenticationMiddleware
+ */
+ private $middleware;
+ private $nextMiddleware;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $this->container['authenticationManager'] = $this
+ ->getMockBuilder('Kanboard\Core\Security\AuthenticationManager')
+ ->setConstructorArgs(array($this->container))
+ ->setMethods(array('checkCurrentSession'))
+ ->getMock();
+
+ $this->container['applicationAuthorization'] = $this
+ ->getMockBuilder('Kanboard\Core\Security\AccessMap')
+ ->setMethods(array('isAllowed'))
+ ->getMock();
+
+ $this->container['response'] = $this
+ ->getMockBuilder('Kanboard\Core\Http\Response')
+ ->setConstructorArgs(array($this->container))
+ ->setMethods(array('redirect'))
+ ->getMock();
+
+ $this->container['userSession'] = $this
+ ->getMockBuilder('Kanboard\Core\User\UserSession')
+ ->setConstructorArgs(array($this->container))
+ ->setMethods(array('isLogged'))
+ ->getMock();
+
+ $this->nextMiddleware = $this
+ ->getMockBuilder('Kanboard\Middleware\AuthenticationMiddleware')
+ ->setConstructorArgs(array($this->container))
+ ->setMethods(array('execute'))
+ ->getMock();
+
+ $this->middleware = new AuthenticationMiddleware($this->container);
+ $this->middleware->setNextMiddleware($this->nextMiddleware);
+ }
+
+ public function testWithBadSession()
+ {
+ $this->container['authenticationManager']
+ ->expects($this->once())
+ ->method('checkCurrentSession')
+ ->will($this->returnValue(false));
+
+ $this->nextMiddleware
+ ->expects($this->never())
+ ->method('execute');
+
+ $this->setExpectedException('Kanboard\Core\Controller\AccessForbiddenException');
+ $this->middleware->execute();
+ }
+
+ public function testWithPublicAction()
+ {
+ $this->container['authenticationManager']
+ ->expects($this->once())
+ ->method('checkCurrentSession')
+ ->will($this->returnValue(true));
+
+ $this->container['applicationAuthorization']
+ ->expects($this->once())
+ ->method('isAllowed')
+ ->will($this->returnValue(true));
+
+ $this->nextMiddleware
+ ->expects($this->never())
+ ->method('execute');
+
+ $this->middleware->execute();
+ }
+
+ public function testWithNotAuthenticatedUser()
+ {
+ $this->container['authenticationManager']
+ ->expects($this->once())
+ ->method('checkCurrentSession')
+ ->will($this->returnValue(true));
+
+ $this->container['applicationAuthorization']
+ ->expects($this->once())
+ ->method('isAllowed')
+ ->will($this->returnValue(false));
+
+ $this->container['userSession']
+ ->expects($this->once())
+ ->method('isLogged')
+ ->will($this->returnValue(false));
+
+ $this->container['response']
+ ->expects($this->once())
+ ->method('redirect');
+
+ $this->nextMiddleware
+ ->expects($this->never())
+ ->method('execute');
+
+ $this->middleware->execute();
+ }
+
+ public function testWithAuthenticatedUser()
+ {
+ $this->container['authenticationManager']
+ ->expects($this->once())
+ ->method('checkCurrentSession')
+ ->will($this->returnValue(true));
+
+ $this->container['applicationAuthorization']
+ ->expects($this->once())
+ ->method('isAllowed')
+ ->will($this->returnValue(false));
+
+ $this->container['userSession']
+ ->expects($this->once())
+ ->method('isLogged')
+ ->will($this->returnValue(true));
+
+ $this->container['response']
+ ->expects($this->never())
+ ->method('redirect');
+
+ $this->nextMiddleware
+ ->expects($this->once())
+ ->method('execute');
+
+ $this->middleware->execute();
+ }
+}