2018-02-28 03:57:35 +05:30
|
|
|
<?php
|
2019-08-01 22:28:18 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
namespace api\tests\unit\rbac\rules;
|
2018-02-28 03:57:35 +05:30
|
|
|
|
|
|
|
use api\components\User\IdentityInterface;
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\Permissions as P;
|
|
|
|
use api\rbac\rules\OauthClientOwner;
|
2018-02-28 03:57:35 +05:30
|
|
|
use common\models\Account;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures\OauthClientFixture;
|
|
|
|
use common\tests\unit\TestCase;
|
2019-08-02 18:27:17 +05:30
|
|
|
use InvalidArgumentException;
|
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();
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
// Client not exists (we expect true to let controller throw corresponding 404 exception)
|
|
|
|
$this->assertTrue($rule->execute('some token', $item, ['clientId' => 'not exists client id']));
|
|
|
|
|
|
|
|
// Client exists, but identity is null
|
|
|
|
$this->assertFalse($rule->execute('some token', $item, ['clientId' => 'ely']));
|
|
|
|
|
|
|
|
// Client exists, identity presented, but have no account
|
2019-12-14 02:46:05 +05:30
|
|
|
$identity = $this->createMock(IdentityInterface::class);
|
|
|
|
$identity->method('getAccount')->willReturn(null);
|
2019-08-02 05:59:20 +05:30
|
|
|
Yii::$app->user->setIdentity($identity);
|
|
|
|
|
|
|
|
$this->assertFalse($rule->execute('some token', $item, ['clientId' => 'ely']));
|
|
|
|
|
|
|
|
// Identity has an account
|
2018-02-28 03:57:35 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->id = 1;
|
|
|
|
$account->status = Account::STATUS_ACTIVE;
|
|
|
|
$account->rules_agreement_version = LATEST_RULES_VERSION;
|
|
|
|
|
2019-12-14 02:46:05 +05:30
|
|
|
$identity = $this->createMock(IdentityInterface::class);
|
|
|
|
$identity->method('getAccount')->willReturn($account);
|
2019-08-02 05:59:20 +05:30
|
|
|
Yii::$app->user->setIdentity($identity);
|
2018-02-28 03:57:35 +05:30
|
|
|
|
|
|
|
$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]));
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
public function testExecuteWithoutClientId() {
|
2019-08-02 18:27:17 +05:30
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
$rule = new OauthClientOwner();
|
|
|
|
$this->assertFalse($rule->execute('token', new Item(), []));
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
}
|