From 9a38481f7de60cdd87849c60534460d8fae664b3 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 3 Apr 2017 14:54:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=B2=D0=BD=D1=83=D1=82=D1=80=D0=B5=D0=BD=D0=BD=D0=B8?= =?UTF-8?q?=D0=B9=20API=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80?= =?UTF-8?q?=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BE=D0=B1=20=D0=B0=D0=BA?= =?UTF-8?q?=D0=BA=D0=B0=D1=83=D0=BD=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controllers/AccountsController.php | 30 ++++++++ common/models/OauthScope.php | 5 ++ .../codeception/api/_pages/InternalRoute.php | 5 ++ .../api/functional/internal/InfoCest.php | 74 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 tests/codeception/api/functional/internal/InfoCest.php diff --git a/api/modules/internal/controllers/AccountsController.php b/api/modules/internal/controllers/AccountsController.php index 34f4dbe..5b734c4 100644 --- a/api/modules/internal/controllers/AccountsController.php +++ b/api/modules/internal/controllers/AccountsController.php @@ -9,6 +9,7 @@ use common\models\Account; use common\models\OauthScope as S; use Yii; use yii\helpers\ArrayHelper; +use yii\web\BadRequestHttpException; use yii\web\NotFoundHttpException; class AccountsController extends Controller { @@ -26,6 +27,11 @@ class AccountsController extends Controller { 'allow' => true, 'roles' => [S::ACCOUNT_BLOCK], ], + [ + 'actions' => ['info'], + 'allow' => true, + 'roles' => [S::INTERNAL_ACCOUNT_INFO], + ], ], ], ]); @@ -34,6 +40,7 @@ class AccountsController extends Controller { public function verbs() { return [ 'ban' => ['POST', 'DELETE'], + 'info' => ['GET'], ]; } @@ -46,6 +53,29 @@ class AccountsController extends Controller { } } + public function actionInfo(int $id = null, string $username = null, string $uuid = null) { + if ($id !== null) { + $account = Account::findOne($id); + } elseif ($username !== null) { + $account = Account::findOne(['username' => $username]); + } elseif ($uuid !== null) { + $account = Account::findOne(['uuid' => $uuid]); + } else { + throw new BadRequestHttpException('One of the required get params must be presented.'); + } + + if ($account === null) { + throw new NotFoundHttpException('User by provided param not found.'); + } + + return [ + 'id' => $account->id, + 'uuid' => $account->uuid, + 'email' => $account->email, + 'username' => $account->username, + ]; + } + private function banAccount(Account $account) { $model = new BanForm($account); $model->load(Yii::$app->request->post()); diff --git a/common/models/OauthScope.php b/common/models/OauthScope.php index bb4da54..e20dc11 100644 --- a/common/models/OauthScope.php +++ b/common/models/OauthScope.php @@ -28,6 +28,11 @@ class OauthScope { * @owner machine */ const ACCOUNT_BLOCK = 'account_block'; + /** + * @internal + * @owner machine + */ + const INTERNAL_ACCOUNT_INFO = 'internal_account_info'; public static function find(): OauthScopeQuery { return new OauthScopeQuery(static::queryScopes()); diff --git a/tests/codeception/api/_pages/InternalRoute.php b/tests/codeception/api/_pages/InternalRoute.php index ad4fbe2..46c7eae 100644 --- a/tests/codeception/api/_pages/InternalRoute.php +++ b/tests/codeception/api/_pages/InternalRoute.php @@ -18,4 +18,9 @@ class InternalRoute extends BasePage { $this->actor->sendDELETE($this->getUrl()); } + public function info(string $param, string $value) { + $this->route = '/internal/accounts/info'; + $this->actor->sendGET($this->getUrl(), [$param => $value]); + } + } diff --git a/tests/codeception/api/functional/internal/InfoCest.php b/tests/codeception/api/functional/internal/InfoCest.php new file mode 100644 index 0000000..986f9ff --- /dev/null +++ b/tests/codeception/api/functional/internal/InfoCest.php @@ -0,0 +1,74 @@ +route = new InternalRoute($I); + } + + public function testGetInfoById(OauthSteps $I) { + $accessToken = $I->getAccessTokenByClientCredentialsGrant([S::INTERNAL_ACCOUNT_INFO]); + $I->amBearerAuthenticated($accessToken); + + $this->route->info('id', 1); + $this->expectSuccessResponse($I); + } + + public function testGetInfoByUuid(OauthSteps $I) { + $accessToken = $I->getAccessTokenByClientCredentialsGrant([S::INTERNAL_ACCOUNT_INFO]); + $I->amBearerAuthenticated($accessToken); + + $this->route->info('uuid', 'df936908-b2e1-544d-96f8-2977ec213022'); + $this->expectSuccessResponse($I); + } + + public function testGetInfoByUsername(OauthSteps $I) { + $accessToken = $I->getAccessTokenByClientCredentialsGrant([S::INTERNAL_ACCOUNT_INFO]); + $I->amBearerAuthenticated($accessToken); + + $this->route->info('username', 'admin'); + $this->expectSuccessResponse($I); + } + + public function testInvalidParams(OauthSteps $I) { + $accessToken = $I->getAccessTokenByClientCredentialsGrant([S::INTERNAL_ACCOUNT_INFO]); + $I->amBearerAuthenticated($accessToken); + + $this->route->info('', ''); + $I->canSeeResponseCodeIs(400); + } + + public function testAccountNotFound(OauthSteps $I) { + $accessToken = $I->getAccessTokenByClientCredentialsGrant([S::INTERNAL_ACCOUNT_INFO]); + $I->amBearerAuthenticated($accessToken); + + $this->route->info('username', 'this-user-not-exists'); + $I->canSeeResponseCodeIs(404); + } + + /** + * @param OauthSteps $I + */ + private function expectSuccessResponse(OauthSteps $I): void { + $I->canSeeResponseCodeIs(200); + $I->canSeeResponseIsJson(); + $I->canSeeResponseContainsJson([ + 'id' => 1, + 'uuid' => 'df936908-b2e1-544d-96f8-2977ec213022', + 'email' => 'admin@ely.by', + 'username' => 'Admin', + ]); + } + +}