Add test coverage for LoginManager methods
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
parent
ebf6151738
commit
704637bfeb
4 changed files with 161 additions and 11 deletions
|
@ -46,7 +46,7 @@ public function __construct(& $globals, $configManager, $sessionManager)
|
||||||
$this->sessionManager = $sessionManager;
|
$this->sessionManager = $sessionManager;
|
||||||
$this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php');
|
$this->banFile = $this->configManager->get('resource.ban_file', 'data/ipbans.php');
|
||||||
$this->readBanFile();
|
$this->readBanFile();
|
||||||
if ($this->configManager->get('security.open_shaarli')) {
|
if ($this->configManager->get('security.open_shaarli') === true) {
|
||||||
$this->openShaarli = true;
|
$this->openShaarli = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,8 +80,6 @@ public function getStaySignedInToken()
|
||||||
*
|
*
|
||||||
* @param array $cookie The $_COOKIE array
|
* @param array $cookie The $_COOKIE array
|
||||||
* @param string $clientIpId Client IP address identifier
|
* @param string $clientIpId Client IP address identifier
|
||||||
*
|
|
||||||
* @return bool true if the user session is valid, false otherwise
|
|
||||||
*/
|
*/
|
||||||
public function checkLoginState($cookie, $clientIpId)
|
public function checkLoginState($cookie, $clientIpId)
|
||||||
{
|
{
|
||||||
|
@ -94,11 +92,12 @@ public function checkLoginState($cookie, $clientIpId)
|
||||||
if (isset($cookie[self::$STAY_SIGNED_IN_COOKIE])
|
if (isset($cookie[self::$STAY_SIGNED_IN_COOKIE])
|
||||||
&& $cookie[self::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
|
&& $cookie[self::$STAY_SIGNED_IN_COOKIE] === $this->staySignedInToken
|
||||||
) {
|
) {
|
||||||
|
// The user client has a valid stay-signed-in cookie
|
||||||
|
// Session information is updated with the current client information
|
||||||
$this->sessionManager->storeLoginInfo($clientIpId);
|
$this->sessionManager->storeLoginInfo($clientIpId);
|
||||||
$this->isLoggedIn = true;
|
$this->isLoggedIn = true;
|
||||||
}
|
|
||||||
|
|
||||||
if ($this->sessionManager->hasSessionExpired()
|
} elseif ($this->sessionManager->hasSessionExpired()
|
||||||
|| $this->sessionManager->hasClientIpChanged($clientIpId)
|
|| $this->sessionManager->hasClientIpChanged($clientIpId)
|
||||||
) {
|
) {
|
||||||
$this->sessionManager->logout();
|
$this->sessionManager->logout();
|
||||||
|
|
|
@ -9,13 +9,40 @@
|
||||||
*/
|
*/
|
||||||
class LoginManagerTest extends TestCase
|
class LoginManagerTest extends TestCase
|
||||||
{
|
{
|
||||||
|
/** @var \FakeConfigManager Configuration Manager instance */
|
||||||
protected $configManager = null;
|
protected $configManager = null;
|
||||||
|
|
||||||
|
/** @var LoginManager Login Manager instance */
|
||||||
protected $loginManager = null;
|
protected $loginManager = null;
|
||||||
|
|
||||||
|
/** @var SessionManager Session Manager instance */
|
||||||
|
protected $sessionManager = null;
|
||||||
|
|
||||||
|
/** @var string Banned IP filename */
|
||||||
protected $banFile = 'sandbox/ipbans.php';
|
protected $banFile = 'sandbox/ipbans.php';
|
||||||
|
|
||||||
|
/** @var string Log filename */
|
||||||
protected $logFile = 'sandbox/shaarli.log';
|
protected $logFile = 'sandbox/shaarli.log';
|
||||||
|
|
||||||
|
/** @var array Simulates the $_COOKIE array */
|
||||||
|
protected $cookie = [];
|
||||||
|
|
||||||
|
/** @var array Simulates the $GLOBALS array */
|
||||||
protected $globals = [];
|
protected $globals = [];
|
||||||
protected $ipAddr = '127.0.0.1';
|
|
||||||
|
/** @var array Simulates the $_SERVER array */
|
||||||
protected $server = [];
|
protected $server = [];
|
||||||
|
|
||||||
|
/** @var array Simulates the $_SESSION array */
|
||||||
|
protected $session = [];
|
||||||
|
|
||||||
|
/** @var string Advertised client IP address */
|
||||||
|
protected $clientIpAddress = '10.1.47.179';
|
||||||
|
|
||||||
|
/** @var string Local client IP address */
|
||||||
|
protected $ipAddr = '127.0.0.1';
|
||||||
|
|
||||||
|
/** @var string Trusted proxy IP address */
|
||||||
protected $trustedProxy = '10.1.1.100';
|
protected $trustedProxy = '10.1.1.100';
|
||||||
|
|
||||||
/** @var string User login */
|
/** @var string User login */
|
||||||
|
@ -52,10 +79,18 @@ public function setUp()
|
||||||
'security.trusted_proxies' => [$this->trustedProxy],
|
'security.trusted_proxies' => [$this->trustedProxy],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$this->cookie = [];
|
||||||
|
|
||||||
$this->globals = &$GLOBALS;
|
$this->globals = &$GLOBALS;
|
||||||
unset($this->globals['IPBANS']);
|
unset($this->globals['IPBANS']);
|
||||||
|
|
||||||
$this->loginManager = new LoginManager($this->globals, $this->configManager, null);
|
$this->session = [
|
||||||
|
'expires_on' => time() + 100,
|
||||||
|
'ip' => $this->clientIpAddress,
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->sessionManager = new SessionManager($this->session, $this->configManager);
|
||||||
|
$this->loginManager = new LoginManager($this->globals, $this->configManager, $this->sessionManager);
|
||||||
$this->server['REMOTE_ADDR'] = $this->ipAddr;
|
$this->server['REMOTE_ADDR'] = $this->ipAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,12 +254,116 @@ public function testCanLoginIpBanExpired()
|
||||||
*/
|
*/
|
||||||
public function testGenerateStaySignedInToken()
|
public function testGenerateStaySignedInToken()
|
||||||
{
|
{
|
||||||
$ipAddress = '10.1.47.179';
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
||||||
$this->loginManager->generateStaySignedInToken($ipAddress);
|
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
sha1($this->passwordHash . $ipAddress . $this->salt),
|
sha1($this->passwordHash . $this->clientIpAddress . $this->salt),
|
||||||
$this->loginManager->getStaySignedInToken()
|
$this->loginManager->getStaySignedInToken()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user login - Shaarli has not yet been configured
|
||||||
|
*/
|
||||||
|
public function testCheckLoginStateNotConfigured()
|
||||||
|
{
|
||||||
|
$configManager = new \FakeConfigManager([
|
||||||
|
'resource.ban_file' => $this->banFile,
|
||||||
|
]);
|
||||||
|
$loginManager = new LoginManager($this->globals, $configManager, null);
|
||||||
|
$loginManager->checkLoginState([], '');
|
||||||
|
|
||||||
|
$this->assertFalse($loginManager->isLoggedIn());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user login - the client cookie does not match the server token
|
||||||
|
*/
|
||||||
|
public function testCheckLoginStateStaySignedInWithInvalidToken()
|
||||||
|
{
|
||||||
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
||||||
|
$this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = 'nope';
|
||||||
|
|
||||||
|
$this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
|
||||||
|
|
||||||
|
$this->assertFalse($this->loginManager->isLoggedIn());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user login - the client cookie matches the server token
|
||||||
|
*/
|
||||||
|
public function testCheckLoginStateStaySignedInWithValidToken()
|
||||||
|
{
|
||||||
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
||||||
|
$this->cookie[LoginManager::$STAY_SIGNED_IN_COOKIE] = $this->loginManager->getStaySignedInToken();
|
||||||
|
|
||||||
|
$this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
|
||||||
|
|
||||||
|
$this->assertTrue($this->loginManager->isLoggedIn());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user login - the session has expired
|
||||||
|
*/
|
||||||
|
public function testCheckLoginStateSessionExpired()
|
||||||
|
{
|
||||||
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
||||||
|
$this->session['expires_on'] = time() - 100;
|
||||||
|
|
||||||
|
$this->loginManager->checkLoginState($this->cookie, $this->clientIpAddress);
|
||||||
|
|
||||||
|
$this->assertFalse($this->loginManager->isLoggedIn());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user login - the remote client IP has changed
|
||||||
|
*/
|
||||||
|
public function testCheckLoginStateClientIpChanged()
|
||||||
|
{
|
||||||
|
$this->loginManager->generateStaySignedInToken($this->clientIpAddress);
|
||||||
|
|
||||||
|
$this->loginManager->checkLoginState($this->cookie, '10.7.157.98');
|
||||||
|
|
||||||
|
$this->assertFalse($this->loginManager->isLoggedIn());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user credentials - wrong login supplied
|
||||||
|
*/
|
||||||
|
public function testCheckCredentialsWrongLogin()
|
||||||
|
{
|
||||||
|
$this->assertFalse(
|
||||||
|
$this->loginManager->checkCredentials('', '', 'b4dl0g1n', $this->password)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user credentials - wrong password supplied
|
||||||
|
*/
|
||||||
|
public function testCheckCredentialsWrongPassword()
|
||||||
|
{
|
||||||
|
$this->assertFalse(
|
||||||
|
$this->loginManager->checkCredentials('', '', $this->login, 'b4dp455wd')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user credentials - wrong login and password supplied
|
||||||
|
*/
|
||||||
|
public function testCheckCredentialsWrongLoginAndPassword()
|
||||||
|
{
|
||||||
|
$this->assertFalse(
|
||||||
|
$this->loginManager->checkCredentials('', '', 'b4dl0g1n', 'b4dp455wd')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check user credentials - correct login and password supplied
|
||||||
|
*/
|
||||||
|
public function testCheckCredentialsGoodLoginAndPassword()
|
||||||
|
{
|
||||||
|
$this->assertTrue(
|
||||||
|
$this->loginManager->checkCredentials('', '', $this->login, $this->password)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ class SessionManagerTest extends TestCase
|
||||||
/** @var array Session ID hashes */
|
/** @var array Session ID hashes */
|
||||||
protected static $sidHashes = null;
|
protected static $sidHashes = null;
|
||||||
|
|
||||||
/** @var FakeConfigManager ConfigManager substitute for testing */
|
/** @var \FakeConfigManager ConfigManager substitute for testing */
|
||||||
protected $conf = null;
|
protected $conf = null;
|
||||||
|
|
||||||
/** @var array $_SESSION array for testing */
|
/** @var array $_SESSION array for testing */
|
||||||
|
|
|
@ -42,4 +42,16 @@ public function get($key)
|
||||||
}
|
}
|
||||||
return $key;
|
return $key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a setting exists
|
||||||
|
*
|
||||||
|
* @param string $setting Asked setting, keys separated with dots
|
||||||
|
*
|
||||||
|
* @return bool true if the setting exists, false otherwise
|
||||||
|
*/
|
||||||
|
public function exists($setting)
|
||||||
|
{
|
||||||
|
return array_key_exists($setting, $this->values);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue