Add additional tests cases, implemented custom error formatter

This commit is contained in:
ErickSkrauch 2022-01-28 12:38:00 +01:00
parent 9593d1e809
commit f4fa5ad2df
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
3 changed files with 92 additions and 5 deletions

View File

@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
namespace api\modules\mojang\behaviors;
use Closure;
use Yii;
use yii\base\Behavior;
use yii\base\Event;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\UnauthorizedHttpException;
final class ServiceErrorConverterBehavior extends Behavior {
public function events(): array {
return [
Response::EVENT_BEFORE_SEND => Closure::fromCallable([$this, 'beforeSend']),
];
}
private function beforeSend(Event $event): void {
/** @var Response $response */
$response = $event->sender;
$data = $response->data;
if ($data === null || !isset($data['status'])) {
return;
}
$request = Yii::$app->request;
$type = $data['type'];
switch ($type) {
case UnauthorizedHttpException::class:
$response->data = [
'path' => '/' . $request->getPathInfo(),
'errorType' => 'UnauthorizedOperationException',
'error' => 'UnauthorizedOperationException',
'errorMessage' => 'Unauthorized',
'developerMessage' => 'Unauthorized',
];
break;
case NotFoundHttpException::class:
$response->data = [
'path' => '/' . $request->getPathInfo(),
'errorType' => 'NOT_FOUND',
'error' => 'NOT_FOUND',
'errorMessage' => 'The server has not found anything matching the request URI',
'developerMessage' => 'The server has not found anything matching the request URI',
];
break;
}
}
}

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace api\modules\mojang\controllers; namespace api\modules\mojang\controllers;
use api\controllers\Controller; use api\controllers\Controller;
use api\modules\mojang\behaviors\ServiceErrorConverterBehavior;
use api\rbac\Permissions; use api\rbac\Permissions;
use common\components\SkinsSystemApi; use common\components\SkinsSystemApi;
use Exception; use Exception;
@ -12,6 +13,7 @@ use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
use yii\web\NotFoundHttpException;
use function Ramsey\Uuid\v3; use function Ramsey\Uuid\v3;
final class ServicesController extends Controller { final class ServicesController extends Controller {
@ -44,9 +46,16 @@ final class ServicesController extends Controller {
]); ]);
} }
public function init(): void {
parent::init();
$this->response->attachBehavior('errorFormatter', ServiceErrorConverterBehavior::class);
}
public function actionProfile(SkinsSystemApi $skinsSystemApi): array { public function actionProfile(SkinsSystemApi $skinsSystemApi): array {
/** @var \common\models\Account $account at this point null value isn't possible */
$account = Yii::$app->user->identity->getAccount(); $account = Yii::$app->user->identity->getAccount();
if ($account === null) {
throw new NotFoundHttpException();
}
try { try {
$textures = $skinsSystemApi->textures($account->username); $textures = $skinsSystemApi->textures($account->username);

View File

@ -3,15 +3,15 @@ declare(strict_types=1);
namespace api\tests\functional\mojang; namespace api\tests\functional\mojang;
use api\tests\functional\_steps\OauthSteps;
use api\tests\FunctionalTester; use api\tests\FunctionalTester;
class ProfileCest { final class ProfileCest {
public function getProfile(FunctionalTester $I): void { public function getProfile(FunctionalTester $I): void {
$I->amAuthenticated(); $I->amAuthenticated();
$I->sendGet('/api/mojang/services/minecraft/profile'); $I->sendGet('/api/mojang/services/minecraft/profile');
$I->canSeeResponseCodeIs(200); $I->canSeeResponseCodeIs(200);
$I->canSeeResponseIsJson();
$I->canSeeResponseContainsJson([ $I->canSeeResponseContainsJson([
'id' => 'df936908b2e1544d96f82977ec213022', 'id' => 'df936908b2e1544d96f82977ec213022',
'name' => 'Admin', 'name' => 'Admin',
@ -28,7 +28,31 @@ class ProfileCest {
]); ]);
} }
// TODO: add cases for unauthenticated user public function getProfileAsServiceAccount(OauthSteps $I): void {
// TODO: add cases for authenticated as a service account user $accessToken = $I->getAccessTokenByClientCredentialsGrant(['internal_account_info']);
$I->amBearerAuthenticated($accessToken);
$I->sendGet('/api/mojang/services/minecraft/profile');
$I->canSeeResponseCodeIs(404);
$I->canSeeResponseContainsJson([
'path' => '/mojang/services/minecraft/profile',
'errorType' => 'NOT_FOUND',
'error' => 'NOT_FOUND',
'errorMessage' => 'The server has not found anything matching the request URI',
'developerMessage' => 'The server has not found anything matching the request URI',
]);
}
public function getProfileWithoutAuthentication(FunctionalTester $I): void {
$I->sendGet('/api/mojang/services/minecraft/profile');
$I->canSeeResponseCodeIs(401);
$I->canSeeResponseContainsJson([
'path' => '/mojang/services/minecraft/profile',
'errorType' => 'UnauthorizedOperationException',
'error' => 'UnauthorizedOperationException',
'errorMessage' => 'Unauthorized',
'developerMessage' => 'Unauthorized',
]);
}
} }