Browse Source

updated Aauth library & Extensions

- updated PHPdocs overall files
- updated tests
- updated Aauth Class
- updated aauth_helper
- updated CAPTCHA extension
- updated TOTP extension
v3-dev
REJack 6 years ago
parent
commit
01faa07a04
  1. 56
      app/Helpers/aauth_helper.php
  2. 263
      app/Libraries/Aauth.php
  3. 18
      app/Libraries/Aauth/CAPTCHA.php
  4. 9
      app/Libraries/Aauth/TOTP.php
  5. 66
      tests/Aauth/Database/GroupToGroupModelTest.php
  6. 70
      tests/Aauth/Database/GroupToUserModelTest.php
  7. 38
      tests/Aauth/Database/GroupVariableModelTest.php
  8. 26
      tests/Aauth/Database/LoginTokenModelTest.php
  9. 93
      tests/Aauth/Database/PermToGroupModelTest.php
  10. 93
      tests/Aauth/Database/PermToUserModelTest.php
  11. 38
      tests/Aauth/Database/UserVariableModelTest.php
  12. 64
      tests/Aauth/Libraries/Aauth/AccessTest.php
  13. 18
      tests/Aauth/Libraries/Aauth/CAPTCHATest.php
  14. 18
      tests/Aauth/Libraries/Aauth/ErrorsTest.php
  15. 101
      tests/Aauth/Libraries/Aauth/GroupTest.php
  16. 16
      tests/Aauth/Libraries/Aauth/GroupVariablesTest.php
  17. 18
      tests/Aauth/Libraries/Aauth/InfosTest.php
  18. 18
      tests/Aauth/Libraries/Aauth/LoginTest.php
  19. 86
      tests/Aauth/Libraries/Aauth/PermTest.php
  20. 35
      tests/Aauth/Libraries/Aauth/TOTPTest.php
  21. 54
      tests/Aauth/Libraries/Aauth/UserTest.php
  22. 16
      tests/Aauth/Libraries/Aauth/UserVariablesTest.php
  23. 6
      tests/Aauth/Libraries/Aauth/UtilityTest.php

56
app/Helpers/aauth_helper.php

@ -8,12 +8,12 @@
* access management, public access etc..
*
* @package CodeIgniter-Aauth
* @since 3.0.0
* @author Emre Akay
* @author Raphael "REJack" Jackstadt
* @copyright 2014-2019 Emre Akay
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/emreakay/CodeIgniter-Aauth
* @since 3.0.0
*/
use App\Libraries\Aauth;
@ -37,16 +37,36 @@ if (! function_exists('is_loggedin'))
}
}
if (! function_exists('is_admin'))
{
/**
* Is member
*
* @param integer $userId User Id
*
* @return boolean
*/
function is_admin(int $userId = null)
{
$aauth = new Aauth();
return $aauth->isAdmin($userId);
}
}
if (! function_exists('is_member'))
{
/**
* Is member
*
* @param integer|string $groupPar Group Name or Id
* @param integer $userId User Id
*
* @return boolean
*/
function is_member($groupPar, $userId)
function is_member($groupPar, int $userId = null)
{
$aauth = new Aauth();
return $aauth->isMember($groupPar, $userId);
}
}
@ -56,11 +76,15 @@ if (! function_exists('is_allowed'))
/**
* Is allowed
*
* @param integer|string $permPar Perm Name or Id
* @param integer $userId User Id
*
* @return boolean
*/
function is_allowed($permPar, $userId)
function is_allowed($permPar, int $userId = null)
{
$aauth = new Aauth();
return $aauth->isAllowed($permPar, $userId);
}
}
@ -70,11 +94,15 @@ if (! function_exists('is_denied'))
/**
* Is denied
*
* @param integer|string $permPar Perm Name or Id
* @param integer $userId User Id
*
* @return boolean
*/
function is_denied($permPar, $userId)
function is_denied($permPar, int $userId = null)
{
$aauth = new Aauth();
return $aauth->isDenied($permPar, $userId);
}
}
@ -84,11 +112,14 @@ if (! function_exists('get_subgroups'))
/**
* Get Sub-Groups by Group Name/Id
*
* @param integer|string $groupPar Group Name or Id
*
* @return array
*/
function get_subgroups($groupPar)
{
$aauth = new Aauth();
return $aauth->getSubgroups($groupPar);
}
}
@ -98,11 +129,14 @@ if (! function_exists('get_user_perms'))
/**
* Get User Groups
*
* @param integer $userId User Id
*
* @return array
*/
function get_user_groups($userId)
function get_user_groups(int $userId = null)
{
$aauth = new Aauth();
return $aauth->getUserGroups($userId);
}
}
@ -112,11 +146,15 @@ if (! function_exists('get_user_perms'))
/**
* Get Group Perms by Group Name/Id
*
* @param integer $userId User Id
* @param integer $state State (0 disabled, 1 enabled)
*
* @return array
*/
function get_user_perms($userId, $state = null)
function get_user_perms(int $userId = null, int $state = null)
{
$aauth = new Aauth();
return $aauth->getUserPerms($userId, $state);
}
}
@ -126,11 +164,15 @@ if (! function_exists('get_group_perms'))
/**
* Get Group Perms by Group Name/Id
*
* @param integer|string $groupPar Group Name or Id
* @param integer $state State (0 disabled, 1 enabled)
*
* @return array
*/
function get_group_perms($groupPar, $state = null)
function get_group_perms($groupPar, int $state = null)
{
$aauth = new Aauth();
return $aauth->getGroupPerms($groupPar, $state);
}
}

263
app/Libraries/Aauth.php

