Browse Source

updated Libraries/Aauth & tests

v3-dev
REJack 6 years ago
parent
commit
e9c4254da3
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 4
      application/Libraries/Aauth.php
  2. 5
      tests/Aauth/Database/GroupToGroupModelTest.php
  3. 5
      tests/Aauth/Database/GroupToUserModelTest.php
  4. 14
      tests/Aauth/Database/LoginAttemptModelTest.php
  5. 5
      tests/Aauth/Database/PermToGroupModelTest.php
  6. 5
      tests/Aauth/Database/PermToUserModelTest.php
  7. 28
      tests/Aauth/Database/UserModelTest.php
  8. 11
      tests/Aauth/Database/UserVariableModelTest.php
  9. 32
      tests/Aauth/Libraries/Aauth/ErrorsTest.php
  10. 33
      tests/Aauth/Libraries/Aauth/InfosTest.php
  11. 228
      tests/Aauth/Libraries/Aauth/LoginTest.php
  12. 82
      tests/Aauth/Libraries/Aauth/UserTest.php

4
application/Libraries/Aauth.php

@ -418,9 +418,7 @@ class Aauth
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
{ {
$this->error(lang('Aauth.notFoundUser')); return true;
return false;
} }
return $userModel->isBanned($userId); return $userModel->isBanned($userId);

5
tests/Aauth/Database/GroupToGroupModelTest.php

