Browse Source

updated Libraries/Aauth, Aauth/GroupTest & Aauth/PermTest

added UserSessionModelTest
v3-dev
REJack 6 years ago
parent
commit
14e29622ad
No known key found for this signature in database
GPG Key ID: 4A44B48700429F46
  1. 23
      app/Libraries/Aauth.php
  2. 54
      tests/Aauth/Database/UserSessionModelTest.php
  3. 7
      tests/Aauth/Libraries/Aauth/GroupTest.php
  4. 62
      tests/Aauth/Libraries/Aauth/PermTest.php

23
app/Libraries/Aauth.php

@ -753,7 +753,9 @@ class Aauth
*/ */
public function deleteUser(int $userId) public function deleteUser(int $userId)
{ {
$userModel = new UserModel(); $userModel = new UserModel();
$groupToUserModel = new GroupToUserModel();
$permToUserModel = new PermToUserModel();
if (! $userModel->existsById($userId)) if (! $userModel->existsById($userId))
{ {
@ -762,7 +764,24 @@ class Aauth
return false; return false;
} }
return $userModel->delete($userId); $userModel->transStart();
$groupToUserModel->deleteAllByUserId($userId);
$permToUserModel->deleteAllByUserId($userId);
$userModel->delete($userId);
$userModel->transComplete();
if ($userModel->transStatus() === false)
{
$userModel->transRollback();
return false;
}
else
{
$userModel->transCommit();
return true;
}
} }
/** /**

54
tests/Aauth/Database/UserSessionModelTest.php

@ -0,0 +1,54 @@
<?php namespace Tests\Aauth\Database;
use Config\Aauth as AauthConfig;
use CodeIgniter\Test\CIDatabaseTestCase;
use App\Models\Aauth\UserSessionModel;
class UserSessionModelTest extends CIDatabaseTestCase
{
protected $refresh = true;
protected $basePath = FCPATH . '../app/Database/Migrations';
protected $namespace = 'App';
public function setUp()
{
parent::setUp();
$this->model = new UserSessionModel($this->db);
$this->config = new AauthConfig();
}
//--------------------------------------------------------------------
public function testDelete()
{
$id = md5(time());
$this->hasInDatabase($this->config->dbTableUserSessions, [
'id' => $id,
'ip_address' => '127.0.0.1',
'timestamp' => time(),
'data' => '',
]);
$this->seeNumRecords(1, $this->config->dbTableUserSessions, []);
$this->model->delete($id);
$this->seeNumRecords(0, $this->config->dbTableUserSessions, []);
}
public function testDBInsert()
{
$this->seeNumRecords(0, $this->config->dbTableUserSessions, []);
$id = md5(time());
$this->model->insert([
'id' => $id,
'ip_address' => '127.0.0.1',
'timestamp' => time(),
'data' => '',
]);
$this->seeNumRecords(1, $this->config->dbTableUserSessions, []);
$this->assertEquals(1, $this->model->affectedRows());
$this->assertEquals(1, $this->model->countAll());
}
}

7
tests/Aauth/Libraries/Aauth/GroupTest.php

@ -220,6 +220,13 @@ class GroupTest extends CIDatabaseTestCase
]); ]);
} }
public function testGetUserGroups()
{
$this->assertCount(2, $this->library->getUserGroups(1));
$this->assertCount(1, $this->library->getUserGroups(2));
$this->assertFalse($this->library->getUserGroups(99));
}
public function testRemoveMemberFromAll() public function testRemoveMemberFromAll()
{ {
$this->assertTrue($this->library->removeMemberFromAll(1)); $this->assertTrue($this->library->removeMemberFromAll(1));

62
tests/Aauth/Libraries/Aauth/PermTest.php

@ -176,6 +176,37 @@ class PermTest extends CIDatabaseTestCase
$this->assertEquals('testPerm1', $permsOrderBy['perms'][1]['name']); $this->assertEquals('testPerm1', $permsOrderBy['perms'][1]['name']);
} }
public function testGetUserPerms()
{
$this->assertCount(0, $this->library->getUserPerms(1, 1));
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 2,
'name' => 'testPerm2',
'definition' => 'Test Perm 2',
]);
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 1,
'user_id' => 1,
'state' => 1,
]);
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 2,
'user_id' => 1,
'state' => 0,
]);
$this->assertCount(1, $this->library->getUserPerms(1, 1));
$this->assertCount(1, $this->library->getUserPerms(1, 0));
$this->assertCount(2, $this->library->getUserPerms(1));
$this->assertFalse($this->library->getUserPerms(99, 1));
}
/** /**
* @runInSeparateProcess * @runInSeparateProcess
* @preserveGlobalState disabled * @preserveGlobalState disabled
@ -387,6 +418,37 @@ class PermTest extends CIDatabaseTestCase
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]); $this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
} }
public function testGetGroupPerms()
{
$this->assertCount(0, $this->library->getGroupPerms(1, 1));
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 2,
'name' => 'testPerm2',
'definition' => 'Test Perm 2',
]);
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 1,
'group_id' => 1,
'state' => 1,
]);
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 2,
'group_id' => 1,
'state' => 0,
]);
$this->assertCount(1, $this->library->getGroupPerms(1, 1));
$this->assertCount(1, $this->library->getGroupPerms(1, 0));
$this->assertCount(2, $this->library->getGroupPerms(1));
$this->assertFalse($this->library->getGroupPerms(99, 1));
}
public function testListGroupPerms() public function testListGroupPerms()
{ {
$this->hasInDatabase($this->config->dbTablePerms, [ $this->hasInDatabase($this->config->dbTablePerms, [

Loading…
Cancel
Save