@ -8,12 +8,12 @@
* access management, public access etc..
*
* @package CodeIgniter-Aauth
* @version 3.0.0-rc2
* @author Emre Akay
* @author Raphael "REJack" Jackstadt
* @copyright 2014-2019 Emre Akay
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/emreakay/CodeIgniter-Aauth
* @version 3.0.0-rc2
*/
namespace App\Libraries;
@ -105,15 +105,20 @@ class Aauth
* Constructor
*
* Prepares config & session variable.
*
* @param \Config\Aauth $config Config Object
* @param \CodeIgniter\Session\Session $session Session Class
*
* @return void
*/
public function __construct($config = null, $session = null)
public function __construct(\Config\Aauth $config = null, \CodeIgniter\Session\Session $session = null)
{
if (is_null($config))
if (! $config)
{
$config = new \Config\Aauth();
}
if (is_null($session))
if (! $session)
{
$session = \Config\Services::session();
}
@ -125,12 +130,13 @@ class Aauth
if ($this->config->captchaEnabled)
{
$this->modules = array_merge($this->config->modules, ['CAPTCHA']);
$this->modules = array_merge($this->modules, ['CAPTCHA']);
}
if ($this->config->totpEnabled)
{
$this->modules = array_merge($this->config->modules, ['TOTP']);
$this->modules = array_merge($this->modules, ['TOTP']);
}
if ($this->config->socialEnabled)
{
@ -152,6 +158,8 @@ class Aauth
* PreCache Perms
*
* Caches all permission IDs for later use.
*
* @return void
*/
private function precachePerms()
{
@ -168,6 +176,8 @@ class Aauth
* PreCache Groups
*
* Caches all group IDs for later use.
*
* @return void
*/
private function precacheGroups()
{
@ -225,7 +235,7 @@ class Aauth
$response = $request->getPostGet('h-captcha-response');
}
if (! $this->verifyCaptchaResponse($response)['success'])
if (! $this->verifyCaptchaResponse((string) $response)['success'])
{
$this->error(lang('Aauth.invalidCaptcha'));
@ -463,7 +473,13 @@ class Aauth
public function logout()
{
helper('cookie');
set_cookie($this->config->loginRememberCookie, '', -3600);
$cookieData['name'] = $this->config->loginRememberCookie;
$cookieData['value'] = '';
$cookieData['expire'] = -YEAR;
\Config\Services::response()->setCookie($cookieData)->send();
$this->session->remove('user');
@$this->session->destroy();
}
@ -477,18 +493,13 @@ class Aauth
*
* @return boolean
*/
protected function loginFast(int $userId)
private function loginFast(int $userId)
{
$userModel = $this->getModel('User');
$userModel->select('id, email, username');
$userModel->where('id', $userId);
$userModel->where('banned', 0);
if (! $user = $userModel->asArray()->first())
{
return false;
}
$this->session->set('user', [
'id' => $user['id'],
'username' => $user['username'],
@ -662,10 +673,9 @@ class Aauth
else
{
$groupAllowed = false;
foreach ($this->listUserGroups($userId) as $group)
foreach ($this->getUserGroups($userId) as $group)
{
if ($this->isGroupAllowed($permId, $group['id']))
if ($this->isGroupAllowed($permId, $group['group_id']))
{
$groupAllowed = true;
break;
@ -682,7 +692,7 @@ class Aauth
*
* Check if group is allowed to do specified action, admin always allowed
*
* @param integer $permPar Permission id or name to check
* @param integer|string $permPar Permission id or name to check
* @param integer|string $groupPar Group id or name to check, or if false checks all user groups
*
* @return boolean
@ -719,10 +729,14 @@ class Aauth
}
}
if ($permToGroupModel->allowed($permId, $groupId))
if ($permToGroupModel->denied($permId, $groupId))
{
return false;
}
else if ($permToGroupModel->allowed($permId, $groupId))
{
return true;
}
else if ($groupAllowed || $permToGroupModel->allowed($permId, $groupId))
{
return true;
@ -743,9 +757,9 @@ class Aauth
return false;
}
foreach ($this->listUserGroups() as $group)
foreach ($this->getUserGroups() as $group)
{
if ($this->isGroupAllowed($permId, $group['id']))
if ($this->isGroupAllowed($permId, $group['group_id']))
{
return true;
}
@ -846,7 +860,11 @@ class Aauth
return false;
}
// @codeCoverageIgnoreStart
if ($this->config->groupDefault)
{
$this->addMember($this->config->groupDefault, $userId);
}
if ($this->config->userVerification)
{
$this->sendVerification($userId, $email);
@ -854,7 +872,6 @@ class Aauth
return $userId;
}
// @codeCoverageIgnoreEnd
$this->info(lang('Aauth.infoCreateSuccess'));
@ -945,11 +962,9 @@ class Aauth
if ($userModel->transStatus() === false)
{
// @codeCoverageIgnoreStart
$userModel->transRollback();
return false;
// @codeCoverageIgnoreEnd
}
else
{
@ -1005,7 +1020,6 @@ class Aauth
*
* @param string|integer $groupPar Specify group id to list group or null for all users
* @param integer $limit Limit of users to be returned
* @param integer $offset Offset for limited number of users
* @param boolean $includeBanneds Include banned users
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
*
@ -1048,8 +1062,6 @@ class Aauth
* @param string $email Email to send verification email to
*
* @return boolean
*
* @codeCoverageIgnore
*/
protected function sendVerification(int $userId, string $email)
{
@ -1064,6 +1076,7 @@ class Aauth
$messageData['code'] = $verificationCode;
$messageData['link'] = site_url($this->config->linkVerification . '/' . $userId . '/' . $verificationCode);
// phpcs:disable CodeIgniter4.NamingConventions.ValidVariableName
if (isset($this->config->emailConfig->protocol))
{
if ($this->config->emailConfig->protocol === 'smtp')
@ -1097,6 +1110,7 @@ class Aauth
return false;
}
// phpcs:enable
return true;
@ -1229,8 +1243,6 @@ class Aauth
* Return users as an object array
*
* @return array Array of active users
*
* @codeCoverageIgnore
*/
public function listActiveUsers()
{
@ -1361,8 +1373,6 @@ class Aauth
* @param string $email Email for account to remind
*
* @return boolean
*
* @codeCoverageIgnore
*/
public function remindPassword(string $email)
{
@ -1384,6 +1394,7 @@ class Aauth
$messageData['code'] = $resetCode;
$messageData['link'] = site_url($this->config->linkResetPassword . '/' . $resetCode);
// phpcs:disable CodeIgniter4.NamingConventions.ValidVariableName
if (isset($this->config->emailConfig->protocol))
{
if ($this->config->emailConfig->protocol === 'smtp')
@ -1417,6 +1428,7 @@ class Aauth
return false;
}
// phpcs:enable
// $emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
// $emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
@ -1444,8 +1456,6 @@ class Aauth
* @param string $resetCode Verification code for account
*
* @return boolean
*
* @codeCoverageIgnore
*/
public function resetPassword(string $resetCode)
{
@ -1490,6 +1500,7 @@ class Aauth
$messageData['password'] = $password;
// phpcs:disable CodeIgniter4.NamingConventions.ValidVariableName
if (isset($this->config->emailConfig->protocol))
{
if ($this->config->emailConfig->protocol === 'smtp')
@ -1523,6 +1534,7 @@ class Aauth
return false;
}
// phpcs:enable
// $emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
// $emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
@ -1545,11 +1557,11 @@ class Aauth
/**
* Set User Variable as key value
*
* if variable not set before, it will be set
* If variable not set before, it will be set
* if set, overwrites the value
*
* @param string $key
* @param string $value
* @param string $key User Variable Key
* @param string $value User Variable Value
* @param integer $userId User id, can be null to use session user
*
* @return boolean
@ -1576,7 +1588,7 @@ class Aauth
/**
* Unset User Variable as key value
*
* @param string $key
* @param string $key User Variable Key
* @param integer $userId User id, can be null to use session user
*
* @return boolean
@ -1603,7 +1615,7 @@ class Aauth
/**
* Get User Variable by key
*
* @param string $key Variable Key
* @param string $key User Variable Key
* @param integer $userId User id, can be null to use session user
*
* @return boolean|string false if var is not set, the value of var if set
@ -1633,7 +1645,7 @@ class Aauth
}
/**
* Get User Variables by user id
* List User Variables by user id
*
* Return array with all user keys & variables
*
@ -1641,7 +1653,7 @@ class Aauth
*
* @return boolean|array , false if var is not set, the value of var if set
*/
public function getUserVars(int $userId = null)
public function listUserVars(int $userId = null)
{
if (! $userId)
{
@ -1661,7 +1673,7 @@ class Aauth
}
/**
* List User Variable Keys by UserId
* Get User Variable Keys by UserId
*
* Return array of variable keys or false
*
@ -1669,7 +1681,7 @@ class Aauth
*
* @return boolean|array
*/
public function listUserVarKeys(int $userId = null)
public function getUserVarKeys(int $userId = null)
{
if (! $userId)
{
@ -1696,7 +1708,7 @@ class Aauth
/**
* Create group
*
* @param string $groupName New group name
* @param string $name New group name
* @param string $definition Description of the group
*
* @return integer|boolean Group id or false on fail
@ -1799,11 +1811,9 @@ class Aauth
if ($groupModel->transStatus() === false)
{
// @codeCoverageIgnoreStart
$groupModel->transRollback();
return false;
// @codeCoverageIgnoreEnd
}
else
{
@ -1817,8 +1827,8 @@ class Aauth
/**
* Add member to group
*
* @param integer $userId User id to add to group
* @param integer|string $groupPar Group id or name to add user to
* @param integer $userId User id to add to group
*
* @return boolean
*/
@ -1852,8 +1862,8 @@ class Aauth
/**
* Remove member from group
*
* @param integer $userId User id to remove from group
* @param integer|string $groupPar Group id or name to remove user from
* @param integer $userId User id to remove from group
*
* @return boolean
*/
@ -1869,14 +1879,19 @@ class Aauth
/**
* Get User Groups
*
* @param integer|string $userId User id
* @param integer $userId User id
*
* @return boolean|array
*/
public function getUserGroups($userId)
public function getUserGroups(int $userId = null)
{
$userModel = $this->getModel('User');
if (! $userId)
{
$userId = (int) @$this->session->user['id'];
}
if (! $userModel->existsById($userId))
{
return false;
@ -1938,12 +1953,6 @@ class Aauth
{
return false;
}
else if ($groupToGroupModel->exists($groupId, $subgroupId))
{
$this->info(lang('Aauth.alreadyMemberSubgroup'));
return true;
}
if ($groupGroups = $groupToGroupModel->findAllByGroupId($groupId))
{
@ -1996,8 +2005,6 @@ class Aauth
*/
public function getSubgroups($groupPar)
{
$groupModel = $this->getModel('Group');
if (! $groupId = $this->getGroupId($groupPar))
{
return false;
@ -2008,6 +2015,61 @@ class Aauth
return $groupToGroupModel->findAllByGroupId($groupId);
}
/**
* List all group subgroups
*
* @param integer|string $groupPar Group id or name to remove
*
* @return array
*/
public function listGroupSubgroups($groupPar)
{
if (! $groupId = $this->getGroupId($groupPar))
{
return false;
}
$groupModel = $this->getModel('Group');
$groupModel->select('id, name, definition, IF(group_id = ' . $groupId . ', 1, 0) as subgroup');
$groupModel->join($this->config->dbTableGroupToGroup, '(' . $this->config->dbTableGroups . '.id = ' . $this->config->dbTableGroupToGroup . '.subgroup_id AND ' . $this->config->dbTableGroupToGroup . '.group_id = ' . $groupId . ')', 'left');
return $groupModel->findAll();
}
/**
* List group subgroups with paginate
*
* Return groups as an object array
*
* @param integer|string $groupPar Group id or name to remove
* @param integer $limit Limit of users to be returned
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
*
* @return array
*/
public function listGroupSubgroupsPaginated($groupPar, int $limit = 10, string $orderBy = null)
{
if (! $groupId = $this->getGroupId($groupPar))
{
return false;
}
$groupModel = $this->getModel('Group');
$groupModel->select('id, name, definition, IF(group_id = ' . $groupId . ', 1, 0) as subgroup');
$groupModel->join($this->config->dbTableGroupToGroup, '(' . $this->config->dbTableGroups . '.id = ' . $this->config->dbTableGroupToGroup . '.subgroup_id AND ' . $this->config->dbTableGroupToGroup . '.group_id = ' . $groupId . ')', 'left');
if (! is_null($orderBy))
{
$groupModel->orderBy($orderBy);
}
return [
'groups' => $groupModel->paginate($limit),
'pager' => $groupModel->pager,
];
}
/**
* Get group perms
*
@ -2169,9 +2231,8 @@ class Aauth
$groupModel = $this->getModel('Group');
$groupModel->select('id, name, definition');
$groupModel->join($this->config->dbTableGroupToUser, $this->config->dbTableGroups . '.id = ' . $this->config->dbTableGroupToUser . '.group_id');
$groupModel->where($this->config->dbTableGroupToUser . '.user_id', $userId);
$groupModel->select('id, name, definition, IF(user_id = ' . $userId . ', 1, 0) as member');
$groupModel->join($this->config->dbTableGroupToUser, $this->config->dbTableGroups . '.id = ' . $this->config->dbTableGroupToUser . '.group_id AND ' . $this->config->dbTableGroupToUser . '.user_id = ' . $userId, 'left');
return $groupModel->get()->getResult('array');
}
@ -2203,9 +2264,8 @@ class Aauth
$groupModel = $this->getModel('Group');
$groupModel->select('id, name, definition');
$groupModel->join($this->config->dbTableGroupToUser, $this->config->dbTableGroups . '.id = ' . $this->config->dbTableGroupToUser . '.group_id');
$groupModel->where($this->config->dbTableGroupToUser . '.user_id', $userId);
$groupModel->select('id, name, definition, IF(user_id = ' . $userId . ', 1, 0) as member');
$groupModel->join($this->config->dbTableGroupToUser, $this->config->dbTableGroups . '.id = ' . $this->config->dbTableGroupToUser . '.group_id AND ' . $this->config->dbTableGroupToUser . '.user_id = ' . $userId, 'left');
if (! is_null($orderBy))
{
@ -2221,16 +2281,16 @@ class Aauth
/**
* Set Group Variable as key value
*
* if variable not set before, it will be set
* If variable not set before, it will be set
* if set, overwrites the value
*
* @param string $key
* @param string $value
* @param integer $groupPar Group name or id
* @param string $key Group Variable Key
* @param string $value Group Variable Value
* @param integer|string $groupPar Group id or name to remove
*
* @return boolean
*/
public function setGroupVar(string $key, string $value, string $groupPar)
public function setGroupVar(string $key, string $value, $groupPar)
{
$groupModel = $this->getModel('Group');
@ -2247,12 +2307,12 @@ class Aauth
/**
* Unset Group Variable as key value
*
* @param string $key
* @param integer $groupPar Group name or id
* @param string $key Group Variable Key
* @param integer|string $groupPar Group id or name to remove
*
* @return boolean
*/
public function unsetGroupVar(string $key, string $groupPar)
public function unsetGroupVar(string $key, $groupPar)
{
$groupModel = $this->getModel('Group');
@ -2269,8 +2329,8 @@ class Aauth
/**
* Get Group Variable by key
*
* @param string $key Variable Key
* @param integer $groupPar Group name or id
* @param string $key Variable Key
* @param string $groupPar Group name or id
*
* @return boolean|string
*/
@ -2294,15 +2354,15 @@ class Aauth
}
/**
* Get Group Variables by group id
* List Group Variables by group id
*
* Return array with all group keys & variables
*
* @param integer $groupPar Group name or id
* @param string $groupPar Group name or id
*
* @return array
*/
public function getGroupVars(string $groupPar = null)
public function listGroupVars(string $groupPar = null)
{
$groupModel = $this->getModel('Group');
@ -2317,15 +2377,15 @@ class Aauth
}
/**
* List Group Variable Keys by GroupId
* Get Group Variable Keys by GroupId
*
* Return array of variable keys or false
*
* @param integer $groupPar Group name or id
* @param string $groupPar Group name or id
*
* @return boolean|array
*/
public function listGroupVarKeys(string $groupPar = null)
public function getGroupVarKeys(string $groupPar = null)
{
$groupModel = $this->getModel('Group');
@ -2453,11 +2513,9 @@ class Aauth
if ($permModel->transStatus() === false)
{
// @codeCoverageIgnoreStart
$permModel->transRollback();
return false;
// @codeCoverageIgnoreEnd
}
else
{
@ -2766,9 +2824,8 @@ class Aauth
$permModel = $this->getModel('Perm');
$permModel->select('id, name, definition, state');
$permModel->join($this->config->dbTablePermToGroup, $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToGroup . '.perm_id');
$permModel->where($this->config->dbTablePermToGroup . '.group_id', $groupId);
$permModel->select('id, name, definition, IF(state = 0 OR state = 1, state, -1) as state');
$permModel->join($this->config->dbTablePermToGroup, '(' . $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToGroup . '.perm_id AND ' . $this->config->dbTablePermToGroup . '.group_id = ' . $groupId . ')', 'left');
return $permModel->get()->getResult('array');
}
@ -2778,14 +2835,13 @@ class Aauth
*
* Return users as an object array
*
* @param integer $limit Limit of users to be returned
* @param integer $offset Offset for limited number of users
* @param boolean $includeBanneds Include banned users
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
* @param integer|string $groupPar Group id or name to get
* @param integer $limit Limit of users to be returned
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
*
* @return boolean|array
*/
public function listGroupPermsPaginated(string $groupPar, int $limit = 10, string $orderBy = null)
public function listGroupPermsPaginated($groupPar, int $limit = 10, string $orderBy = null)
{
if (! $groupId = $this->getGroupId($groupPar))
{
@ -2794,9 +2850,8 @@ class Aauth
$permModel = $this->getModel('Perm');
$permModel->select('id, name, definition, state');
$permModel->join($this->config->dbTablePermToGroup, $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToGroup . '.perm_id');
$permModel->where($this->config->dbTablePermToGroup . '.group_id', $groupId);
$permModel->select('id, name, definition, IF(state = 0 OR state = 1, state, -1) as state');
$permModel->join($this->config->dbTablePermToGroup, '(' . $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToGroup . '.perm_id AND ' . $this->config->dbTablePermToGroup . '.group_id = ' . $groupId . ')', 'left');
if (! is_null($orderBy))
{
@ -2812,7 +2867,7 @@ class Aauth
/**
* List user permissions
*
* @param integer|string $groupPar Group id or name to get
* @param integer $userId User id
*
* @return boolean|array
*/
@ -2830,7 +2885,7 @@ class Aauth
$permModel = $this->getModel('Perm');
$permModel->select('id, name, definition, state');
$permModel->select('id, name, definition, IF(state = 0 OR state = 1, state, -1) as state');
$permModel->join($this->config->dbTablePermToUser, '(' . $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToUser . '.perm_id AND ' . $this->config->dbTablePermToUser . '.user_id = ' . $userId . ')', 'left');
return $permModel->get()->getResult('array');
@ -2841,10 +2896,9 @@ class Aauth
*
* Return users as an object array
*
* @param integer $limit Limit of users to be returned
* @param integer $offset Offset for limited number of users
* @param boolean $includeBanneds Include banned users
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
* @param integer $userId User id
* @param integer $limit Limit of users to be returned
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
*
* @return boolean|array
*/
@ -2862,9 +2916,8 @@ class Aauth
$permModel = $this->getModel('Perm');
$permModel->select('id, name, definition, state');
$permModel->join($this->config->dbTablePermToUser, $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToUser . '.perm_id');
$permModel->where($this->config->dbTablePermToUser . '.user_id', $userId);
$permModel->select('id, name, definition, IF(state = 0 OR state = 1, state, -1) as state');
$permModel->join($this->config->dbTablePermToUser, '(' . $this->config->dbTablePerms . '.id = ' . $this->config->dbTablePermToUser . '.perm_id AND ' . $this->config->dbTablePermToUser . '.user_id = ' . $userId . ')', 'left');
if (! is_null($orderBy))
{
@ -3147,12 +3200,12 @@ class Aauth
* Provides direct access to method in the builder (if available)
* and the database connection.
*
* @param string $name
* @param array $params
* @param string $name Call name
* @param array $params Call params
*
* @return Model|null
*/
public function __call($name, array $params)
public function __call(string $name, array $params)
{
$config = $this->config;
$session = $this->session;

18
app/Libraries/Aauth/CAPTCHA.php

@ -8,12 +8,12 @@
* access management, public access etc..
*
* @package CodeIgniter-Aauth
* @since 3.0.0
* @author Emre Akay
* @author Raphael "REJack" Jackstadt
* @copyright 2014-2019 Emre Akay
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/emreakay/CodeIgniter-Aauth
* @since 3.0.0
*/
namespace App\Libraries\Aauth;
@ -53,7 +53,7 @@ class CAPTCHA extends \App\Libraries\Aauth
if ($this->config->captchaType === 'recaptcha')
{
$siteUrl = 'https://www.google.com/recaptcha/api/siteverify';
$request = $this->_submitGet(
$request = $this->submitGetCaptcha(
$siteUrl,
[
'secret' => $this->config->captchaSecret,
@ -65,7 +65,7 @@ class CAPTCHA extends \App\Libraries\Aauth
else if ($this->config->captchaType === 'hcaptcha')
{
$siteUrl = 'https://hcaptcha.com/siteverify';
$request = $this->_submitPost(
$request = $this->submitPostCaptcha(
$siteUrl,
[
'secret' => $this->config->captchaSecret,
@ -107,12 +107,12 @@ class CAPTCHA extends \App\Libraries\Aauth
if ($this->config->captchaType === 'recaptcha')
{
$content = "<div class='g-recaptcha' data-sitekey='{$siteKey}'></div>";
$content = '<div class="g-recaptcha" data-sitekey="' . $siteKey . '"></div>';
$content .= '<script src="https://www.google.com/recaptcha/api.js" async defer></script>';
}
else if ($this->config->captchaType === 'hcaptcha')
{
$content = "<div class='h-captcha' data-sitekey='{$siteKey}'></div>";
$content = '<div class="h-recaptcha" data-sitekey="' . $siteKey . '"></div>';
$content .= '<script src="https://hcaptcha.com/1/api.js" async defer></script>';
}
}
@ -133,7 +133,7 @@ class CAPTCHA extends \App\Libraries\Aauth
}
/**
* Submit GET
* Submit GET CAPTCHA
*
* Submits an HTTP GET to a CAPTCHA server.
*
@ -142,7 +142,7 @@ class CAPTCHA extends \App\Libraries\Aauth
*
* @return string
*/
private function _submitGet(string $url, array $data)
private function submitGetCaptcha(string $url, array $data)
{
$client = \Config\Services::curlrequest();
$response = $client->request('GET', $url, [
@ -153,7 +153,7 @@ class CAPTCHA extends \App\Libraries\Aauth
}
/**
* Submit POST
* Submit POST CAPTCHA
*
* Submits an HTTP POST to a CAPTCHA server.
*
@ -162,7 +162,7 @@ class CAPTCHA extends \App\Libraries\Aauth
*
* @return string
*/
private function _submitPost(string $url, array $data)
private function submitPostCaptcha(string $url, array $data)
{
$client = \Config\Services::curlrequest();
$response = $client->request('POST', $url, [

9
app/Libraries/Aauth/TOTP.php

@ -8,12 +8,12 @@
* access management, public access etc..
*
* @package CodeIgniter-Aauth
* @since 3.0.0
* @author Emre Akay
* @author Raphael "REJack" Jackstadt
* @copyright 2014-2019 Emre Akay
* @license https://opensource.org/licenses/MIT MIT License
* @link https://github.com/emreakay/CodeIgniter-Aauth
* @since 3.0.0
*/
namespace App\Libraries\Aauth;
@ -64,7 +64,12 @@ class TOTP extends \App\Libraries\Aauth
{
$secret = OTPHP_TOTP::create();
if ($secret->getSecret() !== $userVariableModel->where(['data_key' => 'totp_secret', 'data_value' => $secret->getSecret(), 'system' => 1])->getFirstRow('array'))
$where = [
'data_key' => 'totp_secret',
'data_value' => $secret->getSecret(),
'system' => 1,
];
if ($secret->getSecret() !== $userVariableModel->where($where)->getFirstRow('array'))
{
$endSecret = $secret->getSecret();
}

66
tests/Aauth/Database/GroupToGroupModelTest.php

@ -24,86 +24,86 @@ class GroupToGroupModelTest extends CIDatabaseTestCase
public function testInsert()
{
$groupToGroup = $this->model->insert(99, 99);
$groupToGroup = $this->model->insert(1, 2);
$this->seeInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
}
public function testExists()
{
$this->assertFalse($this->model->exists(99, 99));
$this->assertFalse($this->model->exists(1, 2));
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$this->assertTrue($this->model->exists(99, 99));
$this->assertTrue($this->model->exists(1, 2));
}
public function testFindAllBySubgroupId()
{
$this->assertCount(0, $this->model->findAllBySubgroupId(99));
$this->assertCount(0, $this->model->findAllBySubgroupId(2));
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$this->assertCount(1, $this->model->findAllBySubgroupId(99));
$this->assertCount(1, $this->model->findAllBySubgroupId(2));
}
public function testFindAllByGroupId()
{
$this->assertCount(0, $this->model->findAllByGroupId(99));
$this->assertCount(0, $this->model->findAllByGroupId(1));
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$this->assertCount(1, $this->model->findAllByGroupId(99));
$this->assertCount(1, $this->model->findAllByGroupId(1));
}
public function testDelete()
{
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$criteria = [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTableGroupToGroup, $criteria);
$this->model->delete(99, 99);
$this->model->delete(1, 2);
$this->seeNumRecords(0, $this->config->dbTableGroupToGroup, $criteria);
}
public function testDeleteAllByGroupId()
{
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$criteria = [
'group_id' => 99,
'group_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTableGroupToGroup, $criteria);
$this->model->deleteAllByGroupId(99);
$this->model->deleteAllByGroupId(1);
$this->seeNumRecords(0, $this->config->dbTableGroupToGroup, $criteria);
}
public function testDeleteAllBySubgroupId()
{
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$criteria = [
'subgroup_id' => 99,
'subgroup_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTableGroupToGroup, $criteria);
$this->model->deleteAllBySubgroupId(99);
$this->model->deleteAllBySubgroupId(2);
$this->seeNumRecords(0, $this->config->dbTableGroupToGroup, $criteria);
}
@ -111,14 +111,14 @@ class GroupToGroupModelTest extends CIDatabaseTestCase
{
$this->model = new GroupToGroupModel();
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 99,
'subgroup_id' => 99,
'group_id' => 1,
'subgroup_id' => 2,
]);
$this->hasInDatabase($this->config->dbTableGroupToGroup, [
'group_id' => 98,
'subgroup_id' => 99,
'group_id' => 2,
'subgroup_id' => 2,
]);
$groupsToGroup = $this->model->findAllBySubgroupId(99);
$groupsToGroup = $this->model->findAllBySubgroupId(2);
$this->assertCount(2, $groupsToGroup);
}
}

70
tests/Aauth/Database/GroupToUserModelTest.php

@ -24,86 +24,86 @@ class GroupToUserModelTest extends CIDatabaseTestCase
public function testInsert()
{
$groupToGroup = $this->model->insert(99, 99);
$groupToGroup = $this->model->insert(1, 2);
$this->seeInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
}
public function testExists()
{
$this->assertFalse($this->model->exists(99, 99));
$this->assertFalse($this->model->exists(1, 2));
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$this->assertTrue($this->model->exists(99, 99));
$this->assertTrue($this->model->exists(1, 2));
}
public function testFindAllByUserId()
{
$this->assertCount(0, $this->model->findAllByUserId(99));
$this->assertCount(1, $this->model->findAllByUserId(2));
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$this->assertCount(1, $this->model->findAllByUserId(99));
$this->assertCount(2, $this->model->findAllByUserId(2));
}
public function testFindAllByGroupId()
{
$this->assertCount(0, $this->model->findAllByGroupId(99));
$this->assertCount(1, $this->model->findAllByGroupId(1));
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$this->assertCount(1, $this->model->findAllByGroupId(99));
$this->assertCount(2, $this->model->findAllByGroupId(1));
}
public function testDelete()
{
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$criteria = [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTableGroupToUser, $criteria);
$this->model->delete(99, 99);
$this->model->delete(1, 2);
$this->seeNumRecords(0, $this->config->dbTableGroupToUser, $criteria);
}
public function testDeleteAllByGroupId()
{
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$criteria = [
'group_id' => 99,
'group_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTableGroupToUser, $criteria);
$this->model->deleteAllByGroupId(99);
$this->seeNumRecords(2, $this->config->dbTableGroupToUser, $criteria);
$this->model->deleteAllByGroupId(1);
$this->seeNumRecords(0, $this->config->dbTableGroupToUser, $criteria);
}
public function testDeleteAllByUserId()
{
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$criteria = [
'user_id' => 99,
'user_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTableGroupToUser, $criteria);
$this->model->deleteAllByUserId(99);
$this->seeNumRecords(2, $this->config->dbTableGroupToUser, $criteria);
$this->model->deleteAllByUserId(2);
$this->seeNumRecords(0, $this->config->dbTableGroupToUser, $criteria);
}
@ -111,14 +111,10 @@ class GroupToUserModelTest extends CIDatabaseTestCase
{
$this->model = new GroupToUserModel();
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 99,
'user_id' => 99,
]);
$this->hasInDatabase($this->config->dbTableGroupToUser, [
'group_id' => 98,
'user_id' => 99,
'group_id' => 1,
'user_id' => 2,
]);
$groupToUsers = $this->model->findAllByUserId(99);
$groupToUsers = $this->model->findAllByUserId(2);
$this->assertCount(2, $groupToUsers);
}
}

38
tests/Aauth/Database/GroupVariableModelTest.php

@ -24,41 +24,41 @@ class GroupVariableModelTest extends CIDatabaseTestCase
public function testFind()
{
$groupVariable = $this->model->find(99, 'test');
$groupVariable = $this->model->find(1, 'test');
$this->assertFalse($groupVariable);
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$groupVariable = $this->model->find(99, 'test');
$groupVariable = $this->model->find(1, 'test');
$this->assertEquals('TRUE', $groupVariable);
}
public function testFindAll()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$groupVariables = $this->model->findAll(99);
$groupVariables = $this->model->findAll(1);
$this->assertCount(1, $groupVariables);
}
public function testSave()
{
$this->model->save(99, 'test', 'TRUE');
$this->model->save(1, 'test', 'TRUE');
$this->seeInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$this->model->save(99, 'test', 'TRUE2');
$this->model->save(1, 'test', 'TRUE2');
$this->seeInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE2',
]);
@ -67,37 +67,37 @@ class GroupVariableModelTest extends CIDatabaseTestCase
public function testDelete()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$criteria = [
'group_id' => 99,
'group_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTableGroupVariables, $criteria);
$this->model->delete(99, 'test');
$this->model->delete(1, 'test');
$this->seeNumRecords(0, $this->config->dbTableGroupVariables, $criteria);
}
public function testDeleteAllByGroupId()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$criteria = [
'group_id' => 99,
'group_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTableGroupVariables, $criteria);
$this->model->deleteAllByGroupId(99);
$this->model->deleteAllByGroupId(1);
$this->seeNumRecords(0, $this->config->dbTableGroupVariables, $criteria);
}
public function testAsArrayFirst()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
@ -108,7 +108,7 @@ class GroupVariableModelTest extends CIDatabaseTestCase
public function testAsObjectFirst()
{
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
@ -120,7 +120,7 @@ class GroupVariableModelTest extends CIDatabaseTestCase
{
$this->model = new GroupVariableModel();
$this->hasInDatabase($this->config->dbTableGroupVariables, [
'group_id' => 99,
'group_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
@ -135,7 +135,7 @@ class GroupVariableModelTest extends CIDatabaseTestCase
public function testDBCall()
{
$this->model->save(99, 'test', 'TRUE');
$this->model->save(1, 'test', 'TRUE');
$this->assertEquals(1, $this->model->insertID());
}

26
tests/Aauth/Database/LoginTokenModelTest.php

@ -22,41 +22,41 @@ class LoginTokenModelTest extends CIDatabaseTestCase
public function testInsert()
{
$this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']);
$loginTokens = $this->model->findAllByUserId(99);
$this->model->insert(['user_id' => 1, 'random_hash' => 'random_hash11']);
$loginTokens = $this->model->findAllByUserId(1);
$this->assertCount(1, $loginTokens);
}
public function testUpdate()
{
$this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']);
$oldLoginTokens = $this->model->findAllByUserId(99);
$this->model->insert(['user_id' => 1, 'random_hash' => 'random_hash11']);
$oldLoginTokens = $this->model->findAllByUserId(1);
sleep(2);
$this->model->update($oldLoginTokens[0]['id']);
$loginTokens = $this->model->findAllByUserId(99);
$loginTokens = $this->model->findAllByUserId(1);
$this->assertNotEquals($oldLoginTokens[0]['expires_at'], $loginTokens[0]['expires_at']);
}
public function testDeleteExpired()
{
$this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']);
$this->model->insert(['user_id' => 1, 'random_hash' => 'random_hash11']);
sleep(2);
$this->model->deleteExpired(99);
$this->assertCount(0, $this->model->findAllByUserId(99));
$this->model->deleteExpired(1);
$this->assertCount(0, $this->model->findAllByUserId(1));
}
public function testDeleteAll()
{
$this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']);
$this->model->deleteAll(99);
$this->assertCount(0, $this->model->findAllByUserId(99));
$this->model->insert(['user_id' => 1, 'random_hash' => 'random_hash11']);
$this->model->deleteAll(1);
$this->assertCount(0, $this->model->findAllByUserId(1));
}
public function testConfigDBGroup()
{
$this->model = new LoginTokenModel();
$this->model->insert(['user_id' => 99, 'random_hash' => 'random_hash9999']);
$loginTokens = $this->model->findAllByUserId(99);
$this->model->insert(['user_id' => 1, 'random_hash' => 'random_hash11']);
$loginTokens = $this->model->findAllByUserId(1);
$this->assertCount(1, $loginTokens);
}
}

93
tests/Aauth/Database/PermToGroupModelTest.php

@ -16,120 +16,127 @@ class PermToGroupModelTest extends CIDatabaseTestCase
{
parent::setUp();
$this->model = new PermToGroupModel($this->db);
$this->config = new AauthConfig();
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->model = new PermToGroupModel($this->db);
}
//--------------------------------------------------------------------
public function testSave()
{
$this->model->save(99, 99, 1);
$this->model->save(1, 2, 1);
$this->seeInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
'state' => 1,
]);
$this->model->save(99, 99, 0);
$this->model->save(1, 2, 0);
$this->seeInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
'state' => 0,
]);
}
public function testAllowed()
{
$this->assertFalse($this->model->allowed(99, 99));
$this->assertFalse($this->model->allowed(1, 2));
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
'state' => 1,
]);
$this->assertTrue($this->model->allowed(99, 99));
$this->assertTrue($this->model->allowed(1, 2));
}
public function testDenied()
{
$this->assertFalse($this->model->denied(99, 99));
$this->assertFalse($this->model->denied(1, 2));
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
'state' => 0,
]);
$this->assertTrue($this->model->denied(99, 99));
$this->assertTrue($this->model->denied(1, 2));
}
public function testFindAllByGroupId()
{
$permsToGroup = $this->model->findAllByGroupId(99);
$permsToGroup = $this->model->findAllByGroupId(2);
$this->assertCount(0, $permsToGroup);
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
'state' => 1,
]);
$permsToGroup = $this->model->findAllByGroupId(99, 1);
$permsToGroup = $this->model->findAllByGroupId(2, 1);
$this->assertCount(1, $permsToGroup);
$permsToGroup = $this->model->findAllByGroupId(99, 0);
$permsToGroup = $this->model->findAllByGroupId(2, 0);
$this->assertCount(0, $permsToGroup);
}
public function testFindAllByPermId()
{
$permToGroups = $this->model->findAllByPermId(99);
$permToGroups = $this->model->findAllByPermId(1);
$this->assertCount(0, $permToGroups);
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
]);
$permToGroups = $this->model->findAllByPermId(99);
$permToGroups = $this->model->findAllByPermId(1);
$this->assertCount(1, $permToGroups);
}
public function testDelete()
{
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
]);
$criteria = [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTablePermToGroup, $criteria);
$this->model->delete(99, 99);
$this->model->delete(1, 2);
$this->seeNumRecords(0, $this->config->dbTablePermToGroup, $criteria);
}
public function testDeleteAllByPermId()
{
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
]);
$criteria = [
'perm_id' => 99,
'perm_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTablePermToGroup, $criteria);
$this->model->deleteAllByPermId(99);
$this->model->deleteAllByPermId(1);
$this->seeNumRecords(0, $this->config->dbTablePermToGroup, $criteria);
}
public function testDeleteAllByGroupId()
{
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
]);
$criteria = [
'group_id' => 99,
'group_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTablePermToGroup, $criteria);
$this->model->deleteAllByGroupId(99);
$this->model->deleteAllByGroupId(2);
$this->seeNumRecords(0, $this->config->dbTablePermToGroup, $criteria);
}
@ -137,14 +144,10 @@ class PermToGroupModelTest extends CIDatabaseTestCase
{
$this->model = new PermToGroupModel();
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 99,
'group_id' => 99,
'perm_id' => 1,
'group_id' => 2,
]);
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 98,
'group_id' => 99,
]);
$permsToGroup = $this->model->findAllByGroupId(99);
$this->assertCount(2, $permsToGroup);
$permsToGroup = $this->model->findAllByGroupId(2);
$this->assertCount(1, $permsToGroup);
}
}

93
tests/Aauth/Database/PermToUserModelTest.php

@ -16,120 +16,127 @@ class PermToUserModelTest extends CIDatabaseTestCase
{
parent::setUp();
$this->model = new PermToUserModel($this->db);
$this->config = new AauthConfig();
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->model = new PermToUserModel($this->db);
}
//--------------------------------------------------------------------
public function testSave()
{
$this->model->save(99, 99, 1);
$this->model->save(1, 2, 1);
$this->seeInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
'state' => 1,
]);
$this->model->save(99, 99, 0);
$this->model->save(1, 2, 0);
$this->seeInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
'state' => 0,
]);
}
public function testAllowed()
{
$this->assertFalse($this->model->allowed(99, 99));
$this->assertFalse($this->model->allowed(1, 2));
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
'state' => 1,
]);
$this->assertTrue($this->model->allowed(99, 99));
$this->assertTrue($this->model->allowed(1, 2));
}
public function testDenied()
{
$this->assertFalse($this->model->denied(99, 99));
$this->assertFalse($this->model->denied(1, 2));
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
'state' => 0,
]);
$this->assertTrue($this->model->denied(99, 99));
$this->assertTrue($this->model->denied(1, 2));
}
public function testFindAllByUserId()
{
$permsToUser = $this->model->findAllByUserId(99);
$permsToUser = $this->model->findAllByUserId(2);
$this->assertCount(0, $permsToUser);
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
'state' => 1,
]);
$permsToUser = $this->model->findAllByUserId(99, 1);
$permsToUser = $this->model->findAllByUserId(2, 1);
$this->assertCount(1, $permsToUser);
$permsToUser = $this->model->findAllByUserId(99, 0);
$permsToUser = $this->model->findAllByUserId(2, 0);
$this->assertCount(0, $permsToUser);
}
public function testFindAllByPermId()
{
$permToUsers = $this->model->findAllByPermId(99);
$permToUsers = $this->model->findAllByPermId(1);
$this->assertCount(0, $permToUsers);
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
]);
$permToUsers = $this->model->findAllByPermId(99);
$permToUsers = $this->model->findAllByPermId(1);
$this->assertCount(1, $permToUsers);
}
public function testDelete()
{
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
]);
$criteria = [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTablePermToUser, $criteria);
$this->model->delete(99, 99);
$this->model->delete(1, 2);
$this->seeNumRecords(0, $this->config->dbTablePermToUser, $criteria);
}
public function testDeleteAllByPermId()
{
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
]);
$criteria = [
'perm_id' => 99,
'perm_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTablePermToUser, $criteria);
$this->model->deleteAllByPermId(99);
$this->model->deleteAllByPermId(1);
$this->seeNumRecords(0, $this->config->dbTablePermToUser, $criteria);
}
public function testDeleteAllByUserId()
{
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
]);
$criteria = [
'user_id' => 99,
'user_id' => 2,
];
$this->seeNumRecords(1, $this->config->dbTablePermToUser, $criteria);
$this->model->deleteAllByUserId(99);
$this->model->deleteAllByUserId(2);
$this->seeNumRecords(0, $this->config->dbTablePermToUser, $criteria);
}
@ -137,14 +144,10 @@ class PermToUserModelTest extends CIDatabaseTestCase
{
$this->model = new PermToUserModel();
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 99,
'user_id' => 99,
'perm_id' => 1,
'user_id' => 2,
]);
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 98,
'user_id' => 99,
]);
$permsToUser = $this->model->findAllByUserId(99);
$this->assertCount(2, $permsToUser);
$permsToUser = $this->model->findAllByUserId(2);
$this->assertCount(1, $permsToUser);
}
}

