accounts/api/tests/unit/components/User/IdentityFactoryTest.php

50 lines
1.7 KiB
PHP
Raw Normal View History

2019-08-02 21:02:08 +05:30
<?php
declare(strict_types=1);
namespace api\tests\unit\components\User;
use api\components\User\IdentityFactory;
use api\components\User\JwtIdentity;
use api\components\User\LegacyOAuth2Identity;
2019-08-02 21:02:08 +05:30
use api\tests\unit\TestCase;
use Carbon\Carbon;
2019-09-23 03:23:13 +05:30
use common\tests\fixtures;
2019-08-02 21:02:08 +05:30
use yii\web\UnauthorizedHttpException;
class IdentityFactoryTest extends TestCase {
2019-09-23 03:23:13 +05:30
public function _fixtures(): array {
return [
fixtures\LegacyOauthAccessTokenFixture::class,
fixtures\LegacyOauthAccessTokenScopeFixture::class,
];
}
2019-08-02 21:02:08 +05:30
public function testFindIdentityByAccessToken() {
2019-09-23 03:23:13 +05:30
// Find identity by the JWT
2019-08-02 21:02:08 +05:30
$identity = IdentityFactory::findIdentityByAccessToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJlbHktc2NvcGVzIjoiYWNjb3VudHNfd2ViX3VzZXIiLCJpYXQiOjE1NjQ2MTA1NDIsImV4cCI6MTU2NDYxNDE0Miwic3ViIjoiZWx5fDEifQ.4Oidvuo4spvUf9hkpHR72eeqZUh2Zbxh_L8Od3vcgTj--0iOrcOEp6zwmEW6vF7BTHtjz2b3mXce61bqsCjXjQ');
$this->assertInstanceOf(JwtIdentity::class, $identity);
2019-09-23 03:23:13 +05:30
// Find identity by the legacy OAuth2 token
$identity = IdentityFactory::findIdentityByAccessToken('ZZQP8sS9urzriy8N9h6FwFNMOH3PkZ5T5PLqS6SX');
$this->assertInstanceOf(LegacyOAuth2Identity::class, $identity);
2019-08-02 21:02:08 +05:30
}
public function testFindIdentityByAccessTokenWithEmptyValue() {
$this->expectException(UnauthorizedHttpException::class);
$this->expectExceptionMessage('Incorrect token');
IdentityFactory::findIdentityByAccessToken('');
}
protected function _setUp() {
parent::_setUp();
Carbon::setTestNow(Carbon::create(2019, 8, 1, 1, 2, 22, 'Europe/Minsk'));
}
protected function _tearDown() {
parent::_tearDown();
Carbon::setTestNow();
}
}