X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=sidebyside;f=tests%2Fsecurity%2FLoginManagerTest.php;h=f7609fc676e8601a03dc688cdf2cee6407e1fe97;hb=820cae27cfcc94af552818f3f1e5342e00478f6c;hp=eef0f22a38fac122fe4dd0ec78c63a6c45949612;hpb=c3a04e328f2c8d40890b0b26b118a193110634ce;p=github%2Fshaarli%2FShaarli.git diff --git a/tests/security/LoginManagerTest.php b/tests/security/LoginManagerTest.php index eef0f22a..f7609fc6 100644 --- a/tests/security/LoginManagerTest.php +++ b/tests/security/LoginManagerTest.php @@ -1,16 +1,17 @@ banFile)) { unlink($this->banFile); @@ -69,7 +76,7 @@ class LoginManagerTest extends TestCase $this->passwordHash = sha1($this->password . $this->login . $this->salt); - $this->configManager = new \FakeConfigManager([ + $this->configManager = new FakeConfigManager([ 'credentials.login' => $this->login, 'credentials.hash' => $this->passwordHash, 'credentials.salt' => $this->salt, @@ -78,24 +85,40 @@ class LoginManagerTest extends TestCase 'security.ban_after' => 2, 'security.ban_duration' => 3600, 'security.trusted_proxies' => [$this->trustedProxy], + 'ldap.host' => '', ]); $this->cookie = []; $this->session = []; - $this->sessionManager = new SessionManager($this->session, $this->configManager); - $this->loginManager = new LoginManager($this->configManager, $this->sessionManager); + $this->cookieManager = $this->createMock(CookieManager::class); + $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) { + return $this->cookie[$key] ?? null; + }); + $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path'); + $this->banManager = $this->createMock(BanManager::class); + $this->loginManager = new LoginManager( + $this->configManager, + $this->sessionManager, + $this->cookieManager, + $this->banManager, + $this->createMock(LoggerInterface::class) + ); $this->server['REMOTE_ADDR'] = $this->ipAddr; } /** * Record a failed login attempt */ - public function testHandleFailedLogin() + public function testHandleFailedLogin(): void { + $this->banManager->expects(static::exactly(2))->method('handleFailedAttempt'); + $this->banManager->method('isBanned')->willReturn(true); + $this->loginManager->handleFailedLogin($this->server); $this->loginManager->handleFailedLogin($this->server); - $this->assertFalse($this->loginManager->canLogin($this->server)); + + static::assertFalse($this->loginManager->canLogin($this->server)); } /** @@ -107,8 +130,13 @@ class LoginManagerTest extends TestCase 'REMOTE_ADDR' => $this->trustedProxy, 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, ]; + + $this->banManager->expects(static::exactly(2))->method('handleFailedAttempt'); + $this->banManager->method('isBanned')->willReturn(true); + $this->loginManager->handleFailedLogin($server); $this->loginManager->handleFailedLogin($server); + $this->assertFalse($this->loginManager->canLogin($server)); } @@ -189,11 +217,17 @@ class LoginManagerTest extends TestCase */ public function testCheckLoginStateNotConfigured() { - $configManager = new \FakeConfigManager([ + $configManager = new FakeConfigManager([ 'resource.ban_file' => $this->banFile, ]); - $loginManager = new LoginManager($configManager, null); - $loginManager->checkLoginState([], ''); + $loginManager = new LoginManager( + $configManager, + $this->sessionManager, + $this->cookieManager, + $this->banManager, + $this->createMock(LoggerInterface::class) + ); + $loginManager->checkLoginState(''); $this->assertFalse($loginManager->isLoggedIn()); } @@ -209,9 +243,9 @@ class LoginManagerTest extends TestCase 'expires_on' => time() + 100, ]; $this->loginManager->generateStaySignedInToken($this->clientIpAddress); - $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope'; + $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope'; - $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); + $this->loginManager->checkLoginState($this->clientIpAddress); $this->assertTrue($this->loginManager->isLoggedIn()); $this->assertTrue(empty($this->session['username'])); @@ -223,9 +257,9 @@ class LoginManagerTest extends TestCase public function testCheckLoginStateStaySignedInWithValidToken() { $this->loginManager->generateStaySignedInToken($this->clientIpAddress); - $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken(); + $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken(); - $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); + $this->loginManager->checkLoginState($this->clientIpAddress); $this->assertTrue($this->loginManager->isLoggedIn()); $this->assertEquals($this->login, $this->session['username']); @@ -240,7 +274,7 @@ class LoginManagerTest extends TestCase $this->loginManager->generateStaySignedInToken($this->clientIpAddress); $this->session['expires_on'] = time() - 100; - $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); + $this->loginManager->checkLoginState($this->clientIpAddress); $this->assertFalse($this->loginManager->isLoggedIn()); } @@ -252,7 +286,7 @@ class LoginManagerTest extends TestCase { $this->loginManager->generateStaySignedInToken($this->clientIpAddress); - $this->loginManager->checkLoginState($this->cookie, '10.7.157.98'); + $this->loginManager->checkLoginState('10.7.157.98'); $this->assertFalse($this->loginManager->isLoggedIn()); } @@ -263,7 +297,7 @@ class LoginManagerTest extends TestCase public function testCheckCredentialsWrongLogin() { $this->assertFalse( - $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password) + $this->loginManager->checkCredentials('', 'b4dl0g1n', $this->password) ); } @@ -273,7 +307,7 @@ class LoginManagerTest extends TestCase public function testCheckCredentialsWrongPassword() { $this->assertFalse( - $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd') + $this->loginManager->checkCredentials('', $this->login, 'b4dp455wd') ); } @@ -283,7 +317,7 @@ class LoginManagerTest extends TestCase public function testCheckCredentialsWrongLoginAndPassword() { $this->assertFalse( - $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd') + $this->loginManager->checkCredentials('', 'b4dl0g1n', 'b4dp455wd') ); } @@ -293,7 +327,40 @@ class LoginManagerTest extends TestCase public function testCheckCredentialsGoodLoginAndPassword() { $this->assertTrue( - $this->loginManager->checkCredentials('', '', $this->login, $this->password) + $this->loginManager->checkCredentials('', $this->login, $this->password) + ); + } + + /** + * Check user credentials through LDAP - server unreachable + */ + public function testCheckCredentialsFromUnreachableLdap() + { + $this->configManager->set('ldap.host', 'dummy'); + $this->assertFalse( + $this->loginManager->checkCredentials('', $this->login, $this->password) + ); + } + + /** + * Check user credentials through LDAP - wrong login and password supplied + */ + public function testCheckCredentialsFromLdapWrongLoginAndPassword() + { + $this->configManager->set('ldap.host', 'dummy'); + $this->assertFalse( + $this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return false; }) + ); + } + + /** + * Check user credentials through LDAP - correct login and password supplied + */ + public function testCheckCredentialsFromLdapGoodLoginAndPassword() + { + $this->configManager->set('ldap.host', 'dummy'); + $this->assertTrue( + $this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return true; }) ); } }