diff --git a/application/Config/Aauth.php b/application/Config/Aauth.php index f4a7c08..ad48c5c 100644 --- a/application/Config/Aauth.php +++ b/application/Config/Aauth.php @@ -68,14 +68,14 @@ class Aauth extends BaseConfig | User Verification, if TRUE sends a verification email on account creation | (default: false) | - | 'userAdditionalChars' + | 'userRegexPattern' | | Additional valid chars for username. Non alphanumeric characters that are | allowed by default - | (default: []) + | (default: '[a-zA-Z0-9]{3,}') */ - public $userVerification = false; - public $userAdditionalChars = []; + public $userVerification = false; + public $userRegexPattern = '[a-zA-Z0-9]{3,}'; /* |-------------------------------------------------------------------------- diff --git a/application/Models/Aauth/UserModel.php b/application/Models/Aauth/UserModel.php index ecc980e..694407f 100644 --- a/application/Models/Aauth/UserModel.php +++ b/application/Models/Aauth/UserModel.php @@ -95,7 +95,7 @@ class UserModel extends Model $this->validationRules['email'] = 'required|if_exist|valid_email|is_unique[' . $this->table . '.email,id,{id}]'; $this->validationRules['password'] = 'required|if_exist|min_length[' . $this->config->passwordMin . ']|max_length[' . $this->config->passwordMax . ']'; - $this->validationRules['username'] = 'if_exist|is_unique[' . $this->table . '.username,id,{id}]|alpha_numeric_space|min_length[3]'; + $this->validationRules['username'] = 'if_exist|is_unique[' . $this->table . '.username,id,{id}]|regex_match[/' . $this->config->userRegexPattern . '/]'; $this->validationMessages = [ 'email' => [ @@ -108,13 +108,13 @@ class UserModel extends Model ], 'username' => [ 'is_unique' => lang('Aauth.existsAlreadyUsername'), - 'min_length' => lang('Aauth.invalidUsername'), + 'regex_match' => lang('Aauth.invalidUsername'), ], ]; if ($this->config->loginUseUsername) { - $this->validationRules['username'] = 'is_unique[' . $this->table . '.username,id,{id}]|required|alpha_numeric_space|min_length[3]'; + $this->validationRules['username'] = 'required|if_exist|is_unique[' . $this->table . '.username,id,{id}]|regex_match[/' . $this->config->userRegexPattern . '/]'; $this->validationMessages['username']['required'] = lang('Aauth.requiredUsername'); } diff --git a/tests/Aauth/Database/UserVariableModelTest.php b/tests/Aauth/Database/UserVariableModelTest.php index 9430d90..a5d56dd 100644 --- a/tests/Aauth/Database/UserVariableModelTest.php +++ b/tests/Aauth/Database/UserVariableModelTest.php @@ -128,7 +128,7 @@ class UserVariableModelTest extends CIDatabaseTestCase public function testDBCall() { $this->model->save(99, 'test', 'TRUE'); - $this->assertEquals(99, $this->model->insertID()); + $this->assertEquals(1, $this->model->insertID()); } } diff --git a/tests/Aauth/Libraries/Aauth/UserTest.php b/tests/Aauth/Libraries/Aauth/UserTest.php index c9b6263..d26bb7c 100644 --- a/tests/Aauth/Libraries/Aauth/UserTest.php +++ b/tests/Aauth/Libraries/Aauth/UserTest.php @@ -21,7 +21,7 @@ class UserTest extends CIDatabaseTestCase protected $namespace = 'App'; public function setUp() - { + { parent::setUp(); $this->library = new Aauth(null, true); @@ -30,12 +30,12 @@ class UserTest extends CIDatabaseTestCase } public function tearDown() - { + { } protected function getInstance($options=[]) - { + { $defaults = [ 'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', 'sessionCookieName' => 'ci_session', @@ -61,29 +61,81 @@ class UserTest extends CIDatabaseTestCase //-------------------------------------------------------------------- - public function testUpdateUser() + public function testUpdateUserTrue() { - $userPre = $this->library->getUser(2); + $this->seeInDatabase($this->config->dbTableUserVariables, [ + 'user_id' => 2, + 'email' => 'user@example.com', + 'username' => 'user', + ]); $this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1'); - $user = $this->library->getUser(2); - $this->assertNotEquals($userPre['email'], $user['email']); - $this->assertNotEquals($userPre['username'], $user['username']); - $this->library->updateUser(2, null, null, 'user1'); - $userAfter = $this->library->getUser(2); - $this->assertEquals($user['username'], $userAfter['username']); - $this->assertFalse($this->library->updateUser(2, 'asasdfasd')); + $this->seeInDatabase($this->config->dbTableUserVariables, [ + 'user_id' => 2, + 'email' => 'user1@example.com', + 'username' => 'user1', + ]); + $this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]); + } + + public function testUpdateUserFalseEmailExists() + { + $this->assertFalse($this->library->updateUser(2, 'admin@example.com', null, null)); + $this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]); + } + + public function testUpdateUserFalseEmailInvalid() + { + $this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null)); + $this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]); + } + + public function testUpdateUserFalsePasswordMin() + { + $this->assertFalse($this->library->updateUser(2, null, 'pass', null)); + $this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); + } + + public function testUpdateUserFalsePasswordMax() + { + $this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null)); + $this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]); + } + + public function testUpdateUserFalseUsernameExists() + { + $this->assertFalse($this->library->updateUser(2, null, null, 'admin')); + $this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]); + } + + public function testUpdateUserFalseUsernameInvalid() + { + $this->assertFalse($this->library->updateUser(2, null, null, 'user+')); + $this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]); + } + + public function testUpdateUserFalseEmpty() + { $this->assertFalse($this->library->updateUser(2)); + $this->assertCount(0, $this->library->getErrorsArray()); + } + + public function testUpdateUserFalseUserNotFound() + { $this->assertFalse($this->library->updateUser(99)); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); } public function testDeleteUser() { - $users = $this->library->listUsers(); - $this->assertCount(2, $users); + $this->seeNumRecords(2, $this->config->dbTableUsers); $this->library->deleteUser(2); - $users = $this->library->listUsers(); - $this->assertCount(1, $users); + $this->seeNumRecords(1, $this->config->dbTableUsers); + } + + public function testDeleteUserFalseUserNotFound() + { $this->assertFalse($this->library->deleteUser(99)); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); } public function testListUsers() @@ -92,22 +144,25 @@ class UserTest extends CIDatabaseTestCase $this->assertCount(2, $users); $this->assertEquals('admin', $users[0]['username']); $this->assertEquals('user', $users[1]['username']); + } + + public function testListUsersOrderBy() + { $usersOrderBy = $this->library->listUsers(0, 0, null, 'id DESC'); $this->assertEquals('user', $usersOrderBy[0]['username']); $this->assertEquals('admin', $usersOrderBy[1]['username']); } - public function testGetUser() + public function testGetUserByUserId() { $user = $this->library->getUser(1); $this->assertEquals('1', $user['id']); $this->assertEquals('admin', $user['username']); $this->assertEquals('admin@example.com', $user['email']); - $this->assertFalse($this->library->getUser(99)); - - $userVar = $this->library->getUser(1, true); - $this->assertInternalType('array', $userVar['variables']); + } + public function testGetUserBySession() + { $session = $this->getInstance(); $this->library = new Aauth(NULL, $session); $session->set('user', [ @@ -117,11 +172,26 @@ class UserTest extends CIDatabaseTestCase $this->assertEquals('admin', $userIdNone['username']); } - public function testGetUserId() + public function testGetUserWithVariables() + { + $userVar = $this->library->getUser(1, true); + $this->assertInternalType('array', $userVar['variables']); + } + + public function testGetUserFalseUserNotFound() + { + $this->assertFalse($this->library->getUser(99)); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); + } + + public function testGetUserIdByEmail() { $userIdEmail = $this->library->getUserId('admin@example.com'); $this->assertEquals('1', $userIdEmail); + } + public function testGetUserIdBySession() + { $session = $this->getInstance(); $this->library = new Aauth(NULL, $session); $session->set('user', [ @@ -129,30 +199,52 @@ class UserTest extends CIDatabaseTestCase ]); $userIdNone = $this->library->getUserId(); $this->assertEquals('1', $userIdNone); + } + + public function testGetUserIdFalseUserNotFound() + { $this->assertFalse($this->library->getUserId('none@example.com')); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); } public function testBanUser() { - $this->assertFalse($this->library->isBanned(1)); + $this->seeInDatabase($this->config->dbTableUsers, [ + 'id' => 1, + 'banned' => 0, + ]); $this->library->banUser(1); - $this->assertTrue($this->library->isBanned(1)); + $this->seeInDatabase($this->config->dbTableUsers, [ + 'id' => 1, + 'banned' => 1, + ]); + } + public function testBanUserFalseUserNotFound() + { $this->assertFalse($this->library->banUser(99)); - $this->assertCount(1, $this->library->getErrorsArray()); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); } public function testUnbanUser() { $this->library->banUser(1); - $this->assertTrue($this->library->isBanned(1)); + $this->seeInDatabase($this->config->dbTableUsers, [ + 'id' => 1, + 'banned' => 1, + ]); $this->library->unbanUser(1); - $this->assertFalse($this->library->isBanned(1)); + $this->seeInDatabase($this->config->dbTableUsers, [ + 'id' => 1, + 'banned' => 0, + ]); + } + public function testUnbanUserFalseUserNotFound() + { $this->assertFalse($this->library->unbanUser(99)); - $this->assertCount(1, $this->library->getErrorsArray()); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); } - - public function testUnbanUserSession() + public function testBanUnbanUserSession() { $session = $this->getInstance(); $this->library = new Aauth(NULL, $session); @@ -160,14 +252,31 @@ class UserTest extends CIDatabaseTestCase 'id' => 1, ]); $this->library->banUser(); - $this->assertTrue($this->library->isBanned()); + $this->seeInDatabase($this->config->dbTableUsers, [ + 'id' => 1, + 'banned' => 1, + ]); $this->library->unbanUser(); - $this->assertFalse($this->library->isBanned()); + $this->seeInDatabase($this->config->dbTableUsers, [ + 'id' => 1, + 'banned' => 0, + ]); + } + + public function testIsBannedTrue() + { + $this->library->banUser(1); + $this->assertTrue($this->library->isBanned(1)); + } + + public function testIsBannedFalse() + { + $this->assertFalse($this->library->isBanned(1)); } - public function testIsBanned() + public function testIsBannedFalseUserNotFound() { $this->assertFalse($this->library->isBanned(99)); - $this->assertCount(1, $this->library->getErrorsArray()); + $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]); } }