mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Implementation of the backend for the OAuth2 clients management
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\models;
|
||||
|
||||
use common\models\OauthClient;
|
||||
use tests\codeception\common\fixtures\OauthClientFixture;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
|
||||
class OauthClientQueryTest extends TestCase {
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'oauthClients' => OauthClientFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testDefaultHideDeletedEntries() {
|
||||
/** @var OauthClient[] $clients */
|
||||
$clients = OauthClient::find()->all();
|
||||
$this->assertEmpty(array_filter($clients, function(OauthClient $client) {
|
||||
return (bool)$client->is_deleted === true;
|
||||
}));
|
||||
$this->assertNull(OauthClient::findOne('deleted-oauth-client'));
|
||||
}
|
||||
|
||||
public function testAllowFindDeletedEntries() {
|
||||
/** @var OauthClient[] $clients */
|
||||
$clients = OauthClient::find()->includeDeleted()->all();
|
||||
$this->assertNotEmpty(array_filter($clients, function(OauthClient $client) {
|
||||
return (bool)$client->is_deleted === true;
|
||||
}));
|
||||
$client = OauthClient::find()
|
||||
->includeDeleted()
|
||||
->andWhere(['id' => 'deleted-oauth-client'])
|
||||
->one();
|
||||
$this->assertInstanceOf(OauthClient::class, $client);
|
||||
$deletedClients = OauthClient::find()->onlyDeleted()->all();
|
||||
$this->assertEmpty(array_filter($deletedClients, function(OauthClient $client) {
|
||||
return (bool)$client->is_deleted === false;
|
||||
}));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -40,6 +40,7 @@ class AccountOwnerTest extends TestCase {
|
||||
|
||||
Yii::$app->set('user', $component);
|
||||
|
||||
$this->assertFalse($rule->execute('token', $item, []));
|
||||
$this->assertFalse($rule->execute('token', $item, ['accountId' => 2]));
|
||||
$this->assertFalse($rule->execute('token', $item, ['accountId' => '2']));
|
||||
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1]));
|
||||
@@ -53,11 +54,4 @@ class AccountOwnerTest extends TestCase {
|
||||
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1, 'optionalRules' => true]));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \yii\base\InvalidParamException
|
||||
*/
|
||||
public function testExecuteWithException() {
|
||||
(new AccountOwner())->execute('', new Item(), []);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\rbac\rules;
|
||||
|
||||
use api\components\User\Component;
|
||||
use api\components\User\IdentityInterface;
|
||||
use common\models\Account;
|
||||
use common\rbac\Permissions as P;
|
||||
use common\rbac\rules\OauthClientOwner;
|
||||
use tests\codeception\common\fixtures\OauthClientFixture;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use Yii;
|
||||
use yii\rbac\Item;
|
||||
use const common\LATEST_RULES_VERSION;
|
||||
|
||||
class OauthClientOwnerTest extends TestCase {
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'oauthClients' => OauthClientFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$rule = new OauthClientOwner();
|
||||
$item = new Item();
|
||||
|
||||
$account = new Account();
|
||||
$account->id = 1;
|
||||
$account->status = Account::STATUS_ACTIVE;
|
||||
$account->rules_agreement_version = LATEST_RULES_VERSION;
|
||||
|
||||
/** @var IdentityInterface|\Mockery\MockInterface $identity */
|
||||
$identity = mock(IdentityInterface::class);
|
||||
$identity->shouldReceive('getAccount')->andReturn($account);
|
||||
|
||||
/** @var Component|\Mockery\MockInterface $component */
|
||||
$component = mock(Component::class . '[findIdentityByAccessToken]', [['secret' => 'secret']]);
|
||||
$component->shouldDeferMissing();
|
||||
$component->shouldReceive('findIdentityByAccessToken')->withArgs(['token'])->andReturn($identity);
|
||||
|
||||
Yii::$app->set('user', $component);
|
||||
|
||||
$this->assertFalse($rule->execute('token', $item, []));
|
||||
$this->assertTrue($rule->execute('token', $item, ['clientId' => 'admin-oauth-client']));
|
||||
$this->assertFalse($rule->execute('token', $item, ['clientId' => 'not-exists-client']));
|
||||
$account->id = 2;
|
||||
$this->assertFalse($rule->execute('token', $item, ['clientId' => 'admin-oauth-client']));
|
||||
$item->name = P::VIEW_OWN_OAUTH_CLIENTS;
|
||||
$this->assertTrue($rule->execute('token', $item, ['accountId' => 2]));
|
||||
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1]));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\tasks;
|
||||
|
||||
use common\models\OauthClient;
|
||||
use common\models\OauthSession;
|
||||
use common\tasks\ClearOauthSessions;
|
||||
use tests\codeception\common\fixtures;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
use yii\queue\Queue;
|
||||
|
||||
class ClearOauthSessionsTest extends TestCase {
|
||||
|
||||
public function _fixtures() {
|
||||
return [
|
||||
'oauthClients' => fixtures\OauthClientFixture::class,
|
||||
'oauthSessions' => fixtures\OauthSessionFixture::class,
|
||||
];
|
||||
}
|
||||
|
||||
public function testCreateFromClient() {
|
||||
$client = new OauthClient();
|
||||
$client->id = 'mocked-id';
|
||||
|
||||
$result = ClearOauthSessions::createFromOauthClient($client);
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $result);
|
||||
$this->assertSame('mocked-id', $result->clientId);
|
||||
$this->assertNull($result->notSince);
|
||||
|
||||
$result = ClearOauthSessions::createFromOauthClient($client, time());
|
||||
$this->assertInstanceOf(ClearOauthSessions::class, $result);
|
||||
$this->assertSame('mocked-id', $result->clientId);
|
||||
$this->assertEquals(time(), $result->notSince, '', 1);
|
||||
}
|
||||
|
||||
public function testExecute() {
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'deleted-oauth-client-with-sessions';
|
||||
$task->notSince = 1519510065;
|
||||
$task->execute(mock(Queue::class));
|
||||
|
||||
$this->assertFalse(OauthSession::find()->andWhere(['id' => 3])->exists());
|
||||
$this->assertTrue(OauthSession::find()->andWhere(['id' => 4])->exists());
|
||||
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'deleted-oauth-client-with-sessions';
|
||||
$task->execute(mock(Queue::class));
|
||||
|
||||
$this->assertFalse(OauthSession::find()->andWhere(['id' => 4])->exists());
|
||||
|
||||
$task = new ClearOauthSessions();
|
||||
$task->clientId = 'some-not-exists-client-id';
|
||||
$task->execute(mock(Queue::class));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace tests\codeception\common\unit\validators;
|
||||
|
||||
use common\validators\MinecraftServerAddressValidator;
|
||||
use tests\codeception\common\unit\TestCase;
|
||||
|
||||
class MinecraftServerAddressValidatorTest extends TestCase {
|
||||
|
||||
/**
|
||||
* @dataProvider domainNames
|
||||
*/
|
||||
public function testValidate($address, $shouldBeValid) {
|
||||
$validator = new MinecraftServerAddressValidator();
|
||||
$validator->validate($address, $errors);
|
||||
$this->assertEquals($shouldBeValid, $errors === null);
|
||||
}
|
||||
|
||||
public function domainNames() {
|
||||
return [
|
||||
['localhost', true ],
|
||||
['localhost:25565', true ],
|
||||
['mc.hypixel.net', true ],
|
||||
['mc.hypixel.net:25565', true ],
|
||||
['136.243.88.97', true ],
|
||||
['136.243.88.97:25565', true ],
|
||||
['http://ely.by', false],
|
||||
['http://ely.by:80', false],
|
||||
['ely.by/abcd', false],
|
||||
['ely.by?abcd', false],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user