aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/security/LoginManagerTest.php
diff options
context:
space:
mode:
authorArthurHoaro <arthur@hoa.ro>2020-10-13 12:05:08 +0200
committerArthurHoaro <arthur@hoa.ro>2020-10-13 12:05:08 +0200
commitb6f678a5a1d15acf284ebcec16c905e976671ce1 (patch)
tree33c7da831482ed79c44896ef19c73c72ada84f2e /tests/security/LoginManagerTest.php
parentb14687036b9b800681197f51fdc47e62f0c88e2e (diff)
parent1c1520b6b98ab20201bfe15577782a52320339df (diff)
downloadShaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.gz
Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.tar.zst
Shaarli-b6f678a5a1d15acf284ebcec16c905e976671ce1.zip
Merge branch 'v0.12' into latest
Diffstat (limited to 'tests/security/LoginManagerTest.php')
-rw-r--r--tests/security/LoginManagerTest.php68
1 files changed, 54 insertions, 14 deletions
diff --git a/tests/security/LoginManagerTest.php b/tests/security/LoginManagerTest.php
index eef0f22a..d302983d 100644
--- a/tests/security/LoginManagerTest.php
+++ b/tests/security/LoginManagerTest.php
@@ -1,9 +1,8 @@
1<?php 1<?php
2namespace Shaarli\Security;
3 2
4require_once 'tests/utils/FakeConfigManager.php'; 3namespace Shaarli\Security;
5 4
6use PHPUnit\Framework\TestCase; 5use Shaarli\TestCase;
7 6
8/** 7/**
9 * Test coverage for LoginManager 8 * Test coverage for LoginManager
@@ -58,10 +57,13 @@ class LoginManagerTest extends TestCase
58 /** @var string Salt used by hash functions */ 57 /** @var string Salt used by hash functions */
59 protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2'; 58 protected $salt = '669e24fa9c5a59a613f98e8e38327384504a4af2';
60 59
60 /** @var CookieManager */
61 protected $cookieManager;
62
61 /** 63 /**
62 * Prepare or reset test resources 64 * Prepare or reset test resources
63 */ 65 */
64 public function setUp() 66 protected function setUp(): void
65 { 67 {
66 if (file_exists($this->banFile)) { 68 if (file_exists($this->banFile)) {
67 unlink($this->banFile); 69 unlink($this->banFile);
@@ -78,13 +80,18 @@ class LoginManagerTest extends TestCase
78 'security.ban_after' => 2, 80 'security.ban_after' => 2,
79 'security.ban_duration' => 3600, 81 'security.ban_duration' => 3600,
80 'security.trusted_proxies' => [$this->trustedProxy], 82 'security.trusted_proxies' => [$this->trustedProxy],
83 'ldap.host' => '',
81 ]); 84 ]);
82 85
83 $this->cookie = []; 86 $this->cookie = [];
84 $this->session = []; 87 $this->session = [];
85 88
86 $this->sessionManager = new SessionManager($this->session, $this->configManager); 89 $this->cookieManager = $this->createMock(CookieManager::class);
87 $this->loginManager = new LoginManager($this->configManager, $this->sessionManager); 90 $this->cookieManager->method('getCookieParameter')->willReturnCallback(function (string $key) {
91 return $this->cookie[$key] ?? null;
92 });
93 $this->sessionManager = new SessionManager($this->session, $this->configManager, 'session_path');
94 $this->loginManager = new LoginManager($this->configManager, $this->sessionManager, $this->cookieManager);
88 $this->server['REMOTE_ADDR'] = $this->ipAddr; 95 $this->server['REMOTE_ADDR'] = $this->ipAddr;
89 } 96 }
90 97
@@ -192,8 +199,8 @@ class LoginManagerTest extends TestCase
192 $configManager = new \FakeConfigManager([ 199 $configManager = new \FakeConfigManager([
193 'resource.ban_file' => $this->banFile, 200 'resource.ban_file' => $this->banFile,
194 ]); 201 ]);
195 $loginManager = new LoginManager($configManager, null); 202 $loginManager = new LoginManager($configManager, null, $this->cookieManager);
196 $loginManager->checkLoginState([], ''); 203 $loginManager->checkLoginState('');
197 204
198 $this->assertFalse($loginManager->isLoggedIn()); 205 $this->assertFalse($loginManager->isLoggedIn());
199 } 206 }
@@ -209,9 +216,9 @@ class LoginManagerTest extends TestCase
209 'expires_on' => time() + 100, 216 'expires_on' => time() + 100,
210 ]; 217 ];
211 $this->loginManager->generateStaySignedInToken($this->clientIpAddress); 218 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
212 $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope'; 219 $this->cookie[CookieManager::STAY_SIGNED_IN] = 'nope';
213 220
214 $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); 221 $this->loginManager->checkLoginState($this->clientIpAddress);
215 222
216 $this->assertTrue($this->loginManager->isLoggedIn()); 223 $this->assertTrue($this->loginManager->isLoggedIn());
217 $this->assertTrue(empty($this->session['username'])); 224 $this->assertTrue(empty($this->session['username']));
@@ -223,9 +230,9 @@ class LoginManagerTest extends TestCase
223 public function testCheckLoginStateStaySignedInWithValidToken() 230 public function testCheckLoginStateStaySignedInWithValidToken()
224 { 231 {
225 $this->loginManager->generateStaySignedInToken($this->clientIpAddress); 232 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
226 $this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken(); 233 $this->cookie[CookieManager::STAY_SIGNED_IN] = $this->loginManager->getStaySignedInToken();
227 234
228 $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); 235 $this->loginManager->checkLoginState($this->clientIpAddress);
229 236
230 $this->assertTrue($this->loginManager->isLoggedIn()); 237 $this->assertTrue($this->loginManager->isLoggedIn());
231 $this->assertEquals($this->login, $this->session['username']); 238 $this->assertEquals($this->login, $this->session['username']);
@@ -240,7 +247,7 @@ class LoginManagerTest extends TestCase
240 $this->loginManager->generateStaySignedInToken($this->clientIpAddress); 247 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
241 $this->session['expires_on'] = time() - 100; 248 $this->session['expires_on'] = time() - 100;
242 249
243 $this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress); 250 $this->loginManager->checkLoginState($this->clientIpAddress);
244 251
245 $this->assertFalse($this->loginManager->isLoggedIn()); 252 $this->assertFalse($this->loginManager->isLoggedIn());
246 } 253 }
@@ -252,7 +259,7 @@ class LoginManagerTest extends TestCase
252 { 259 {
253 $this->loginManager->generateStaySignedInToken($this->clientIpAddress); 260 $this->loginManager->generateStaySignedInToken($this->clientIpAddress);
254 261
255 $this->loginManager->checkLoginState($this->cookie, '10.7.157.98'); 262 $this->loginManager->checkLoginState('10.7.157.98');
256 263
257 $this->assertFalse($this->loginManager->isLoggedIn()); 264 $this->assertFalse($this->loginManager->isLoggedIn());
258 } 265 }
@@ -296,4 +303,37 @@ class LoginManagerTest extends TestCase
296 $this->loginManager->checkCredentials('', '', $this->login, $this->password) 303 $this->loginManager->checkCredentials('', '', $this->login, $this->password)
297 ); 304 );
298 } 305 }
306
307 /**
308 * Check user credentials through LDAP - server unreachable
309 */
310 public function testCheckCredentialsFromUnreachableLdap()
311 {
312 $this->configManager->set('ldap.host', 'dummy');
313 $this->assertFalse(
314 $this->loginManager->checkCredentials('', '', $this->login, $this->password)
315 );
316 }
317
318 /**
319 * Check user credentials through LDAP - wrong login and password supplied
320 */
321 public function testCheckCredentialsFromLdapWrongLoginAndPassword()
322 {
323 $this->configManager->set('ldap.host', 'dummy');
324 $this->assertFalse(
325 $this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return false; })
326 );
327 }
328
329 /**
330 * Check user credentials through LDAP - correct login and password supplied
331 */
332 public function testCheckCredentialsFromLdapGoodLoginAndPassword()
333 {
334 $this->configManager->set('ldap.host', 'dummy');
335 $this->assertTrue(
336 $this->loginManager->checkCredentialsFromLdap($this->login, $this->password, function() { return null; }, function() { return true; })
337 );
338 }
299} 339}