2016-05-10 15:07:32 +03:00
|
|
|
<?php
|
2019-02-20 22:58:52 +03:00
|
|
|
declare(strict_types=1);
|
2016-05-10 15:07:32 +03:00
|
|
|
|
2019-02-20 22:58:52 +03:00
|
|
|
namespace api\tests;
|
|
|
|
|
|
|
|
use api\tests\_generated\FunctionalTesterActions;
|
2016-05-16 11:21:12 +03:00
|
|
|
use Codeception\Actor;
|
2017-09-19 20:06:16 +03:00
|
|
|
use common\models\Account;
|
2016-05-16 11:21:12 +03:00
|
|
|
use InvalidArgumentException;
|
2017-01-24 02:00:08 +03:00
|
|
|
use Yii;
|
2016-05-10 15:07:32 +03:00
|
|
|
|
2016-05-16 11:21:12 +03:00
|
|
|
class FunctionalTester extends Actor {
|
2019-02-20 22:58:52 +03:00
|
|
|
use FunctionalTesterActions;
|
2016-05-10 15:07:32 +03:00
|
|
|
|
2019-08-01 19:58:18 +03:00
|
|
|
public function amAuthenticated(string $asUsername = 'admin') { // Do not declare type
|
2017-09-19 20:06:16 +03:00
|
|
|
/** @var Account $account */
|
|
|
|
$account = Account::findOne(['username' => $asUsername]);
|
2017-01-24 02:00:08 +03:00
|
|
|
if ($account === null) {
|
2019-08-01 12:17:12 +03:00
|
|
|
throw new InvalidArgumentException("Cannot find account with username \"{$asUsername}\"");
|
2016-05-16 11:21:12 +03:00
|
|
|
}
|
|
|
|
|
2019-12-04 21:10:15 +03:00
|
|
|
$token = Yii::$app->tokensFactory->createForWebAccount($account);
|
2019-08-01 12:17:12 +03:00
|
|
|
$this->amBearerAuthenticated((string)$token);
|
2017-09-19 20:06:16 +03:00
|
|
|
|
|
|
|
return $account->id;
|
2016-05-10 15:07:32 +03:00
|
|
|
}
|
|
|
|
|
2019-02-20 22:58:52 +03:00
|
|
|
public function notLoggedIn(): void {
|
2016-05-10 15:07:32 +03:00
|
|
|
$this->haveHttpHeader('Authorization', null);
|
2019-02-20 22:58:52 +03:00
|
|
|
Yii::$app->user->logout();
|
2016-05-10 15:07:32 +03:00
|
|
|
}
|
|
|
|
|
2019-08-01 12:17:12 +03:00
|
|
|
public function canSeeAuthCredentials($expectRefreshToken = false): void {
|
2016-05-30 02:44:17 +03:00
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.access_token');
|
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.expires_in');
|
2019-08-01 12:17:12 +03:00
|
|
|
if ($expectRefreshToken) {
|
2016-05-30 02:44:17 +03:00
|
|
|
$this->canSeeResponseJsonMatchesJsonPath('$.refresh_token');
|
|
|
|
} else {
|
|
|
|
$this->cantSeeResponseJsonMatchesJsonPath('$.refresh_token');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 15:07:32 +03:00
|
|
|
}
|