Добавлен экшен для получения инфы о текущем аутентифицированном пользователе

This commit is contained in:
ErickSkrauch 2016-02-24 01:34:01 +03:00
parent 1fff80c7e8
commit d9987a3185
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,44 @@
<?php
namespace api\controllers;
use common\models\Account;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
class UsersController extends Controller {
public function behaviors() {
return ArrayHelper::merge(parent::behaviors(), [
'access' => [
'class' => AccessControl::class,
'rules' => [
[
'actions' => ['current'],
'allow' => true,
'roles' => ['@'],
],
],
],
]);
}
public function verbs() {
return [
'current' => ['GET'],
];
}
public function actionCurrent() {
/** @var Account $account */
$account = Yii::$app->user->identity;
return [
'id' => $account->id,
'username' => $account->username,
'email' => $account->email,
'shouldChangePassword' => $account->password_hash_strategy === Account::PASS_HASH_STRATEGY_OLD_ELY,
];
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace tests\codeception\api\_pages;
use yii\codeception\BasePage;
/**
* @property \tests\codeception\api\FunctionalTester $actor
*/
class UsersRoute extends BasePage {
public function current() {
$this->route = ['users/current'];
$this->actor->sendGET($this->getUrl());
}
}

View File

@ -0,0 +1,36 @@
<?php
namespace tests\codeception\api\functional;
use Codeception\Scenario;
use Codeception\Specify;
use tests\codeception\api\_pages\UsersRoute;
use tests\codeception\api\functional\_steps\AccountSteps;
use tests\codeception\api\FunctionalTester;
class UsersCest {
/**
* @var UsersRoute
*/
private $route;
public function _before(FunctionalTester $I) {
$this->route = new UsersRoute($I);
}
public function testCurrent(FunctionalTester $I, Scenario $scenario) {
$I = new AccountSteps($scenario);
$I->loggedInAsActiveAccount();
$this->route->current();
$I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([
'id' => 1,
'username' => 'Admin',
'email' => 'admin@ely.by',
'shouldChangePassword' => false,
]);
}
}