Объединены сущности для авторизации посредством JWT токенов и токенов, выданных через oAuth2.

Все действия, связанные с аккаунтами, теперь вызываются через url `/api/v1/accounts/<id>/<action>`.
Добавлена вменяемая система разграничения прав на основе RBAC.
Теперь oAuth2 токены генерируются как случайная строка в 40 символов длинной, а не UUID.
Исправлен баг с неправильным временем жизни токена в ответе успешного запроса аутентификации.
Теперь все unit тесты можно успешно прогнать без наличия интернета.
This commit is contained in:
ErickSkrauch
2017-09-19 20:06:16 +03:00
parent 928b3aa7fc
commit dd2c4bc413
173 changed files with 2719 additions and 2748 deletions

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace tests\codeception\common\helpers;
use phpmock\mockery\PHPMockery;
use ReflectionClass;
class Mock {
/**
* @param string $className
* @param string $function
*
* @return \Mockery\Expectation
*/
public static function func(string $className, string $function) {
return PHPMockery::mock(self::getClassNamespace($className), $function);
}
private static function getClassNamespace(string $className): string {
return (new ReflectionClass($className))->getNamespaceName();
}
}

View File

@@ -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\rules\AccountOwner;
use tests\codeception\common\unit\TestCase;
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();
$account = new Account();
$account->id = 1;
$account->status = Account::STATUS_ACTIVE;
$account->rules_agreement_version = LATEST_RULES_VERSION;
$identity = mock(IdentityInterface::class);
$identity->shouldReceive('getAccount')->andReturn($account);
$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, ['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]));
}
/**
* @expectedException \yii\base\InvalidParamException
*/
public function testExecuteWithException() {
(new AccountOwner())->execute('', new Item(), []);
}
}

View File

@@ -3,8 +3,10 @@ namespace codeception\common\unit\validators;
use common\validators\EmailValidator;
use tests\codeception\common\fixtures\AccountFixture;
use tests\codeception\common\helpers\Mock;
use tests\codeception\common\unit\TestCase;
use yii\base\Model;
use yii\validators\EmailValidator as YiiEmailValidator;
class EmailValidatorTest extends TestCase {
@@ -29,6 +31,7 @@ class EmailValidatorTest extends TestCase {
}
public function testValidateAttributeLength() {
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->andReturnTrue();
$model = $this->createModel(
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
'emailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemailemail' .
@@ -44,6 +47,8 @@ class EmailValidatorTest extends TestCase {
}
public function testValidateAttributeEmail() {
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(3)->andReturnValues([false, false, true]);
$model = $this->createModel('non-email');
$this->validator->validateAttribute($model, 'field');
$this->assertEquals(['error.email_invalid'], $model->getErrors('field'));
@@ -58,6 +63,8 @@ class EmailValidatorTest extends TestCase {
}
public function testValidateAttributeTempmail() {
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(2)->andReturnTrue();
$model = $this->createModel('ibrpycwyjdnt@dropmail.me');
$this->validator->validateAttribute($model, 'field');
$this->assertEquals(['error.email_is_tempmail'], $model->getErrors('field'));
@@ -68,6 +75,8 @@ class EmailValidatorTest extends TestCase {
}
public function testValidateAttributeUnique() {
Mock::func(YiiEmailValidator::class, 'checkdnsrr')->times(3)->andReturnTrue();
$this->tester->haveFixtures([
'accounts' => AccountFixture::class,
]);