2016-05-31 03:33:30 +05:30
|
|
|
<?php
|
2019-08-01 14:47:12 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
namespace api\tests\unit\models\authentication;
|
2016-05-31 03:33:30 +05:30
|
|
|
|
|
|
|
use api\models\authentication\RefreshTokenForm;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2016-05-31 03:33:30 +05:30
|
|
|
use common\models\AccountSession;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountSessionFixture;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\web\Request;
|
2016-05-31 03:33:30 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class RefreshTokenFormTest extends TestCase {
|
2016-05-31 03:33:30 +05:30
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-05-31 03:33:30 +05:30
|
|
|
return [
|
|
|
|
'sessions' => AccountSessionFixture::class,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testRenew(): void {
|
2019-12-11 01:21:11 +05:30
|
|
|
$request = $this->createPartialMock(Request::class, ['getUserIP']);
|
|
|
|
$request->method('getUserIP')->willReturn('10.1.2.3');
|
2019-08-02 05:59:20 +05:30
|
|
|
Yii::$app->set('request', $request);
|
|
|
|
|
|
|
|
$model = new RefreshTokenForm();
|
|
|
|
$model->refresh_token = 'SOutIr6Seeaii3uqMVy3Wan8sKFVFrNz';
|
|
|
|
$result = $model->renew();
|
|
|
|
$this->assertNotNull($result);
|
|
|
|
$this->assertSame('SOutIr6Seeaii3uqMVy3Wan8sKFVFrNz', $result->getRefreshToken());
|
|
|
|
|
|
|
|
$token = $result->getToken();
|
2024-12-02 15:40:55 +05:30
|
|
|
$this->assertSame('ely|1', $token->claims()->get('sub'));
|
|
|
|
$this->assertSame('accounts_web_user', $token->claims()->get('scope'));
|
|
|
|
$this->assertEqualsWithDelta(time(), $token->claims()->get('iat')->getTimestamp(), 5);
|
|
|
|
$this->assertEqualsWithDelta(time() + 3600, $token->claims()->get('exp')->getTimestamp(), 5);
|
|
|
|
$this->assertSame(1, (int)$token->claims()->get('jti'));
|
2019-08-02 05:59:20 +05:30
|
|
|
|
|
|
|
/** @var AccountSession $session */
|
|
|
|
$session = AccountSession::findOne(['refresh_token' => 'SOutIr6Seeaii3uqMVy3Wan8sKFVFrNz']);
|
|
|
|
$this->assertEqualsWithDelta(time(), $session->last_refreshed_at, 5);
|
|
|
|
$this->assertSame('10.1.2.3', $session->getReadableIp());
|
2016-05-31 03:33:30 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testRenewWithInvalidRefreshToken(): void {
|
2016-10-29 03:17:31 +05:30
|
|
|
$model = new RefreshTokenForm();
|
2019-08-02 05:59:20 +05:30
|
|
|
$model->refresh_token = 'unknown refresh token';
|
|
|
|
$this->assertNull($model->renew());
|
|
|
|
$this->assertSame(['error.refresh_token_not_exist'], $model->getErrors('refresh_token'));
|
2016-05-31 03:33:30 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|