38
tests/Aauth/Database/UserVariableModelTest.php

@ -24,41 +24,41 @@ class UserVariableModelTest extends CIDatabaseTestCase
public function testFind()
{
$userVariable = $this->model->find(99, 'test');
$userVariable = $this->model->find(1, 'test');
$this->assertFalse($userVariable);
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$userVariable = $this->model->find(99, 'test');
$userVariable = $this->model->find(1, 'test');
$this->assertEquals('TRUE', $userVariable);
}
public function testFindAll()
{
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$userVariables = $this->model->findAll(99);
$userVariables = $this->model->findAll(1);
$this->assertCount(1, $userVariables);
}
public function testSave()
{
$this->model->save(99, 'test', 'TRUE');
$this->model->save(1, 'test', 'TRUE');
$this->seeInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$this->model->save(99, 'test', 'TRUE2');
$this->model->save(1, 'test', 'TRUE2');
$this->seeInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE2',
]);
@ -67,37 +67,37 @@ class UserVariableModelTest extends CIDatabaseTestCase
public function testDelete()
{
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$criteria = [
'user_id' => 99,
'user_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTableUserVariables, $criteria);
$this->model->delete(99, 'test');
$this->model->delete(1, 'test');
$this->seeNumRecords(0, $this->config->dbTableUserVariables, $criteria);
}
public function testDeleteAllByUserId()
{
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
$criteria = [
'user_id' => 99,
'user_id' => 1,
];
$this->seeNumRecords(1, $this->config->dbTableUserVariables, $criteria);
$this->model->deleteAllByUserId(99);
$this->model->deleteAllByUserId(1);
$this->seeNumRecords(0, $this->config->dbTableUserVariables, $criteria);
}
public function testAsArrayFirst()
{
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
@ -108,7 +108,7 @@ class UserVariableModelTest extends CIDatabaseTestCase
public function testAsObjectFirst()
{
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
@ -120,7 +120,7 @@ class UserVariableModelTest extends CIDatabaseTestCase
{
$this->model = new UserVariableModel();
$this->hasInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 1,
'data_key' => 'test',
'data_value' => 'TRUE',
]);
@ -135,7 +135,7 @@ class UserVariableModelTest extends CIDatabaseTestCase
public function testDBCall()
{
$this->model->save(99, 'test', 'TRUE');
$this->model->save(1, 'test', 'TRUE');
$this->assertEquals(1, $this->model->insertID());
}

64
tests/Aauth/Libraries/Aauth/AccessTest.php

@ -14,6 +14,10 @@ use CodeIgniter\Session\Handlers\FileHandler;
use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class AccessTest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -31,7 +35,7 @@ class AccessTest extends CIDatabaseTestCase
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
Services::injectMock('request', $this->request);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$_COOKIE = [];
$_SESSION = [];
}
@ -67,10 +71,6 @@ class AccessTest extends CIDatabaseTestCase
//--------------------------------------------------------------------
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsLoggedIn()
{
$session = $this->getInstance();
@ -82,10 +82,6 @@ class AccessTest extends CIDatabaseTestCase
$session->remove('user');
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsMember()
{
$config = new AauthConfig();
@ -103,10 +99,6 @@ class AccessTest extends CIDatabaseTestCase
$session->remove('user');
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsAdmin()
{
$this->assertTrue($this->library->isAdmin(1));
@ -121,18 +113,16 @@ class AccessTest extends CIDatabaseTestCase
$session->remove('user');
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsAllowed()
{
$config = new AauthConfig();
$this->hasInDatabase($config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->isAllowed('testPerm1', 1));
$this->assertFalse($this->library->isAllowed('testPerm1', 2));
@ -142,7 +132,7 @@ class AccessTest extends CIDatabaseTestCase
'group_id' => 2,
'state' => 1,
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->isAllowed('testPerm1', 2));
$this->hasInDatabase($config->dbTablePermToUser, [
@ -152,6 +142,14 @@ class AccessTest extends CIDatabaseTestCase
]);
$this->assertTrue($this->library->isAllowed('testPerm1', 2));
$this->db->table($config->dbTablePermToUser)->delete(['perm_id' => 1, 'user_id' => 2]);
$this->hasInDatabase($config->dbTablePermToUser, [
'perm_id' => 1,
'user_id' => 2,
'state' => 0,
]);
$this->assertFalse($this->library->isAllowed('testPerm1', 2));
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$session->set('user', [
@ -178,10 +176,6 @@ class AccessTest extends CIDatabaseTestCase
$this->assertFalse($this->library->isAllowed('testPerm1', 99));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testControl()
{
$config = new AauthConfig();
@ -234,10 +228,6 @@ class AccessTest extends CIDatabaseTestCase
$this->assertTrue($this->library->control() instanceof \Tests\Support\HTTP\MockResponse);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testControlErrorNoPerm($value = '')
{
$session = $this->getInstance();
@ -250,10 +240,6 @@ class AccessTest extends CIDatabaseTestCase
$this->assertFalse($this->library->control());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testControlErrorPermDenied($value = '')
{
$session = $this->getInstance();
@ -272,10 +258,6 @@ class AccessTest extends CIDatabaseTestCase
$this->assertFalse($this->library->control('testPerm1'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsGroupAllowed()
{
$config = new AauthConfig();
@ -318,16 +300,20 @@ class AccessTest extends CIDatabaseTestCase
$this->assertTrue($this->library->isGroupAllowed('testPerm1'));
$session->remove('user');
$this->db->table($config->dbTablePermToGroup)->delete(['perm_id' => 1, 'group_id' => 2]);
$this->hasInDatabase($config->dbTablePermToGroup, [
'perm_id' => 1,
'group_id' => 2,
'state' => 0,
]);
$this->assertFalse($this->library->isGroupAllowed('testPerm1', 2));
$this->assertFalse($this->library->isGroupAllowed('testPerm1'));
$this->assertFalse($this->library->isGroupAllowed('testPerm1', 3));
$this->assertFalse($this->library->isGroupAllowed('testPerm99', 2));
$this->assertFalse($this->library->isGroupAllowed('testPerm1', 99));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsGroupAllowedSubgroup()
{
$config = new AauthConfig();
@ -351,7 +337,7 @@ class AccessTest extends CIDatabaseTestCase
'state' => 1,
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->isGroupAllowed('testPerm1', 2));
}
}

18
tests/Aauth/Libraries/Aauth/CAPTCHATest.php

@ -16,6 +16,10 @@ use App\Libraries\Aauth;
use App\Models\Aauth\UserVariableModel;
use App\Models\Aauth\LoginTokenModel;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class CAPTCHATest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -33,7 +37,7 @@ class CAPTCHATest extends CIDatabaseTestCase
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
Services::injectMock('request', $this->request);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$_COOKIE = [];
$_SESSION = [];
}
@ -73,7 +77,7 @@ class CAPTCHATest extends CIDatabaseTestCase
{
$config = new AauthConfig();
$config->captchaEnabled = true;
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertEquals('', $this->library->generateCaptchaHtml());
@ -89,7 +93,7 @@ class CAPTCHATest extends CIDatabaseTestCase
$this->assertContains('https://www.google.com/recaptcha', $this->library->generateCaptchaHtml());
$config->captchaType = 'hcaptcha';
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$_POST['h-recaptcha-response'] = '0123456789';
$this->library->login('admina@example.com', 'password123456');
$this->assertEquals(lang('Aauth.invalidCaptcha'), $this->library->getErrorsArray()[0]);
@ -100,17 +104,17 @@ class CAPTCHATest extends CIDatabaseTestCase
{
$config = new AauthConfig();
$config->captchaEnabled = true;
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertContains('missing-input', $this->library->verifyCaptchaResponse(null)['errorCodes']);
$this->assertContains('missing-input', $this->library->verifyCaptchaResponse('')['errorCodes']);
$this->assertContains('invalid-input-response', $this->library->verifyCaptchaResponse('0123456789')['errorCodes']);
$config->captchaType = 'hcaptcha';
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertContains('invalid-input-response', $this->library->verifyCaptchaResponse('0123456789')['errorCodes']);
$config->captchaType = 'hcaptcha';
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertTrue($this->library->verifyCaptchaResponse('testing')['success']);
}
}

18
tests/Aauth/Libraries/Aauth/ErrorsTest.php

@ -7,13 +7,17 @@ use Tests\Support\Session\MockSession;
use CodeIgniter\Session\Handlers\FileHandler;
use App\Libraries\Aauth;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class ErrorsTest extends \CIUnitTestCase
{
public function setUp()
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$_COOKIE = [];
$_SESSION = [];
}
@ -74,10 +78,6 @@ class ErrorsTest extends \CIUnitTestCase
$this->expectOutputString('test message 1<br />test message 2');
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testClearErrors()
{
$session = $this->getInstance();
@ -89,10 +89,6 @@ class ErrorsTest extends \CIUnitTestCase
$this->assertNull($session->getFlashdata('errors'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testErrorsFlash()
{
$session = $this->getInstance();
@ -108,10 +104,6 @@ class ErrorsTest extends \CIUnitTestCase
$this->assertEquals(['test message 1', 'test message 2'], $session->getFlashdata('errors'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testKeepErrors()
{
$session = $this->getInstance();

101
tests/Aauth/Libraries/Aauth/GroupTest.php

@ -14,6 +14,10 @@ use CodeIgniter\Session\Handlers\FileHandler;
use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class GroupTest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -32,7 +36,7 @@ class GroupTest extends CIDatabaseTestCase
Services::injectMock('request', $this->request);
$this->config = new AauthConfig();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$_COOKIE = [];
$_SESSION = [];
}
@ -79,7 +83,7 @@ class GroupTest extends CIDatabaseTestCase
$this->assertFalse($this->library->createGroup('admin'));
$this->assertEquals(lang('Aauth.existsAlreadyGroup'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createGroup(''));
$this->assertEquals(lang('Aauth.requiredGroupName'), $this->library->getErrorsArray()[0]);
}
@ -91,7 +95,7 @@ class GroupTest extends CIDatabaseTestCase
'name' => 'testGroup1',
'definition' => 'Test Group 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->library->updateGroup('testGroup1', 'testGroup1N', 'Test Group 1 New');
$this->seeInDatabase($this->config->dbTableGroups, [
'id' => 4,
@ -102,15 +106,15 @@ class GroupTest extends CIDatabaseTestCase
$this->assertFalse($this->library->updateGroup($this->config->groupAdmin, $this->config->groupDefault));
$this->assertEquals(lang('Aauth.existsAlreadyGroup'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->updateGroup($this->config->groupAdmin));
$this->assertCount(0, $this->library->getErrorsArray());
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateGroup(99, ''));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateGroup('testGroup99 ', ''));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
}
@ -122,7 +126,7 @@ class GroupTest extends CIDatabaseTestCase
'name' => 'testGroup1',
'definition' => 'Test Group 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->deleteGroup('testGroup1'));
$this->dontSeeInDatabase($this->config->dbTableGroups, [
'id' => 4,
@ -130,11 +134,11 @@ class GroupTest extends CIDatabaseTestCase
'definition' => 'Test Group 1 New',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->deleteGroup(99, ''));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->deleteGroup('testGroup99 ', ''));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
}
@ -147,15 +151,15 @@ class GroupTest extends CIDatabaseTestCase
'user_id' => 2,
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->addMember(2, 2));
$this->assertEquals(lang('Aauth.alreadyMemberGroup'), $this->library->getInfosArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->addMember(99, 2));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->addMember(2, 99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
}
@ -187,22 +191,23 @@ class GroupTest extends CIDatabaseTestCase
'definition' => 'Test Group 3',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup2'));
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup3'));
$this->assertFalse($this->library->addSubgroup('testGroup2', 'testGroup1'));
$this->assertFalse($this->library->addSubgroup('testGroup1', 'testGroup1'));
$this->library = new Aauth(null, true);
$this->assertTrue($this->library->addSubgroup(4, 5));
$this->assertEquals(lang('Aauth.alreadyMemberSubgroup'), $this->library->getInfosArray()[0]);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->addSubgroup(4, 5));
$this->assertFalse($this->library->addSubgroup(4, 4));
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->addSubgroup(4, 6));
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->addSubgroup(99, 1));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->addSubgroup(1, 99));
$this->assertEquals(lang('Aauth.notFoundSubgroup'), $this->library->getErrorsArray()[0]);
}
@ -261,31 +266,69 @@ class GroupTest extends CIDatabaseTestCase
$this->assertEquals($this->config->groupDefault, $groupsOrderBy['groups'][1]['name']);
}
public function testListGroupSubgroups()
{
$groups = $this->library->listGroupSubgroups(1);
$this->assertCount(3, $groups);
$this->assertEquals($this->config->groupAdmin, $groups[0]['name']);
$this->assertEquals($this->config->groupDefault, $groups[1]['name']);
$this->assertEquals(0, $groups[0]['subgroup']);
$this->assertEquals(0, $groups[1]['subgroup']);
$this->assertFalse($this->library->listGroupSubgroups(0));
}
public function testListGroupSubgroupsPaginated()
{
$groups = $this->library->listGroupSubgroupsPaginated(1);
$this->assertTrue(isset($groups['pager']));
$this->assertCount(3, $groups['groups']);
$this->assertEquals($this->config->groupAdmin, $groups['groups'][0]['name']);
$this->assertEquals($this->config->groupDefault, $groups['groups'][1]['name']);
$this->assertEquals(0, $groups['groups'][0]['subgroup']);
$this->assertEquals(0, $groups['groups'][1]['subgroup']);
$groupsOrderBy = $this->library->listGroupSubgroupsPaginated(1, 10, 'id DESC');
$this->assertEquals($this->config->groupPublic, $groupsOrderBy['groups'][0]['name']);
$this->assertEquals($this->config->groupDefault, $groupsOrderBy['groups'][1]['name']);
$this->assertEquals(0, $groupsOrderBy['groups'][0]['subgroup']);
$this->assertEquals(0, $groupsOrderBy['groups'][1]['subgroup']);
$this->assertFalse($this->library->listGroupSubgroupsPaginated(0));
}
public function testListUserGroups()
{
$groups = $this->library->listUserGroups(1);
$this->assertCount(2, $groups);
$this->assertCount(3, $groups);
$this->assertEquals($this->config->groupAdmin, $groups[0]['name']);
$this->assertEquals($this->config->groupDefault, $groups[1]['name']);
$this->assertEquals(1, $groups[1]['member']);
$this->assertEquals(0, $groups[2]['member']);
$this->assertFalse($this->library->listUserGroups(99));
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
$session->set('user', [
'id' => 1,
'loggedIn' => true,
]);
$groups = $this->library->listUserGroups();
$this->assertCount(3, $groups);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testListUserGroupsPaginated()
{
$groups = $this->library->listUserGroupsPaginated(1);
$this->assertTrue(isset($groups['pager']));
$this->assertCount(2, $groups['groups']);
$this->assertCount(3, $groups['groups']);
$this->assertEquals($this->config->groupAdmin, $groups['groups'][0]['name']);
$this->assertEquals($this->config->groupDefault, $groups['groups'][1]['name']);
$groupsOrderBy = $this->library->listUserGroupsPaginated(1, 10, 'id DESC');
$this->assertEquals($this->config->groupDefault, $groupsOrderBy['groups'][0]['name']);
$this->assertEquals($this->config->groupAdmin, $groupsOrderBy['groups'][1]['name']);
$this->assertEquals($this->config->groupDefault, $groupsOrderBy['groups'][1]['name']);
$this->assertEquals($this->config->groupAdmin, $groupsOrderBy['groups'][2]['name']);
$this->assertFalse($this->library->listUserGroupsPaginated(99));
@ -296,7 +339,7 @@ class GroupTest extends CIDatabaseTestCase
'loggedIn' => true,
]);
$groups = $this->library->listUserGroupsPaginated();
$this->assertCount(2, $groups['groups']);
$this->assertCount(3, $groups['groups']);
}
public function testGetGroupName()
@ -340,7 +383,7 @@ class GroupTest extends CIDatabaseTestCase
'definition' => 'Test Group 3',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup2'));
$this->assertTrue($this->library->addSubgroup('testGroup1', 'testGroup3'));

16
tests/Aauth/Libraries/Aauth/GroupVariablesTest.php

@ -10,6 +10,10 @@ use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth;
use App\Models\Aauth\GroupVariableModel;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class GroupVariablesTest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -22,7 +26,7 @@ class GroupVariablesTest extends CIDatabaseTestCase
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->config = new AauthConfig();
}
@ -81,9 +85,9 @@ class GroupVariablesTest extends CIDatabaseTestCase
'data_value' => 'test2',
]);
$this->assertCount(2, $this->library->getGroupVars(1));
$this->assertCount(2, $this->library->listGroupVars(1));
$this->assertFalse($this->library->getGroupVars(99));
$this->assertFalse($this->library->listGroupVars(99));
}
public function testListGroupVarKeys()
@ -99,9 +103,9 @@ class GroupVariablesTest extends CIDatabaseTestCase
'data_value' => 'test2',
]);
$this->assertCount(2, $this->library->listGroupVarKeys(1));
$this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->listGroupVarKeys(1));
$this->assertCount(2, $this->library->getGroupVarKeys(1));
$this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->getGroupVarKeys(1));
$this->assertFalse($this->library->listGroupVarKeys(99));
$this->assertFalse($this->library->getGroupVarKeys(99));
}
}

