|
|
|
@ -2,69 +2,61 @@
|
|
|
|
|
|
|
|
|
|
namespace tests\unit\models; |
|
|
|
|
|
|
|
|
|
use Yii; |
|
|
|
|
use yii\codeception\TestCase; |
|
|
|
|
use app\models\LoginForm; |
|
|
|
|
use app\models\User; |
|
|
|
|
use AspectMock\Test as test; |
|
|
|
|
|
|
|
|
|
class LoginFormTest extends TestCase |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
use \Codeception\Specify; |
|
|
|
|
|
|
|
|
|
protected function tearDown() |
|
|
|
|
{ |
|
|
|
|
test::clean(); |
|
|
|
|
parent::tearDown(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testLoginNoUser() |
|
|
|
|
{ |
|
|
|
|
$user = $this->mockUser(null); |
|
|
|
|
$model = $this->mockUser(null); |
|
|
|
|
|
|
|
|
|
$model = new LoginForm(); |
|
|
|
|
$model->username = 'some_username'; |
|
|
|
|
$model->password = 'some_password'; |
|
|
|
|
|
|
|
|
|
$this->specify('user should not be able to login, when there is no identity' , function () use ($user,$model) { |
|
|
|
|
$this->specify('user should not be able to login, when there is no identity' , function () use ($model) { |
|
|
|
|
$this->assertFalse($model->login()); |
|
|
|
|
$user->verifyInvoked('findByUsername',['some_username']); |
|
|
|
|
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in'); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testLoginWrongPassword() |
|
|
|
|
{ |
|
|
|
|
$this->mockUser(new User); |
|
|
|
|
$model = $this->mockUser(new User); |
|
|
|
|
|
|
|
|
|
$model = new LoginForm(); |
|
|
|
|
$model->username = 'demo'; |
|
|
|
|
$model->password = 'wrong-password'; |
|
|
|
|
|
|
|
|
|
$this->specify('user should not be able to login with wrong password', function () use ($model){ |
|
|
|
|
$this->specify('user should not be able to login with wrong password', function () use ($model) { |
|
|
|
|
$this->assertFalse($model->login()); |
|
|
|
|
$this->assertArrayHasKey('password',$model->errors); |
|
|
|
|
$this->assertTrue(Yii::$app->user->isGuest,'user should not be logged in'); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function testLoginCorrect() |
|
|
|
|
{ |
|
|
|
|
$this->mockUser(new User(['password' => 'demo'])); |
|
|
|
|
$model = $this->mockUser(new User(['password' => 'demo'])); |
|
|
|
|
|
|
|
|
|
$model = new LoginForm(); |
|
|
|
|
$model->username = 'demo'; |
|
|
|
|
$model->password = 'demo'; |
|
|
|
|
|
|
|
|
|
$this->specify('user should not be able to login with correct credentials', function() use($model) { |
|
|
|
|
$this->specify('user should be able to login with correct credentials', function() use ($model) { |
|
|
|
|
$this->assertTrue($model->login()); |
|
|
|
|
$this->assertArrayNotHasKey('password',$model->errors); |
|
|
|
|
$this->assertFalse(Yii::$app->user->isGuest,'user should be logged in'); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private function mockUser($user) |
|
|
|
|
{ |
|
|
|
|
return test::double('app\models\User', [ |
|
|
|
|
'findByUsername' => $user, |
|
|
|
|
]); |
|
|
|
|
$loginForm = $this->getMock('app\models\LoginForm',['getUser']); |
|
|
|
|
$loginForm->expects($this->any())->method('getUser')->will($this->returnValue($user)); |
|
|
|
|
return $loginForm; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |