2016-05-10 17:37:32 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
declare(strict_types=1);
|
2016-05-10 17:37:32 +05:30
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests;
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
use api\components\Tokens\TokensFactory;
|
2019-02-21 01:28:52 +05:30
|
|
|
use api\tests\_generated\FunctionalTesterActions;
|
2016-05-16 13:51:12 +05:30
|
|
|
use Codeception\Actor;
|
2017-09-19 22:36:16 +05:30
|
|
|
use common\models\Account;
|
2016-05-16 13:51:12 +05:30
|
|
|
use InvalidArgumentException;
|
2017-01-24 04:30:08 +05:30
|
|
|
use Yii;
|
2016-05-10 17:37:32 +05:30
|
|
|
|
2016-05-16 13:51:12 +05:30
|
|
|
class FunctionalTester extends Actor {
|
2019-02-21 01:28:52 +05:30
|
|
|
use FunctionalTesterActions;
|
2016-05-10 17:37:32 +05:30
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
public function amAuthenticated(string $asUsername = 'admin'): int {
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @var Account $account */
|
|
|
|
$account = Account::findOne(['username' => $asUsername]);
|
2017-01-24 04:30:08 +05:30
|
|
|
if ($account === null) {
|
2019-08-01 14:47:12 +05:30
|
|
|
throw new InvalidArgumentException("Cannot find account with username \"{$asUsername}\"");
|
2016-05-16 13:51:12 +05:30
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
$token = TokensFactory::createForAccount($account);
|
|
|
|
$this->amBearerAuthenticated((string)$token);
|
2017-09-19 22:36:16 +05:30
|
|
|
|
|
|
|
return $account->id;
|
2016-05-10 17:37:32 +05:30
|
|
|
}
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
public function notLoggedIn(): void {
|
2016-05-10 17:37:32 +05:30
|
|
|
$this->haveHttpHeader('Authorization', null);
|
2019-02-21 01:28:52 +05:30
|
|
|
Yii::$app->user->logout();
|
2016-05-10 17:37:32 +05:30
|
|
|
}
|
|
|
|
|
2019-08-01 14:47:12 +05:30
|
|
|
public function canSeeAuthCredentials($expectRefreshToken = false): void {
|
2016-05-30 05:14:17 +05:30
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.access_token');
|
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.expires_in');
|
2019-08-01 14:47:12 +05:30
|
|
|
if ($expectRefreshToken) {
|
2016-05-30 05:14:17 +05:30
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.refresh_token');
|
|
|
|
} else {
|
|
|
|
$this->cantSeeResponseJsonMatchesJsonPath('$.refresh_token');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 17:37:32 +05:30
|
|
|
}
|