namespace Shaarli\Security;
+use Psr\Log\LoggerInterface;
+use Shaarli\FakeConfigManager;
use Shaarli\TestCase;
/**
*/
class LoginManagerTest extends TestCase
{
- /** @var \FakeConfigManager Configuration Manager instance */
+ /** @var FakeConfigManager Configuration Manager instance */
protected $configManager = null;
/** @var LoginManager Login Manager instance */
/** @var CookieManager */
protected $cookieManager;
+ /** @var BanManager */
+ protected $banManager;
+
/**
* Prepare or reset test resources
*/
$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,
return $this->cookie[$key] ?? null;
});
$this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path');
- $this->loginManager = new LoginManager($this->configManager, $this->sessionManager, $this->cookieManager);
+ $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));
}
/**
'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));
}
*/
public function testCheckLoginStateNotConfigured()
{
- $configManager = new \FakeConfigManager([
+ $configManager = new FakeConfigManager([
'resource.ban_file' => $this->banFile,
]);
- $loginManager = new LoginManager($configManager, null, $this->cookieManager);
+ $loginManager = new LoginManager(
+ $configManager,
+ $this->sessionManager,
+ $this->cookieManager,
+ $this->banManager,
+ $this->createMock(LoggerInterface::class)
+ );
$loginManager->checkLoginState('');
$this->assertFalse($loginManager->isLoggedIn());
public function testCheckCredentialsWrongLogin()
{
$this->assertFalse(
- $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password)
+ $this->loginManager->checkCredentials('', 'b4dl0g1n', $this->password)
);
}
public function testCheckCredentialsWrongPassword()
{
$this->assertFalse(
- $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd')
+ $this->loginManager->checkCredentials('', $this->login, 'b4dp455wd')
);
}
public function testCheckCredentialsWrongLoginAndPassword()
{
$this->assertFalse(
- $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
+ $this->loginManager->checkCredentials('', 'b4dl0g1n', 'b4dp455wd')
);
}
public function testCheckCredentialsGoodLoginAndPassword()
{
$this->assertTrue(
- $this->loginManager->checkCredentials('', '', $this->login, $this->password)
+ $this->loginManager->checkCredentials('', $this->login, $this->password)
);
}
{
$this->configManager->set('ldap.host', 'dummy');
$this->assertFalse(
- $this->loginManager->checkCredentials('', '', $this->login, $this->password)
+ $this->loginManager->checkCredentials('', $this->login, $this->password)
);
}