From 4eaab1f6da1a56376a840231f7b8a10469308f97 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sun, 15 May 2016 20:56:01 -0400 Subject: Added unit tests for middleware --- .../Middleware/AuthenticationMiddlewareTest.php | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 tests/units/Middleware/AuthenticationMiddlewareTest.php (limited to 'tests/units/Middleware/AuthenticationMiddlewareTest.php') 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 @@ +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(); + } +} -- cgit v1.2.3