mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Отрефакторен компонент User\Component
Добавлен метод getActiveSession Добавлена логика для функции "Разлогинить всех" в форме смены пароля
This commit is contained in:
@ -9,10 +9,14 @@ use Codeception\Specify;
|
||||
use common\models\AccountSession;
|
||||
use Emarref\Jwt\Algorithm\AlgorithmInterface;
|
||||
use Emarref\Jwt\Claim\ClaimInterface;
|
||||
use Emarref\Jwt\Token;
|
||||
use tests\codeception\api\unit\DbTestCase;
|
||||
use tests\codeception\common\_support\ProtectedCaller;
|
||||
use tests\codeception\common\fixtures\AccountFixture;
|
||||
use tests\codeception\common\fixtures\AccountSessionFixture;
|
||||
use Yii;
|
||||
use yii\web\HeaderCollection;
|
||||
use yii\web\Request;
|
||||
|
||||
/**
|
||||
* @property AccountFixture $accounts
|
||||
@ -22,29 +26,14 @@ class ComponentTest extends DbTestCase {
|
||||
use Specify;
|
||||
use ProtectedCaller;
|
||||
|
||||
private $originalRemoteHost;
|
||||
|
||||
/**
|
||||
* @var Component
|
||||
*/
|
||||
private $component;
|
||||
|
||||
public function _before() {
|
||||
$this->originalRemoteHost = $_SERVER['REMOTE_ADDR'] ?? null;
|
||||
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
||||
parent::_before();
|
||||
|
||||
$this->component = new Component([
|
||||
'identityClass' => AccountIdentity::class,
|
||||
'enableSession' => false,
|
||||
'loginUrl' => null,
|
||||
'secret' => 'secret',
|
||||
]);
|
||||
}
|
||||
|
||||
public function _after() {
|
||||
parent::_after();
|
||||
$_SERVER['REMOTE_ADDR'] = $this->originalRemoteHost;
|
||||
$this->component = new Component($this->getComponentArguments());
|
||||
}
|
||||
|
||||
public function fixtures() {
|
||||
@ -55,6 +44,7 @@ class ComponentTest extends DbTestCase {
|
||||
}
|
||||
|
||||
public function testLogin() {
|
||||
$this->mockRequest();
|
||||
$this->specify('success get LoginResult object without session value', function() {
|
||||
$account = new AccountIdentity(['id' => 1]);
|
||||
$result = $this->component->login($account, false);
|
||||
@ -78,29 +68,84 @@ class ComponentTest extends DbTestCase {
|
||||
|
||||
public function testRenew() {
|
||||
$this->specify('success get RenewResult object', function() {
|
||||
$userIP = '192.168.0.1';
|
||||
$this->mockRequest($userIP);
|
||||
/** @var AccountSession $session */
|
||||
$session = AccountSession::findOne($this->sessions['admin']['id']);
|
||||
$callTime = time();
|
||||
$usedRemoteAddr = $_SERVER['REMOTE_ADDR'] ?? null;
|
||||
$_SERVER['REMOTE_ADDR'] = '192.168.0.1';
|
||||
$result = $this->component->renew($session);
|
||||
expect($result)->isInstanceOf(RenewResult::class);
|
||||
expect(is_string($result->getJwt()))->true();
|
||||
expect($result->getIdentity()->getId())->equals($session->account_id);
|
||||
$session->refresh();
|
||||
expect($session->last_refreshed_at)->greaterOrEquals($callTime);
|
||||
expect($session->getReadableIp())->equals($_SERVER['REMOTE_ADDR']);
|
||||
$_SERVER['REMOTE_ADDR'] = $usedRemoteAddr;
|
||||
expect($session->getReadableIp())->equals($userIP);
|
||||
});
|
||||
}
|
||||
|
||||
public function testGetJWT() {
|
||||
public function testParseToken() {
|
||||
$this->mockRequest();
|
||||
$this->specify('success get RenewResult object', function() {
|
||||
$identity = new AccountIdentity(['id' => 1]);
|
||||
$token = $this->callProtected($this->component, 'createToken', $identity);
|
||||
$jwt = $this->callProtected($this->component, 'serializeToken', $token);
|
||||
|
||||
expect($this->component->parseToken($jwt))->isInstanceOf(Token::class);
|
||||
});
|
||||
}
|
||||
|
||||
public function testGetActiveSession() {
|
||||
$this->specify('get used account session', function() {
|
||||
/** @var AccountIdentity $identity */
|
||||
$identity = AccountIdentity::findOne($this->accounts['admin']['id']);
|
||||
$result = $this->component->login($identity, true);
|
||||
$this->component->logout();
|
||||
|
||||
/** @var Component|\PHPUnit_Framework_MockObject_MockObject $component */
|
||||
$component = $this->getMock(Component::class, ['getIsGuest'], [$this->getComponentArguments()]);
|
||||
$component
|
||||
->expects($this->any())
|
||||
->method('getIsGuest')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
/** @var HeaderCollection|\PHPUnit_Framework_MockObject_MockObject $headersCollection */
|
||||
$headersCollection = $this->getMock(HeaderCollection::class, ['get']);
|
||||
$headersCollection
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->with($this->equalTo('Authorization'))
|
||||
->will($this->returnValue('Bearer ' . $result->getJwt()));
|
||||
|
||||
/** @var Request|\PHPUnit_Framework_MockObject_MockObject $request */
|
||||
$request = $this->getMock(Request::class, ['getHeaders']);
|
||||
$request
|
||||
->expects($this->any())
|
||||
->method('getHeaders')
|
||||
->will($this->returnValue($headersCollection));
|
||||
|
||||
Yii::$app->set('request', $request);
|
||||
|
||||
$session = $component->getActiveSession();
|
||||
expect($session)->isInstanceOf(AccountSession::class);
|
||||
expect($session->id)->equals($result->getSession()->id);
|
||||
});
|
||||
}
|
||||
|
||||
public function testSerializeToken() {
|
||||
$this->specify('get string, contained jwt token', function() {
|
||||
expect($this->component->getJWT(new AccountIdentity(['id' => 1])))
|
||||
$token = new Token();
|
||||
expect($this->callProtected($this->component, 'serializeToken', $token))
|
||||
->regExp('/^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+\/=]*$/');
|
||||
});
|
||||
}
|
||||
|
||||
public function testCreateToken() {
|
||||
$this->specify('create token', function() {
|
||||
expect($this->callProtected($this->component, 'createToken', new AccountIdentity(['id' => 1])))
|
||||
->isInstanceOf(Token::class);
|
||||
});
|
||||
}
|
||||
|
||||
public function testGetAlgorithm() {
|
||||
$this->specify('get expected hash algorithm object', function() {
|
||||
expect($this->component->getAlgorithm())->isInstanceOf(AlgorithmInterface::class);
|
||||
@ -117,4 +162,33 @@ class ComponentTest extends DbTestCase {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
private function mockRequest($userIP = '127.0.0.1') {
|
||||
$request = $this->getMock(Request::class, ['getHostInfo', 'getUserIP']);
|
||||
$request
|
||||
->expects($this->any())
|
||||
->method('getHostInfo')
|
||||
->will($this->returnValue('http://localhost'));
|
||||
|
||||
$request
|
||||
->expects($this->any())
|
||||
->method('getUserIP')
|
||||
->will($this->returnValue($userIP));
|
||||
|
||||
Yii::$app->set('request', $request);
|
||||
|
||||
return $request;
|
||||
}
|
||||
|
||||
private function getComponentArguments() {
|
||||
return [
|
||||
'identityClass' => AccountIdentity::class,
|
||||
'enableSession' => false,
|
||||
'loginUrl' => null,
|
||||
'secret' => 'secret',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user