2016-08-29 02:17:45 +03:00
|
|
|
<?php
|
2019-08-02 15:57:17 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-29 02:17:45 +03:00
|
|
|
namespace codeception\api\unit\modules\authserver\models;
|
|
|
|
|
2019-08-02 15:57:17 +03:00
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2016-08-29 02:17:45 +03:00
|
|
|
use api\modules\authserver\models\AuthenticationForm;
|
2019-02-23 02:11:57 +03:00
|
|
|
use api\tests\unit\TestCase;
|
2019-02-20 22:58:52 +03:00
|
|
|
use common\tests\fixtures\AccountFixture;
|
2019-02-23 02:11:57 +03:00
|
|
|
use Ramsey\Uuid\Uuid;
|
2016-08-29 02:17:45 +03:00
|
|
|
|
2016-10-29 00:47:31 +03:00
|
|
|
class AuthenticationFormTest extends TestCase {
|
2016-08-29 02:17:45 +03:00
|
|
|
|
2019-05-14 01:58:29 +03:00
|
|
|
public function _fixtures(): array {
|
2016-08-29 02:17:45 +03:00
|
|
|
return [
|
|
|
|
'accounts' => AccountFixture::class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-05 00:52:27 +03:00
|
|
|
public function testAuthenticateByValidCredentials() {
|
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = 'admin';
|
|
|
|
$authForm->password = 'password_0';
|
|
|
|
$authForm->clientToken = Uuid::uuid4()->toString();
|
|
|
|
$result = $authForm->authenticate()->getResponseData();
|
|
|
|
$this->assertRegExp('/^[\w=-]+\.[\w=-]+\.[\w=-]+$/', $result['accessToken']);
|
|
|
|
$this->assertSame($authForm->clientToken, $result['clientToken']);
|
|
|
|
$this->assertSame('df936908-b2e1-544d-96f8-2977ec213022', $result['selectedProfile']['id']);
|
|
|
|
$this->assertSame('Admin', $result['selectedProfile']['name']);
|
|
|
|
$this->assertFalse($result['selectedProfile']['legacy']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getInvalidCredentialsCases
|
|
|
|
*/
|
|
|
|
public function testAuthenticateByWrongNicknamePass(string $expectedFieldError, string $login, string $password) {
|
2019-08-02 15:57:17 +03:00
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
2019-12-05 00:52:27 +03:00
|
|
|
$this->expectExceptionMessage("Invalid credentials. Invalid {$expectedFieldError} or password.");
|
2016-08-29 02:17:45 +03:00
|
|
|
|
2019-12-05 00:52:27 +03:00
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = $login;
|
|
|
|
$authForm->password = $password;
|
|
|
|
$authForm->clientToken = Uuid::uuid4()->toString();
|
2016-08-29 02:17:45 +03:00
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
2019-12-05 00:52:27 +03:00
|
|
|
public function getInvalidCredentialsCases() {
|
|
|
|
yield ['nickname', 'wrong-username', 'wrong-password'];
|
|
|
|
yield ['email', 'wrong-email@ely.by', 'wrong-password'];
|
2016-08-29 02:17:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAuthenticateByValidCredentialsIntoBlockedAccount() {
|
2019-08-02 15:57:17 +03:00
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
|
|
|
$this->expectExceptionMessage('This account has been suspended.');
|
|
|
|
|
2019-12-05 00:52:27 +03:00
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = 'Banned';
|
2016-08-29 02:17:45 +03:00
|
|
|
$authForm->password = 'password_0';
|
2019-12-05 00:52:27 +03:00
|
|
|
$authForm->clientToken = Uuid::uuid4()->toString();
|
2016-08-29 02:17:45 +03:00
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|