Browse Source

updated Config/Aauth, UserModel, UserVariableModelTest & Aauth/UserTest

v3-dev
REJack 6 years ago
parent
commit
87e3978caa
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 8
      application/Config/Aauth.php
  2. 6
      application/Models/Aauth/UserModel.php
  3. 2
      tests/Aauth/Database/UserVariableModelTest.php
  4. 177
      tests/Aauth/Libraries/Aauth/UserTest.php

8
application/Config/Aauth.php

@ -68,14 +68,14 @@ class Aauth extends BaseConfig
| User Verification, if TRUE sends a verification email on account creation | User Verification, if TRUE sends a verification email on account creation
| (default: false) | (default: false)
| |
| 'userAdditionalChars' | 'userRegexPattern'
| |
| Additional valid chars for username. Non alphanumeric characters that are | Additional valid chars for username. Non alphanumeric characters that are
| allowed by default | allowed by default
| (default: []) | (default: '[a-zA-Z0-9]{3,}')
*/ */
public $userVerification = false; public $userVerification = false;
public $userAdditionalChars = []; public $userRegexPattern = '[a-zA-Z0-9]{3,}';
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

6
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['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['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 = [ $this->validationMessages = [
'email' => [ 'email' => [
@ -108,13 +108,13 @@ class UserModel extends Model
], ],
'username' => [ 'username' => [
'is_unique' => lang('Aauth.existsAlreadyUsername'), 'is_unique' => lang('Aauth.existsAlreadyUsername'),
'min_length' => lang('Aauth.invalidUsername'), 'regex_match' => lang('Aauth.invalidUsername'),
], ],
]; ];
if ($this->config->loginUseUsername) 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'); $this->validationMessages['username']['required'] = lang('Aauth.requiredUsername');
} }

2
tests/Aauth/Database/UserVariableModelTest.php

@ -128,7 +128,7 @@ class UserVariableModelTest extends CIDatabaseTestCase
public function testDBCall() public function testDBCall()
{ {
$this->model->save(99, 'test', 'TRUE'); $this->model->save(99, 'test', 'TRUE');
$this->assertEquals(99, $this->model->insertID()); $this->assertEquals(1, $this->model->insertID());
} }
} }

177
tests/Aauth/Libraries/Aauth/UserTest.php

@ -21,7 +21,7 @@ class UserTest extends CIDatabaseTestCase
protected $namespace = 'App'; 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);
@ -30,12 +30,12 @@ class UserTest extends CIDatabaseTestCase
} }
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',
@ -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'); $this->library->updateUser(2, 'user1@example.com', 'password987654', 'user1');
$user = $this->library->getUser(2); $this->seeInDatabase($this->config->dbTableUserVariables, [
$this->assertNotEquals($userPre['email'], $user['email']); 'user_id' => 2,
$this->assertNotEquals($userPre['username'], $user['username']); 'email' => 'user1@example.com',
$this->library->updateUser(2, null, null, 'user1'); 'username' => 'user1',
$userAfter = $this->library->getUser(2); ]);
$this->assertEquals($user['username'], $userAfter['username']); $this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]);
$this->assertFalse($this->library->updateUser(2, 'asasdfasd')); }
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->assertFalse($this->library->updateUser(2));
$this->assertCount(0, $this->library->getErrorsArray());
}
public function testUpdateUserFalseUserNotFound()
{
$this->assertFalse($this->library->updateUser(99)); $this->assertFalse($this->library->updateUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
public function testDeleteUser() public function testDeleteUser()
{ {
$users = $this->library->listUsers(); $this->seeNumRecords(2, $this->config->dbTableUsers);
$this->assertCount(2, $users);
$this->library->deleteUser(2); $this->library->deleteUser(2);
$users = $this->library->listUsers(); $this->seeNumRecords(1, $this->config->dbTableUsers);
$this->assertCount(1, $users); }
public function testDeleteUserFalseUserNotFound()
{
$this->assertFalse($this->library->deleteUser(99)); $this->assertFalse($this->library->deleteUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
public function testListUsers() public function testListUsers()
@ -92,22 +144,25 @@ 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 testGetUser() public function testGetUserByUserId()
{ {
$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']);
$this->assertFalse($this->library->getUser(99)); }
$userVar = $this->library->getUser(1, true);
$this->assertInternalType('array', $userVar['variables']);
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', [
@ -117,11 +172,26 @@ class UserTest extends CIDatabaseTestCase
$this->assertEquals('admin', $userIdNone['username']); $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'); $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', [
@ -129,30 +199,52 @@ 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]);
} }
public function testBanUser() public function testBanUser()
{ {
$this->assertFalse($this->library->isBanned(1)); $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1,
'banned' => 0,
]);
$this->library->banUser(1); $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->assertFalse($this->library->banUser(99));
$this->assertCount(1, $this->library->getErrorsArray()); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
public function testUnbanUser() public function testUnbanUser()
{ {
$this->library->banUser(1); $this->library->banUser(1);
$this->assertTrue($this->library->isBanned(1)); $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1,
'banned' => 1,
]);
$this->library->unbanUser(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->assertFalse($this->library->unbanUser(99));
$this->assertCount(1, $this->library->getErrorsArray()); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
public function testBanUnbanUserSession()
public function testUnbanUserSession()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(NULL, $session);
@ -160,14 +252,31 @@ class UserTest extends CIDatabaseTestCase
'id' => 1, 'id' => 1,
]); ]);
$this->library->banUser(); $this->library->banUser();
$this->assertTrue($this->library->isBanned()); $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1,
'banned' => 1,
]);
$this->library->unbanUser(); $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->assertFalse($this->library->isBanned(99));
$this->assertCount(1, $this->library->getErrorsArray()); $this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
} }
} }

Loading…
Cancel
Save