@ -31,14 +31,11 @@ class GroupToGroupModelTest extends CIDatabaseTestCase
]); ]);
} }
public function testExistsFalse() public function testExists()
{ {
$groupToGroup = $this->model->exists(99, 99); $groupToGroup = $this->model->exists(99, 99);
$this->assertFalse($groupToGroup); $this->assertFalse($groupToGroup);
}
public function testExistsTrue()
{
$this->hasInDatabase($this->config->dbTableGroupToGroup, [ $this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99, 'group_id' => 99,
'subgroup_id' => 99, 'subgroup_id' => 99,

5
tests/Aauth/Database/GroupToUserModelTest.php

@ -31,14 +31,11 @@ class GroupToUserModelTest extends CIDatabaseTestCase
]); ]);
} }
public function testExistsFalse() public function testExists()
{ {
$groupToUser = $this->model->exists(99, 99); $groupToUser = $this->model->exists(99, 99);
$this->assertFalse($groupToUser); $this->assertFalse($groupToUser);
}
public function testExistsTrue()
{
$this->hasInDatabase($this->config->dbTableGroupToUser, [ $this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99, 'group_id' => 99,
'user_id' => 99, 'user_id' => 99,

14
tests/Aauth/Database/LoginAttemptModelTest.php

@ -26,26 +26,16 @@ class LoginAttemptModelTest extends CIDatabaseTestCase
$this->assertEquals(0, $loginAttempt); $this->assertEquals(0, $loginAttempt);
} }
public function testSaveInsert() public function testSave()
{ {
$this->model->save(); $this->assertTrue($this->model->save());
$loginAttempt = $this->model->find(); $loginAttempt = $this->model->find();
$this->assertEquals(1, $loginAttempt); $this->assertEquals(1, $loginAttempt);
}
public function testSaveUpdate()
{
$this->model->save();
$this->assertTrue($this->model->save()); $this->assertTrue($this->model->save());
$loginAttempt = $this->model->find(); $loginAttempt = $this->model->find();
$this->assertEquals(2, $loginAttempt); $this->assertEquals(2, $loginAttempt);
}
public function testSaveUpdateFalse()
{
$this->model->save();
$this->model->save();
$this->model->save();
$this->model->save(); $this->model->save();
$this->model->save(); $this->model->save();
$this->model->save(); $this->model->save();

5
tests/Aauth/Database/PermToGroupModelTest.php

@ -30,14 +30,11 @@ class PermToGroupModelTest extends CIDatabaseTestCase
]); ]);
} }
public function testExistsFalse() public function testExists()
{ {
$permToGroup = $this->model->exists(99, 99); $permToGroup = $this->model->exists(99, 99);
$this->assertFalse($permToGroup); $this->assertFalse($permToGroup);
}
public function testExistsTrue()
{
$this->hasInDatabase($this->config->dbTablePermToGroup, [ $this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99, 'perm_id' => 99,
'group_id' => 99, 'group_id' => 99,

5
tests/Aauth/Database/PermToUserModelTest.php

@ -30,14 +30,11 @@ class PermToUserModelTest extends CIDatabaseTestCase
]); ]);
} }
public function testExistsFalse() public function testExists()
{ {
$permToUser = $this->model->exists(99, 99); $permToUser = $this->model->exists(99, 99);
$this->assertFalse($permToUser); $this->assertFalse($permToUser);
}
public function testExistsTrue()
{
$this->hasInDatabase($this->config->dbTablePermToUser, [ $this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99, 'perm_id' => 99,
'user_id' => 99, 'user_id' => 99,

28
tests/Aauth/Database/UserModelTest.php

@ -40,45 +40,32 @@ class UserModelTest extends CIDatabaseTestCase
$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 testUpdateBannedTrue() 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)); $this->assertTrue($this->model->isBanned(1));
} }
public function testUpdateBannedFalse() public function testExistsById()
{
$this->model->updateBanned(1, FALSE);
$this->assertFalse($this->model->isBanned(1));
}
public function testExistsByIdTrue()
{ {
$this->assertTrue($this->model->existsById(1)); $this->assertTrue($this->model->existsById(1));
}
public function testExistsByIdFalse()
{
$this->assertFalse($this->model->existsById(0)); $this->assertFalse($this->model->existsById(0));
} }
public function testExistsByEmailTrue() public function testExistsByEmail()
{ {
$this->assertTrue($this->model->existsByEmail("admin@example.com")); $this->assertTrue($this->model->existsByEmail("admin@example.com"));
}
public function testExistsByEmailFalse()
{
$this->assertFalse($this->model->existsByEmail("")); $this->assertFalse($this->model->existsByEmail(""));
} }
public function testExistsByUsernameTrue() public function testExistsByUsername()
{ {
$this->assertTrue($this->model->existsByUsername("admin")); $this->assertTrue($this->model->existsByUsername("admin"));
}
public function testExistsByUsernameFalse()
{
$this->assertFalse($this->model->existsByUsername("")); $this->assertFalse($this->model->existsByUsername(""));
} }
@ -88,10 +75,7 @@ class UserModelTest extends CIDatabaseTestCase
$this->model->update(1, ['id' => 1, 'password' => 'password123456']); $this->model->update(1, ['id' => 1, 'password' => 'password123456']);
$userNew = $this->model->asArray()->find(1); $userNew = $this->model->asArray()->find(1);
$this->assertTrue($userOld['password'] != $userNew['password'] && $userNew['password'] != 'password123456'); $this->assertTrue($userOld['password'] != $userNew['password'] && $userNew['password'] != 'password123456');
}
public function testHashPasswordNotSet()
{
$userOld = $this->model->asArray()->find(1); $userOld = $this->model->asArray()->find(1);
$this->model->update(1, ['id' => 1, 'username' => 'admin']); $this->model->update(1, ['id' => 1, 'username' => 'admin']);
$userNew = $this->model->asArray()->find(1); $userNew = $this->model->asArray()->find(1);

11
tests/Aauth/Database/UserVariableModelTest.php

@ -22,14 +22,11 @@ class UserVariableModelTest extends CIDatabaseTestCase
//-------------------------------------------------------------------- //--------------------------------------------------------------------
public function testFindFalse() public function testFind()
{ {
$userVariable = $this->model->find(99, 'test'); $userVariable = $this->model->find(99, 'test');
$this->assertFalse($userVariable); $this->assertFalse($userVariable);
}
public function testFindReturn()
{
$this->hasInDatabase($this->config->dbTableUserVariables, [ $this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99, 'user_id' => 99,
'data_key' => 'test', 'data_key' => 'test',
@ -50,7 +47,7 @@ class UserVariableModelTest extends CIDatabaseTestCase
$this->assertCount(1, $userVariables); $this->assertCount(1, $userVariables);
} }
public function testSaveInsert() public function testSave()
{ {
$this->model->save(99, 'test', 'TRUE'); $this->model->save(99, 'test', 'TRUE');
$this->seeInDatabase($this->config->dbTableUserVariables, [ $this->seeInDatabase($this->config->dbTableUserVariables, [
@ -58,11 +55,7 @@ class UserVariableModelTest extends CIDatabaseTestCase
'data_key' => 'test', 'data_key' => 'test',
'data_value' => 'TRUE', 'data_value' => 'TRUE',
]); ]);
}
public function testSaveUpdate()
{
$this->model->save(99, 'test', 'TRUE');
$this->model->save(99, 'test', 'TRUE2'); $this->model->save(99, 'test', 'TRUE2');
$this->seeInDatabase($this->config->dbTableUserVariables, [ $this->seeInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99, 'user_id' => 99,

32
tests/Aauth/Libraries/Aauth/ErrorsTest.php

@ -68,19 +68,16 @@ class ErrorsTest extends \CIUnitTestCase
$this->assertEquals(['test message 1','test message 2'], $this->library->getErrorsArray()); $this->assertEquals(['test message 1','test message 2'], $this->library->getErrorsArray());
} }
public function testPrintErrorsReturn() public function testPrintErrors()
{ {
$this->library->error('test message 1'); $this->library->error('test message 1');
$this->assertEquals('test message 1', $this->library->printErrors('<br />', true)); $this->assertEquals('test message 1', $this->library->printErrors('<br />', true));
$this->library->error('test message 2'); $this->library->error('test message 2');
$this->assertEquals('test message 1<br />test message 2', $this->library->printErrors('<br />', true)); $this->assertEquals('test message 1<br />test message 2', $this->library->printErrors('<br />', true));
}
public function testPrintErrorsEcho()
{
$this->library->error('test message 1');
$this->library->printErrors('<br />'); $this->library->printErrors('<br />');
$this->expectOutputString('test message 1'); $this->expectOutputString('test message 1<br />test message 2');
} }
public function testClearErrors() public function testClearErrors()
@ -102,21 +99,14 @@ class ErrorsTest extends \CIUnitTestCase
$this->library->error('test message 1', true); $this->library->error('test message 1', true);
$session->start(); $session->start();
$this->assertEquals(['test message 1'], $session->getFlashdata('errors')); $this->assertEquals(['test message 1'], $session->getFlashdata('errors'));
}
public function testErrorsFlashKeep()
{
$session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(NULL, $session);
$this->assertNull($session->getFlashdata('errors')); $this->library->error(['test message 1','test message 2'], true);
$this->library->error('test message 1', true);
$session->start();
$this->library->keepErrors();
$session->start(); $session->start();
$this->assertEquals(['test message 1'], $session->getFlashdata('errors')); $this->assertEquals(['test message 1','test message 2'], $session->getFlashdata('errors'));
} }
public function testErrorsFlashKeepMerge() public function testErrorsFlashKeep()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(NULL, $session);
@ -129,14 +119,4 @@ class ErrorsTest extends \CIUnitTestCase
$session->start(); $session->start();
$this->assertEquals(['test message 1 Flash', 'test message 1 NonFlash'], $session->getFlashdata('errors')); $this->assertEquals(['test message 1 Flash', 'test message 1 NonFlash'], $session->getFlashdata('errors'));
} }
public function testErrorsFlashArray()
{
$session = $this->getInstance();
$this->library = new Aauth(NULL, $session);
$this->assertNull($session->getFlashdata('errors'));
$this->library->error(['test message 1','test message 2'], true);
$session->start();
$this->assertEquals(['test message 1','test message 2'], $session->getFlashdata('errors'));
}
} }

