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

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
namespace tests\codeception\api;
use api\components\User\LoginResult;
use api\models\authentication\LoginForm;
use api\models\AccountIdentity;
use Codeception\Actor;
use InvalidArgumentException;
use Yii;
/**
* Inherited Methods
@ -24,23 +24,15 @@ use InvalidArgumentException;
class FunctionalTester extends Actor {
use _generated\FunctionalTesterActions;
public function loggedInAsActiveAccount($login = null, $password = null) {
$form = new LoginForm();
if ($login === null && $password === null) {
$form->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() {

View File

@ -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();

View File

@ -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);

View File

@ -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([

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

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

View File

@ -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,

View File

@ -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',

View File

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

View File

@ -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);

View File

@ -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);

View File

@ -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,
]);

View File

@ -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',

View File

@ -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,

View File

@ -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,

View File

@ -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,
]);

View File

@ -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',

View File

@ -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',