2016-05-30 05:14:17 +05:30
|
|
|
<?php
|
2019-07-26 19:34:57 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
namespace codeception\api\unit\components\User;
|
|
|
|
|
|
|
|
use api\components\User\Component;
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\components\User\JwtIdentity;
|
2019-09-22 21:12:21 +05:30
|
|
|
use api\components\User\LegacyOAuth2Identity;
|
2019-02-23 04:41:57 +05:30
|
|
|
use api\tests\unit\TestCase;
|
2017-09-19 22:36:16 +05:30
|
|
|
use common\models\Account;
|
2016-05-30 05:14:17 +05:30
|
|
|
use common\models\AccountSession;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\models\OauthClient;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\fixtures\AccountSessionFixture;
|
|
|
|
use common\tests\fixtures\MinecraftAccessKeyFixture;
|
2019-12-11 01:21:11 +05:30
|
|
|
use common\tests\fixtures\OauthClientFixture;
|
|
|
|
use common\tests\fixtures\OauthSessionFixture;
|
2019-08-02 05:59:20 +05:30
|
|
|
use Lcobucci\JWT\Claim\Basic;
|
|
|
|
use Lcobucci\JWT\Token;
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
class ComponentTest extends TestCase {
|
2016-05-30 05:14:17 +05:30
|
|
|
|
|
|
|
/**
|
2019-05-14 04:28:29 +05:30
|
|
|
* @var Component|\PHPUnit\Framework\MockObject\MockObject
|
2016-05-30 05:14:17 +05:30
|
|
|
*/
|
|
|
|
private $component;
|
|
|
|
|
|
|
|
public function _before() {
|
|
|
|
parent::_before();
|
2019-08-01 22:28:18 +05:30
|
|
|
$this->component = new Component();
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2016-05-30 05:14:17 +05:30
|
|
|
return [
|
|
|
|
'accounts' => AccountFixture::class,
|
|
|
|
'sessions' => AccountSessionFixture::class,
|
2017-02-23 04:31:32 +05:30
|
|
|
'minecraftSessions' => MinecraftAccessKeyFixture::class,
|
2019-12-11 01:21:11 +05:30
|
|
|
'oauthClients' => OauthClientFixture::class,
|
|
|
|
'oauthSessions' => OauthSessionFixture::class,
|
2016-05-30 05:14:17 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
public function testGetActiveSession() {
|
2019-08-02 05:59:20 +05:30
|
|
|
// User is guest
|
|
|
|
$component = new Component();
|
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is a Oauth2Identity
|
2019-12-14 02:46:05 +05:30
|
|
|
$component->setIdentity($this->createMock(LegacyOAuth2Identity::class));
|
2019-08-02 05:59:20 +05:30
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is correct, but have no jti claim
|
2019-12-14 02:46:05 +05:30
|
|
|
$identity = $this->createMock(JwtIdentity::class);
|
|
|
|
$identity->method('getToken')->willReturn(new Token());
|
2019-08-02 05:59:20 +05:30
|
|
|
$component->setIdentity($identity);
|
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is correct and has jti claim, but there is no associated session
|
2019-12-14 02:46:05 +05:30
|
|
|
$identity = $this->createMock(JwtIdentity::class);
|
|
|
|
$identity->method('getToken')->willReturn(new Token([], ['jti' => new Basic('jti', 999999)]));
|
2019-08-02 05:59:20 +05:30
|
|
|
$component->setIdentity($identity);
|
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is correct, has jti claim and associated session exists
|
2019-12-14 02:46:05 +05:30
|
|
|
$identity = $this->createMock(JwtIdentity::class);
|
|
|
|
$identity->method('getToken')->willReturn(new Token([], ['jti' => new Basic('jti', 1)]));
|
2019-08-02 05:59:20 +05:30
|
|
|
$component->setIdentity($identity);
|
|
|
|
$session = $component->getActiveSession();
|
|
|
|
$this->assertNotNull($session);
|
|
|
|
$this->assertSame(1, $session->id);
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function testTerminateSessions() {
|
|
|
|
/** @var AccountSession $session */
|
2019-07-26 19:34:57 +05:30
|
|
|
$session = $this->tester->grabFixture('sessions', 'admin2');
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2019-12-14 02:46:05 +05:30
|
|
|
$component = $this->createPartialMock(Component::class, ['getActiveSession']);
|
|
|
|
$component->expects($this->once())->method('getActiveSession')->willReturn($session);
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @var Account $account */
|
|
|
|
$account = $this->tester->grabFixture('accounts', 'admin');
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
// Dry run: no sessions should be removed
|
2017-09-19 22:36:16 +05:30
|
|
|
$component->terminateSessions($account, Component::KEEP_MINECRAFT_SESSIONS | Component::KEEP_SITE_SESSIONS);
|
|
|
|
$this->assertNotEmpty($account->getMinecraftAccessKeys()->all());
|
|
|
|
$this->assertNotEmpty($account->getSessions()->all());
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
// All Minecraft sessions should be removed. Web sessions should be kept
|
2017-09-19 22:36:16 +05:30
|
|
|
$component->terminateSessions($account, Component::KEEP_SITE_SESSIONS);
|
|
|
|
$this->assertEmpty($account->getMinecraftAccessKeys()->all());
|
|
|
|
$this->assertNotEmpty($account->getSessions()->all());
|
2019-12-11 01:21:11 +05:30
|
|
|
$this->assertEqualsWithDelta(time(), $account->getOauthSessions()->andWhere(['client_id' => OauthClient::UNAUTHORIZED_MINECRAFT_GAME_LAUNCHER])->one()->revoked_at, 5);
|
2016-06-05 19:31:35 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
// All sessions should be removed except the current one
|
2017-09-19 22:36:16 +05:30
|
|
|
$component->terminateSessions($account, Component::KEEP_CURRENT_SESSION);
|
|
|
|
$sessions = $account->getSessions()->all();
|
2019-02-26 04:56:02 +05:30
|
|
|
$this->assertCount(1, $sessions);
|
|
|
|
$this->assertSame($session->id, $sessions[0]->id);
|
2016-05-30 05:14:17 +05:30
|
|
|
|
2019-07-26 19:34:57 +05:30
|
|
|
// With no arguments each and every session should be removed
|
2017-09-19 22:36:16 +05:30
|
|
|
$component->terminateSessions($account);
|
|
|
|
$this->assertEmpty($account->getSessions()->all());
|
|
|
|
$this->assertEmpty($account->getMinecraftAccessKeys()->all());
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|