33
tests/Aauth/Libraries/Aauth/InfosTest.php

@ -68,19 +68,15 @@ class InfosTest extends \CIUnitTestCase
$this->assertEquals(['test message 1','test message 2'], $this->library->getInfosArray()); $this->assertEquals(['test message 1','test message 2'], $this->library->getInfosArray());
} }
public function testPrintInfosReturn() public function testPrintInfos()
{ {
$this->library->info('test message 1'); $this->library->info('test message 1');
$this->assertEquals('test message 1', $this->library->printInfos('<br />', true)); $this->assertEquals('test message 1', $this->library->printInfos('<br />', true));
$this->library->info('test message 2'); $this->library->info('test message 2');
$this->assertEquals('test message 1<br />test message 2', $this->library->printInfos('<br />', true)); $this->assertEquals('test message 1<br />test message 2', $this->library->printInfos('<br />', true));
}
public function testPrintInfosEcho()
{
$this->library->info('test message 1');
$this->library->printInfos('<br />'); $this->library->printInfos('<br />');
$this->expectOutputString('test message 1'); $this->expectOutputString('test message 1<br />test message 2');
} }
public function testClearInfos() public function testClearInfos()
@ -102,21 +98,14 @@ class InfosTest extends \CIUnitTestCase
$this->library->info('test message 1', true); $this->library->info('test message 1', true);
$session->start(); $session->start();
$this->assertEquals(['test message 1'], $session->getFlashdata('infos')); $this->assertEquals(['test message 1'], $session->getFlashdata('infos'));
}
public function testInfosFlashKeep() $this->library->clearInfos();
{ $this->library->info(['test message 1','test message 2'], true);
$session = $this->getInstance();
$this->library = new Aauth(NULL, $session);
$this->assertNull($session->getFlashdata('infos'));
$this->library->info('test message 1', true);
$session->start();
$this->library->keepInfos();
$session->start(); $session->start();
$this->assertEquals(['test message 1'], $session->getFlashdata('infos')); $this->assertEquals(['test message 1','test message 2'], $session->getFlashdata('infos'));
} }
public function testInfosFlashKeepMerge() public function testInfosFlashKeep()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(NULL, $session);
@ -129,14 +118,4 @@ class InfosTest extends \CIUnitTestCase
$session->start(); $session->start();
$this->assertEquals(['test message 1 Flash', 'test message 1 NonFlash'], $session->getFlashdata('infos')); $this->assertEquals(['test message 1 Flash', 'test message 1 NonFlash'], $session->getFlashdata('infos'));
} }
public function testInfosFlashArray()
{
$session = $this->getInstance();
$this->library = new Aauth(NULL, $session);
$this->assertNull($session->getFlashdata('infos'));
$this->library->info(['test message 1','test message 2'], true);
$session->start();
$this->assertEquals(['test message 1','test message 2'], $session->getFlashdata('infos'));
}
} }