18
tests/Aauth/Libraries/Aauth/InfosTest.php

@ -7,13 +7,17 @@ use Tests\Support\Session\MockSession;
use CodeIgniter\Session\Handlers\FileHandler;
use App\Libraries\Aauth;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class InfosTest extends \CIUnitTestCase
{
public function setUp()
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$_COOKIE = [];
$_SESSION = [];
}
@ -74,10 +78,6 @@ class InfosTest extends \CIUnitTestCase
$this->expectOutputString('test message 1<br />test message 2');
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testClearInfos()
{
$session = $this->getInstance();
@ -89,10 +89,6 @@ class InfosTest extends \CIUnitTestCase
$this->assertNull($session->getFlashdata('infos'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testInfosFlash()
{
$session = $this->getInstance();
@ -108,10 +104,6 @@ class InfosTest extends \CIUnitTestCase
$this->assertEquals(['test message 1', 'test message 2'], $session->getFlashdata('infos'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testKeepInfos()
{
$session = $this->getInstance();

18
tests/Aauth/Libraries/Aauth/LoginTest.php

@ -42,6 +42,14 @@ class LoginTest extends CIDatabaseTestCase
$_SESSION = [];
}
public function invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
public function tearDown()
{
}
@ -177,7 +185,7 @@ class LoginTest extends CIDatabaseTestCase
$randomString = random_string('alnum', 32);
$selectorString = random_string('alnum', 16);
$_COOKIE['remember'] = base64_encode(1) . ';' . $randomString . ';' . $selectorString;
$_COOKIE[$config->loginRememberCookie] = base64_encode(1) . ';' . $randomString . ';' . $selectorString;
$this->hasInDatabase($config->dbTableLoginTokens, [
'user_id' => 1,
@ -189,7 +197,7 @@ class LoginTest extends CIDatabaseTestCase
$this->library->logout();
}
public function testIsLoggedInCookieInvalidUser($value = '')
public function testIsLoggedInCookieInvalidUser()
{
helper('text');
$session = $this->getInstance();
@ -198,16 +206,16 @@ class LoginTest extends CIDatabaseTestCase
$randomString = random_string('alnum', 32);
$selectorString = random_string('alnum', 16);
$this->hasInDatabase($config->dbTableLoginTokens, [
'user_id' => 99,
'user_id' => 1,
'random_hash' => password_hash($randomString, PASSWORD_DEFAULT),
'selector_hash' => password_hash($selectorString, PASSWORD_DEFAULT),
'expires_at' => date('Y-m-d H:i:s', strtotime('+1 week')),
]);
$_COOKIE['remember'] = base64_encode(99) . ';' . $randomString . ';' . $selectorString;
$_COOKIE['remember'] = base64_encode(2) . ';' . $randomString . ';' . $selectorString;
$this->assertFalse($this->library->isLoggedIn());
unset($_COOKIE['remember']);
unset($_COOKIE);
}
public function testIsLoggedInCookieInvalidCookie($value = '')

86
tests/Aauth/Libraries/Aauth/PermTest.php

@ -10,6 +10,10 @@ use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth;
use App\Models\Aauth\UserVariableModel;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class PermTest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -22,7 +26,7 @@ class PermTest extends CIDatabaseTestCase
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->config = new AauthConfig();
$_COOKIE = [];
$_SESSION = [];
@ -70,7 +74,7 @@ class PermTest extends CIDatabaseTestCase
$this->assertFalse($this->library->createPerm('testPerm1'));
$this->assertEquals(lang('Aauth.existsAlreadyPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createPerm(''));
$this->assertEquals(lang('Aauth.requiredPermName'), $this->library->getErrorsArray()[0]);
}
@ -87,7 +91,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm2',
'definition' => 'Test Perm 2',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->library->updatePerm('testPerm1', 'testPerm1N', 'Test Perm 1 New');
$this->seeInDatabase($this->config->dbTablePerms, [
'id' => 1,
@ -98,15 +102,15 @@ class PermTest extends CIDatabaseTestCase
$this->assertFalse($this->library->updatePerm('testPerm1N', 'testPerm2'));
$this->assertEquals(lang('Aauth.existsAlreadyPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->updatePerm('testPerm1'));
$this->assertCount(0, $this->library->getErrorsArray());
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updatePerm(99, ''));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updatePerm('testPerm99', ''));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
}
@ -117,7 +121,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->deletePerm('testPerm1'));
$this->dontSeeInDatabase($this->config->dbTablePerms, [
'name' => 'testPerm1',
@ -125,11 +129,11 @@ class PermTest extends CIDatabaseTestCase
'deleted' => 0,
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->deletePerm(99));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->deletePerm('testPerm99'));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
}
@ -207,10 +211,6 @@ class PermTest extends CIDatabaseTestCase
$this->assertFalse($this->library->getUserPerms(99, 1));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testListUserPerms()
{
$perms = $this->library->listUserPerms(1);
@ -245,10 +245,6 @@ class PermTest extends CIDatabaseTestCase
$this->assertCount(1, $perms);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testListUserPermsPaginated()
{
$perms = $this->library->listUserPermsPaginated(1);
@ -303,7 +299,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertEquals(1, $this->library->getPermId('testPerm1'));
$this->assertEquals(1, $this->library->getPermId(1));
@ -316,7 +312,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$perm = $this->library->getPerm('testPerm1');
$this->assertEquals(1, $perm['id']);
@ -333,7 +329,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->allowUser(1, 1));
$this->seeInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 1,
@ -344,7 +340,7 @@ class PermTest extends CIDatabaseTestCase
$this->assertTrue($this->library->allowUser(1, 1));
$this->assertFalse($this->library->allowUser(99, 1));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->allowUser(1, 99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
}
@ -356,7 +352,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->denyUser(1, 1));
$this->seeInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 1,
@ -367,7 +363,7 @@ class PermTest extends CIDatabaseTestCase
$this->assertTrue($this->library->denyUser(1, 1));
$this->assertFalse($this->library->denyUser(99, 1));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->denyUser(1, 99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
}
@ -379,7 +375,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->allowGroup(1, 1));
$this->seeInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 1,
@ -390,7 +386,7 @@ class PermTest extends CIDatabaseTestCase
$this->assertTrue($this->library->allowGroup(1, 1));
$this->assertFalse($this->library->allowGroup(99, 1));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->allowGroup(1, 99));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
}
@ -402,7 +398,7 @@ class PermTest extends CIDatabaseTestCase
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->denyGroup(1, 1));
$this->seeInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 1,
@ -413,7 +409,7 @@ class PermTest extends CIDatabaseTestCase
$this->assertTrue($this->library->denyGroup(1, 1));
$this->assertFalse($this->library->denyGroup(99, 1));
$this->assertEquals(lang('Aauth.notFoundPerm'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->denyGroup(1, 99));
$this->assertEquals(lang('Aauth.notFoundGroup'), $this->library->getErrorsArray()[0]);
}
@ -520,4 +516,40 @@ class PermTest extends CIDatabaseTestCase
$this->assertFalse($this->library->listGroupPermsPaginated(99));
}
public function testRemoveUserPerm()
{
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->hasInDatabase($this->config->dbTablePermToUser, [
'perm_id' => 1,
'user_id' => 1,
'state' => 1,
]);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->removeUserPerm(1, 1));
$this->assertFalse($this->library->removeUserPerm(99, 1));
$this->assertFalse($this->library->removeUserPerm(1, 99));
}
public function testRemoveGroupPerm()
{
$this->hasInDatabase($this->config->dbTablePerms, [
'id' => 1,
'name' => 'testPerm1',
'definition' => 'Test Perm 1',
]);
$this->hasInDatabase($this->config->dbTablePermToGroup, [
'perm_id' => 1,
'group_id' => 1,
'state' => 1,
]);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->removeGroupPerm(1, 1));
$this->assertFalse($this->library->removeGroupPerm(99, 1));
$this->assertFalse($this->library->removeGroupPerm(1, 99));
}
}

