14 changed files with 353 additions and 357 deletions
@ -14,7 +14,7 @@ class UserModelTest extends CIDatabaseTestCase
|
||||
|
||||
public function setUp() |
||||
{ |
||||
parent::setUp(); |
||||
parent::setUp(); |
||||
|
||||
$this->model = new UserModel($this->db); |
||||
} |
||||
@ -31,21 +31,21 @@ class UserModelTest extends CIDatabaseTestCase
|
||||
{ |
||||
$this->model->updateLastLogin(1); |
||||
$user = $this->model->asArray()->find(1); |
||||
$this->assertTrue((strtotime("-5 seconds") < strtotime($user['last_login']) && strtotime("+5 seconds") > strtotime($user['last_login'])) && strtotime("-5 seconds") < strtotime($user['last_activity']) && strtotime("+5 seconds") > strtotime($user['last_activity'])); |
||||
$this->assertTrue((strtotime('-5 seconds') < strtotime($user['last_login']) && strtotime('+5 seconds') > strtotime($user['last_login'])) && strtotime('-5 seconds') < strtotime($user['last_activity']) && strtotime('+5 seconds') > strtotime($user['last_activity'])); |
||||
} |
||||
|
||||
public function testUpdateLastActivity() |
||||
{ |
||||
$this->model->updateLastActivity(1); |
||||
$user = $this->model->asArray()->find(1); |
||||
$this->assertTrue(strtotime("-5 seconds") < strtotime($user['last_activity']) && strtotime("+5 seconds") > strtotime($user['last_activity'])); |
||||
$this->assertTrue(strtotime('-5 seconds') < strtotime($user['last_activity']) && strtotime('+5 seconds') > strtotime($user['last_activity'])); |
||||
} |
||||
|
||||
public function testUpdateBanned() |
||||
{ |
||||
$this->assertFalse($this->model->isBanned(1)); |
||||
|
||||
$this->model->updateBanned(1, TRUE); |
||||
$this->model->updateBanned(1, true); |
||||
$this->assertTrue($this->model->isBanned(1)); |
||||
} |
||||
|
||||
@ -58,16 +58,16 @@ class UserModelTest extends CIDatabaseTestCase
|
||||
|
||||
public function testExistsByEmail() |
||||
{ |
||||
$this->assertTrue($this->model->existsByEmail("[email protected]")); |
||||
$this->assertTrue($this->model->existsByEmail('[email protected]')); |
||||
|
||||
$this->assertFalse($this->model->existsByEmail("")); |
||||
$this->assertFalse($this->model->existsByEmail('')); |
||||
} |
||||
|
||||
public function testExistsByUsername() |
||||
{ |
||||
$this->assertTrue($this->model->existsByUsername("admin")); |
||||
$this->assertTrue($this->model->existsByUsername('admin')); |
||||
|
||||
$this->assertFalse($this->model->existsByUsername("")); |
||||
$this->assertFalse($this->model->existsByUsername('')); |
||||
} |
||||
|
||||
public function testHashPasswordFilled() |
||||
@ -75,7 +75,7 @@ class UserModelTest extends CIDatabaseTestCase
|
||||
$userOld = $this->model->asArray()->find(1); |
||||
$this->model->update(1, ['id' => 1, 'password' => 'password123456']); |
||||
$userNew = $this->model->asArray()->find(1); |
||||
$this->assertTrue($userOld['password'] != $userNew['password'] && $userNew['password'] != 'password123456'); |
||||
$this->assertTrue($userOld['password'] !== $userNew['password'] && $userNew['password'] !== 'password123456'); |
||||
|
||||
$userOld = $this->model->asArray()->find(1); |
||||
$this->model->update(1, ['id' => 1, 'username' => 'admin']); |
||||
@ -85,11 +85,11 @@ class UserModelTest extends CIDatabaseTestCase
|
||||
|
||||
public function testLoginUseUsernameDummy() |
||||
{ |
||||
$config = new AauthConfig(); |
||||
$config = new AauthConfig(); |
||||
$config->loginUseUsername = true; |
||||
|
||||
$this->model = new UserModel($this->db, null, $config); |
||||
$newUser = $this->model->insert(['email' => '[email protected]', 'password' => 'password123456']); |
||||
$newUser = $this->model->insert(['email' => '[email protected]', 'password' => 'password123456']); |
||||
$this->assertFalse($newUser); |
||||
} |
||||
} |
||||
|
@ -15,7 +15,7 @@ use App\Models\Aauth\LoginTokenModel;
|
||||
|
||||
/** |
||||
* @runTestsInSeparateProcesses |
||||
* @preserveGlobalState disabled |
||||
* @preserveGlobalState disabled |
||||
*/ |
||||
class LoginTest extends CIDatabaseTestCase |
||||
{ |
||||
@ -31,30 +31,29 @@ class LoginTest extends CIDatabaseTestCase
|
||||
|
||||
Services::injectMock('response', new MockResponse(new App())); |
||||
$this->response = service('response'); |
||||
$this->library = new Aauth(null, true); |
||||
$_COOKIE = []; |
||||
$_SESSION = []; |
||||
$this->library = new Aauth(null, true); |
||||
$_COOKIE = []; |
||||
$_SESSION = []; |
||||
} |
||||
|
||||
public function tearDown() |
||||
{ |
||||
|
||||
} |
||||
|
||||
protected function getInstance($options=[]) |
||||
protected function getInstance($options = []) |
||||
{ |
||||
$defaults = [ |
||||
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', |
||||
'sessionCookieName' => 'ci_session', |
||||
'sessionExpiration' => 7200, |
||||
'sessionSavePath' => 'null', |
||||
'sessionMatchIP' => false, |
||||
'sessionTimeToUpdate' => 300, |
||||
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', |
||||
'sessionCookieName' => 'ci_session', |
||||
'sessionExpiration' => 7200, |
||||
'sessionSavePath' => 'null', |
||||
'sessionMatchIP' => false, |
||||
'sessionTimeToUpdate' => 300, |
||||
'sessionRegenerateDestroy' => false, |
||||
'cookieDomain' => '', |
||||
'cookiePrefix' => '', |
||||
'cookiePath' => '/', |
||||
'cookieSecure' => false, |
||||
'cookieDomain' => '', |
||||
'cookiePrefix' => '', |
||||
'cookiePath' => '/', |
||||
'cookieSecure' => false, |
||||
]; |
||||
|
||||
$config = (object)$defaults; |
||||
@ -70,8 +69,8 @@ class LoginTest extends CIDatabaseTestCase
|
||||
|
||||
public function testLogin() |
||||
{ |
||||
$session = $this->getInstance(); |
||||
$config = new AauthConfig(); |
||||
$session = $this->getInstance(); |
||||
$config = new AauthConfig(); |
||||
$config->loginUseUsername = true; |
||||
|
||||
$this->library = new Aauth($config, $session); |
||||
@ -104,7 +103,7 @@ class LoginTest extends CIDatabaseTestCase
|
||||
$this->assertEquals(lang('Aauth.loginFailedAll'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->banUser(1); |
||||
$this->library->clearErrors(); |
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->login('[email protected]', 'password123456')); |
||||
$this->assertEquals(lang('Aauth.invalidUserBanned'), $this->library->getErrorsArray()[0]); |
||||
|
||||
@ -124,7 +123,7 @@ class LoginTest extends CIDatabaseTestCase
|
||||
|
||||
public function testIsLoggedIn() |
||||
{ |
||||
$session = $this->getInstance(); |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(null, $session); |
||||
$session->set('user', [ |
||||
'loggedIn' => true, |
||||
@ -134,7 +133,7 @@ class LoginTest extends CIDatabaseTestCase
|
||||
|
||||
public function testLogout() |
||||
{ |
||||
$session = $this->getInstance(); |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(null, $session); |
||||
$session->set('user', [ |
||||
'loggedIn' => true, |
||||
|
@ -21,24 +21,24 @@ class UserTest extends CIDatabaseTestCase
|
||||
|
||||
protected $namespace = 'App'; |
||||
|
||||
public function setUp() |
||||
public function setUp() |
||||
{ |
||||
parent::setUp(); |
||||
parent::setUp(); |
||||
|
||||
$this->library = new Aauth(null, true); |
||||
$this->library = new Aauth(null, true); |
||||
$this->config = new AauthConfig(); |
||||
$_COOKIE = []; |
||||
$_SESSION = []; |
||||
} |
||||
$_COOKIE = []; |
||||
$_SESSION = []; |
||||
} |
||||
|
||||
public function tearDown() |
||||
public function tearDown() |
||||
{ |
||||
|
||||
} |
||||
} |
||||
|
||||
protected function getInstance($options=[]) |
||||
protected function getInstance($options=[]) |
||||
{ |
||||
$defaults = [ |
||||
$defaults = [ |
||||
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', |
||||
'sessionCookieName' => 'ci_session', |
||||
'sessionExpiration' => 7200, |
||||
@ -50,68 +50,68 @@ class UserTest extends CIDatabaseTestCase
|
||||
'cookiePrefix' => '', |
||||
'cookiePath' => '/', |
||||
'cookieSecure' => false, |
||||
]; |
||||
]; |
||||
|
||||
$config = (object)$defaults; |
||||
$config = (object)$defaults; |
||||
|
||||
$session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config); |
||||
$session->setLogger(new TestLogger(new Logger())); |
||||
$session->start(); |
||||
$session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config); |
||||
$session->setLogger(new TestLogger(new Logger())); |
||||
$session->start(); |
||||
|
||||
return $session; |
||||
} |
||||
return $session; |
||||
} |
||||
|
||||
//-------------------------------------------------------------------- |
||||
|
||||
public function testUpdateUser() |
||||
{ |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
|
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 2, |
||||
'email' => '[email protected]', |
||||
'username' => 'user', |
||||
'id' => 2, |
||||
'email' => '[email protected]', |
||||
'username' => 'user', |
||||
]); |
||||
$this->library->updateUser(2, '[email protected]', 'password987654', 'user1'); |
||||
$this->library->updateUser(2, '[email protected]', 'password987654', 'user1'); |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 2, |
||||
'email' => '[email protected]', |
||||
'username' => 'user1', |
||||
'id' => 2, |
||||
'email' => '[email protected]', |
||||
'username' => 'user1', |
||||
]); |
||||
$this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]); |
||||
$this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]); |
||||
|
||||
// $this->library->clearInfos(); |
||||
// $this->assertFalse($this->library->updateUser(2, '[email protected]', null, null)); |
||||
// $this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2, '[email protected]', null, null)); |
||||
$this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null)); |
||||
$this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null)); |
||||
$this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(2, null, 'pass', null)); |
||||
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2, null, 'pass', null)); |
||||
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null)); |
||||
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null)); |
||||
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(2, null, null, 'admin')); |
||||
$this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2, null, null, 'admin')); |
||||
$this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(2, null, null, 'user+')); |
||||
$this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2, null, null, 'user+')); |
||||
$this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(2)); |
||||
$this->assertCount(0, $this->library->getErrorsArray()); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(2)); |
||||
$this->assertCount(0, $this->library->getErrorsArray()); |
||||
|
||||
$this->library->clearErrors(); |
||||
$this->assertFalse($this->library->updateUser(99)); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$this->assertFalse($this->library->updateUser(99)); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
} |
||||
|
||||
public function testDeleteUser() |
||||
@ -121,54 +121,54 @@ class UserTest extends CIDatabaseTestCase
|
||||
$this->seeNumRecords(1, $this->config->dbTableUsers, ['deleted' => 0]); |
||||
|
||||
$this->assertFalse($this->library->deleteUser(99)); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
} |
||||
|
||||
public function testListUsers() |
||||
{ |
||||
$users = $this->library->listUsers(); |
||||
$users = $this->library->listUsers(); |
||||
$this->assertCount(2, $users); |
||||
$this->assertEquals('admin', $users[0]['username']); |
||||
$this->assertEquals('user', $users[1]['username']); |
||||
|
||||
$usersOrderBy = $this->library->listUsers(0, 0, null, 'id DESC'); |
||||
$usersOrderBy = $this->library->listUsers(0, 0, null, 'id DESC'); |
||||
$this->assertEquals('user', $usersOrderBy[0]['username']); |
||||
$this->assertEquals('admin', $usersOrderBy[1]['username']); |
||||
} |
||||
|
||||
public function testGetUser() |
||||
{ |
||||
$user = $this->library->getUser(1); |
||||
$user = $this->library->getUser(1); |
||||
$this->assertEquals('1', $user['id']); |
||||
$this->assertEquals('admin', $user['username']); |
||||
$this->assertEquals('[email protected]', $user['email']); |
||||
|
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session->set('user', [ |
||||
'id' => 1, |
||||
]); |
||||
$userIdNone = $this->library->getUser(); |
||||
$userIdNone = $this->library->getUser(); |
||||
$this->assertEquals('admin', $userIdNone['username']); |
||||
|
||||
$userVar = $this->library->getUser(1, true); |
||||
$userVar = $this->library->getUser(1, true); |
||||
$this->assertInternalType('array', $userVar['variables']); |
||||
|
||||
$this->assertFalse($this->library->getUser(99)); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
} |
||||
|
||||
public function testGetUserId() |
||||
{ |
||||
$userIdEmail = $this->library->getUserId('[email protected]'); |
||||
$userIdEmail = $this->library->getUserId('[email protected]'); |
||||
$this->assertEquals('1', $userIdEmail); |
||||
|
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session->set('user', [ |
||||
'id' => 1, |
||||
]); |
||||
$userIdNone = $this->library->getUserId(); |
||||
$userIdNone = $this->library->getUserId(); |
||||
$this->assertEquals('1', $userIdNone); |
||||
|
||||
$this->assertFalse($this->library->getUserId('[email protected]')); |
||||
@ -177,52 +177,52 @@ class UserTest extends CIDatabaseTestCase
|
||||
public function testBanUser() |
||||
{ |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 1, |
||||
'banned' => 0, |
||||
'id' => 1, |
||||
'banned' => 0, |
||||
]); |
||||
$this->library->banUser(1); |
||||
$this->library->banUser(1); |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 1, |
||||
'banned' => 1, |
||||
'id' => 1, |
||||
'banned' => 1, |
||||
]); |
||||
|
||||
$this->assertFalse($this->library->banUser(99)); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
} |
||||
|
||||
public function testUnbanUser() |
||||
{ |
||||
$this->library->banUser(1); |
||||
$this->library->banUser(1); |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 1, |
||||
'banned' => 1, |
||||
'id' => 1, |
||||
'banned' => 1, |
||||
]); |
||||
$this->library->unbanUser(1); |
||||
$this->library->unbanUser(1); |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 1, |
||||
'banned' => 0, |
||||
'id' => 1, |
||||
'banned' => 0, |
||||
]); |
||||
|
||||
$this->assertFalse($this->library->unbanUser(99)); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); |
||||
} |
||||
|
||||
public function testBanUnbanUserSession() |
||||
{ |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session = $this->getInstance(); |
||||
$this->library = new Aauth(NULL, $session); |
||||
$session->set('user', [ |
||||
'id' => 1, |
||||
]); |
||||
$this->library->banUser(); |
||||
$this->library->banUser(); |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 1, |
||||
'banned' => 1, |
||||
'id' => 1, |
||||
'banned' => 1, |
||||
]); |
||||
$this->library->unbanUser(); |
||||
$this->library->unbanUser(); |
||||
$this->seeInDatabase($this->config->dbTableUsers, [ |
||||
'id' => 1, |
||||
'banned' => 0, |
||||
'id' => 1, |
||||
'banned' => 0, |
||||
]); |
||||
} |
||||
|
||||
@ -230,7 +230,7 @@ class UserTest extends CIDatabaseTestCase
|
||||
{ |
||||
$this->assertFalse($this->library->isBanned(1)); |
||||
|
||||
$this->library->banUser(1); |
||||
$this->library->banUser(1); |
||||
$this->assertTrue($this->library->isBanned(1)); |
||||
|
||||
$this->assertTrue($this->library->isBanned(99)); |
||||
|
Loading…
Reference in new issue