2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\tests\unit\models\authentication;
|
|
|
|
|
|
|
|
use api\models\authentication\AuthenticationResult;
|
|
|
|
use api\tests\unit\TestCase;
|
2024-12-02 15:40:55 +05:30
|
|
|
use DateTimeImmutable;
|
|
|
|
use Lcobucci\JWT\Builder as BuilderInterface;
|
|
|
|
use Lcobucci\JWT\JwtFacade;
|
|
|
|
use Lcobucci\JWT\Signer\Blake2b;
|
|
|
|
use Lcobucci\JWT\Signer\Key;
|
2019-08-01 14:47:12 +05:30
|
|
|
use Yii;
|
|
|
|
|
|
|
|
class AuthenticationResultTest extends TestCase {
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetters(): void {
|
|
|
|
$token = (new JwtFacade())
|
|
|
|
->issue(
|
|
|
|
new Blake2b(),
|
|
|
|
Key\InMemory::plainText('MpQd6dDPiqnzFSWmpUfLy4+Rdls90Ca4C8e0QD0IxqY='),
|
|
|
|
static fn(BuilderInterface $builder, DateTimeImmutable $issuedAt): BuilderInterface => $builder,
|
|
|
|
);
|
2019-08-01 14:47:12 +05:30
|
|
|
$model = new AuthenticationResult($token);
|
|
|
|
$this->assertSame($token, $model->getToken());
|
|
|
|
$this->assertNull($model->getRefreshToken());
|
|
|
|
|
|
|
|
$model = new AuthenticationResult($token, 'refresh_token');
|
|
|
|
$this->assertSame('refresh_token', $model->getRefreshToken());
|
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function testGetAsResponse(): void {
|
2019-12-06 21:01:04 +05:30
|
|
|
$time = time() + 3600;
|
2024-12-02 15:40:55 +05:30
|
|
|
$token = Yii::$app->tokens->create(['exp' => new DateTimeImmutable("@{$time}", null)]);
|
|
|
|
$jwt = $token->toString();
|
2019-08-01 14:47:12 +05:30
|
|
|
|
|
|
|
$model = new AuthenticationResult($token);
|
|
|
|
$result = $model->formatAsOAuth2Response();
|
|
|
|
$this->assertSame($jwt, $result['access_token']);
|
2019-12-06 21:01:04 +05:30
|
|
|
$this->assertSame(3600, $result['expires_in']);
|
2019-08-01 14:47:12 +05:30
|
|
|
$this->assertArrayNotHasKey('refresh_token', $result);
|
|
|
|
|
|
|
|
$model = new AuthenticationResult($token, 'refresh_token');
|
|
|
|
$result = $model->formatAsOAuth2Response();
|
|
|
|
$this->assertSame($jwt, $result['access_token']);
|
2019-12-06 21:01:04 +05:30
|
|
|
$this->assertSame(3600, $result['expires_in']);
|
2019-08-01 14:47:12 +05:30
|
|
|
$this->assertSame('refresh_token', $result['refresh_token']);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|