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.
75 lines
2.0 KiB
75 lines
2.0 KiB
7 years ago
|
<?php
|
||
|
|
||
|
namespace common\tests\unit\models;
|
||
|
|
||
|
use common\auth\Identity;
|
||
|
use core\entities\user\User;
|
||
|
use core\repositories\user\UserRepository;
|
||
|
use core\services\auth\AuthService;
|
||
|
use core\dispatchers\SimpleEventDispatcher;
|
||
|
use Yii;
|
||
|
use common\models\LoginForm;
|
||
|
use common\fixtures\UserFixture;
|
||
|
|
||
|
/**
|
||
|
* Login form test
|
||
|
*/
|
||
|
class LoginFormTest extends \Codeception\Test\Unit
|
||
|
{
|
||
|
/**
|
||
|
* @var \common\tests\UnitTester
|
||
|
*/
|
||
|
protected $tester;
|
||
|
|
||
|
/**
|
||
|
* @return array
|
||
|
*/
|
||
|
public function _fixtures()
|
||
|
{
|
||
|
return [
|
||
|
'user' => [
|
||
|
'class' => UserFixture::className(),
|
||
|
'dataFile' => codecept_data_dir() . 'user.php'
|
||
|
]
|
||
|
];
|
||
|
}
|
||
|
|
||
|
public function testLoginNoUser()
|
||
|
{
|
||
|
$form = new \core\forms\auth\LoginForm();
|
||
|
$form->username = 'not_existing_username';
|
||
|
$form->password = 'not_existing_password';
|
||
|
|
||
|
$model = new User($form);
|
||
|
|
||
|
//$as = new AuthService(new UserRepository(new SimpleEventDispatcher(null, null)));
|
||
|
//$as->auth($form);
|
||
|
expect('model should not login user', Yii::$app->user->login(new Identity($model)))->false();
|
||
|
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
|
||
|
}
|
||
|
|
||
|
public function testLoginWrongPassword()
|
||
|
{
|
||
|
$model = new LoginForm([
|
||
|
'username' => 'bayer.hudson',
|
||
|
'password' => 'wrong_password',
|
||
|
]);
|
||
|
|
||
|
expect('model should not login user', $model->login())->false();
|
||
|
expect('error message should be set', $model->errors)->hasKey('password');
|
||
|
expect('user should not be logged in', Yii::$app->user->isGuest)->true();
|
||
|
}
|
||
|
|
||
|
public function testLoginCorrect()
|
||
|
{
|
||
|
$model = new LoginForm([
|
||
|
'username' => 'bayer.hudson',
|
||
|
'password' => 'password_0',
|
||
|
]);
|
||
|
|
||
|
expect('model should login user', $model->login())->true();
|
||
|
expect('error message should not be set', $model->errors)->hasntKey('password');
|
||
|
expect('user should be logged in', Yii::$app->user->isGuest)->false();
|
||
|
}
|
||
|
}
|