2018-02-28 03:57:35 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace common\tests\unit\rbac\rules;
|
2018-02-28 03:57:35 +05:30
|
|
|
|
|
|
|
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;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\OauthClientFixture;
|
|
|
|
use common\tests\unit\TestCase;
|
2018-02-28 03:57:35 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\rbac\Item;
|
|
|
|
use const common\LATEST_RULES_VERSION;
|
|
|
|
|
|
|
|
class OauthClientOwnerTest extends TestCase {
|
|
|
|
|
2019-05-14 04:28:29 +05:30
|
|
|
public function _fixtures(): array {
|
2018-02-28 03:57:35 +05:30
|
|
|
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 */
|
2019-07-25 18:59:08 +05:30
|
|
|
$component = mock(Component::class . '[findIdentityByAccessToken]', [[
|
|
|
|
'secret' => 'secret',
|
2019-07-26 13:41:09 +05:30
|
|
|
'publicKeyPath' => 'data/certs/public.crt',
|
|
|
|
'privateKeyPath' => 'data/certs/private.key',
|
2019-07-25 18:59:08 +05:30
|
|
|
]]);
|
2018-02-28 03:57:35 +05:30
|
|
|
$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']));
|
2018-03-26 00:51:22 +05:30
|
|
|
$this->assertTrue($rule->execute('token', $item, ['clientId' => 'not-exists-client']));
|
2018-02-28 03:57:35 +05:30
|
|
|
$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]));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|