35
tests/Aauth/Libraries/Aauth/TOTPTest.php

@ -12,6 +12,10 @@ use App\Models\Aauth\UserModel;
use App\Models\Aauth\UserVariableModel;
use OTPHP\TOTP;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class TOTPTest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -24,7 +28,7 @@ class TOTPTest extends CIDatabaseTestCase
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->config = new AauthConfig();
$_COOKIE = [];
$_SESSION = [];
@ -61,11 +65,6 @@ class TOTPTest extends CIDatabaseTestCase
//--------------------------------------------------------------------
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testLogin()
{
$config = new AauthConfig();
@ -121,19 +120,15 @@ class TOTPTest extends CIDatabaseTestCase
$this->assertTrue($this->library->login('admin@example.com', 'password123456'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testUpdateUserTotpSecret()
{
$config = new AauthConfig();
$config->totpEnabled = true;
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertTrue($this->library->updateUserTotpSecret(99, 'TESTSECRET99'));
$this->assertTrue($this->library->updateUserTotpSecret(2, 'TESTSECRET99'));
$this->seeInDatabase($this->config->dbTableUserVariables, [
'user_id' => 99,
'user_id' => 2,
'data_key' => 'totp_secret',
'data_value' => 'TESTSECRET99',
'system' => true,
@ -159,7 +154,7 @@ class TOTPTest extends CIDatabaseTestCase
{
$config = new AauthConfig();
$config->totpEnabled = true;
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertInternalType('string', $this->library->generateUniqueTotpSecret());
}
@ -168,15 +163,11 @@ class TOTPTest extends CIDatabaseTestCase
{
$config = new AauthConfig();
$config->totpEnabled = true;
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertInternalType('string', $this->library->generateTotpQrCode('testsecret'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testVerifyUserTotpCode()
{
$config = new AauthConfig();
@ -208,15 +199,11 @@ class TOTPTest extends CIDatabaseTestCase
$this->assertTrue($this->library->verifyUserTotpCode($totpCode, 1));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testIsTotpRequired()
{
$config = new AauthConfig();
$config->totpEnabled = true;
$this->library = new Aauth($config, true);
$this->library = new Aauth($config, null);
$this->assertFalse($this->library->isTotpRequired());

54
tests/Aauth/Libraries/Aauth/UserTest.php

@ -12,6 +12,10 @@ use CodeIgniter\Test\CIDatabaseTestCase;
use App\Libraries\Aauth;
use App\Models\Aauth\UserVariableModel;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class UserTest extends CIDatabaseTestCase
{
protected $refresh = true;
@ -24,7 +28,7 @@ class UserTest extends CIDatabaseTestCase
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->config = new AauthConfig();
$_COOKIE = [];
$_SESSION = [];
@ -70,27 +74,27 @@ class UserTest extends CIDatabaseTestCase
]);
$this->assertEquals(lang('Aauth.infoCreateSuccess'), $this->library->getInfosArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createUser('admin@example.com', 'password123456', null));
$this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createUser('adminexample.com', 'password123456', null));
$this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createUser('test@example.com', 'pass', null));
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createUser('test@example.com', 'password12345678901011121314151617', null));
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createUser('test@example.com', 'password123456', 'admin'));
$this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->createUser('test@example.com', 'password123456', 'user+'));
$this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]);
}
@ -110,35 +114,35 @@ class UserTest extends CIDatabaseTestCase
]);
$this->assertEquals(lang('Aauth.infoUpdateSuccess'), $this->library->getInfosArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(2, 'admin@example.com', null, null));
$this->assertEquals(lang('Aauth.existsAlreadyEmail'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(2, 'adminexample.com', null, null));
$this->assertEquals(lang('Aauth.invalidEmail'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(2, null, 'pass', null));
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(2, null, 'password12345678901011121314151617', null));
$this->assertEquals(lang('Aauth.invalidPassword'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(2, null, null, 'admin'));
$this->assertEquals(lang('Aauth.existsAlreadyUsername'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(2, null, null, 'user+'));
$this->assertEquals(lang('Aauth.invalidUsername'), $this->library->getErrorsArray()[0]);
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertTrue($this->library->updateUser(2));
$this->assertCount(0, $this->library->getErrorsArray());
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->assertFalse($this->library->updateUser(99));
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
}
@ -194,10 +198,6 @@ class UserTest extends CIDatabaseTestCase
$this->assertEquals(lang('Aauth.infoVerification'), $this->library->getInfosArray()[0]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetUser()
{
$user = $this->library->getUser(1);
@ -220,10 +220,6 @@ class UserTest extends CIDatabaseTestCase
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetUserId()
{
$userIdEmail = $this->library->getUserId('admin@example.com');
@ -239,10 +235,6 @@ class UserTest extends CIDatabaseTestCase
$this->assertFalse($this->library->getUserId('none@example.com'));
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testGetActiveUsersCount()
{
$this->assertEquals(0, $this->library->getActiveUsersCount());
@ -256,10 +248,6 @@ class UserTest extends CIDatabaseTestCase
// $this->assertEquals(1, $this->library->getActiveUsersCount());
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testListActiveUsers()
{
$this->assertEquals([], $this->library->listActiveUsers());
@ -305,10 +293,6 @@ class UserTest extends CIDatabaseTestCase
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testBanUnbanUserSession()
{
$session = $this->getInstance();

16
tests/Aauth/Libraries/Aauth/UserVariablesTest.php

@ -26,7 +26,7 @@ class UserVariablesTest extends CIDatabaseTestCase
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
$this->config = new AauthConfig();
$_COOKIE = [];
$_SESSION = [];
@ -145,7 +145,7 @@ class UserVariablesTest extends CIDatabaseTestCase
'data_value' => 'test2',
]);
$this->assertCount(2, $this->library->getUserVars(1));
$this->assertCount(2, $this->library->listUserVars(1));
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
@ -153,9 +153,9 @@ class UserVariablesTest extends CIDatabaseTestCase
'id' => 1,
]);
$this->assertCount(2, $this->library->getUserVars());
$this->assertCount(2, $this->library->listUserVars());
$this->assertFalse($this->library->getUserVars(99));
$this->assertFalse($this->library->listUserVars(99));
}
public function testListUserVarKeys()
@ -171,8 +171,8 @@ class UserVariablesTest extends CIDatabaseTestCase
'data_value' => 'test2',
]);
$this->assertCount(2, $this->library->listUserVarKeys(1));
$this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->listUserVarKeys(1));
$this->assertCount(2, $this->library->getUserVarKeys(1));
$this->assertEquals([['key' => 'test_var'], ['key' => 'test_var2']], $this->library->getUserVarKeys(1));
$session = $this->getInstance();
$this->library = new Aauth(null, $session);
@ -180,8 +180,8 @@ class UserVariablesTest extends CIDatabaseTestCase
'id' => 1,
]);
$this->assertCount(2, $this->library->listUserVarKeys());
$this->assertCount(2, $this->library->getUserVarKeys());
$this->assertFalse($this->library->listUserVarKeys(99));
$this->assertFalse($this->library->getUserVarKeys(99));
}
}

6
tests/Aauth/Libraries/Aauth/UtilityTest.php

@ -2,13 +2,17 @@
use App\Libraries\Aauth;
/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class UtilityTest extends \CIUnitTestCase
{
public function setUp()
{
parent::setUp();
$this->library = new Aauth(null, true);
$this->library = new Aauth(null, null);
}
//--------------------------------------------------------------------

Loading…
Cancel
Save