feat: 增强管理员 API 鉴权,新增 token 有效天数配置,更新相关异常处理与错误码引用

This commit is contained in:
2026-05-09 11:26:39 +08:00
parent 8a70c029f6
commit f1b38ef421
13 changed files with 124 additions and 42 deletions

View File

@@ -34,7 +34,7 @@ test('admin login returns bearer token when captcha passes validation', function
]);
$resp->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.admin.username', 'tester')
->assertJsonPath('data.admin.nickname', '测试昵称')
->assertJsonStructure(['data' => ['token', 'token_type', 'admin' => ['id', 'username', 'nickname', 'email']]]);
@@ -52,7 +52,7 @@ test('admin captcha exposes key and image base64', function () {
$resp = $this->getJson('/api/v1/admin/auth/captcha');
$resp->assertOk()
->assertJsonPath('code', 0);
->assertJsonPath('code', ErrorCode::Success->value);
$data = $resp->json('data');
expect($data)->toBeArray()

View File

@@ -1,5 +1,6 @@
<?php
use App\Lottery\ErrorCode;
use App\Models\Player;
use Firebase\JWT\JWT;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -20,25 +21,26 @@ test('player me returns profile with dev bearer', function () {
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/player/me')
->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.id', $player->id)
->assertJsonPath('data.site_player_id', 'uid-42')
->assertJsonPath('data.username', 'alice');
});
test('player auth missing bearer returns localized sso 8001', function () {
$code = ErrorCode::PlayerAuthorizationInvalid->value;
$this->withHeader('Accept-Language', 'zh-CN,zh;q=0.9')
->getJson('/api/v1/player/me')
->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertJsonPath('code', 8001)
->assertJsonPath('msg', __('sso.8001', [], 'zh'));
->assertJsonPath('code', $code)
->assertJsonPath('msg', __("sso.$code", [], 'zh'));
});
test('api unknown route returns unified not_found json without hitting locale middleware', function () {
$this->withHeader('X-Locale', 'zh')
->getJson('/api/v1/player/__no_route__xxx')
->assertStatus(Response::HTTP_NOT_FOUND)
->assertJsonPath('code', 9004)
->assertJsonPath('code', ErrorCode::NotFound->value)
->assertJsonPath('msg', __('api.not_found', [], 'zh'));
});

View File

@@ -1,5 +1,6 @@
<?php
use App\Lottery\ErrorCode;
use App\Models\Player;
use App\Models\PlayerWallet;
use Illuminate\Foundation\Testing\RefreshDatabase;
@@ -22,7 +23,7 @@ test('wallet balance creates lottery wallet row and returns zeros', function ()
])->getJson('/api/v1/wallet/balance');
$response->assertOk()
->assertJsonPath('code', 0)
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.balance', 0)
->assertJsonPath('data.frozen_balance', 0)
->assertJsonPath('data.currency_code', 'NPR')
@@ -45,5 +46,5 @@ test('wallet balance rejects illegal currency query', function () {
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/wallet/balance?currency=!!')
->assertStatus(400)
->assertJsonPath('code', 1005);
->assertJsonPath('code', ErrorCode::WalletInvalidCurrency->value);
});