2016-08-29 04:47:45 +05:30
|
|
|
<?php
|
2019-08-02 18:27:17 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-29 04:47:45 +05:30
|
|
|
namespace codeception\api\unit\modules\authserver\models;
|
|
|
|
|
2019-08-02 18:27:17 +05:30
|
|
|
use api\modules\authserver\exceptions\ForbiddenOperationException;
|
2016-08-29 04:47:45 +05:30
|
|
|
use api\modules\authserver\models\AuthenticationForm;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\models\OauthSession;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\tests\fixtures\OauthClientFixture;
|
2019-02-23 04:41:57 +05:30
|
|
|
use Ramsey\Uuid\Uuid;
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class AuthenticationFormTest extends TestCase {
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-08-29 04:47:45 +05:30
|
|
|
return [
|
|
|
|
'accounts' => AccountFixture::class,
|
2019-12-11 01:21:11 +05:30
|
|
|
'oauthClients' => OauthClientFixture::class,
|
2016-08-29 04:47:45 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:22:27 +05:30
|
|
|
public function testAuthenticateByValidCredentials() {
|
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = 'admin';
|
|
|
|
$authForm->password = 'password_0';
|
|
|
|
$authForm->clientToken = Uuid::uuid4()->toString();
|
|
|
|
$result = $authForm->authenticate()->getResponseData();
|
2021-02-18 05:11:38 +05:30
|
|
|
$this->assertMatchesRegularExpression('/^[\w=-]+\.[\w=-]+\.[\w=-]+$/', $result['accessToken']);
|
2019-12-05 03:22:27 +05:30
|
|
|
$this->assertSame($authForm->clientToken, $result['clientToken']);
|
2021-02-18 05:11:38 +05:30
|
|
|
$this->assertSame('df936908b2e1544d96f82977ec213022', $result['selectedProfile']['id']);
|
2019-12-05 03:22:27 +05:30
|
|
|
$this->assertSame('Admin', $result['selectedProfile']['name']);
|
|
|
|
$this->assertFalse($result['selectedProfile']['legacy']);
|
2019-12-11 01:21:11 +05:30
|
|
|
$this->assertTrue(OauthSession::find()->andWhere([
|
|
|
|
'account_id' => 1,
|
|
|
|
'client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER,
|
|
|
|
])->exists());
|
2019-12-05 03:22:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getInvalidCredentialsCases
|
|
|
|
*/
|
2019-12-11 01:21:11 +05:30
|
|
|
public function testAuthenticateByWrongNicknamePass(string $expectedExceptionMessage, string $login, string $password) {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->expectException(ForbiddenOperationException::class);
|
2019-12-11 01:21:11 +05:30
|
|
|
$this->expectExceptionMessage($expectedExceptionMessage);
|
2016-08-29 04:47:45 +05:30
|
|
|
|
2019-12-05 03:22:27 +05:30
|
|
|
$authForm = new AuthenticationForm();
|
|
|
|
$authForm->username = $login;
|
|
|
|
$authForm->password = $password;
|
|
|
|
$authForm->clientToken = Uuid::uuid4()->toString();
|
2016-08-29 04:47:45 +05:30
|
|
|
$authForm->authenticate();
|
|
|
|
}
|
|
|
|
|
2019-12-05 03:22:27 +05:30
|
|
|
public function getInvalidCredentialsCases() {
|
2019-12-11 01:21:11 +05:30
|
|
|
yield ['Invalid credentials. Invalid nickname or password.', 'wrong-username', 'wrong-password'];
|
|
|
|
yield ['Invalid credentials. Invalid email or password.', 'wrong-email@ely.by', 'wrong-password'];
|
|
|
|
yield ['This account has been suspended.', 'Banned', 'password_0'];
|
|
|
|
yield ['Account protected with two factor auth.', 'AccountWithEnabledOtp', 'password_0'];
|
2016-08-29 04:47:45 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|