diff --git a/.env b/.env index 4195f17..6ec8976 100644 --- a/.env +++ b/.env @@ -13,6 +13,7 @@ migrations.enabled = true aauth.dbProfile = 'tests' +aauth.loginRemember = '-1 day' #-------------------------------------------------------------------- # APP diff --git a/application/Libraries/Aauth.php b/application/Libraries/Aauth.php index 8a3947b..c164daa 100644 --- a/application/Libraries/Aauth.php +++ b/application/Libraries/Aauth.php @@ -94,10 +94,19 @@ class Aauth * * Prepares config & session variable. */ - public function __construct() + public function __construct($config = NULL, $session = NULL) { - $this->config = new \Config\Aauth(); - $this->session = \Config\Services::session(); + if (is_null($config)) + { + $config = new \Config\Aauth(); + } + $this->config = $config; + + if (is_null($session)) + { + $session = \Config\Services::session(); + } + $this->session = $session; } //-------------------------------------------------------------------- diff --git a/application/Models/Aauth/GroupToGroupModel.php b/application/Models/Aauth/GroupToGroupModel.php index ebe207b..8fbf103 100644 --- a/application/Models/Aauth/GroupToGroupModel.php +++ b/application/Models/Aauth/GroupToGroupModel.php @@ -88,8 +88,6 @@ class GroupToGroupModel { $this->db = Database::connect($this->DBGroup); } - - $this->request = Services::request(); } /** @@ -222,12 +220,6 @@ class GroupToGroupModel $table = empty($table) ? $this->table : $table; - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - $this->builder = $this->db->table($table); return $this->builder; diff --git a/application/Models/Aauth/GroupToUserModel.php b/application/Models/Aauth/GroupToUserModel.php index 78a0a98..189be2a 100644 --- a/application/Models/Aauth/GroupToUserModel.php +++ b/application/Models/Aauth/GroupToUserModel.php @@ -88,8 +88,6 @@ class GroupToUserModel { $this->db = Database::connect($this->DBGroup); } - - $this->request = Services::request(); } /** @@ -222,12 +220,6 @@ class GroupToUserModel $table = empty($table) ? $this->table : $table; - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - $this->builder = $this->db->table($table); return $this->builder; diff --git a/application/Models/Aauth/LoginAttemptModel.php b/application/Models/Aauth/LoginAttemptModel.php index f8631a6..2fa0004 100644 --- a/application/Models/Aauth/LoginAttemptModel.php +++ b/application/Models/Aauth/LoginAttemptModel.php @@ -193,12 +193,6 @@ class LoginAttemptModel $table = empty($table) ? $this->table : $table; - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - $this->builder = $this->db->table($table); return $this->builder; diff --git a/application/Models/Aauth/LoginTokenModel.php b/application/Models/Aauth/LoginTokenModel.php index 171dda6..e75ce51 100644 --- a/application/Models/Aauth/LoginTokenModel.php +++ b/application/Models/Aauth/LoginTokenModel.php @@ -19,7 +19,6 @@ namespace App\Models\Aauth; use Config\Aauth as AauthConfig; use Config\Database; -use Config\Services; use CodeIgniter\Database\BaseBuilder; use CodeIgniter\Database\BaseConnection; use CodeIgniter\Database\ConnectionInterface; @@ -89,8 +88,6 @@ class LoginTokenModel { $this->db = Database::connect($this->DBGroup); } - - $this->request = Services::request(); } /** @@ -121,6 +118,7 @@ class LoginTokenModel $builder = $this->builder(); $data['created_at'] = date('Y-m-d H:i:s'); + $data['expires_at'] = date('Y-m-d H:i:s', strtotime($this->config->loginRemember)); $data['updated_at'] = date('Y-m-d H:i:s'); return $builder->insert($data); @@ -176,12 +174,6 @@ class LoginTokenModel $table = empty($table) ? $this->table : $table; - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - $this->builder = $this->db->table($table); return $this->builder; diff --git a/application/Models/Aauth/PermToGroupModel.php b/application/Models/Aauth/PermToGroupModel.php index 85c6955..1f96e15 100644 --- a/application/Models/Aauth/PermToGroupModel.php +++ b/application/Models/Aauth/PermToGroupModel.php @@ -78,7 +78,7 @@ class PermToGroupModel { $this->config = new AauthConfig(); $this->DBGroup = $this->config->dbProfile; - $this->table = $this->config->dbTablePermToUser; + $this->table = $this->config->dbTablePermToGroup; if ($db instanceof ConnectionInterface) { @@ -88,8 +88,6 @@ class PermToGroupModel { $this->db = Database::connect($this->DBGroup); } - - $this->request = Services::request(); } /** @@ -222,12 +220,6 @@ class PermToGroupModel $table = empty($table) ? $this->table : $table; - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - $this->builder = $this->db->table($table); return $this->builder; diff --git a/application/Models/Aauth/PermToUserModel.php b/application/Models/Aauth/PermToUserModel.php index 172717a..4506177 100644 --- a/application/Models/Aauth/PermToUserModel.php +++ b/application/Models/Aauth/PermToUserModel.php @@ -88,8 +88,6 @@ class PermToUserModel { $this->db = Database::connect($this->DBGroup); } - - $this->request = Services::request(); } /** @@ -222,12 +220,6 @@ class PermToUserModel $table = empty($table) ? $this->table : $table; - // Ensure we have a good db connection - if (! $this->db instanceof BaseConnection) - { - $this->db = Database::connect($this->DBGroup); - } - $this->builder = $this->db->table($table); return $this->builder; diff --git a/phpunit.xml b/phpunit.xml index 7ae6cff..4ab85de 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -27,6 +27,7 @@ ./application/Config ./application/Database ./application/Filters + ./application/Helpers ./application/Language ./application/Views diff --git a/tests/Aauth/Database/GroupToGroupModelTest.php b/tests/Aauth/Database/GroupToGroupModelTest.php new file mode 100644 index 0000000..4fdada3 --- /dev/null +++ b/tests/Aauth/Database/GroupToGroupModelTest.php @@ -0,0 +1,92 @@ +model = new GroupToGroupModel($this->db); + } + + //-------------------------------------------------------------------- + + public function testExistsFalse() + { + $groupToGroup = $this->model->exists(99, 99); + $this->assertFalse($groupToGroup); + } + + public function testExistsTrue() + { + $this->model->insert(99, 99); + $groupToGroup = $this->model->exists(99, 99); + $this->assertTrue($groupToGroup); + } + + public function testFindAllBySubgroupId() + { + $groupsToGroup = $this->model->findAllBySubgroupId(99); + $this->assertCount(0, $groupsToGroup); + $this->model->insert(99, 99); + $groupsToGroup = $this->model->findAllBySubgroupId(99); + $this->assertCount(1, $groupsToGroup); + } + + public function testFindAllByGroupId() + { + $groupToGroups = $this->model->findAllByGroupId(99); + $this->assertCount(0, $groupToGroups); + $this->model->insert(99, 99); + $groupToGroups = $this->model->findAllByGroupId(99); + $this->assertCount(1, $groupToGroups); + } + + public function testDelete() + { + $this->model->insert(99, 99); + $groupToGroup = $this->model->exists(99, 99); + $this->assertTrue($groupToGroup); + $this->model->delete(99, 99); + $groupToGroup = $this->model->exists(99, 99); + $this->assertFalse($groupToGroup); + } + + public function testDeleteAllByGroupId() + { + $this->model->insert(99, 99); + $groupToGroups = $this->model->findAllByGroupId(99); + $this->assertCount(1, $groupToGroups); + $this->model->deleteAllByGroupId(99); + $groupToGroups = $this->model->findAllByGroupId(99); + $this->assertCount(0, $groupToGroups); + } + + public function testDeleteAllBySubgroupId() + { + $this->model->insert(99, 99); + $groupsToGroup = $this->model->findAllBySubgroupId(99); + $this->assertCount(1, $groupsToGroup); + $this->model->deleteAllBySubgroupId(99); + $groupsToGroup = $this->model->findAllBySubgroupId(99); + $this->assertCount(0, $groupsToGroup); + } + + public function testConfigDBGroup() + { + $this->model = new GroupToGroupModel(); + $this->model->insert(99, 99); + $this->model->insert(98, 99); + $groupsToGroup = $this->model->findAllBySubgroupId(99); + $this->assertCount(2, $groupsToGroup); + } +} diff --git a/tests/Aauth/Database/GroupToUserModelTest.php b/tests/Aauth/Database/GroupToUserModelTest.php new file mode 100644 index 0000000..18ede05 --- /dev/null +++ b/tests/Aauth/Database/GroupToUserModelTest.php @@ -0,0 +1,92 @@ +model = new GroupToUserModel($this->db); + } + + //-------------------------------------------------------------------- + + public function testExistsFalse() + { + $groupToUser = $this->model->exists(99, 99); + $this->assertFalse($groupToUser); + } + + public function testExistsTrue() + { + $this->model->insert(99, 99); + $groupToUser = $this->model->exists(99, 99); + $this->assertTrue($groupToUser); + } + + public function testFindAllByUserId() + { + $groupToUsers = $this->model->findAllByUserId(99); + $this->assertCount(0, $groupToUsers); + $this->model->insert(99, 99); + $groupToUsers = $this->model->findAllByUserId(99); + $this->assertCount(1, $groupToUsers); + } + + public function testFindAllByGroupId() + { + $groupToUsers = $this->model->findAllByGroupId(99); + $this->assertCount(0, $groupToUsers); + $this->model->insert(99, 99); + $groupToUsers = $this->model->findAllByGroupId(99); + $this->assertCount(1, $groupToUsers); + } + + public function testDelete() + { + $this->model->insert(99, 99); + $groupToUser = $this->model->exists(99, 99); + $this->assertTrue($groupToUser); + $this->model->delete(99, 99); + $groupToUser = $this->model->exists(99, 99); + $this->assertFalse($groupToUser); + } + + public function testDeleteAllByGroupId() + { + $this->model->insert(99, 99); + $groupToUsers = $this->model->findAllByGroupId(99); + $this->assertCount(1, $groupToUsers); + $this->model->deleteAllByGroupId(99); + $groupToUsers = $this->model->findAllByGroupId(99); + $this->assertCount(0, $groupToUsers); + } + + public function testDeleteAllByUserId() + { + $this->model->insert(99, 99); + $groupToUsers = $this->model->findAllByUserId(99); + $this->assertCount(1, $groupToUsers); + $this->model->deleteAllByUserId(99); + $groupToUsers = $this->model->findAllByUserId(99); + $this->assertCount(0, $groupToUsers); + } + + public function testConfigDBGroup() + { + $this->model = new GroupToUserModel(); + $this->model->insert(99, 99); + $this->model->insert(98, 99); + $groupToUsers = $this->model->findAllByUserId(99); + $this->assertCount(2, $groupToUsers); + } +} diff --git a/tests/Aauth/Database/LoginAttemptModelTest.php b/tests/Aauth/Database/LoginAttemptModelTest.php new file mode 100644 index 0000000..f483287 --- /dev/null +++ b/tests/Aauth/Database/LoginAttemptModelTest.php @@ -0,0 +1,76 @@ +model = new LoginAttemptModel($this->db); + } + + //-------------------------------------------------------------------- + + public function testFind() + { + $loginAttempt = $this->model->find(); + $this->assertEquals(0, $loginAttempt); + } + + public function testSaveInsert() + { + $this->model->save(); + $loginAttempt = $this->model->find(); + $this->assertEquals(1, $loginAttempt); + } + + public function testSaveUpdate() + { + $this->model->save(); + $this->assertTrue($this->model->save()); + $loginAttempt = $this->model->find(); + $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(); + $this->model->save(); + $this->assertFalse($this->model->save()); + } + + public function testDelete() + { + $this->model->save(); + $loginAttempt = $this->model->find(); + $this->assertEquals(1, $loginAttempt); + $this->model->delete(); + $loginAttempt = $this->model->find(); + $this->assertEquals(0, $loginAttempt); + } + + public function testConfigDBGroup() + { + $this->model = new LoginAttemptModel(); + $this->model->save(); + $groupsToGroup = $this->model->find(); + $this->assertEquals(1, $groupsToGroup); + } +} diff --git a/tests/Aauth/Database/LoginTokenModelTest.php b/tests/Aauth/Database/LoginTokenModelTest.php new file mode 100644 index 0000000..5de04d1 --- /dev/null +++ b/tests/Aauth/Database/LoginTokenModelTest.php @@ -0,0 +1,58 @@ +model = new LoginTokenModel($this->db); + } + + //-------------------------------------------------------------------- + + public function testInsert() + { + $this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']); + $loginTokens = $this->model->findAllByUserId(99); + $this->assertCount(1, $loginTokens); + } + + public function testUpdate() + { + $this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']); + $oldLoginTokens = $this->model->findAllByUserId(99); + $oldLoginToken = $oldLoginTokens[0]; + sleep(5); + $this->model->update($oldLoginToken['id']); + $loginTokens = $this->model->findAllByUserId(99); + $loginToken = $loginTokens[0]; + $this->assertNotEquals($oldLoginToken['expires_at'], $loginToken['expires_at']); + } + + public function testDeleteExpired() + { + $this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']); + sleep(5); + $this->model->deleteExpired(99); + $loginTokens = $this->model->findAllByUserId(99); + $this->assertCount(0, $loginTokens); + } + + public function testConfigDBGroup() + { + $this->model = new LoginTokenModel(); + $this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']); + $loginTokens = $this->model->findAllByUserId(99); + $this->assertCount(1, $loginTokens); + } +} diff --git a/tests/Aauth/Database/PermToGroupModelTest.php b/tests/Aauth/Database/PermToGroupModelTest.php new file mode 100644 index 0000000..0c7a21a --- /dev/null +++ b/tests/Aauth/Database/PermToGroupModelTest.php @@ -0,0 +1,92 @@ +model = new PermToGroupModel($this->db); + } + + //-------------------------------------------------------------------- + + public function testExistsFalse() + { + $permToGroup = $this->model->exists(99, 99); + $this->assertFalse($permToGroup); + } + + public function testExistsTrue() + { + $this->model->insert(99, 99); + $permToGroup = $this->model->exists(99, 99); + $this->assertTrue($permToGroup); + } + + public function testFindAllByGroupId() + { + $permsToGroup = $this->model->findAllByGroupId(99); + $this->assertCount(0, $permsToGroup); + $this->model->insert(99, 99); + $permsToGroup = $this->model->findAllByGroupId(99); + $this->assertCount(1, $permsToGroup); + } + + public function testFindAllByPermId() + { + $permToGroups = $this->model->findAllByPermId(99); + $this->assertCount(0, $permToGroups); + $this->model->insert(99, 99); + $permToGroups = $this->model->findAllByPermId(99); + $this->assertCount(1, $permToGroups); + } + + public function testDelete() + { + $this->model->insert(99, 99); + $permToGroup = $this->model->exists(99, 99); + $this->assertTrue($permToGroup); + $this->model->delete(99, 99); + $permToGroup = $this->model->exists(99, 99); + $this->assertFalse($permToGroup); + } + + public function testDeleteAllByPermId() + { + $this->model->insert(99, 99); + $permToGroups = $this->model->findAllByPermId(99); + $this->assertCount(1, $permToGroups); + $this->model->deleteAllByPermId(99); + $permToGroups = $this->model->findAllByPermId(99); + $this->assertCount(0, $permToGroups); + } + + public function testDeleteAllByGroupId() + { + $this->model->insert(99, 99); + $permsToGroup = $this->model->findAllByGroupId(99); + $this->assertCount(1, $permsToGroup); + $this->model->deleteAllByGroupId(99); + $permsToGroup = $this->model->findAllByGroupId(99); + $this->assertCount(0, $permsToGroup); + } + + public function testConfigDBPerm() + { + $this->model = new PermToGroupModel(); + $this->model->insert(99, 99); + $this->model->insert(98, 99); + $permsToGroup = $this->model->findAllByGroupId(99); + $this->assertCount(2, $permsToGroup); + } +} diff --git a/tests/Aauth/Database/PermToUserModelTest.php b/tests/Aauth/Database/PermToUserModelTest.php new file mode 100644 index 0000000..c22636c --- /dev/null +++ b/tests/Aauth/Database/PermToUserModelTest.php @@ -0,0 +1,92 @@ +model = new PermToUserModel($this->db); + } + + //-------------------------------------------------------------------- + + public function testExistsFalse() + { + $permToUser = $this->model->exists(99, 99); + $this->assertFalse($permToUser); + } + + public function testExistsTrue() + { + $this->model->insert(99, 99); + $permToUser = $this->model->exists(99, 99); + $this->assertTrue($permToUser); + } + + public function testFindAllByUserId() + { + $permsToUser = $this->model->findAllByUserId(99); + $this->assertCount(0, $permsToUser); + $this->model->insert(99, 99); + $permsToUser = $this->model->findAllByUserId(99); + $this->assertCount(1, $permsToUser); + } + + public function testFindAllByPermId() + { + $permToUsers = $this->model->findAllByPermId(99); + $this->assertCount(0, $permToUsers); + $this->model->insert(99, 99); + $permToUsers = $this->model->findAllByPermId(99); + $this->assertCount(1, $permToUsers); + } + + public function testDelete() + { + $this->model->insert(99, 99); + $permToUser = $this->model->exists(99, 99); + $this->assertTrue($permToUser); + $this->model->delete(99, 99); + $permToUser = $this->model->exists(99, 99); + $this->assertFalse($permToUser); + } + + public function testDeleteAllByPermId() + { + $this->model->insert(99, 99); + $permToUsers = $this->model->findAllByPermId(99); + $this->assertCount(1, $permToUsers); + $this->model->deleteAllByPermId(99); + $permToUsers = $this->model->findAllByPermId(99); + $this->assertCount(0, $permToUsers); + } + + public function testDeleteAllByUserId() + { + $this->model->insert(99, 99); + $permsToUser = $this->model->findAllByUserId(99); + $this->assertCount(1, $permsToUser); + $this->model->deleteAllByUserId(99); + $permsToUser = $this->model->findAllByUserId(99); + $this->assertCount(0, $permsToUser); + } + + public function testConfigDBPerm() + { + $this->model = new PermToUserModel(); + $this->model->insert(99, 99); + $this->model->insert(98, 99); + $permsToUser = $this->model->findAllByUserId(99); + $this->assertCount(2, $permsToUser); + } +} diff --git a/tests/Aauth/Libraries/AauthTest.php b/tests/Aauth/Libraries/AauthTest.php new file mode 100644 index 0000000..edce6fa --- /dev/null +++ b/tests/Aauth/Libraries/AauthTest.php @@ -0,0 +1,77 @@ + 'CodeIgniter\Session\Handlers\FileHandler', + 'sessionCookieName' => 'ci_session', + 'sessionExpiration' => 7200, + 'sessionSavePath' => 'null', + 'sessionMatchIP' => false, + 'sessionTimeToUpdate' => 300, + 'sessionRegenerateDestroy' => false, + 'cookieDomain' => '', + 'cookiePrefix' => '', + 'cookiePath' => '/', + 'cookieSecure' => false, + ]; + + $config = (object)$defaults; + + $session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config); + $session->setLogger(new TestLogger(new Logger())); + $session->start(); + + var_dump($_SESSION); + + return $session; + } + + //-------------------------------------------------------------------- + + public function testIsLoggedIn() + { + $session = $this->getInstance(); + + // $this->library = new Aauth(NULL, $session); + + // var_dump(session()); + // $this->assertFalse($this->library->isLoggedIn()); + } + + public function testTest() + { + $session = $this->getInstance(); + $session->start(); + + $session->set('foo', 'bar'); + + $this->assertEquals('bar', $_SESSION['foo']); + } +}