You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.8 KiB
171 lines
5.8 KiB
6 years ago
|
<?php namespace Tests\Aauth\Libraries\Aauth;
|
||
|
|
||
6 years ago
|
use Config\Aauth as AauthConfig;
|
||
6 years ago
|
use Config\App;
|
||
6 years ago
|
use Config\Logger;
|
||
|
use Config\Services;
|
||
|
use Tests\Support\Log\TestLogger;
|
||
6 years ago
|
use Tests\Support\HTTP\MockResponse;
|
||
6 years ago
|
use Tests\Support\Session\MockSession;
|
||
|
use CodeIgniter\Session\Handlers\FileHandler;
|
||
|
use CodeIgniter\Test\CIDatabaseTestCase;
|
||
|
use App\Libraries\Aauth;
|
||
6 years ago
|
use App\Models\Aauth\UserVariableModel;
|
||
|
use App\Models\Aauth\LoginTokenModel;
|
||
6 years ago
|
|
||
|
/**
|
||
|
* @runTestsInSeparateProcesses
|
||
6 years ago
|
* @preserveGlobalState disabled
|
||
6 years ago
|
*/
|
||
|
class LoginTest extends CIDatabaseTestCase
|
||
|
{
|
||
|
protected $refresh = true;
|
||
|
|
||
|
protected $basePath = TESTPATH . '../application' . 'Database/Migrations';
|
||
|
|
||
|
protected $namespace = 'App';
|
||
|
|
||
6 years ago
|
public function setUp()
|
||
|
{
|
||
|
parent::setUp();
|
||
6 years ago
|
|
||
6 years ago
|
Services::injectMock('response', new MockResponse(new App()));
|
||
|
$this->response = service('response');
|
||
6 years ago
|
$this->library = new Aauth(null, true);
|
||
|
$_COOKIE = [];
|
||
|
$_SESSION = [];
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
public function tearDown()
|
||
|
{
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
protected function getInstance($options = [])
|
||
6 years ago
|
{
|
||
|
$defaults = [
|
||
6 years ago
|
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler',
|
||
|
'sessionCookieName' => 'ci_session',
|
||
|
'sessionExpiration' => 7200,
|
||
|
'sessionSavePath' => 'null',
|
||
|
'sessionMatchIP' => false,
|
||
|
'sessionTimeToUpdate' => 300,
|
||
6 years ago
|
'sessionRegenerateDestroy' => false,
|
||
6 years ago
|
'cookieDomain' => '',
|
||
|
'cookiePrefix' => '',
|
||
|
'cookiePath' => '/',
|
||
|
'cookieSecure' => false,
|
||
6 years ago
|
];
|
||
6 years ago
|
|
||
6 years ago
|
$config = (object)$defaults;
|
||
6 years ago
|
|
||
6 years ago
|
$session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config);
|
||
|
$session->setLogger(new TestLogger(new Logger()));
|
||
|
$session->start();
|
||
6 years ago
|
|
||
6 years ago
|
return $session;
|
||
|
}
|
||
6 years ago
|
|
||
|
//--------------------------------------------------------------------
|
||
|
|
||
6 years ago
|
public function testLogin()
|
||
|
{
|
||
6 years ago
|
$session = $this->getInstance();
|
||
|
$config = new AauthConfig();
|
||
6 years ago
|
$config->loginUseUsername = true;
|
||
|
|
||
|
$this->library = new Aauth($config, $session);
|
||
|
$this->assertTrue($this->library->login('admin', 'password123456'));
|
||
|
|
||
6 years ago
|
$this->assertTrue($this->library->login('admin', 'password123456', true));
|
||
6 years ago
|
$this->seeInDatabase($config->dbTableLoginTokens, [
|
||
6 years ago
|
'user_id' => 1,
|
||
|
]);
|
||
|
$this->assertTrue($this->response->hasCookie('remember'));
|
||
|
|
||
6 years ago
|
$this->assertFalse($this->library->login('admin', 'passwor'));
|
||
|
$this->assertEquals(lang('Aauth.loginFailedUsername'), $this->library->getErrorsArray()[0]);
|
||
|
|
||
|
$this->library->clearErrors();
|
||
|
$this->assertFalse($this->library->login('user99', 'password123456'));
|
||
|
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
|
||
|
// $config->loginUseUsername = false;
|
||
|
|
||
|
$this->library = new Aauth(null, $session);
|
||
|
$this->assertTrue($this->library->login('[email protected]', 'password123456'));
|
||
|
|
||
|
$this->assertFalse($this->library->login('adminaexample.com', 'password123456'));
|
||
|
$this->assertEquals(lang('Aauth.loginFailedEmail'), $this->library->getErrorsArray()[0]);
|
||
|
|
||
|
$this->library->clearErrors();
|
||
|
$this->assertFalse($this->library->login('[email protected]', 'passwor'));
|
||
|
$this->assertEquals(lang('Aauth.loginFailedEmail'), $this->library->getErrorsArray()[0]);
|
||
|
|
||
|
$this->library->clearErrors();
|
||
|
$this->assertFalse($this->library->login('[email protected]', 'password123456'));
|
||
|
$this->assertEquals(lang('Aauth.notFoundUser'), $this->library->getErrorsArray()[0]);
|
||
|
|
||
6 years ago
|
$this->library->clearErrors();
|
||
6 years ago
|
$this->assertFalse($this->library->login('[email protected]', 'password1234567'));
|
||
|
$this->assertEquals(lang('Aauth.loginFailedAll'), $this->library->getErrorsArray()[0]);
|
||
6 years ago
|
|
||
|
$this->library->banUser(1);
|
||
6 years ago
|
$this->library->clearErrors();
|
||
6 years ago
|
$this->assertFalse($this->library->login('[email protected]', 'password123456'));
|
||
|
$this->assertEquals(lang('Aauth.invalidUserBanned'), $this->library->getErrorsArray()[0]);
|
||
|
|
||
6 years ago
|
$userVariableModel = new UserVariableModel();
|
||
|
$userVariableModel->save(1, 'verification_code', '12345678', true);
|
||
6 years ago
|
$this->library->clearErrors();
|
||
6 years ago
|
$this->assertFalse($this->library->login('[email protected]', 'password123456'));
|
||
|
$this->assertEquals(lang('Aauth.notVerified'), $this->library->getErrorsArray()[0]);
|
||
6 years ago
|
|
||
|
$this->library->login('[email protected]', 'password123456');
|
||
|
$this->library->login('[email protected]', 'password123456');
|
||
|
$this->library->login('[email protected]', 'password123456');
|
||
|
$this->library->clearErrors();
|
||
|
$this->assertFalse($this->library->login('[email protected]', 'password123456'));
|
||
|
$this->assertEquals(lang('Aauth.loginAttemptsExceeded'), $this->library->getErrorsArray()[0]);
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
public function testIsLoggedIn()
|
||
|
{
|
||
6 years ago
|
$session = $this->getInstance();
|
||
6 years ago
|
$this->library = new Aauth(null, $session);
|
||
6 years ago
|
$session->set('user', [
|
||
6 years ago
|
'loggedIn' => true,
|
||
|
]);
|
||
6 years ago
|
$session = $this->getInstance();
|
||
|
$this->library = new Aauth(null, $session);
|
||
|
|
||
|
$config = new AauthConfig();
|
||
|
$expire = $config->loginRemember;
|
||
|
$userId = base64_encode(1);
|
||
|
$randomString = random_string('alnum', 32);
|
||
|
$selectorString = random_string('alnum', 16);
|
||
|
|
||
|
$this->response->setCookie('remember', $userId . ';' . $randomString . ';' . $selectorString, YEAR);
|
||
|
|
||
|
$this->hasInDatabase($this->config->dbTablePermToGroup, [
|
||
|
'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($expire)),
|
||
|
]);
|
||
|
|
||
6 years ago
|
$this->assertTrue($this->library->isLoggedIn());
|
||
6 years ago
|
}
|
||
|
|
||
|
public function testLogout()
|
||
|
{
|
||
6 years ago
|
$session = $this->getInstance();
|
||
6 years ago
|
$this->library = new Aauth(null, $session);
|
||
6 years ago
|
$session->set('user', [
|
||
6 years ago
|
'loggedIn' => true,
|
||
|
]);
|
||
|
$this->assertTrue($this->library->isLoggedIn());
|
||
|
$this->library->logout();
|
||
|
$this->library = new Aauth(null, $session);
|
||
|
$this->assertFalse($this->library->isLoggedIn());
|
||
6 years ago
|
}
|
||
|
}
|