diff --git a/tests/codeception/api/_support/FunctionalTester.php b/tests/codeception/api/_support/FunctionalTester.php index a06b732..e8b42d1 100644 --- a/tests/codeception/api/_support/FunctionalTester.php +++ b/tests/codeception/api/_support/FunctionalTester.php @@ -1,10 +1,10 @@ login = 'Admin'; - $form->password = 'password_0'; - } elseif ($login !== null && $password !== null) { - $form->login = $login; - $form->password = $password; - } else { - throw new InvalidArgumentException('login and password should be presented both.'); + public function amAuthenticated(string $asUsername = 'admin') { + /** @var AccountIdentity $account */ + $account = AccountIdentity::findOne(['username' => $asUsername]); + if ($account === null) { + throw new InvalidArgumentException("Cannot find account for username \"$asUsername\""); } - $result = $form->login(); - $this->assertInstanceOf(LoginResult::class, $result); - if ($result !== false) { - $this->amBearerAuthenticated($result->getJwt()); - } + $result = Yii::$app->user->login($account); + $this->amBearerAuthenticated($result->getJwt()); } public function notLoggedIn() { diff --git a/tests/codeception/api/functional/AccountsAcceptRulesCest.php b/tests/codeception/api/functional/AccountsAcceptRulesCest.php index 4f6fee7..ae8269d 100644 --- a/tests/codeception/api/functional/AccountsAcceptRulesCest.php +++ b/tests/codeception/api/functional/AccountsAcceptRulesCest.php @@ -16,7 +16,7 @@ class AccountsAcceptRulesCest { } public function testCurrent(FunctionalTester $I) { - $I->loggedInAsActiveAccount('Veleyaba', 'password_0'); + $I->amAuthenticated('Veleyaba'); $this->route->acceptRules(); $I->canSeeResponseCodeIs(200); $I->canSeeResponseIsJson(); diff --git a/tests/codeception/api/functional/AccountsChangeEmailConfirmNewEmailCest.php b/tests/codeception/api/functional/AccountsChangeEmailConfirmNewEmailCest.php index 0b8016d..53b31d8 100644 --- a/tests/codeception/api/functional/AccountsChangeEmailConfirmNewEmailCest.php +++ b/tests/codeception/api/functional/AccountsChangeEmailConfirmNewEmailCest.php @@ -17,7 +17,7 @@ class AccountsChangeEmailConfirmNewEmailCest { public function testConfirmNewEmail(FunctionalTester $I) { $I->wantTo('change my email and get changed value'); - $I->loggedInAsActiveAccount('CrafterGameplays', 'password_0'); + $I->amAuthenticated('CrafterGameplays'); $this->route->changeEmailConfirmNewEmail('H28HBDCHHAG2HGHGHS'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/AccountsChangeEmailInitializeCest.php b/tests/codeception/api/functional/AccountsChangeEmailInitializeCest.php index 5e1f6b9..0dfa82e 100644 --- a/tests/codeception/api/functional/AccountsChangeEmailInitializeCest.php +++ b/tests/codeception/api/functional/AccountsChangeEmailInitializeCest.php @@ -17,7 +17,7 @@ class AccountsChangeEmailInitializeCest { public function testChangeEmailInitialize(FunctionalTester $I) { $I->wantTo('send current email confirmation'); - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->changeEmailInitialize('password_0'); $I->canSeeResponseCodeIs(200); @@ -29,7 +29,7 @@ class AccountsChangeEmailInitializeCest { public function testChangeEmailInitializeFrequencyError(FunctionalTester $I) { $I->wantTo('see change email request frequency error'); - $I->loggedInAsActiveAccount('ILLIMUNATI', 'password_0'); + $I->amAuthenticated('ILLIMUNATI'); $this->route->changeEmailInitialize('password_0'); $I->canSeeResponseContainsJson([ diff --git a/tests/codeception/api/functional/AccountsChangeEmailSubmitNewEmailCest.php b/tests/codeception/api/functional/AccountsChangeEmailSubmitNewEmailCest.php index f70ac36..d09383b 100644 --- a/tests/codeception/api/functional/AccountsChangeEmailSubmitNewEmailCest.php +++ b/tests/codeception/api/functional/AccountsChangeEmailSubmitNewEmailCest.php @@ -18,7 +18,7 @@ class AccountsChangeEmailSubmitNewEmailCest { public function testSubmitNewEmail(FunctionalTester $I) { $I->wantTo('submit new email'); - $I->loggedInAsActiveAccount('ILLIMUNATI', 'password_0'); + $I->amAuthenticated('ILLIMUNATI'); $this->route->changeEmailSubmitNewEmail('H27HBDCHHAG2HGHGHS', 'my-new-email@ely.by'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/AccountsChangeLangCest.php b/tests/codeception/api/functional/AccountsChangeLangCest.php index 4fc16fb..ffe229f 100644 --- a/tests/codeception/api/functional/AccountsChangeLangCest.php +++ b/tests/codeception/api/functional/AccountsChangeLangCest.php @@ -18,7 +18,7 @@ class AccountsChangeLangCest { public function testSubmitNewEmail(FunctionalTester $I) { $I->wantTo('change my account language'); - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->changeLang('ru'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/AccountsChangePasswordCest.php b/tests/codeception/api/functional/AccountsChangePasswordCest.php index 099d809..056a340 100644 --- a/tests/codeception/api/functional/AccountsChangePasswordCest.php +++ b/tests/codeception/api/functional/AccountsChangePasswordCest.php @@ -27,7 +27,7 @@ class AccountsChangePasswordCest { public function testChangePassword(FunctionalTester $I) { $I->wantTo('change my password'); - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->changePassword('password_0', 'new-password', 'new-password'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/AccountsChangeUsernameCest.php b/tests/codeception/api/functional/AccountsChangeUsernameCest.php index 73c54e6..b4a696f 100644 --- a/tests/codeception/api/functional/AccountsChangeUsernameCest.php +++ b/tests/codeception/api/functional/AccountsChangeUsernameCest.php @@ -26,7 +26,7 @@ class AccountsChangeUsernameCest { public function testChangeUsername(FunctionalTester $I) { $I->wantTo('change my nickname'); - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->changeUsername('password_0', 'bruce_wayne'); $I->canSeeResponseCodeIs(200); @@ -38,7 +38,7 @@ class AccountsChangeUsernameCest { public function testChangeUsernameNotAvailable(FunctionalTester $I) { $I->wantTo('see, that nickname "in use" is not available'); - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->changeUsername('password_0', 'Jon'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/AccountsCurrentCest.php b/tests/codeception/api/functional/AccountsCurrentCest.php index ef4e4df..c53be87 100644 --- a/tests/codeception/api/functional/AccountsCurrentCest.php +++ b/tests/codeception/api/functional/AccountsCurrentCest.php @@ -16,7 +16,7 @@ class AccountsCurrentCest { } public function testCurrent(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->current(); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/LogoutCest.php b/tests/codeception/api/functional/LogoutCest.php index 5acaa92..f0235ae 100644 --- a/tests/codeception/api/functional/LogoutCest.php +++ b/tests/codeception/api/functional/LogoutCest.php @@ -8,7 +8,7 @@ class LogoutCest { public function testLoginEmailOrUsername(FunctionalTester $I) { $route = new AuthenticationRoute($I); - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $route->logout(); $I->canSeeResponseContainsJson([ 'success' => true, diff --git a/tests/codeception/api/functional/OauthAuthCodeCest.php b/tests/codeception/api/functional/OauthAuthCodeCest.php index be61f3e..55ac48b 100644 --- a/tests/codeception/api/functional/OauthAuthCodeCest.php +++ b/tests/codeception/api/functional/OauthAuthCodeCest.php @@ -51,7 +51,7 @@ class OauthAuthCodeCest { } public function testValidateWithDescriptionReplaceRequest(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $I->wantTo('validate and get information with description replacement'); $this->route->validate($this->buildQueryParams( 'ely', @@ -73,13 +73,13 @@ class OauthAuthCodeCest { } public function testCompleteValidationAction(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $I->wantTo('validate all oAuth params on complete request'); $this->testOauthParamsValidation($I, 'complete'); } public function testCompleteActionOnWrongConditions(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $I->wantTo('get accept_required if I don\'t require any scope, but this is first time request'); $this->route->complete($this->buildQueryParams( @@ -112,7 +112,7 @@ class OauthAuthCodeCest { } public function testCompleteActionSuccess(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $I->wantTo('get auth code if I require some scope and pass accept field'); $this->route->complete($this->buildQueryParams( 'ely', @@ -155,7 +155,7 @@ class OauthAuthCodeCest { } public function testAcceptRequiredOnNewScope(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $I->wantTo('get accept_required if I have previous successful request, but now require some new scope'); $this->route->complete($this->buildQueryParams( 'ely', @@ -179,7 +179,7 @@ class OauthAuthCodeCest { } public function testCompleteActionWithDismissState(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $I->wantTo('get access_denied error if I pass accept in false state'); $this->route->complete($this->buildQueryParams( 'ely', diff --git a/tests/codeception/api/functional/TwoFactorAuthCredentialsCest.php b/tests/codeception/api/functional/TwoFactorAuthCredentialsCest.php index 1084965..ce95923 100644 --- a/tests/codeception/api/functional/TwoFactorAuthCredentialsCest.php +++ b/tests/codeception/api/functional/TwoFactorAuthCredentialsCest.php @@ -16,7 +16,7 @@ class TwoFactorAuthCredentialsCest { } public function testGetCredentials(FunctionalTester $I) { - $I->loggedInAsActiveAccount(); + $I->amAuthenticated(); $this->route->credentials(); $I->canSeeResponseCodeIs(200); $I->canSeeResponseIsJson(); diff --git a/tests/codeception/api/functional/TwoFactorAuthDisableCest.php b/tests/codeception/api/functional/TwoFactorAuthDisableCest.php index 974857e..5e41f83 100644 --- a/tests/codeception/api/functional/TwoFactorAuthDisableCest.php +++ b/tests/codeception/api/functional/TwoFactorAuthDisableCest.php @@ -17,7 +17,7 @@ class TwoFactorAuthDisableCest { } public function testFails(FunctionalTester $I) { - $I->loggedInAsActiveAccount('AccountWithEnabledOtp', 'password_0'); + $I->amAuthenticated('AccountWithEnabledOtp'); $this->route->disable(); $I->canSeeResponseContainsJson([ @@ -37,7 +37,7 @@ class TwoFactorAuthDisableCest { ], ]); - $I->loggedInAsActiveAccount('AccountWithOtpSecret', 'password_0'); + $I->amAuthenticated('AccountWithOtpSecret'); $this->route->disable('123456', 'invalid_password'); $I->canSeeResponseContainsJson([ 'success' => false, @@ -48,7 +48,7 @@ class TwoFactorAuthDisableCest { } public function testSuccessEnable(FunctionalTester $I) { - $I->loggedInAsActiveAccount('AccountWithEnabledOtp', 'password_0'); + $I->amAuthenticated('AccountWithEnabledOtp'); $totp = new TOTP(null, 'secret-secret-secret'); $this->route->disable($totp->now(), 'password_0'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/TwoFactorAuthEnableCest.php b/tests/codeception/api/functional/TwoFactorAuthEnableCest.php index efec01f..aee002e 100644 --- a/tests/codeception/api/functional/TwoFactorAuthEnableCest.php +++ b/tests/codeception/api/functional/TwoFactorAuthEnableCest.php @@ -17,7 +17,7 @@ class TwoFactorAuthEnableCest { } public function testFails(FunctionalTester $I) { - $I->loggedInAsActiveAccount('AccountWithOtpSecret', 'password_0'); + $I->amAuthenticated('AccountWithOtpSecret'); $this->route->enable(); $I->canSeeResponseContainsJson([ @@ -37,7 +37,7 @@ class TwoFactorAuthEnableCest { ], ]); - $I->loggedInAsActiveAccount('AccountWithEnabledOtp', 'password_0'); + $I->amAuthenticated('AccountWithEnabledOtp'); $this->route->enable('123456', 'invalid_password'); $I->canSeeResponseContainsJson([ 'success' => false, @@ -48,7 +48,7 @@ class TwoFactorAuthEnableCest { } public function testSuccessEnable(FunctionalTester $I) { - $I->loggedInAsActiveAccount('AccountWithOtpSecret', 'password_0'); + $I->amAuthenticated('AccountWithOtpSecret'); $totp = new TOTP(null, 'some otp secret value'); $this->route->enable($totp->now(), 'password_0'); $I->canSeeResponseCodeIs(200); diff --git a/tests/codeception/api/functional/_steps/AuthserverSteps.php b/tests/codeception/api/functional/_steps/AuthserverSteps.php index e065d36..9648ec2 100644 --- a/tests/codeception/api/functional/_steps/AuthserverSteps.php +++ b/tests/codeception/api/functional/_steps/AuthserverSteps.php @@ -7,12 +7,12 @@ use tests\codeception\api\FunctionalTester; class AuthserverSteps extends FunctionalTester { - public function amAuthenticated() { + public function amAuthenticated(string $asUsername = 'admin', string $password = 'password_0') { $route = new AuthserverRoute($this); $clientToken = Uuid::uuid4()->toString(); $route->authenticate([ - 'username' => 'admin', - 'password' => 'password_0', + 'username' => $asUsername, + 'password' => $password, 'clientToken' => $clientToken, ]); diff --git a/tests/codeception/api/functional/_steps/OauthSteps.php b/tests/codeception/api/functional/_steps/OauthSteps.php index 3e0c467..c8327e7 100644 --- a/tests/codeception/api/functional/_steps/OauthSteps.php +++ b/tests/codeception/api/functional/_steps/OauthSteps.php @@ -9,7 +9,7 @@ class OauthSteps extends FunctionalTester { public function getAuthCode(array $permissions = []) { // TODO: по идее можно напрямую сделать запись в базу, что ускорит процесс тестирования - $this->loggedInAsActiveAccount(); + $this->amAuthenticated(); $route = new OauthRoute($this); $route->complete([ 'client_id' => 'ely', diff --git a/tests/codeception/api/functional/authserver/InvalidateCest.php b/tests/codeception/api/functional/authserver/InvalidateCest.php index 7cab088..de2bcb0 100644 --- a/tests/codeception/api/functional/authserver/InvalidateCest.php +++ b/tests/codeception/api/functional/authserver/InvalidateCest.php @@ -18,7 +18,7 @@ class InvalidateCest { public function invalidate(AuthserverSteps $I) { $I->wantTo('invalidate my token'); - list($accessToken, $clientToken) = $I->amAuthenticated(); + [$accessToken, $clientToken] = $I->amAuthenticated(); $this->route->invalidate([ 'accessToken' => $accessToken, 'clientToken' => $clientToken, diff --git a/tests/codeception/api/functional/authserver/RefreshCest.php b/tests/codeception/api/functional/authserver/RefreshCest.php index e26ccf7..e18751b 100644 --- a/tests/codeception/api/functional/authserver/RefreshCest.php +++ b/tests/codeception/api/functional/authserver/RefreshCest.php @@ -18,7 +18,7 @@ class RefreshCest { public function refresh(AuthserverSteps $I) { $I->wantTo('refresh my accessToken'); - list($accessToken, $clientToken) = $I->amAuthenticated(); + [$accessToken, $clientToken] = $I->amAuthenticated(); $this->route->refresh([ 'accessToken' => $accessToken, 'clientToken' => $clientToken, diff --git a/tests/codeception/api/functional/authserver/ValidateCest.php b/tests/codeception/api/functional/authserver/ValidateCest.php index 1cd5b9b..704b02a 100644 --- a/tests/codeception/api/functional/authserver/ValidateCest.php +++ b/tests/codeception/api/functional/authserver/ValidateCest.php @@ -18,7 +18,7 @@ class ValidateCest { public function validate(AuthserverSteps $I) { $I->wantTo('validate my accessToken'); - list($accessToken) = $I->amAuthenticated(); + [$accessToken] = $I->amAuthenticated(); $this->route->validate([ 'accessToken' => $accessToken, ]); diff --git a/tests/codeception/api/functional/sessionserver/JoinCest.php b/tests/codeception/api/functional/sessionserver/JoinCest.php index 986521e..ed34400 100644 --- a/tests/codeception/api/functional/sessionserver/JoinCest.php +++ b/tests/codeception/api/functional/sessionserver/JoinCest.php @@ -21,7 +21,7 @@ class JoinCest { public function joinByLegacyAuthserver(AuthserverSteps $I) { $I->wantTo('join to server, using legacy authserver access token'); - list($accessToken) = $I->amAuthenticated(); + [$accessToken] = $I->amAuthenticated(); $this->route->join([ 'accessToken' => $accessToken, 'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022', @@ -32,7 +32,7 @@ class JoinCest { public function joinByPassJsonInPost(AuthserverSteps $I) { $I->wantTo('join to server, passing data in body as encoded json'); - list($accessToken) = $I->amAuthenticated(); + [$accessToken] = $I->amAuthenticated(); $this->route->join(json_encode([ 'accessToken' => $accessToken, 'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022', diff --git a/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php b/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php index e82695a..d691bce 100644 --- a/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php +++ b/tests/codeception/api/functional/sessionserver/JoinLegacyCest.php @@ -21,7 +21,7 @@ class JoinLegacyCest { public function joinByLegacyAuthserver(AuthserverSteps $I) { $I->wantTo('join to server by legacy protocol, using legacy authserver access token'); - list($accessToken) = $I->amAuthenticated(); + [$accessToken] = $I->amAuthenticated(); $this->route->joinLegacy([ 'sessionId' => $accessToken, 'user' => 'Admin', @@ -32,7 +32,7 @@ class JoinLegacyCest { public function joinByNewSessionFormat(AuthserverSteps $I) { $I->wantTo('join to server by legacy protocol with new launcher session format, using legacy authserver'); - list($accessToken) = $I->amAuthenticated(); + [$accessToken] = $I->amAuthenticated(); $this->route->joinLegacy([ 'sessionId' => 'token:' . $accessToken . ':' . 'df936908-b2e1-544d-96f8-2977ec213022', 'user' => 'Admin',