Files
lotteryLaravel/tests/Feature/WalletBalanceTest.php
kang fe0594beaa feat: 增强钱包 API URL 验证与配置
- 在 AdminIntegrationSiteStoreRequest 和 AdminIntegrationSiteUpdateRequest 中引入 WalletApiUrlRule,确保 wallet_api_url 字段符合 HTTPS 公开域名要求。
- 更新 HttpMainSiteWalletBalanceClient 和 HttpMainSiteWalletGateway,使用 WalletApiUrlSanitizer 进行 URL 规范化与验证,防止 SSRF 攻击。
- 新增测试用例,验证 wallet_api_url 的有效性,确保系统安全性与稳定性。
- 更新 .env.example 文件,添加 LOTTERY_RISK_POOL_USE_REDIS_LUA 配置项以支持 Redis Lua 原子扣减功能。
- 修改 package-lock.json 中的项目名称,确保一致性。
- 在 API 路由中新增 integration/runtime-origins 路由,提供运行时白名单功能。
2026-05-28 10:10:26 +08:00

107 lines
3.3 KiB
PHP

<?php
use App\Models\Player;
use App\Lottery\ErrorCode;
use App\Models\PlayerWallet;
use Database\Seeders\CurrencySeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
uses(RefreshDatabase::class);
beforeEach(function (): void {
$this->seed(CurrencySeeder::class);
config(['lottery.main_site.wallet_api_url' => null]);
});
test('wallet balance creates lottery wallet row and returns zeros', function () {
$player = Player::query()->create([
'site_code' => 'test',
'site_player_id' => 'p1',
'username' => null,
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$response = $this->withHeaders([
'Authorization' => 'Bearer dev:'.$player->id,
'X-Locale' => 'zh',
])->getJson('/api/v1/wallet/balance');
$response->assertOk()
->assertJsonPath('code', ErrorCode::Success->value)
->assertJsonPath('data.balance', 0)
->assertJsonPath('data.available_balance', 0)
->assertJsonPath('data.frozen_balance', 0)
->assertJsonPath('data.currency_code', 'NPR')
->assertJsonPath('data.wallet_type', 'lottery')
->assertJsonPath('data.main_balance', null);
expect(PlayerWallet::query()->where('player_id', $player->id)->count())->toBe(1);
});
test('wallet balance rejects illegal currency query', function () {
$player = Player::query()->create([
'site_code' => 'test',
'site_player_id' => 'p2',
'username' => null,
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/wallet/balance?currency=!!')
->assertStatus(400)
->assertJsonPath('code', ErrorCode::WalletInvalidCurrency->value);
});
test('wallet balance returns main_balance when main site wallet api is configured', function () {
config([
'lottery.main_site.wallet_api_url' => 'https://fake-main.test',
'lottery.main_site.wallet_balance_path' => '/wallet/balance',
]);
Http::preventStrayRequests();
Http::fake([
'https://fake-main.test/wallet/balance*' => Http::response([
'success' => true,
'data' => [
'main_balance' => 499_900,
'currency_code' => 'NPR',
],
], 200),
]);
$player = Player::query()->create([
'site_code' => 'main-site',
'site_player_id' => '10003',
'username' => null,
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/wallet/balance')
->assertOk()
->assertJsonPath('data.main_balance', 499_900);
});
test('wallet balance rejects currency that is not configured or disabled', function () {
$player = Player::query()->create([
'site_code' => 'test',
'site_player_id' => 'p3',
'username' => null,
'nickname' => null,
'default_currency' => 'NPR',
'status' => 0,
]);
$this->withHeader('Authorization', 'Bearer dev:'.$player->id)
->getJson('/api/v1/wallet/balance?currency=ABC')
->assertStatus(400)
->assertJsonPath('code', ErrorCode::WalletInvalidCurrency->value);
});