228
tests/Aauth/Libraries/Aauth/LoginTest.php

@ -25,25 +25,25 @@ class LoginTest extends CIDatabaseTestCase
protected $namespace = 'App'; protected $namespace = 'App';
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
Services::injectMock('response', new MockResponse(new App())); Services::injectMock('response', new MockResponse(new App()));
$this->response = service('response'); $this->response = service('response');
$this->library = new Aauth(null, true); $this->library = new Aauth(null, true);
$_COOKIE = []; $_COOKIE = [];
$_SESSION = []; $_SESSION = [];
} }
public function tearDown() public function tearDown()
{ {
} }
protected function getInstance($options=[]) protected function getInstance($options=[])
{ {
$defaults = [ $defaults = [
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', 'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler',
'sessionCookieName' => 'ci_session', 'sessionCookieName' => 'ci_session',
'sessionExpiration' => 7200, 'sessionExpiration' => 7200,
@ -55,145 +55,93 @@ class LoginTest extends CIDatabaseTestCase
'cookiePrefix' => '', 'cookiePrefix' => '',
'cookiePath' => '/', 'cookiePath' => '/',
'cookieSecure' => false, 'cookieSecure' => false,
]; ];
$config = (object)$defaults; $config = (object)$defaults;
$session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config); $session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config);
$session->setLogger(new TestLogger(new Logger())); $session->setLogger(new TestLogger(new Logger()));
$session->start(); $session->start();
return $session; return $session;
} }
//-------------------------------------------------------------------- //--------------------------------------------------------------------
public function testLoginEmailTrue() public function testLogin()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(null, $session); $config = new AauthConfig();
$this->assertTrue($this->library->login('admin@example.com', 'password123456')); $config->loginUseUsername = true;
}
$this->library = new Aauth($config, $session);
public function testLoginFalseEmailFailedEmail() $this->assertTrue($this->library->login('admin', 'password123456'));
{
$session = $this->getInstance(); $this->assertFalse($this->library->login('admin', 'passwor'));
$this->library = new Aauth(null, $session); $this->assertEquals(lang('Aauth.loginFailedUsername'), $this->library->getErrorsArray()[0]);
$this->assertFalse($this->library->login('adminaexample.com', 'password123456'));
$this->assertEquals(lang('Aauth.loginFailedEmail'), $this->library->getErrorsArray()[0]); $this->library->clearErrors();
} $this->assertFalse($this->library->login('user99', 'password123456'));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
public function testLoginFalseEmailFailedPassword() // $config->loginUseUsername = false;
{
$session = $this->getInstance(); $this->library = new Aauth(null, $session);
$this->library = new Aauth(null, $session); $this->assertTrue($this->library->login('admin@example.com', 'password123456'));
$this->assertFalse($this->library->login('admin@example.com', 'passwor'));
$this->assertEquals(lang('Aauth.loginFailedEmail'), $this->library->getErrorsArray()[0]); $this->assertFalse($this->library->login('adminaexample.com', 'password123456'));
} $this->assertEquals(lang('Aauth.loginFailedEmail'), $this->library->getErrorsArray()[0]);
public function testLoginFalseEmailNotFound() $this->library->clearErrors();
{ $this->assertFalse($this->library->login('admin@example.com', 'passwor'));
$session = $this->getInstance(); $this->assertEquals(lang('Aauth.loginFailedEmail'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, $session);
$this->assertFalse($this->library->login('admina@example.com', 'password123456')); $this->library->clearErrors();
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); $this->assertFalse($this->library->login('admina@example.com', 'password123456'));
} $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
public function testLoginUsernameTrue() $userVariableModel = new UserVariableModel();
{ $userVariableModel->save(1, 'verification_code', '12345678', true);
$session = $this->getInstance(); $this->assertFalse($this->library->login('admin@example.com', 'password123456'));
$config = new AauthConfig(); $this->assertEquals(lang('Aauth.notVerified'), $this->library->getErrorsArray()[0]);
$config->loginUseUsername = true;
$this->library = new Aauth($config, $session); $this->library->banUser(1);
$this->assertTrue($this->library->login('admin', 'password123456')); $this->library->clearErrors();
} $this->assertFalse($this->library->login('admin@example.com', 'password123456'));
$this->assertEquals(lang('Aauth.invalidUserBanned'), $this->library->getErrorsArray()[0]);
public function testLoginFalseUsernameFailedPassword()
{
$session = $this->getInstance(); $this->library->clearErrors();
$config = new AauthConfig(); $this->assertFalse($this->library->login('admin@example.com', 'password1234567'));
$config->loginUseUsername = true; $this->assertEquals(lang('Aauth.loginFailedAll'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth($config, $session);
$this->assertFalse($this->library->login('admin', 'passwor')); $this->library->login('admina@example.com', 'password123456');
$this->assertEquals(lang('Aauth.loginFailedUsername'), $this->library->getErrorsArray()[0]); $this->library->login('admina@example.com', 'password123456');
} $this->library->login('admina@example.com', 'password123456');
$this->library->clearErrors();
public function testLoginFalseUsernameNotFound() $this->assertFalse($this->library->login('admina@example.com', 'password123456'));
{ $this->assertEquals(lang('Aauth.loginAttemptsExceeded'), $this->library->getErrorsArray()[0]);
$session = $this->getInstance(); }
$config = new AauthConfig();
$config->loginUseUsername = true;
$this->library = new Aauth($config, $session);
$this->assertFalse($this->library->login('user99', 'password123456'));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
$config->loginUseUsername = false;
}
public function testLoginFalseNotVerified()
{
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$userVariableModel = new UserVariableModel();
$userVariableModel->save(1, 'verification_code', '12345678', true);
$this->assertFalse($this->library->login('admin@example.com', 'password123456'));
$this->assertEquals(lang('Aauth.notVerified'), $this->library->getErrorsArray()[0]);
}
public function testLoginFalseBanned()
{
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$this->library->banUser(1);
$this->assertFalse($this->library->login('admin@example.com', 'password123456'));
$this->assertEquals(lang('Aauth.invalidUserBanned'), $this->library->getErrorsArray()[0]);
}
public function testLoginFalse()
{
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$this->assertFalse($this->library->login('admin@example.com', 'password1234567'));
$this->assertEquals(lang('Aauth.loginFailedAll'), $this->library->getErrorsArray()[0]);
}
public function testLoginFalseLoginAttemptsExceeded()
{
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->login('admina@example.com', 'password123456');
$this->library->clearErrors();
$this->assertFalse($this->library->login('admina@example.com', 'password123456'));
$this->assertEquals(lang('Aauth.loginAttemptsExceeded'), $this->library->getErrorsArray()[0]);
}
public function testIsLoggedIn() public function testIsLoggedIn()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(null, $session); $this->library = new Aauth(null, $session);
$session->set('user', [ $session->set('user', [
'loggedIn' => true, 'loggedIn' => true,
]); ]);
$this->assertTrue($this->library->isLoggedIn()); $this->assertTrue($this->library->isLoggedIn());
} }
public function testLogout() public function testLogout()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(null, $session); $this->library = new Aauth(null, $session);
$session->set('user', [ $session->set('user', [
'loggedIn' => true, 'loggedIn' => true,
]); ]);
$this->assertTrue($this->library->isLoggedIn()); $this->assertTrue($this->library->isLoggedIn());
$this->library->logout(); $this->library->logout();
$this->library = new Aauth(null, $session); $this->library = new Aauth(null, $session);
$this->assertFalse($this->library->isLoggedIn()); $this->assertFalse($this->library->isLoggedIn());
} }
} }

