Add unit test for LastLogin class
This commit is contained in:
@@ -22,6 +22,7 @@ Improvements:
|
|||||||
* Make sure that some event subscribers are not executed multiple times
|
* Make sure that some event subscribers are not executed multiple times
|
||||||
* Show rendering time of individual templates when debug mode is enabled
|
* Show rendering time of individual templates when debug mode is enabled
|
||||||
* Make sure that no events are fired if nothing have been modified in the task
|
* Make sure that no events are fired if nothing have been modified in the task
|
||||||
|
* Add unit tests for LastLogin
|
||||||
|
|
||||||
Bug fixes:
|
Bug fixes:
|
||||||
|
|
||||||
|
|||||||
@@ -36,11 +36,31 @@ class LastLogin extends Base
|
|||||||
*/
|
*/
|
||||||
public function create($auth_type, $user_id, $ip, $user_agent)
|
public function create($auth_type, $user_id, $ip, $user_agent)
|
||||||
{
|
{
|
||||||
// Cleanup old sessions if necessary
|
$this->cleanup($user_id);
|
||||||
|
|
||||||
|
return $this->db
|
||||||
|
->table(self::TABLE)
|
||||||
|
->insert(array(
|
||||||
|
'auth_type' => $auth_type,
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'ip' => $ip,
|
||||||
|
'user_agent' => substr($user_agent, 0, 255),
|
||||||
|
'date_creation' => time(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanup login history
|
||||||
|
*
|
||||||
|
* @access public
|
||||||
|
* @param integer $user_id
|
||||||
|
*/
|
||||||
|
public function cleanup($user_id)
|
||||||
|
{
|
||||||
$connections = $this->db
|
$connections = $this->db
|
||||||
->table(self::TABLE)
|
->table(self::TABLE)
|
||||||
->eq('user_id', $user_id)
|
->eq('user_id', $user_id)
|
||||||
->desc('date_creation')
|
->desc('id')
|
||||||
->findAllByColumn('id');
|
->findAllByColumn('id');
|
||||||
|
|
||||||
if (count($connections) >= self::NB_LOGINS) {
|
if (count($connections) >= self::NB_LOGINS) {
|
||||||
@@ -49,16 +69,6 @@ class LastLogin extends Base
|
|||||||
->notin('id', array_slice($connections, 0, self::NB_LOGINS - 1))
|
->notin('id', array_slice($connections, 0, self::NB_LOGINS - 1))
|
||||||
->remove();
|
->remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->db
|
|
||||||
->table(self::TABLE)
|
|
||||||
->insert(array(
|
|
||||||
'auth_type' => $auth_type,
|
|
||||||
'user_id' => $user_id,
|
|
||||||
'ip' => $ip,
|
|
||||||
'user_agent' => $user_agent,
|
|
||||||
'date_creation' => time(),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -73,7 +83,7 @@ class LastLogin extends Base
|
|||||||
return $this->db
|
return $this->db
|
||||||
->table(self::TABLE)
|
->table(self::TABLE)
|
||||||
->eq('user_id', $user_id)
|
->eq('user_id', $user_id)
|
||||||
->desc('date_creation')
|
->desc('id')
|
||||||
->columns('id', 'auth_type', 'ip', 'user_agent', 'date_creation')
|
->columns('id', 'auth_type', 'ip', 'user_agent', 'date_creation')
|
||||||
->findAll();
|
->findAll();
|
||||||
}
|
}
|
||||||
|
|||||||
43
tests/units/Model/LastLoginTest.php
Normal file
43
tests/units/Model/LastLoginTest.php
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once __DIR__.'/../Base.php';
|
||||||
|
|
||||||
|
use Kanboard\Model\LastLogin;
|
||||||
|
|
||||||
|
class LastLoginTest extends Base
|
||||||
|
{
|
||||||
|
public function testCreate()
|
||||||
|
{
|
||||||
|
$lastLoginModel = new LastLogin($this->container);
|
||||||
|
|
||||||
|
$this->assertTrue($lastLoginModel->create('Test1', 1, '127.0.0.1', 'My browser'));
|
||||||
|
$this->assertTrue($lastLoginModel->create('Test2', 1, '127.0.0.1', str_repeat('Too long', 50)));
|
||||||
|
$this->assertTrue($lastLoginModel->create('Test3', 1, '2001:0db8:0000:0000:0000:ff00:0042:8329', 'My Ipv6 browser'));
|
||||||
|
|
||||||
|
$connections = $lastLoginModel->getAll(1);
|
||||||
|
$this->assertCount(3, $connections);
|
||||||
|
|
||||||
|
$this->assertEquals('Test3', $connections[0]['auth_type']);
|
||||||
|
$this->assertEquals('2001:0db8:0000:0000:0000:ff00:0042:8329', $connections[0]['ip']);
|
||||||
|
|
||||||
|
$this->assertEquals('Test2', $connections[1]['auth_type']);
|
||||||
|
$this->assertEquals('127.0.0.1', $connections[1]['ip']);
|
||||||
|
|
||||||
|
$this->assertEquals('Test1', $connections[2]['auth_type']);
|
||||||
|
$this->assertEquals('127.0.0.1', $connections[2]['ip']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCleanup()
|
||||||
|
{
|
||||||
|
$lastLoginModel = new LastLogin($this->container);
|
||||||
|
|
||||||
|
for ($i = 0; $i < $lastLoginModel::NB_LOGINS + 5; $i++) {
|
||||||
|
$this->assertTrue($lastLoginModel->create('Test' . $i, 1, '127.0.0.1', 'My browser'));
|
||||||
|
}
|
||||||
|
|
||||||
|
$connections = $lastLoginModel->getAll(1);
|
||||||
|
$this->assertCount(10, $connections);
|
||||||
|
$this->assertEquals('Test14', $connections[0]['auth_type']);
|
||||||
|
$this->assertEquals('Test5', $connections[9]['auth_type']);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user