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;
|
|
|
|
use api\components\User\Oauth2Identity;
|
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-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\AccountFixture;
|
|
|
|
use common\tests\fixtures\AccountSessionFixture;
|
|
|
|
use common\tests\fixtures\MinecraftAccessKeyFixture;
|
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,
|
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
|
|
|
|
$component->setIdentity(mock(Oauth2Identity::class));
|
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is correct, but have no jti claim
|
|
|
|
/** @var JwtIdentity|\Mockery\MockInterface $identity */
|
|
|
|
$identity = mock(JwtIdentity::class);
|
|
|
|
$identity->shouldReceive('getToken')->andReturn(new Token());
|
|
|
|
$component->setIdentity($identity);
|
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is correct and has jti claim, but there is no associated session
|
|
|
|
/** @var JwtIdentity|\Mockery\MockInterface $identity */
|
|
|
|
$identity = mock(JwtIdentity::class);
|
|
|
|
$identity->shouldReceive('getToken')->andReturn(new Token([], ['jti' => new Basic('jti', 999999)]));
|
|
|
|
$component->setIdentity($identity);
|
|
|
|
$this->assertNull($component->getActiveSession());
|
|
|
|
|
|
|
|
// Identity is correct, has jti claim and associated session exists
|
|
|
|
/** @var JwtIdentity|\Mockery\MockInterface $identity */
|
|
|
|
$identity = mock(JwtIdentity::class);
|
|
|
|
$identity->shouldReceive('getToken')->andReturn(new Token([], ['jti' => new Basic('jti', 1)]));
|
|
|
|
$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
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @var Component|\Mockery\MockInterface $component */
|
2019-08-01 22:28:18 +05:30
|
|
|
$component = mock(Component::class . '[getActiveSession]')->makePartial();
|
2017-09-19 22:36:16 +05:30
|
|
|
$component->shouldReceive('getActiveSession')->times(1)->andReturn($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());
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|