82
tests/Aauth/Libraries/Aauth/UserTest.php

@ -63,66 +63,50 @@ class UserTest extends CIDatabaseTestCase
//-------------------------------------------------------------------- //--------------------------------------------------------------------
public function testUpdateUserTrue() public function testUpdateUser()
{ {
$this->seeInDatabase($this->config->dbTableUserVariables, [ $this->seeInDatabase($this->config->dbTableUserVariables, [
'user_id' => 2, 'id' => 2,
'email' => 'user@example.com', 'email' => 'user@example.com',
'username' => 'user', 'username' => 'user',
]); ]);
$this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1'); $this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1');
$this->seeInDatabase($this->config->dbTableUserVariables, [ $this->seeInDatabase($this->config->dbTableUserVariables, [
'user_id' => 2, 'id' => 2,
'email' => 'user1@example.com', 'email' => 'user1@example.com',
'username' => 'user1', 'username' => 'user1',
]); ]);
$this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]); $this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]);
}
public function testUpdateUserFalseEmailExists() $this->library->clearInfos();
{
$this->assertFalse($this->library->updateUser(2, 'admin@example.com', null, null)); $this->assertFalse($this->library->updateUser(2, 'admin@example.com', null, null));
$this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]);
}
public function testUpdateUserFalseEmailInvalid() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null)); $this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null));
$this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]);
}
public function testUpdateUserFalsePasswordMin() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(2, null, 'pass', null)); $this->assertFalse($this->library->updateUser(2, null, 'pass', null));
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
}
public function testUpdateUserFalsePasswordMax() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null)); $this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null));
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
}
public function testUpdateUserFalseUsernameExists() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(2, null, null, 'admin')); $this->assertFalse($this->library->updateUser(2, null, null, 'admin'));
$this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]);
}
public function testUpdateUserFalseUsernameInvalid() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(2, null, null, 'user+')); $this->assertFalse($this->library->updateUser(2, null, null, 'user+'));
$this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]);
}
public function testUpdateUserFalseEmpty() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(2)); $this->assertFalse($this->library->updateUser(2));
$this->assertCount(0, $this->library->getErrorsArray()); $this->assertCount(0, $this->library->getErrorsArray());
}
public function testUpdateUserFalseUserNotFound() $this->library->clearErrors();
{
$this->assertFalse($this->library->updateUser(99)); $this->assertFalse($this->library->updateUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
@ -132,10 +116,7 @@ class UserTest extends CIDatabaseTestCase
$this->seeNumRecords(2, $this->config->dbTableUsers); $this->seeNumRecords(2, $this->config->dbTableUsers);
$this->library->deleteUser(2); $this->library->deleteUser(2);
$this->seeNumRecords(1, $this->config->dbTableUsers); $this->seeNumRecords(1, $this->config->dbTableUsers);
}
public function testDeleteUserFalseUserNotFound()
{
$this->assertFalse($this->library->deleteUser(99)); $this->assertFalse($this->library->deleteUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
@ -146,25 +127,19 @@ class UserTest extends CIDatabaseTestCase
$this->assertCount(2, $users); $this->assertCount(2, $users);
$this->assertEquals('admin', $users[0]['username']); $this->assertEquals('admin', $users[0]['username']);
$this->assertEquals('user', $users[1]['username']); $this->assertEquals('user', $users[1]['username']);
}
public function testListUsersOrderBy()
{
$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('user', $usersOrderBy[0]['username']);
$this->assertEquals('admin', $usersOrderBy[1]['username']); $this->assertEquals('admin', $usersOrderBy[1]['username']);
} }
public function testGetUserByUserId() public function testGetUser()
{ {
$user = $this->library->getUser(1); $user = $this->library->getUser(1);
$this->assertEquals('1', $user['id']); $this->assertEquals('1', $user['id']);
$this->assertEquals('admin', $user['username']); $this->assertEquals('admin', $user['username']);
$this->assertEquals('admin@example.com', $user['email']); $this->assertEquals('admin@example.com', $user['email']);
}
public function testGetUserBySession()
{
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(NULL, $session);
$session->set('user', [ $session->set('user', [
@ -172,28 +147,19 @@ class UserTest extends CIDatabaseTestCase
]); ]);
$userIdNone = $this->library->getUser(); $userIdNone = $this->library->getUser();
$this->assertEquals('admin', $userIdNone['username']); $this->assertEquals('admin', $userIdNone['username']);
}
public function testGetUserWithVariables()
{
$userVar = $this->library->getUser(1, true); $userVar = $this->library->getUser(1, true);
$this->assertInternalType('array', $userVar['variables']); $this->assertInternalType('array', $userVar['variables']);
}
public function testGetUserFalseUserNotFound()
{
$this->assertFalse($this->library->getUser(99)); $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 testGetUserIdByEmail() public function testGetUserId()
{ {
$userIdEmail = $this->library->getUserId('admin@example.com'); $userIdEmail = $this->library->getUserId('admin@example.com');
$this->assertEquals('1', $userIdEmail); $this->assertEquals('1', $userIdEmail);
}
public function testGetUserIdBySession()
{
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(NULL, $session);
$session->set('user', [ $session->set('user', [
@ -201,10 +167,7 @@ class UserTest extends CIDatabaseTestCase
]); ]);
$userIdNone = $this->library->getUserId(); $userIdNone = $this->library->getUserId();
$this->assertEquals('1', $userIdNone); $this->assertEquals('1', $userIdNone);
}
public function testGetUserIdFalseUserNotFound()
{
$this->assertFalse($this->library->getUserId('none@example.com')); $this->assertFalse($this->library->getUserId('none@example.com'));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
@ -220,9 +183,7 @@ class UserTest extends CIDatabaseTestCase
'id' => 1, 'id' => 1,
'banned' => 1, 'banned' => 1,
]); ]);
}
public function testBanUserFalseUserNotFound()
{
$this->assertFalse($this->library->banUser(99)); $this->assertFalse($this->library->banUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
@ -239,9 +200,7 @@ class UserTest extends CIDatabaseTestCase
'id' => 1, 'id' => 1,
'banned' => 0, 'banned' => 0,
]); ]);
}
public function testUnbanUserFalseUserNotFound()
{
$this->assertFalse($this->library->unbanUser(99)); $this->assertFalse($this->library->unbanUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
@ -265,20 +224,13 @@ class UserTest extends CIDatabaseTestCase
]); ]);
} }
public function testIsBannedTrue() public function testIsBanned()
{ {
$this->library->banUser(1); $this->library->banUser(1);
$this->assertTrue($this->library->isBanned(1)); $this->assertTrue($this->library->isBanned(1));
}
public function testIsBannedFalse()
{
$this->assertFalse($this->library->isBanned(1)); $this->assertFalse($this->library->isBanned(1));
}
public function testIsBannedFalseUserNotFound() $this->assertTrue($this->library->isBanned(99));
{
$this->assertFalse($this->library->isBanned(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
} }

Loading…
Cancel
Save