diff options
Diffstat (limited to 'tests/security/LoginManagerTest.php')
-rw-r--r-- | tests/security/LoginManagerTest.php | 81 |
1 files changed, 57 insertions, 24 deletions
diff --git a/tests/security/LoginManagerTest.php b/tests/security/LoginManagerTest.php index 8fd1698c..f7609fc6 100644 --- a/tests/security/LoginManagerTest.php +++ b/tests/security/LoginManagerTest.php | |||
@@ -1,16 +1,17 @@ | |||
1 | <?php | 1 | <?php |
2 | namespace Shaarli\Security; | ||
3 | 2 | ||
4 | require_once 'tests/utils/FakeConfigManager.php'; | 3 | namespace Shaarli\Security; |
5 | 4 | ||
6 | use PHPUnit\Framework\TestCase; | 5 | use Psr\Log\LoggerInterface; |
6 | use Shaarli\FakeConfigManager; | ||
7 | use Shaarli\TestCase; | ||
7 | 8 | ||
8 | /** | 9 | /** |
9 | * Test coverage for LoginManager | 10 | * Test coverage for LoginManager |
10 | */ | 11 | */ |
11 | class LoginManagerTest extends TestCase | 12 | class LoginManagerTest extends TestCase |
12 | { | 13 | { |
13 | /** @var \FakeConfigManager Configuration Manager instance */ | 14 | /** @var FakeConfigManager Configuration Manager instance */ |
14 | protected $configManager = null; | 15 | protected $configManager = null; |
15 | 16 | ||
16 | /** @var LoginManager Login Manager instance */ | 17 | /** @var LoginManager Login Manager instance */ |
@@ -58,10 +59,16 @@ class LoginManagerTest extends TestCase | |||
58 | /** @var string Salt used by hash functions */ | 59 | /** @var string Salt used by hash functions */ |
59 | protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2'; | 60 | protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2'; |
60 | 61 | ||
62 | /** @var CookieManager */ | ||
63 | protected $cookieManager; | ||
64 | |||
65 | /** @var BanManager */ | ||
66 | protected $banManager; | ||
67 | |||
61 | /** | 68 | /** |
62 | * Prepare or reset test resources | 69 | * Prepare or reset test resources |
63 | */ | 70 | */ |
64 | public function setUp() | 71 | protected function setUp(): void |
65 | { | 72 | { |
66 | if (file_exists($this->banFile)) { | 73 | if (file_exists($this->banFile)) { |
67 | unlink($this->banFile); | 74 | unlink($this->banFile); |
@@ -69,7 +76,7 @@ class LoginManagerTest extends TestCase | |||
69 | 76 | ||
70 | $this->passwordHash = sha1($this->password . $this->login . $this->salt); | 77 | $this->passwordHash = sha1($this->password . $this->login . $this->salt); |
71 | 78 | ||
72 | $this->configManager = new \FakeConfigManager([ | 79 | $this->configManager = new FakeConfigManager([ |
73 | 'credentials.login' => $this->login, | 80 | 'credentials.login' => $this->login, |
74 | 'credentials.hash' => $this->passwordHash, | 81 | 'credentials.hash' => $this->passwordHash, |
75 | 'credentials.salt' => $this->salt, | 82 | 'credentials.salt' => $this->salt, |
@@ -84,19 +91,34 @@ class LoginManagerTest extends TestCase | |||
84 | $this->cookie = []; | 91 | $this->cookie = []; |
85 | $this->session = []; | 92 | $this->session = []; |
86 | 93 | ||
87 | $this->sessionManager = new SessionManager($this->session, $this->configManager); | 94 | $this->cookieManager = $this->createMock(CookieManager::class); |
88 | $this->loginManager = new LoginManager($this->configManager, $this->sessionManager); | 95 | $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) { |
96 | return $this->cookie[$key] ?? null; | ||
97 | }); | ||
98 | $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path'); | ||
99 | $this->banManager = $this->createMock(BanManager::class); | ||
100 | $this->loginManager = new LoginManager( | ||
101 | $this->configManager, | ||
102 | $this->sessionManager, | ||
103 | $this->cookieManager, | ||
104 | $this->banManager, | ||
105 | $this->createMock(LoggerInterface::class) | ||
106 | ); | ||
89 | $this->server['REMOTE_ADDR'] = $this->ipAddr; | 107 | $this->server['REMOTE_ADDR'] = $this->ipAddr; |
90 | } | 108 | } |
91 | 109 | ||
92 | /** | 110 | /** |
93 | * Record a failed login attempt | 111 | * Record a failed login attempt |
94 | */ | 112 | */ |
95 | public function testHandleFailedLogin() | 113 | public function testHandleFailedLogin(): void |
96 | { | 114 | { |
115 | $this->banManager->expects(static::exactly(2))->method('handleFailedAttempt'); | ||
116 | $this->banManager->method('isBanned')->willReturn(true); | ||
117 | |||
97 | $this->loginManager->handleFailedLogin($this->server); | 118 | $this->loginManager->handleFailedLogin($this->server); |
98 | $this->loginManager->handleFailedLogin($this->server); | 119 | $this->loginManager->handleFailedLogin($this->server); |
99 | $this->assertFalse($this->loginManager->canLogin($this->server)); | 120 | |
121 | static::assertFalse($this->loginManager->canLogin($this->server)); | ||
100 | } | 122 | } |
101 | 123 | ||
102 | /** | 124 | /** |
@@ -108,8 +130,13 @@ class LoginManagerTest extends TestCase | |||
108 | 'REMOTE_ADDR' => $this->trustedProxy, | 130 | 'REMOTE_ADDR' => $this->trustedProxy, |
109 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, | 131 | 'HTTP_X_FORWARDED_FOR' => $this->ipAddr, |
110 | ]; | 132 | ]; |
133 | |||
134 | $this->banManager->expects(static::exactly(2))->method('handleFailedAttempt'); | ||
135 | $this->banManager->method('isBanned')->willReturn(true); | ||
136 | |||
111 | $this->loginManager->handleFailedLogin($server); | 137 | $this->loginManager->handleFailedLogin($server); |
112 | $this->loginManager->handleFailedLogin($server); | 138 | $this->loginManager->handleFailedLogin($server); |
139 | |||
113 | $this->assertFalse($this->loginManager->canLogin($server)); | 140 | $this->assertFalse($this->loginManager->canLogin($server)); |
114 | } | 141 | } |
115 | 142 | ||
@@ -190,11 +217,17 @@ class LoginManagerTest extends TestCase | |||
190 | */ | 217 | */ |
191 | public function testCheckLoginStateNotConfigured() | 218 | public function testCheckLoginStateNotConfigured() |
192 | { | 219 | { |
193 | $configManager = new \FakeConfigManager([ | 220 | $configManager = new FakeConfigManager([ |
194 | 'resource.ban_file' => $this->banFile, | 221 | 'resource.ban_file' => $this->banFile, |
195 | ]); | 222 | ]); |
196 | $loginManager = new LoginManager($configManager, null); | 223 | $loginManager = new LoginManager( |
197 | $loginManager->checkLoginState([], ''); | 224 | $configManager, |
225 | $this->sessionManager, | ||
226 | $this->cookieManager, | ||
227 | $this->banManager, | ||
228 | $this->createMock(LoggerInterface::class) | ||
229 | ); | ||
230 | $loginManager->checkLoginState(''); | ||
198 | 231 | ||
199 | $this->assertFalse($loginManager->isLoggedIn()); | 232 | $this->assertFalse($loginManager->isLoggedIn()); |
200 | } | 233 | } |
@@ -210,9 +243,9 @@ class LoginManagerTest extends TestCase | |||
210 | 'expires_on' => time() + 100, | 243 | 'expires_on' => time() + 100, |
211 | ]; | 244 | ]; |
212 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 245 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
213 | $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope'; | 246 | $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope'; |
214 | 247 | ||
215 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | 248 | $this->loginManager->checkLoginState($this->clientIpAddress); |
216 | 249 | ||
217 | $this->assertTrue($this->loginManager->isLoggedIn()); | 250 | $this->assertTrue($this->loginManager->isLoggedIn()); |
218 | $this->assertTrue(empty($this->session['username'])); | 251 | $this->assertTrue(empty($this->session['username'])); |
@@ -224,9 +257,9 @@ class LoginManagerTest extends TestCase | |||
224 | public function testCheckLoginStateStaySignedInWithValidToken() | 257 | public function testCheckLoginStateStaySignedInWithValidToken() |
225 | { | 258 | { |
226 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 259 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
227 | $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken(); | 260 | $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken(); |
228 | 261 | ||
229 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | 262 | $this->loginManager->checkLoginState($this->clientIpAddress); |
230 | 263 | ||
231 | $this->assertTrue($this->loginManager->isLoggedIn()); | 264 | $this->assertTrue($this->loginManager->isLoggedIn()); |
232 | $this->assertEquals($this->login, $this->session['username']); | 265 | $this->assertEquals($this->login, $this->session['username']); |
@@ -241,7 +274,7 @@ class LoginManagerTest extends TestCase | |||
241 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 274 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
242 | $this->session['expires_on'] = time() - 100; | 275 | $this->session['expires_on'] = time() - 100; |
243 | 276 | ||
244 | $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); | 277 | $this->loginManager->checkLoginState($this->clientIpAddress); |
245 | 278 | ||
246 | $this->assertFalse($this->loginManager->isLoggedIn()); | 279 | $this->assertFalse($this->loginManager->isLoggedIn()); |
247 | } | 280 | } |
@@ -253,7 +286,7 @@ class LoginManagerTest extends TestCase | |||
253 | { | 286 | { |
254 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); | 287 | $this->loginManager->generateStaySignedInToken($this->clientIpAddress); |
255 | 288 | ||
256 | $this->loginManager->checkLoginState($this->cookie, '10.7.157.98'); | 289 | $this->loginManager->checkLoginState('10.7.157.98'); |
257 | 290 | ||
258 | $this->assertFalse($this->loginManager->isLoggedIn()); | 291 | $this->assertFalse($this->loginManager->isLoggedIn()); |
259 | } | 292 | } |
@@ -264,7 +297,7 @@ class LoginManagerTest extends TestCase | |||
264 | public function testCheckCredentialsWrongLogin() | 297 | public function testCheckCredentialsWrongLogin() |
265 | { | 298 | { |
266 | $this->assertFalse( | 299 | $this->assertFalse( |
267 | $this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password) | 300 | $this->loginManager->checkCredentials('', 'b4dl0g1n', $this->password) |
268 | ); | 301 | ); |
269 | } | 302 | } |
270 | 303 | ||
@@ -274,7 +307,7 @@ class LoginManagerTest extends TestCase | |||
274 | public function testCheckCredentialsWrongPassword() | 307 | public function testCheckCredentialsWrongPassword() |
275 | { | 308 | { |
276 | $this->assertFalse( | 309 | $this->assertFalse( |
277 | $this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd') | 310 | $this->loginManager->checkCredentials('', $this->login, 'b4dp455wd') |
278 | ); | 311 | ); |
279 | } | 312 | } |
280 | 313 | ||
@@ -284,7 +317,7 @@ class LoginManagerTest extends TestCase | |||
284 | public function testCheckCredentialsWrongLoginAndPassword() | 317 | public function testCheckCredentialsWrongLoginAndPassword() |
285 | { | 318 | { |
286 | $this->assertFalse( | 319 | $this->assertFalse( |
287 | $this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd') | 320 | $this->loginManager->checkCredentials('', 'b4dl0g1n', 'b4dp455wd') |
288 | ); | 321 | ); |
289 | } | 322 | } |
290 | 323 | ||
@@ -294,7 +327,7 @@ class LoginManagerTest extends TestCase | |||
294 | public function testCheckCredentialsGoodLoginAndPassword() | 327 | public function testCheckCredentialsGoodLoginAndPassword() |
295 | { | 328 | { |
296 | $this->assertTrue( | 329 | $this->assertTrue( |
297 | $this->loginManager->checkCredentials('', '', $this->login, $this->password) | 330 | $this->loginManager->checkCredentials('', $this->login, $this->password) |
298 | ); | 331 | ); |
299 | } | 332 | } |
300 | 333 | ||
@@ -305,7 +338,7 @@ class LoginManagerTest extends TestCase | |||
305 | { | 338 | { |
306 | $this->configManager->set('ldap.host', 'dummy'); | 339 | $this->configManager->set('ldap.host', 'dummy'); |
307 | $this->assertFalse( | 340 | $this->assertFalse( |
308 | $this->loginManager->checkCredentials('', '', $this->login, $this->password) | 341 | $this->loginManager->checkCredentials('', $this->login, $this->password) |
309 | ); | 342 | ); |
310 | } | 343 | } |
311 | 344 | ||