2016-05-30 05:14:17 +05:30
|
|
|
<?php
|
|
|
|
namespace codeception\api\unit\components\User;
|
|
|
|
|
2018-04-18 02:17:25 +05:30
|
|
|
use api\components\User\AuthenticationResult;
|
2016-05-30 05:14:17 +05:30
|
|
|
use api\components\User\Component;
|
2017-09-19 22:36:16 +05:30
|
|
|
use api\components\User\Identity;
|
|
|
|
use common\models\Account;
|
2016-05-30 05:14:17 +05:30
|
|
|
use common\models\AccountSession;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Emarref\Jwt\Claim;
|
|
|
|
use Emarref\Jwt\Jwt;
|
2016-06-05 19:31:35 +05:30
|
|
|
use Emarref\Jwt\Token;
|
2016-10-29 03:17:31 +05:30
|
|
|
use tests\codeception\api\unit\TestCase;
|
2016-05-30 05:14:17 +05:30
|
|
|
use tests\codeception\common\_support\ProtectedCaller;
|
|
|
|
use tests\codeception\common\fixtures\AccountFixture;
|
|
|
|
use tests\codeception\common\fixtures\AccountSessionFixture;
|
2017-02-23 04:31:32 +05:30
|
|
|
use tests\codeception\common\fixtures\MinecraftAccessKeyFixture;
|
2016-06-05 19:31:35 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\web\Request;
|
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
|
|
|
use ProtectedCaller;
|
|
|
|
|
|
|
|
/**
|
2016-12-11 20:28:08 +05:30
|
|
|
* @var Component|\PHPUnit_Framework_MockObject_MockObject
|
2016-05-30 05:14:17 +05:30
|
|
|
*/
|
|
|
|
private $component;
|
|
|
|
|
|
|
|
public function _before() {
|
|
|
|
parent::_before();
|
2017-09-19 22:36:16 +05:30
|
|
|
$this->component = new Component($this->getComponentConfig());
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
2016-10-29 03:17:31 +05:30
|
|
|
public function _fixtures() {
|
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
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function testCreateJwtAuthenticationToken() {
|
2016-06-05 19:31:35 +05:30
|
|
|
$this->mockRequest();
|
2017-09-19 22:36:16 +05:30
|
|
|
|
|
|
|
$account = new Account(['id' => 1]);
|
|
|
|
$result = $this->component->createJwtAuthenticationToken($account, false);
|
|
|
|
$this->assertInstanceOf(AuthenticationResult::class, $result);
|
|
|
|
$this->assertNull($result->getSession());
|
|
|
|
$this->assertEquals($account, $result->getAccount());
|
|
|
|
$payloads = (new Jwt())->deserialize($result->getJwt())->getPayload();
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals(time(), $payloads->findClaimByName(Claim\IssuedAt::NAME)->getValue(), '', 3);
|
|
|
|
/** @noinspection SummerTimeUnsafeTimeManipulationInspection */
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals(time() + 60 * 60 * 24 * 7, $payloads->findClaimByName('exp')->getValue(), '', 3);
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals('ely|1', $payloads->findClaimByName('sub')->getValue());
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals('accounts_web_user', $payloads->findClaimByName('ely-scopes')->getValue());
|
|
|
|
$this->assertNull($payloads->findClaimByName('jti'));
|
|
|
|
|
|
|
|
/** @var Account $account */
|
|
|
|
$account = $this->tester->grabFixture('accounts', 'admin');
|
|
|
|
$result = $this->component->createJwtAuthenticationToken($account, true);
|
|
|
|
$this->assertInstanceOf(AuthenticationResult::class, $result);
|
|
|
|
$this->assertInstanceOf(AccountSession::class, $result->getSession());
|
|
|
|
$this->assertEquals($account, $result->getAccount());
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertTrue($result->getSession()->refresh());
|
|
|
|
$payloads = (new Jwt())->deserialize($result->getJwt())->getPayload();
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals(time(), $payloads->findClaimByName(Claim\IssuedAt::NAME)->getValue(), '', 3);
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals(time() + 3600, $payloads->findClaimByName('exp')->getValue(), '', 3);
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals('ely|1', $payloads->findClaimByName('sub')->getValue());
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals('accounts_web_user', $payloads->findClaimByName('ely-scopes')->getValue());
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals($result->getSession()->id, $payloads->findClaimByName('jti')->getValue());
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function testRenewJwtAuthenticationToken() {
|
|
|
|
$userIP = '192.168.0.1';
|
|
|
|
$this->mockRequest($userIP);
|
|
|
|
/** @var AccountSession $session */
|
|
|
|
$session = $this->tester->grabFixture('sessions', 'admin');
|
|
|
|
$result = $this->component->renewJwtAuthenticationToken($session);
|
|
|
|
$this->assertInstanceOf(AuthenticationResult::class, $result);
|
|
|
|
$this->assertEquals($session, $result->getSession());
|
|
|
|
$this->assertEquals($session->account_id, $result->getAccount()->id);
|
|
|
|
$session->refresh(); // reload data from db
|
|
|
|
$this->assertEquals(time(), $session->last_refreshed_at, '', 3);
|
|
|
|
$this->assertEquals($userIP, $session->getReadableIp());
|
|
|
|
$payloads = (new Jwt())->deserialize($result->getJwt())->getPayload();
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals(time(), $payloads->findClaimByName(Claim\IssuedAt::NAME)->getValue(), '', 3);
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals(time() + 3600, $payloads->findClaimByName('exp')->getValue(), '', 3);
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals('ely|1', $payloads->findClaimByName('sub')->getValue());
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals('accounts_web_user', $payloads->findClaimByName('ely-scopes')->getValue());
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals($session->id, $payloads->findClaimByName('jti')->getValue(), 'session has not changed');
|
2016-06-05 19:31:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
public function testParseToken() {
|
|
|
|
$this->mockRequest();
|
2017-09-19 22:36:16 +05:30
|
|
|
$token = $this->callProtected($this->component, 'createToken', new Account(['id' => 1]));
|
|
|
|
$jwt = $this->callProtected($this->component, 'serializeToken', $token);
|
|
|
|
$this->assertInstanceOf(Token::class, $this->component->parseToken($jwt), 'success get RenewResult object');
|
2016-05-31 03:33:30 +05:30
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
public function testGetActiveSession() {
|
2017-09-19 22:36:16 +05:30
|
|
|
$account = $this->tester->grabFixture('accounts', 'admin');
|
|
|
|
$result = $this->component->createJwtAuthenticationToken($account, true);
|
|
|
|
$this->component->logout();
|
2017-02-23 04:31:32 +05:30
|
|
|
|
|
|
|
/** @var Component|\PHPUnit_Framework_MockObject_MockObject $component */
|
|
|
|
$component = $this->getMockBuilder(Component::class)
|
2017-09-19 22:36:16 +05:30
|
|
|
->setMethods(['getIsGuest'])
|
|
|
|
->setConstructorArgs([$this->getComponentConfig()])
|
2017-02-23 04:31:32 +05:30
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$component
|
2017-09-19 22:36:16 +05:30
|
|
|
->expects($this->any())
|
|
|
|
->method('getIsGuest')
|
|
|
|
->willReturn(false);
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$this->mockAuthorizationHeader($result->getJwt());
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$session = $component->getActiveSession();
|
|
|
|
$this->assertInstanceOf(AccountSession::class, $session);
|
|
|
|
/** @noinspection NullPointerExceptionInspection */
|
|
|
|
$this->assertEquals($session->id, $result->getSession()->id);
|
|
|
|
}
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
public function testTerminateSessions() {
|
|
|
|
/** @var AccountSession $session */
|
|
|
|
$session = AccountSession::findOne($this->tester->grabFixture('sessions', 'admin2')['id']);
|
2017-02-23 04:31:32 +05:30
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @var Component|\Mockery\MockInterface $component */
|
|
|
|
$component = mock(Component::class . '[getActiveSession]', [$this->getComponentConfig()])->shouldDeferMissing();
|
|
|
|
$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');
|
|
|
|
$component->createJwtAuthenticationToken($account, true);
|
2017-02-23 04:31:32 +05:30
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
$component->terminateSessions($account, Component::KEEP_CURRENT_SESSION);
|
|
|
|
$sessions = $account->getSessions()->all();
|
|
|
|
$this->assertEquals(1, count($sessions));
|
|
|
|
$this->assertTrue($sessions[0]->id === $session->id);
|
2016-05-30 05:14:17 +05:30
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-06-05 19:31:35 +05:30
|
|
|
private function mockRequest($userIP = '127.0.0.1') {
|
2017-09-19 22:36:16 +05:30
|
|
|
/** @var Request|\Mockery\MockInterface $request */
|
|
|
|
$request = mock(Request::class . '[getHostInfo,getUserIP]')->shouldDeferMissing();
|
|
|
|
$request->shouldReceive('getHostInfo')->andReturn('http://localhost');
|
|
|
|
$request->shouldReceive('getUserIP')->andReturn($userIP);
|
2016-06-05 19:31:35 +05:30
|
|
|
|
|
|
|
Yii::$app->set('request', $request);
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:28:08 +05:30
|
|
|
/**
|
|
|
|
* @param string $bearerToken
|
|
|
|
*/
|
|
|
|
private function mockAuthorizationHeader($bearerToken = null) {
|
|
|
|
if ($bearerToken !== null) {
|
|
|
|
$bearerToken = 'Bearer ' . $bearerToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
Yii::$app->request->headers->set('Authorization', $bearerToken);
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
private function getComponentConfig() {
|
2016-06-05 19:31:35 +05:30
|
|
|
return [
|
2017-09-19 22:36:16 +05:30
|
|
|
'identityClass' => Identity::class,
|
2016-06-05 19:31:35 +05:30
|
|
|
'enableSession' => false,
|
|
|
|
'loginUrl' => null,
|
|
|
|
'secret' => 'secret',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-05-30 05:14:17 +05:30
|
|
|
}
|