mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Добавлен контроллер для блокировки аккаунта
Добавлен client_credentials grant для oAuth Рефакторинг структуры OauthScopes чтобы можно было разделить владельца прав на пользовательские и общие (машинные) Исправлена стилистика кода, внедряются фишки PHP 7.1
This commit is contained in:
16
tests/codeception/api/_pages/InternalRoute.php
Normal file
16
tests/codeception/api/_pages/InternalRoute.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\_pages;
|
||||
|
||||
use yii\codeception\BasePage;
|
||||
|
||||
/**
|
||||
* @property \tests\codeception\api\FunctionalTester $actor
|
||||
*/
|
||||
class InternalRoute extends BasePage {
|
||||
|
||||
public function ban($accountId) {
|
||||
$this->route = '/internal/accounts/' . $accountId . '/ban';
|
||||
$this->actor->sendPOST($this->getUrl());
|
||||
}
|
||||
|
||||
}
|
@@ -3,11 +3,12 @@ namespace tests\codeception\api\functional\_steps;
|
||||
|
||||
use common\models\OauthScope as S;
|
||||
use tests\codeception\api\_pages\OauthRoute;
|
||||
use tests\codeception\api\FunctionalTester;
|
||||
|
||||
class OauthSteps extends \tests\codeception\api\FunctionalTester {
|
||||
class OauthSteps extends FunctionalTester {
|
||||
|
||||
public function getAuthCode(array $permissions = []) {
|
||||
// TODO: по идее можно напрямую сделать зпись в базу, что ускорит процесс тестирования
|
||||
// TODO: по идее можно напрямую сделать запись в базу, что ускорит процесс тестирования
|
||||
$this->loggedInAsActiveAccount();
|
||||
$route = new OauthRoute($this);
|
||||
$route->complete([
|
||||
@@ -31,7 +32,7 @@ class OauthSteps extends \tests\codeception\api\FunctionalTester {
|
||||
}
|
||||
|
||||
public function getRefreshToken(array $permissions = []) {
|
||||
// TODO: по идее можно напрямую сделать зпись в базу, что ускорит процесс тестирования
|
||||
// TODO: по идее можно напрямую сделать запись в базу, что ускорит процесс тестирования
|
||||
$authCode = $this->getAuthCode(array_merge([S::OFFLINE_ACCESS], $permissions));
|
||||
$response = $this->issueToken($authCode);
|
||||
|
||||
@@ -51,4 +52,18 @@ class OauthSteps extends \tests\codeception\api\FunctionalTester {
|
||||
return json_decode($this->grabResponse(), true);
|
||||
}
|
||||
|
||||
public function getAccessTokenByClientCredentialsGrant(array $permissions = [], $useTrusted = true) {
|
||||
$route = new OauthRoute($this);
|
||||
$route->issueToken([
|
||||
'client_id' => $useTrusted ? 'trusted-client' : 'default-client',
|
||||
'client_secret' => $useTrusted ? 'tXBbyvMcyaOgHMOAXBpN2EC7uFoJAaL9' : 'AzWRy7ZjS1yRQUk2vRBDic8fprOKDB1W',
|
||||
'grant_type' => 'client_credentials',
|
||||
'scope' => implode(',', $permissions),
|
||||
]);
|
||||
|
||||
$response = json_decode($this->grabResponse(), true);
|
||||
|
||||
return $response['access_token'];
|
||||
}
|
||||
|
||||
}
|
||||
|
47
tests/codeception/api/functional/internal/BanCest.php
Normal file
47
tests/codeception/api/functional/internal/BanCest.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\functional\internal;
|
||||
|
||||
use common\models\OauthScope as S;
|
||||
use tests\codeception\api\_pages\InternalRoute;
|
||||
use tests\codeception\api\functional\_steps\OauthSteps;
|
||||
use tests\codeception\api\FunctionalTester;
|
||||
|
||||
class BanCest {
|
||||
|
||||
/**
|
||||
* @var InternalRoute
|
||||
*/
|
||||
private $route;
|
||||
|
||||
public function _before(FunctionalTester $I) {
|
||||
$this->route = new InternalRoute($I);
|
||||
}
|
||||
|
||||
public function testBanAccount(OauthSteps $I) {
|
||||
$accessToken = $I->getAccessTokenByClientCredentialsGrant([S::ACCOUNT_BLOCK]);
|
||||
$I->amBearerAuthenticated($accessToken);
|
||||
|
||||
$this->route->ban(1);
|
||||
$I->canSeeResponseCodeIs(200);
|
||||
$I->canSeeResponseIsJson();
|
||||
$I->canSeeResponseContainsJson([
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testBanBannedAccount(OauthSteps $I) {
|
||||
$accessToken = $I->getAccessTokenByClientCredentialsGrant([S::ACCOUNT_BLOCK]);
|
||||
$I->amBearerAuthenticated($accessToken);
|
||||
|
||||
$this->route->ban(10);
|
||||
$I->canSeeResponseCodeIs(200);
|
||||
$I->canSeeResponseIsJson();
|
||||
$I->canSeeResponseContainsJson([
|
||||
'success' => false,
|
||||
'errors' => [
|
||||
'account' => 'error.account_already_banned',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
@@ -1,11 +1,26 @@
|
||||
<?php
|
||||
namespace tests\codeception\api\unit\modules\internal\models;
|
||||
|
||||
use api\modules\internal\models\BlockForm;
|
||||
use api\modules\internal\helpers\Error as E;
|
||||
use api\modules\internal\models\BanForm;
|
||||
use common\models\Account;
|
||||
use tests\codeception\api\unit\TestCase;
|
||||
|
||||
class BlockFormTest extends TestCase {
|
||||
class BanFormTest extends TestCase {
|
||||
|
||||
public function testValidateAccountActivity() {
|
||||
$account = new Account();
|
||||
$account->status = Account::STATUS_ACTIVE;
|
||||
$form = new BanForm($account);
|
||||
$form->validateAccountActivity();
|
||||
$this->assertEmpty($form->getErrors('account'));
|
||||
|
||||
$account = new Account();
|
||||
$account->status = Account::STATUS_BANNED;
|
||||
$form = new BanForm($account);
|
||||
$form->validateAccountActivity();
|
||||
$this->assertEquals([E::ACCOUNT_ALREADY_BANNED], $form->getErrors('account'));
|
||||
}
|
||||
|
||||
public function testBan() {
|
||||
/** @var Account|\PHPUnit_Framework_MockObject_MockObject $account */
|
||||
@@ -17,7 +32,7 @@ class BlockFormTest extends TestCase {
|
||||
->method('save')
|
||||
->willReturn(true);
|
||||
|
||||
$model = new BlockForm($account);
|
||||
$model = new BanForm($account);
|
||||
$this->assertTrue($model->ban());
|
||||
$this->assertEquals(Account::STATUS_BANNED, $account->status);
|
||||
$this->tester->canSeeAmqpMessageIsCreated('events');
|
||||
@@ -27,14 +42,14 @@ class BlockFormTest extends TestCase {
|
||||
$account = new Account();
|
||||
$account->id = 3;
|
||||
|
||||
$model = new BlockForm($account);
|
||||
$model = new BanForm($account);
|
||||
$model->createTask();
|
||||
$message = json_decode($this->tester->grabLastSentAmqpMessage('events')->body, true);
|
||||
$this->assertSame(3, $message['accountId']);
|
||||
$this->assertSame(-1, $message['duration']);
|
||||
$this->assertSame('', $message['message']);
|
||||
|
||||
$model = new BlockForm($account);
|
||||
$model = new BanForm($account);
|
||||
$model->duration = 123;
|
||||
$model->message = 'test';
|
||||
$model->createTask();
|
Reference in New Issue
Block a user