2017-09-19 22:36:16 +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;
|
2017-09-19 22:36:16 +05:30
|
|
|
|
|
|
|
use api\components\User\IdentityInterface;
|
2019-08-02 05:59:20 +05:30
|
|
|
use api\rbac\rules\AccountOwner;
|
2017-09-19 22:36:16 +05:30
|
|
|
use common\models\Account;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\unit\TestCase;
|
2017-09-19 22:36:16 +05:30
|
|
|
use Yii;
|
|
|
|
use yii\rbac\Item;
|
|
|
|
use const common\LATEST_RULES_VERSION;
|
|
|
|
|
|
|
|
class AccountOwnerTest extends TestCase {
|
|
|
|
|
|
|
|
public function testExecute() {
|
|
|
|
$rule = new AccountOwner();
|
|
|
|
$item = new Item();
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
// Identity is null
|
|
|
|
$this->assertFalse($rule->execute('some token', $item, ['accountId' => 123]));
|
|
|
|
|
|
|
|
// Identity presented, but have no account
|
|
|
|
/** @var IdentityInterface|\Mockery\MockInterface $identity */
|
|
|
|
$identity = mock(IdentityInterface::class);
|
|
|
|
$identity->shouldReceive('getAccount')->andReturn(null);
|
|
|
|
Yii::$app->user->setIdentity($identity);
|
|
|
|
|
|
|
|
$this->assertFalse($rule->execute('some token', $item, ['accountId' => 123]));
|
|
|
|
|
|
|
|
// Identity has an account
|
2017-09-19 22:36:16 +05:30
|
|
|
$account = new Account();
|
|
|
|
$account->id = 1;
|
|
|
|
$account->status = Account::STATUS_ACTIVE;
|
|
|
|
$account->rules_agreement_version = LATEST_RULES_VERSION;
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
/** @var IdentityInterface|\Mockery\MockInterface $identity */
|
2017-09-19 22:36:16 +05:30
|
|
|
$identity = mock(IdentityInterface::class);
|
|
|
|
$identity->shouldReceive('getAccount')->andReturn($account);
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
Yii::$app->user->setIdentity($identity);
|
2017-09-19 22:36:16 +05:30
|
|
|
|
|
|
|
$this->assertFalse($rule->execute('token', $item, ['accountId' => 2]));
|
|
|
|
$this->assertFalse($rule->execute('token', $item, ['accountId' => '2']));
|
|
|
|
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1]));
|
|
|
|
$this->assertTrue($rule->execute('token', $item, ['accountId' => '1']));
|
|
|
|
$account->rules_agreement_version = null;
|
|
|
|
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1]));
|
|
|
|
$this->assertTrue($rule->execute('token', $item, ['accountId' => 1, 'optionalRules' => true]));
|
|
|
|
$account->rules_agreement_version = LATEST_RULES_VERSION;
|
|
|
|
$account->status = Account::STATUS_BANNED;
|
|
|
|
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1]));
|
|
|
|
$this->assertFalse($rule->execute('token', $item, ['accountId' => 1, 'optionalRules' => true]));
|
|
|
|
}
|
|
|
|
|
2019-08-02 05:59:20 +05:30
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testExecuteWithoutAccountId() {
|
|
|
|
$rule = new AccountOwner();
|
|
|
|
$this->assertFalse($rule->execute('token', new Item(), []));
|
|
|
|
}
|
|
|
|
|
2017-09-19 22:36:16 +05:30
|
|
|
}
|