Реорганизован процесс авторизации для функциональных тестов

This commit is contained in:
ErickSkrauch
2017-01-24 02:00:08 +03:00
parent 4695b6e724
commit fbaf48591f
21 changed files with 44 additions and 52 deletions

View File

@@ -1,10 +1,10 @@
<?php <?php
namespace tests\codeception\api; namespace tests\codeception\api;
use api\components\User\LoginResult; use api\models\AccountIdentity;
use api\models\authentication\LoginForm;
use Codeception\Actor; use Codeception\Actor;
use InvalidArgumentException; use InvalidArgumentException;
use Yii;
/** /**
* Inherited Methods * Inherited Methods
@@ -24,24 +24,16 @@ use InvalidArgumentException;
class FunctionalTester extends Actor { class FunctionalTester extends Actor {
use _generated\FunctionalTesterActions; use _generated\FunctionalTesterActions;
public function loggedInAsActiveAccount($login = null, $password = null) { public function amAuthenticated(string $asUsername = 'admin') {
$form = new LoginForm(); /** @var AccountIdentity $account */
if ($login === null && $password === null) { $account = AccountIdentity::findOne(['username' => $asUsername]);
$form->login = 'Admin'; if ($account === null) {
$form->password = 'password_0'; throw new InvalidArgumentException("Cannot find account for username \"$asUsername\"");
} elseif ($login !== null && $password !== null) {
$form->login = $login;
$form->password = $password;
} else {
throw new InvalidArgumentException('login and password should be presented both.');
} }
$result = $form->login(); $result = Yii::$app->user->login($account);
$this->assertInstanceOf(LoginResult::class, $result);
if ($result !== false) {
$this->amBearerAuthenticated($result->getJwt()); $this->amBearerAuthenticated($result->getJwt());
} }
}
public function notLoggedIn() { public function notLoggedIn() {
$this->haveHttpHeader('Authorization', null); $this->haveHttpHeader('Authorization', null);

View File

@@ -16,7 +16,7 @@ class AccountsAcceptRulesCest {
} }
public function testCurrent(FunctionalTester $I) { public function testCurrent(FunctionalTester $I) {
$I->loggedInAsActiveAccount('Veleyaba', 'password_0'); $I->amAuthenticated('Veleyaba');
$this->route->acceptRules(); $this->route->acceptRules();
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson(); $I->canSeeResponseIsJson();

View File

@@ -17,7 +17,7 @@ class AccountsChangeEmailConfirmNewEmailCest {
public function testConfirmNewEmail(FunctionalTester $I) { public function testConfirmNewEmail(FunctionalTester $I) {
$I->wantTo('change my email and get changed value'); $I->wantTo('change my email and get changed value');
$I->loggedInAsActiveAccount('CrafterGameplays', 'password_0'); $I->amAuthenticated('CrafterGameplays');
$this->route->changeEmailConfirmNewEmail('H28HBDCHHAG2HGHGHS'); $this->route->changeEmailConfirmNewEmail('H28HBDCHHAG2HGHGHS');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -17,7 +17,7 @@ class AccountsChangeEmailInitializeCest {
public function testChangeEmailInitialize(FunctionalTester $I) { public function testChangeEmailInitialize(FunctionalTester $I) {
$I->wantTo('send current email confirmation'); $I->wantTo('send current email confirmation');
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->changeEmailInitialize('password_0'); $this->route->changeEmailInitialize('password_0');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);
@@ -29,7 +29,7 @@ class AccountsChangeEmailInitializeCest {
public function testChangeEmailInitializeFrequencyError(FunctionalTester $I) { public function testChangeEmailInitializeFrequencyError(FunctionalTester $I) {
$I->wantTo('see change email request frequency error'); $I->wantTo('see change email request frequency error');
$I->loggedInAsActiveAccount('ILLIMUNATI', 'password_0'); $I->amAuthenticated('ILLIMUNATI');
$this->route->changeEmailInitialize('password_0'); $this->route->changeEmailInitialize('password_0');
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([

View File

@@ -18,7 +18,7 @@ class AccountsChangeEmailSubmitNewEmailCest {
public function testSubmitNewEmail(FunctionalTester $I) { public function testSubmitNewEmail(FunctionalTester $I) {
$I->wantTo('submit new email'); $I->wantTo('submit new email');
$I->loggedInAsActiveAccount('ILLIMUNATI', 'password_0'); $I->amAuthenticated('ILLIMUNATI');
$this->route->changeEmailSubmitNewEmail('H27HBDCHHAG2HGHGHS', 'my-new-email@ely.by'); $this->route->changeEmailSubmitNewEmail('H27HBDCHHAG2HGHGHS', 'my-new-email@ely.by');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -18,7 +18,7 @@ class AccountsChangeLangCest {
public function testSubmitNewEmail(FunctionalTester $I) { public function testSubmitNewEmail(FunctionalTester $I) {
$I->wantTo('change my account language'); $I->wantTo('change my account language');
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->changeLang('ru'); $this->route->changeLang('ru');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -27,7 +27,7 @@ class AccountsChangePasswordCest {
public function testChangePassword(FunctionalTester $I) { public function testChangePassword(FunctionalTester $I) {
$I->wantTo('change my password'); $I->wantTo('change my password');
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->changePassword('password_0', 'new-password', 'new-password'); $this->route->changePassword('password_0', 'new-password', 'new-password');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -26,7 +26,7 @@ class AccountsChangeUsernameCest {
public function testChangeUsername(FunctionalTester $I) { public function testChangeUsername(FunctionalTester $I) {
$I->wantTo('change my nickname'); $I->wantTo('change my nickname');
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->changeUsername('password_0', 'bruce_wayne'); $this->route->changeUsername('password_0', 'bruce_wayne');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);
@@ -38,7 +38,7 @@ class AccountsChangeUsernameCest {
public function testChangeUsernameNotAvailable(FunctionalTester $I) { public function testChangeUsernameNotAvailable(FunctionalTester $I) {
$I->wantTo('see, that nickname "in use" is not available'); $I->wantTo('see, that nickname "in use" is not available');
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->changeUsername('password_0', 'Jon'); $this->route->changeUsername('password_0', 'Jon');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -16,7 +16,7 @@ class AccountsCurrentCest {
} }
public function testCurrent(FunctionalTester $I) { public function testCurrent(FunctionalTester $I) {
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->current(); $this->route->current();
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -8,7 +8,7 @@ class LogoutCest {
public function testLoginEmailOrUsername(FunctionalTester $I) { public function testLoginEmailOrUsername(FunctionalTester $I) {
$route = new AuthenticationRoute($I); $route = new AuthenticationRoute($I);
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$route->logout(); $route->logout();
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
'success' => true, 'success' => true,

View File

@@ -51,7 +51,7 @@ class OauthAuthCodeCest {
} }
public function testValidateWithDescriptionReplaceRequest(FunctionalTester $I) { public function testValidateWithDescriptionReplaceRequest(FunctionalTester $I) {
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$I->wantTo('validate and get information with description replacement'); $I->wantTo('validate and get information with description replacement');
$this->route->validate($this->buildQueryParams( $this->route->validate($this->buildQueryParams(
'ely', 'ely',
@@ -73,13 +73,13 @@ class OauthAuthCodeCest {
} }
public function testCompleteValidationAction(FunctionalTester $I) { public function testCompleteValidationAction(FunctionalTester $I) {
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$I->wantTo('validate all oAuth params on complete request'); $I->wantTo('validate all oAuth params on complete request');
$this->testOauthParamsValidation($I, 'complete'); $this->testOauthParamsValidation($I, 'complete');
} }
public function testCompleteActionOnWrongConditions(FunctionalTester $I) { 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'); $I->wantTo('get accept_required if I don\'t require any scope, but this is first time request');
$this->route->complete($this->buildQueryParams( $this->route->complete($this->buildQueryParams(
@@ -112,7 +112,7 @@ class OauthAuthCodeCest {
} }
public function testCompleteActionSuccess(FunctionalTester $I) { public function testCompleteActionSuccess(FunctionalTester $I) {
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$I->wantTo('get auth code if I require some scope and pass accept field'); $I->wantTo('get auth code if I require some scope and pass accept field');
$this->route->complete($this->buildQueryParams( $this->route->complete($this->buildQueryParams(
'ely', 'ely',
@@ -155,7 +155,7 @@ class OauthAuthCodeCest {
} }
public function testAcceptRequiredOnNewScope(FunctionalTester $I) { 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'); $I->wantTo('get accept_required if I have previous successful request, but now require some new scope');
$this->route->complete($this->buildQueryParams( $this->route->complete($this->buildQueryParams(
'ely', 'ely',
@@ -179,7 +179,7 @@ class OauthAuthCodeCest {
} }
public function testCompleteActionWithDismissState(FunctionalTester $I) { public function testCompleteActionWithDismissState(FunctionalTester $I) {
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$I->wantTo('get access_denied error if I pass accept in false state'); $I->wantTo('get access_denied error if I pass accept in false state');
$this->route->complete($this->buildQueryParams( $this->route->complete($this->buildQueryParams(
'ely', 'ely',

View File

@@ -16,7 +16,7 @@ class TwoFactorAuthCredentialsCest {
} }
public function testGetCredentials(FunctionalTester $I) { public function testGetCredentials(FunctionalTester $I) {
$I->loggedInAsActiveAccount(); $I->amAuthenticated();
$this->route->credentials(); $this->route->credentials();
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson(); $I->canSeeResponseIsJson();

View File

@@ -17,7 +17,7 @@ class TwoFactorAuthDisableCest {
} }
public function testFails(FunctionalTester $I) { public function testFails(FunctionalTester $I) {
$I->loggedInAsActiveAccount('AccountWithEnabledOtp', 'password_0'); $I->amAuthenticated('AccountWithEnabledOtp');
$this->route->disable(); $this->route->disable();
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
@@ -37,7 +37,7 @@ class TwoFactorAuthDisableCest {
], ],
]); ]);
$I->loggedInAsActiveAccount('AccountWithOtpSecret', 'password_0'); $I->amAuthenticated('AccountWithOtpSecret');
$this->route->disable('123456', 'invalid_password'); $this->route->disable('123456', 'invalid_password');
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
'success' => false, 'success' => false,
@@ -48,7 +48,7 @@ class TwoFactorAuthDisableCest {
} }
public function testSuccessEnable(FunctionalTester $I) { public function testSuccessEnable(FunctionalTester $I) {
$I->loggedInAsActiveAccount('AccountWithEnabledOtp', 'password_0'); $I->amAuthenticated('AccountWithEnabledOtp');
$totp = new TOTP(null, 'secret-secret-secret'); $totp = new TOTP(null, 'secret-secret-secret');
$this->route->disable($totp->now(), 'password_0'); $this->route->disable($totp->now(), 'password_0');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -17,7 +17,7 @@ class TwoFactorAuthEnableCest {
} }
public function testFails(FunctionalTester $I) { public function testFails(FunctionalTester $I) {
$I->loggedInAsActiveAccount('AccountWithOtpSecret', 'password_0'); $I->amAuthenticated('AccountWithOtpSecret');
$this->route->enable(); $this->route->enable();
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
@@ -37,7 +37,7 @@ class TwoFactorAuthEnableCest {
], ],
]); ]);
$I->loggedInAsActiveAccount('AccountWithEnabledOtp', 'password_0'); $I->amAuthenticated('AccountWithEnabledOtp');
$this->route->enable('123456', 'invalid_password'); $this->route->enable('123456', 'invalid_password');
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
'success' => false, 'success' => false,
@@ -48,7 +48,7 @@ class TwoFactorAuthEnableCest {
} }
public function testSuccessEnable(FunctionalTester $I) { public function testSuccessEnable(FunctionalTester $I) {
$I->loggedInAsActiveAccount('AccountWithOtpSecret', 'password_0'); $I->amAuthenticated('AccountWithOtpSecret');
$totp = new TOTP(null, 'some otp secret value'); $totp = new TOTP(null, 'some otp secret value');
$this->route->enable($totp->now(), 'password_0'); $this->route->enable($totp->now(), 'password_0');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);

View File

@@ -7,12 +7,12 @@ use tests\codeception\api\FunctionalTester;
class AuthserverSteps extends FunctionalTester { class AuthserverSteps extends FunctionalTester {
public function amAuthenticated() { public function amAuthenticated(string $asUsername = 'admin', string $password = 'password_0') {
$route = new AuthserverRoute($this); $route = new AuthserverRoute($this);
$clientToken = Uuid::uuid4()->toString(); $clientToken = Uuid::uuid4()->toString();
$route->authenticate([ $route->authenticate([
'username' => 'admin', 'username' => $asUsername,
'password' => 'password_0', 'password' => $password,
'clientToken' => $clientToken, 'clientToken' => $clientToken,
]); ]);

View File

@@ -9,7 +9,7 @@ class OauthSteps extends FunctionalTester {
public function getAuthCode(array $permissions = []) { public function getAuthCode(array $permissions = []) {
// TODO: по идее можно напрямую сделать запись в базу, что ускорит процесс тестирования // TODO: по идее можно напрямую сделать запись в базу, что ускорит процесс тестирования
$this->loggedInAsActiveAccount(); $this->amAuthenticated();
$route = new OauthRoute($this); $route = new OauthRoute($this);
$route->complete([ $route->complete([
'client_id' => 'ely', 'client_id' => 'ely',

View File

@@ -18,7 +18,7 @@ class InvalidateCest {
public function invalidate(AuthserverSteps $I) { public function invalidate(AuthserverSteps $I) {
$I->wantTo('invalidate my token'); $I->wantTo('invalidate my token');
list($accessToken, $clientToken) = $I->amAuthenticated(); [$accessToken, $clientToken] = $I->amAuthenticated();
$this->route->invalidate([ $this->route->invalidate([
'accessToken' => $accessToken, 'accessToken' => $accessToken,
'clientToken' => $clientToken, 'clientToken' => $clientToken,

View File

@@ -18,7 +18,7 @@ class RefreshCest {
public function refresh(AuthserverSteps $I) { public function refresh(AuthserverSteps $I) {
$I->wantTo('refresh my accessToken'); $I->wantTo('refresh my accessToken');
list($accessToken, $clientToken) = $I->amAuthenticated(); [$accessToken, $clientToken] = $I->amAuthenticated();
$this->route->refresh([ $this->route->refresh([
'accessToken' => $accessToken, 'accessToken' => $accessToken,
'clientToken' => $clientToken, 'clientToken' => $clientToken,

View File

@@ -18,7 +18,7 @@ class ValidateCest {
public function validate(AuthserverSteps $I) { public function validate(AuthserverSteps $I) {
$I->wantTo('validate my accessToken'); $I->wantTo('validate my accessToken');
list($accessToken) = $I->amAuthenticated(); [$accessToken] = $I->amAuthenticated();
$this->route->validate([ $this->route->validate([
'accessToken' => $accessToken, 'accessToken' => $accessToken,
]); ]);

View File

@@ -21,7 +21,7 @@ class JoinCest {
public function joinByLegacyAuthserver(AuthserverSteps $I) { public function joinByLegacyAuthserver(AuthserverSteps $I) {
$I->wantTo('join to server, using legacy authserver access token'); $I->wantTo('join to server, using legacy authserver access token');
list($accessToken) = $I->amAuthenticated(); [$accessToken] = $I->amAuthenticated();
$this->route->join([ $this->route->join([
'accessToken' => $accessToken, 'accessToken' => $accessToken,
'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022', 'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022',
@@ -32,7 +32,7 @@ class JoinCest {
public function joinByPassJsonInPost(AuthserverSteps $I) { public function joinByPassJsonInPost(AuthserverSteps $I) {
$I->wantTo('join to server, passing data in body as encoded json'); $I->wantTo('join to server, passing data in body as encoded json');
list($accessToken) = $I->amAuthenticated(); [$accessToken] = $I->amAuthenticated();
$this->route->join(json_encode([ $this->route->join(json_encode([
'accessToken' => $accessToken, 'accessToken' => $accessToken,
'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022', 'selectedProfile' => 'df936908-b2e1-544d-96f8-2977ec213022',

View File

@@ -21,7 +21,7 @@ class JoinLegacyCest {
public function joinByLegacyAuthserver(AuthserverSteps $I) { public function joinByLegacyAuthserver(AuthserverSteps $I) {
$I->wantTo('join to server by legacy protocol, using legacy authserver access token'); $I->wantTo('join to server by legacy protocol, using legacy authserver access token');
list($accessToken) = $I->amAuthenticated(); [$accessToken] = $I->amAuthenticated();
$this->route->joinLegacy([ $this->route->joinLegacy([
'sessionId' => $accessToken, 'sessionId' => $accessToken,
'user' => 'Admin', 'user' => 'Admin',
@@ -32,7 +32,7 @@ class JoinLegacyCest {
public function joinByNewSessionFormat(AuthserverSteps $I) { public function joinByNewSessionFormat(AuthserverSteps $I) {
$I->wantTo('join to server by legacy protocol with new launcher session format, using legacy authserver'); $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([ $this->route->joinLegacy([
'sessionId' => 'token:' . $accessToken . ':' . 'df936908-b2e1-544d-96f8-2977ec213022', 'sessionId' => 'token:' . $accessToken . ':' . 'df936908-b2e1-544d-96f8-2977ec213022',
'user' => 'Admin', 'user' => 'Admin',