Browse Source

updated UserModelTest & UserTest

v3-dev
REJack 6 years ago
parent
commit
adea239517
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 27
      tests/Aauth/Database/UserModelTest.php
  2. 193
      tests/Aauth/Libraries/Aauth/UserTest.php

27
tests/Aauth/Database/UserModelTest.php

@ -92,4 +92,31 @@ class UserModelTest extends CIDatabaseTestCase
$newUser = $this->model->insert(['email' => 'test@test.local', 'password' => 'password123456']); $newUser = $this->model->insert(['email' => 'test@test.local', 'password' => 'password123456']);
$this->assertFalse($newUser); $this->assertFalse($newUser);
} }
public function testUpdate()
{
$this->assertFalse($this->model->update(2, ['id' => 2, 'email' => 'admin@example.com']));
$this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->model->errors());
// $this->assertFalse($this->library->updateUser(2, 'admin@example.com', null, null));
// $this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]);
// $this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null));
// $this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]);
// $this->assertFalse($this->library->updateUser(2, null, 'pass', null));
// $this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
// $this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null));
// $this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
// $this->assertFalse($this->library->updateUser(2, null, null, 'admin'));
// $this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]);
// $this->assertFalse($this->library->updateUser(2, null, null, 'user+'));
// $this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]);
// $this->assertFalse($this->library->updateUser(2));
// $this->assertCount(0, $this->library->getErrorsArray());
}
} }

193
tests/Aauth/Libraries/Aauth/UserTest.php

@ -11,7 +11,7 @@ use App\Libraries\Aauth;
/** /**
* @runTestsInSeparateProcesses * @runTestsInSeparateProcesses
* @preserveGlobalState disabled * @preserveGlobalState disabled
*/ */
class UserTest extends CIDatabaseTestCase class UserTest extends CIDatabaseTestCase
{ {
@ -21,97 +21,88 @@ 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);
$this->config = new AauthConfig(); $this->config = new AauthConfig();
$_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,
'sessionSavePath' => 'null', 'sessionSavePath' => 'null',
'sessionMatchIP' => false, 'sessionMatchIP' => false,
'sessionTimeToUpdate' => 300, 'sessionTimeToUpdate' => 300,
'sessionRegenerateDestroy' => false, 'sessionRegenerateDestroy' => false,
'cookieDomain' => '', 'cookieDomain' => '',
'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 testUpdateUser() public function testUpdateUser()
{ {
$session = $this->getInstance(); $this->library = new Aauth(null, true);
$this->library = new Aauth(NULL, $session);
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'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->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'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]);
$this->library = new Aauth(NULL, $session); // moved to UserModelTest
$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]);
$this->library = new Aauth(NULL, $session); // $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]);
$this->library = new Aauth(NULL, $session); // $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]);
$this->library = new Aauth(NULL, $session); // $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]);
$this->library = new Aauth(NULL, $session); // $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]);
$this->library = new Aauth(NULL, $session); // $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]);
$this->library = new Aauth(NULL, $session); // $this->assertFalse($this->library->updateUser(2));
$this->assertFalse($this->library->updateUser(2)); // $this->assertCount(0, $this->library->getErrorsArray());
$this->assertCount(0, $this->library->getErrorsArray());
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(null, true);
$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]);
} }
public function testDeleteUser() public function testDeleteUser()
@ -121,54 +112,54 @@ class UserTest extends CIDatabaseTestCase
$this->seeNumRecords(1, $this->config->dbTableUsers, ['deleted' => 0]); $this->seeNumRecords(1, $this->config->dbTableUsers, ['deleted' => 0]);
$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]);
} }
public function testListUsers() public function testListUsers()
{ {
$users = $this->library->listUsers(); $users = $this->library->listUsers();
$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']);
$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 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']);
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(null, $session);
$session->set('user', [ $session->set('user', [
'id' => 1, 'id' => 1,
]); ]);
$userIdNone = $this->library->getUser(); $userIdNone = $this->library->getUser();
$this->assertEquals('admin', $userIdNone['username']); $this->assertEquals('admin', $userIdNone['username']);
$userVar = $this->library->getUser(1, true); $userVar = $this->library->getUser(1, true);
$this->assertInternalType('array', $userVar['variables']); $this->assertInternalType('array', $userVar['variables']);
$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 testGetUserId() 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);
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(null, $session);
$session->set('user', [ $session->set('user', [
'id' => 1, 'id' => 1,
]); ]);
$userIdNone = $this->library->getUserId(); $userIdNone = $this->library->getUserId();
$this->assertEquals('1', $userIdNone); $this->assertEquals('1', $userIdNone);
$this->assertFalse($this->library->getUserId('none@example.com')); $this->assertFalse($this->library->getUserId('none@example.com'));
@ -177,52 +168,52 @@ class UserTest extends CIDatabaseTestCase
public function testBanUser() public function testBanUser()
{ {
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1, 'id' => 1,
'banned' => 0, 'banned' => 0,
]); ]);
$this->library->banUser(1); $this->library->banUser(1);
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1, 'id' => 1,
'banned' => 1, 'banned' => 1,
]); ]);
$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]);
} }
public function testUnbanUser() public function testUnbanUser()
{ {
$this->library->banUser(1); $this->library->banUser(1);
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1, 'id' => 1,
'banned' => 1, 'banned' => 1,
]); ]);
$this->library->unbanUser(1); $this->library->unbanUser(1);
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1, 'id' => 1,
'banned' => 0, 'banned' => 0,
]); ]);
$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]);
} }
public function testBanUnbanUserSession() public function testBanUnbanUserSession()
{ {
$session = $this->getInstance(); $session = $this->getInstance();
$this->library = new Aauth(NULL, $session); $this->library = new Aauth(null, $session);
$session->set('user', [ $session->set('user', [
'id' => 1, 'id' => 1,
]); ]);
$this->library->banUser(); $this->library->banUser();
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1, 'id' => 1,
'banned' => 1, 'banned' => 1,
]); ]);
$this->library->unbanUser(); $this->library->unbanUser();
$this->seeInDatabase($this->config->dbTableUsers, [ $this->seeInDatabase($this->config->dbTableUsers, [
'id' => 1, 'id' => 1,
'banned' => 0, 'banned' => 0,
]); ]);
} }
@ -230,7 +221,7 @@ class UserTest extends CIDatabaseTestCase
{ {
$this->assertFalse($this->library->isBanned(1)); $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(1));
$this->assertTrue($this->library->isBanned(99)); $this->assertTrue($this->library->isBanned(99));

Loading…
Cancel
Save