X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ffront%2Fcontroller%2Fvisitor%2FLoginControllerTest.php;h=1312ccb79199c1620651e805ffdded4bd71a838a;hb=a5a9cf23acd1248585173aa32757d9720b5f2d62;hp=9d2233169690cb1521854acd53b732108b1d82ac;hpb=2899ebb5b5e82890c877151f5c02045266ac9973;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/front/controller/visitor/LoginControllerTest.php b/tests/front/controller/visitor/LoginControllerTest.php index 9d223316..1312ccb7 100644 --- a/tests/front/controller/visitor/LoginControllerTest.php +++ b/tests/front/controller/visitor/LoginControllerTest.php @@ -4,9 +4,13 @@ declare(strict_types=1); namespace Shaarli\Front\Controller\Visitor; -use PHPUnit\Framework\TestCase; use Shaarli\Config\ConfigManager; use Shaarli\Front\Exception\LoginBannedException; +use Shaarli\Front\Exception\WrongTokenException; +use Shaarli\Render\TemplatePage; +use Shaarli\Security\CookieManager; +use Shaarli\Security\SessionManager; +use Shaarli\TestCase; use Slim\Http\Request; use Slim\Http\Response; @@ -21,15 +25,25 @@ class LoginControllerTest extends TestCase { $this->createContainer(); + $this->container->cookieManager = $this->createMock(CookieManager::class); + $this->container->sessionManager->method('checkToken')->willReturn(true); + $this->controller = new LoginController($this->container); } + /** + * Test displaying login form with valid parameters. + */ public function testValidControllerInvoke(): void { - $this->createValidContainerMockSet(); - $request = $this->createMock(Request::class); - $request->expects(static::once())->method('getServerParam')->willReturn('> referer'); + $request + ->expects(static::atLeastOnce()) + ->method('getParam') + ->willReturnCallback(function (string $key) { + return 'returnurl' === $key ? '> referer' : null; + }) + ; $response = new Response(); $assignedVariables = []; @@ -48,20 +62,32 @@ class LoginControllerTest extends TestCase static::assertInstanceOf(Response::class, $result); static::assertSame(200, $result->getStatusCode()); - static::assertSame('loginform', (string) $result->getBody()); + static::assertSame(TemplatePage::LOGIN, (string) $result->getBody()); static::assertSame('> referer', $assignedVariables['returnurl']); static::assertSame(true, $assignedVariables['remember_user_default']); static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']); } + /** + * Test displaying login form with username defined in the request. + */ public function testValidControllerInvokeWithUserName(): void { - $this->createValidContainerMockSet(); + $this->container->environment = ['HTTP_REFERER' => '> referer']; $request = $this->createMock(Request::class); - $request->expects(static::once())->method('getServerParam')->willReturn('> referer'); - $request->expects(static::exactly(2))->method('getParam')->willReturn('myUser>'); + $request + ->expects(static::atLeastOnce()) + ->method('getParam') + ->willReturnCallback(function (string $key, $default) { + if ('login' === $key) { + return 'myUser>'; + } + + return $default; + }) + ; $response = new Response(); $assignedVariables = []; @@ -88,10 +114,11 @@ class LoginControllerTest extends TestCase static::assertSame('Login - Shaarli', $assignedVariables['pagetitle']); } + /** + * Test displaying login page while being logged in. + */ public function testLoginControllerWhileLoggedIn(): void { - $this->createValidContainerMockSet(); - $request = $this->createMock(Request::class); $response = new Response(); @@ -101,13 +128,14 @@ class LoginControllerTest extends TestCase static::assertInstanceOf(Response::class, $result); static::assertSame(302, $result->getStatusCode()); - static::assertSame(['./'], $result->getHeader('Location')); + static::assertSame(['/subfolder/'], $result->getHeader('Location')); } + /** + * Test displaying login page with open shaarli configured: redirect to homepage. + */ public function testLoginControllerOpenShaarli(): void { - $this->createValidContainerMockSet(); - $request = $this->createMock(Request::class); $response = new Response(); @@ -124,13 +152,14 @@ class LoginControllerTest extends TestCase static::assertInstanceOf(Response::class, $result); static::assertSame(302, $result->getStatusCode()); - static::assertSame(['./'], $result->getHeader('Location')); + static::assertSame(['/subfolder/'], $result->getHeader('Location')); } + /** + * Test displaying login page while being banned. + */ public function testLoginControllerWhileBanned(): void { - $this->createValidContainerMockSet(); - $request = $this->createMock(Request::class); $response = new Response(); @@ -141,4 +170,235 @@ class LoginControllerTest extends TestCase $this->controller->index($request, $response); } + + /** + * Test processing login with valid parameters. + */ + public function testProcessLoginWithValidParameters(): void + { + $parameters = [ + 'login' => 'bob', + 'password' => 'pass', + ]; + $request = $this->createMock(Request::class); + $request + ->expects(static::atLeastOnce()) + ->method('getParam') + ->willReturnCallback(function (string $key) use ($parameters) { + return $parameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->loginManager->method('canLogin')->willReturn(true); + $this->container->loginManager->expects(static::once())->method('handleSuccessfulLogin'); + $this->container->loginManager + ->expects(static::once()) + ->method('checkCredentials') + ->with('1.2.3.4', '1.2.3.4', 'bob', 'pass') + ->willReturn(true) + ; + $this->container->loginManager->method('getStaySignedInToken')->willReturn(bin2hex(random_bytes(8))); + + $this->container->sessionManager->expects(static::never())->method('extendSession'); + $this->container->sessionManager->expects(static::once())->method('destroy'); + $this->container->sessionManager + ->expects(static::once()) + ->method('cookieParameters') + ->with(0, '/subfolder/', 'shaarli') + ; + $this->container->sessionManager->expects(static::once())->method('start'); + $this->container->sessionManager->expects(static::once())->method('regenerateId')->with(true); + + $result = $this->controller->login($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/', $result->getHeader('location')[0]); + } + + /** + * Test processing login with return URL. + */ + public function testProcessLoginWithReturnUrl(): void + { + $parameters = [ + 'returnurl' => 'http://shaarli/subfolder/admin/shaare', + ]; + $request = $this->createMock(Request::class); + $request + ->expects(static::atLeastOnce()) + ->method('getParam') + ->willReturnCallback(function (string $key) use ($parameters) { + return $parameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->loginManager->method('canLogin')->willReturn(true); + $this->container->loginManager->expects(static::once())->method('handleSuccessfulLogin'); + $this->container->loginManager->expects(static::once())->method('checkCredentials')->willReturn(true); + $this->container->loginManager->method('getStaySignedInToken')->willReturn(bin2hex(random_bytes(8))); + + $result = $this->controller->login($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/admin/shaare', $result->getHeader('location')[0]); + } + + /** + * Test processing login with remember me session enabled. + */ + public function testProcessLoginLongLastingSession(): void + { + $parameters = [ + 'longlastingsession' => true, + ]; + $request = $this->createMock(Request::class); + $request + ->expects(static::atLeastOnce()) + ->method('getParam') + ->willReturnCallback(function (string $key) use ($parameters) { + return $parameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->loginManager->method('canLogin')->willReturn(true); + $this->container->loginManager->expects(static::once())->method('handleSuccessfulLogin'); + $this->container->loginManager->expects(static::once())->method('checkCredentials')->willReturn(true); + $this->container->loginManager->method('getStaySignedInToken')->willReturn(bin2hex(random_bytes(8))); + + $this->container->sessionManager->expects(static::once())->method('destroy'); + $this->container->sessionManager + ->expects(static::once()) + ->method('cookieParameters') + ->with(42, '/subfolder/', 'shaarli') + ; + $this->container->sessionManager->expects(static::once())->method('start'); + $this->container->sessionManager->expects(static::once())->method('regenerateId')->with(true); + $this->container->sessionManager->expects(static::once())->method('extendSession')->willReturn(42); + + $this->container->cookieManager = $this->createMock(CookieManager::class); + $this->container->cookieManager + ->expects(static::once()) + ->method('setCookieParameter') + ->willReturnCallback(function (string $name): CookieManager { + static::assertSame(CookieManager::STAY_SIGNED_IN, $name); + + return $this->container->cookieManager; + }) + ; + + $result = $this->controller->login($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/', $result->getHeader('location')[0]); + } + + /** + * Test processing login with invalid credentials + */ + public function testProcessLoginWrongCredentials(): void + { + $parameters = [ + 'returnurl' => 'http://shaarli/subfolder/admin/shaare', + ]; + $request = $this->createMock(Request::class); + $request + ->expects(static::atLeastOnce()) + ->method('getParam') + ->willReturnCallback(function (string $key) use ($parameters) { + return $parameters[$key] ?? null; + }) + ; + $response = new Response(); + + $this->container->loginManager->method('canLogin')->willReturn(true); + $this->container->loginManager->expects(static::once())->method('handleFailedLogin'); + $this->container->loginManager->expects(static::once())->method('checkCredentials')->willReturn(false); + + $this->container->sessionManager + ->expects(static::once()) + ->method('setSessionParameter') + ->with(SessionManager::KEY_ERROR_MESSAGES, ['Wrong login/password.']) + ; + + $result = $this->controller->login($request, $response); + + static::assertSame(200, $result->getStatusCode()); + static::assertSame(TemplatePage::LOGIN, (string) $result->getBody()); + } + + /** + * Test processing login with wrong token + */ + public function testProcessLoginWrongToken(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->sessionManager = $this->createMock(SessionManager::class); + $this->container->sessionManager->method('checkToken')->willReturn(false); + + $this->expectException(WrongTokenException::class); + + $this->controller->login($request, $response); + } + + /** + * Test processing login with wrong token + */ + public function testProcessLoginAlreadyLoggedIn(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->loginManager->method('isLoggedIn')->willReturn(true); + $this->container->loginManager->expects(static::never())->method('handleSuccessfulLogin'); + $this->container->loginManager->expects(static::never())->method('handleFailedLogin'); + + $result = $this->controller->login($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/', $result->getHeader('location')[0]); + } + + /** + * Test processing login with wrong token + */ + public function testProcessLoginInOpenShaarli(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->conf = $this->createMock(ConfigManager::class); + $this->container->conf->method('get')->willReturnCallback(function (string $key, $value) { + return 'security.open_shaarli' === $key ? true : $value; + }); + + $this->container->loginManager->expects(static::never())->method('handleSuccessfulLogin'); + $this->container->loginManager->expects(static::never())->method('handleFailedLogin'); + + $result = $this->controller->login($request, $response); + + static::assertSame(302, $result->getStatusCode()); + static::assertSame('/subfolder/', $result->getHeader('location')[0]); + } + + /** + * Test processing login while being banned + */ + public function testProcessLoginWhileBanned(): void + { + $request = $this->createMock(Request::class); + $response = new Response(); + + $this->container->loginManager->method('canLogin')->willReturn(false); + $this->container->loginManager->expects(static::never())->method('handleSuccessfulLogin'); + $this->container->loginManager->expects(static::never())->method('handleFailedLogin'); + + $this->expectException(LoginBannedException::class); + + $this->controller->login($request, $response); + } }