feat: 添加E2E测试环境配置支持
Some checks failed
lotterLaravel CI / test (push) Has been cancelled

添加LOTTERY_E2E环境变量来控制E2E测试相关功能,
包括绕过验证码、登录限制和钱包API URL验证,
同时更新composer.json以包含E2E专用的提供者和服务。
This commit is contained in:
2026-06-18 14:42:22 +08:00
parent 6b2ea39ea1
commit 6ec9634704
45 changed files with 3702 additions and 6 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Http\Controllers\Api\V1\E2E;
use App\Services\AdminCaptchaService;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* E2E 辅助peek captcha bypass 码。
*
* digest HMAC(app.key) 单向,存进 cache 的就是 digest无法反查明文。
* 改用 e2e 约定captcha_code == "LOTTERY_E2E_BYPASS" LOTTERY_E2E=true 时视为通过。
* 本端点不返回任何"答案",仅返回约定码提示。
*/
final class E2ECaptchaPeekController extends \App\Http\Controllers\Controller
{
public function player(Request $request): JsonResponse
{
$key = (string) $request->input('captcha_key', '');
if ($key === '') {
return ApiResponse::error('captcha_key required', 'e2e_invalid_input', null, 422);
}
return $this->peek($key, AdminCaptchaService::SCOPE_PLAYER);
}
public function admin(Request $request): JsonResponse
{
$key = (string) $request->input('captcha_key', '');
if ($key === '') {
return ApiResponse::error('captcha_key required', 'e2e_invalid_input', null, 422);
}
return $this->peek($key, AdminCaptchaService::SCOPE_ADMIN);
}
private function peek(string $key, string $scope): JsonResponse
{
return ApiResponse::success([
'bypass_code' => 'LOTTERY_E2E_BYPASS',
'scope' => $scope,
'key' => $key,
'hint' => 'POST captcha_code=LOTTERY_E2E_BYPASS to login when LOTTERY_E2E=true',
]);
}
}

View File

@@ -0,0 +1,127 @@
<?php
namespace App\Http\Controllers\Api\V1\E2E;
use App\Lottery\DrawStatus;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* E2E 辅助:期号时间/状态快进。
*
* 真实生产链路下"等 12 分钟后封盘"是脆的——CI e2e 不可能等。
* 这里允许 e2e 直接 SQL close_time / draw_time模拟
* - close-now: close_time 改到 1 秒前、status=closed
* - reopen: status=open、重置冷却期
* - inspect: 返回 draw 完整时间/状态快照
*
* 严格仅在 LOTTERY_E2E=true 时由 E2EServiceProvider 注册。
*/
final class E2EDrawController extends \App\Http\Controllers\Controller
{
public function closeNow(Request $request, string $drawNo): JsonResponse
{
$draw = DB::table('draws')->where('draw_no', $drawNo)->first();
if (! $draw) {
return ApiResponse::error('draw not found', 'e2e_draw_missing', null, 404);
}
DB::table('draws')
->where('id', $draw->id)
->update([
'status' => DrawStatus::Closed->value,
'close_time' => now()->subSeconds(5),
'draw_time' => now()->subSeconds(3),
]);
return $this->inspectById((int) $draw->id);
}
/** 将冷静期结束时间改到过去,便于 tick 推进 cooldown → settling。 */
public function finishCooldown(Request $request, string $drawNo): JsonResponse
{
$draw = DB::table('draws')->where('draw_no', $drawNo)->first();
if (! $draw) {
return ApiResponse::error('draw not found', 'e2e_draw_missing', null, 404);
}
DB::table('draws')
->where('id', $draw->id)
->update([
'cooling_end_time' => now()->subSeconds(5),
'draw_time' => now()->subMinutes(2),
'close_time' => now()->subMinutes(3),
]);
return $this->inspectById((int) $draw->id);
}
public function reopen(Request $request, string $drawNo): JsonResponse
{
$draw = DB::table('draws')->where('draw_no', $drawNo)->first();
if (! $draw) {
return ApiResponse::error('draw not found', 'e2e_draw_missing', null, 404);
}
DB::table('draws')
->where('id', $draw->id)
->update([
'status' => DrawStatus::Open->value,
'start_time' => now()->subMinutes(5),
'close_time' => now()->addMinutes(20),
'draw_time' => now()->addMinutes(21),
'cooling_end_time' => null,
]);
return $this->inspectById((int) $draw->id);
}
public function inspect(Request $request, string $drawNo): JsonResponse
{
$draw = DB::table('draws')->where('draw_no', $drawNo)->first();
if (! $draw) {
return ApiResponse::error('draw not found', 'e2e_draw_missing', null, 404);
}
return $this->inspectById((int) $draw->id);
}
/** 同步跑一次 lottery:draw-tick不开调度。返回该 tick 推进的 draw_no 列表。 */
public function tick(Request $request): JsonResponse
{
try {
$exit = \Illuminate\Support\Facades\Artisan::call('lottery:draw-tick');
} catch (\Throwable $e) {
return ApiResponse::error(
'draw-tick failed: '.$e->getMessage(),
'e2e_tick_failed',
['trace' => array_slice(explode("\n", $e->getTraceAsString()), 0, 5)],
500,
);
}
return ApiResponse::success([
'exit_code' => $exit,
'output' => \Illuminate\Support\Facades\Artisan::output(),
]);
}
private function inspectById(int $id): JsonResponse
{
$d = DB::table('draws')->where('id', $id)->first();
return ApiResponse::success([
'id' => (int) $d->id,
'draw_no' => $d->draw_no,
'status' => (string) $d->status,
'business_date' => $d->business_date,
'start_time' => $d->start_time,
'close_time' => $d->close_time,
'draw_time' => $d->draw_time,
'cooling_end_time' => $d->cooling_end_time,
'current_result_version' => (int) $d->current_result_version,
'settle_version' => (int) $d->settle_version,
'is_reopened' => (bool) $d->is_reopened,
]);
}
}

View File

@@ -0,0 +1,62 @@
<?php
namespace App\Http\Controllers\Api\V1\E2E;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* E2E 辅助:只读探查(用于断言前的""快照)。
*
* - credit-ledger: 玩家最近 N credit_ledger 流水
* - wallet-txns: 玩家最近 N wallet_transactions
* - ticket-items: 玩家最近 N ticket_items
*
* 这些是真实表 + 真实 SQL但只读不写只用于 e2e 写断言时核对中间态。
* 严格仅在 LOTTERY_E2E=true 时由 E2EServiceProvider 注册。
*/
final class E2EInspectController extends \App\Http\Controllers\Controller
{
public function creditLedger(Request $request): JsonResponse
{
return $this->tailTable('credit_ledger', $request);
}
public function walletTxns(Request $request): JsonResponse
{
return $this->tailTable('wallet_txns', $request);
}
public function ticketItems(Request $request): JsonResponse
{
return $this->tailTable('ticket_items', $request);
}
/**
* @return JsonResponse
*/
private function tailTable(string $table, Request $request): JsonResponse
{
$limit = max(1, min(100, (int) $request->input('limit', 20)));
$playerId = $request->input('player_id');
$q = DB::table($table);
if ($playerId !== null) {
$pid = (int) $playerId;
if ($table === 'credit_ledger') {
$q->where('owner_type', 'player')->where('owner_id', $pid);
} else {
$q->where('player_id', $pid);
}
}
$rows = $q->orderByDesc('id')->limit($limit)->get();
return ApiResponse::success([
'table' => $table,
'count' => $rows->count(),
'rows' => $rows->map(fn ($r) => (array) $r)->values()->all(),
]);
}
}

View File

@@ -0,0 +1,162 @@
<?php
namespace App\Http\Controllers\Api\V1\E2E;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
/**
* E2E 辅助:重置/查询玩家状态。
*
* - reset: 失败计数 / 锁定 / 余额 / 状态 全部归位
* - set-balance: 强制把 player_wallets.balance 写成指定值(不改 frozen避开版本锁
* - unlock: 仅清失败计数 + 锁定(不动余额)
* - inspect: 返回玩家当前完整状态快照(用于断言前的状态探查)
*
* 严格仅在 LOTTERY_E2E=true 时由 E2EServiceProvider 注册。
*/
final class E2EPlayerStateController extends \App\Http\Controllers\Controller
{
public function reset(Request $request): JsonResponse
{
$username = (string) env('E2E_PLAYER_USERNAME', 'demo_player');
$siteCode = (string) env('E2E_PLAYER_SITE_CODE', 'demo');
$balance = (int) env('DEV_SEED_WALLET_BALANCE_MINOR', 1_250_000);
$currency = strtoupper((string) env('LOTTERY_DEFAULT_CURRENCY', config('lottery.default_currency', 'NPR')));
$playerId = DB::table('players')
->where('site_code', $siteCode)
->where('username', $username)
->value('id');
if (! $playerId) {
return ApiResponse::error('e2e player not found', 'e2e_player_missing', null, 404);
}
DB::transaction(function () use ($playerId, $balance, $currency) {
DB::table('players')
->where('id', $playerId)
->update([
'login_failed_count' => 0,
'login_locked_until' => null,
'status' => 0,
]);
DB::table('player_wallets')
->where('player_id', $playerId)
->where('wallet_type', 'lottery')
->where('currency_code', $currency)
->update([
'balance' => $balance,
'frozen_balance' => 0,
'version' => DB::raw('version + 1'),
]);
});
return ApiResponse::success([
'player_id' => (int) $playerId,
'username' => $username,
'balance' => $balance,
'currency' => $currency,
]);
}
public function setBalance(Request $request): JsonResponse
{
$username = (string) env('E2E_PLAYER_USERNAME', 'demo_player');
$siteCode = (string) env('E2E_PLAYER_SITE_CODE', 'demo');
$currency = strtoupper((string) $request->input('currency', config('lottery.default_currency', 'NPR')));
$balance = (int) $request->input('balance', -1);
if ($balance < 0) {
return ApiResponse::error('balance must be >= 0', 'e2e_invalid_input', null, 422);
}
$playerId = DB::table('players')
->where('site_code', $siteCode)
->where('username', $username)
->value('id');
if (! $playerId) {
return ApiResponse::error('e2e player not found', 'e2e_player_missing', null, 404);
}
DB::table('player_wallets')
->where('player_id', $playerId)
->where('wallet_type', 'lottery')
->where('currency_code', $currency)
->update([
'balance' => $balance,
'frozen_balance' => 0,
'version' => DB::raw('version + 1'),
]);
return ApiResponse::success([
'player_id' => (int) $playerId,
'balance' => $balance,
'currency' => $currency,
]);
}
public function unlock(Request $request): JsonResponse
{
$username = (string) env('E2E_PLAYER_USERNAME', 'demo_player');
$siteCode = (string) env('E2E_PLAYER_SITE_CODE', 'demo');
$affected = DB::table('players')
->where('site_code', $siteCode)
->where('username', $username)
->update([
'login_failed_count' => 0,
'login_locked_until' => null,
'status' => 0,
]);
return ApiResponse::success([
'username' => $username,
'affected_rows' => $affected,
]);
}
public function inspect(Request $request): JsonResponse
{
$username = (string) env('E2E_PLAYER_USERNAME', 'demo_player');
$siteCode = (string) env('E2E_PLAYER_SITE_CODE', 'demo');
$player = DB::table('players')
->where('site_code', $siteCode)
->where('username', $username)
->first();
if (! $player) {
return ApiResponse::error('e2e player not found', 'e2e_player_missing', null, 404);
}
$wallets = DB::table('player_wallets')
->where('player_id', $player->id)
->get(['wallet_type', 'currency_code', 'balance', 'frozen_balance', 'status', 'version']);
return ApiResponse::success([
'player' => [
'id' => (int) $player->id,
'username' => $player->username,
'site_code' => $player->site_code,
'auth_source' => $player->auth_source,
'funding_mode' => $player->funding_mode,
'status' => (int) $player->status,
'login_failed_count' => (int) $player->login_failed_count,
'login_locked_until' => $player->login_locked_until,
'default_currency' => $player->default_currency,
],
'wallets' => $wallets->map(fn ($w) => [
'wallet_type' => $w->wallet_type,
'currency_code' => $w->currency_code,
'balance' => (int) $w->balance,
'frozen_balance' => (int) $w->frozen_balance,
'status' => (int) $w->status,
'version' => (int) $w->version,
])->values()->all(),
]);
}
}

View File

@@ -0,0 +1,214 @@
<?php
namespace App\Http\Controllers\Api\V1\E2E;
use App\Models\AdminSite;
use App\Models\AgentNode;
use App\Models\Player;
use App\Models\PlayerWallet;
use App\Services\Agent\AgentNodeService;
use App\Services\Integration\PartnerSiteConfigResolver;
use App\Support\ApiResponse;
use App\Support\PlayerAuthSource;
use Firebase\JWT\JWT;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
/**
* E2E 辅助:信用玩家 / SSO JWT / 主站钱包 mock 配置。
*/
final class E2EProvisionController extends \App\Http\Controllers\Controller
{
public function setupCreditPlayer(Request $request): JsonResponse
{
$siteCode = (string) ($request->input('site_code') ?: env('E2E_PLAYER_SITE_CODE', 'demo'));
$username = (string) ($request->input('username') ?: 'e2e_credit_player');
$password = (string) ($request->input('password') ?: env('E2E_PLAYER_PASSWORD', '12345678'));
$creditLimit = max(1, (int) $request->input('credit_limit', 50_000));
$currency = strtoupper((string) ($request->input('currency') ?: config('lottery.default_currency', 'NPR')));
$site = AdminSite::query()->where('code', $siteCode)->first();
if ($site === null) {
return ApiResponse::error('site not found', 'e2e_site_missing', null, 404);
}
$extra = $site->extra_json ?? [];
if (! is_array($extra)) {
$extra = json_decode((string) $extra, true);
if (! is_array($extra)) {
$extra = [];
}
}
$extra['credit_line_mode'] = true;
$site->extra_json = $extra;
$site->save();
$rootId = (int) DB::table('agent_nodes')
->where('admin_site_id', $site->id)
->where('depth', 0)
->value('id');
if ($rootId <= 0) {
return ApiResponse::error('root agent missing', 'e2e_agent_missing', null, 500);
}
$leafCode = (string) $request->input('agent_code', 'e2e_leaf');
$leaf = AgentNode::query()
->where('admin_site_id', $site->id)
->where('code', $leafCode)
->first();
if ($leaf === null) {
$super = \App\Models\AdminUser::query()->where('username', 'admin')->first();
if ($super === null) {
return ApiResponse::error('admin user missing', 'e2e_admin_missing', null, 500);
}
$leaf = app(AgentNodeService::class)->createChild($super, [
'parent_id' => $rootId,
'code' => $leafCode,
'name' => 'E2E Leaf Agent',
'username' => 'e2e_leaf_agent',
'password' => (string) env('E2E_PLAYER_PASSWORD', '12345678'),
'total_share_rate' => 25,
'credit_limit' => 200_000,
'rebate_limit' => 0.01,
'default_player_rebate' => 0.005,
]);
}
/** @var Player $player */
$player = Player::query()->updateOrCreate(
['site_code' => $siteCode, 'username' => $username],
[
'site_player_id' => 'e2e-credit-'.substr(md5($username), 0, 8),
'password_hash' => Hash::make($password),
'auth_source' => PlayerAuthSource::LOTTERY_NATIVE,
'funding_mode' => 'credit',
'nickname' => 'E2E Credit',
'default_currency' => $currency,
'status' => 0,
'login_failed_count' => 0,
'login_locked_until' => null,
'agent_node_id' => $leaf->id,
],
);
PlayerWallet::query()->updateOrCreate(
['player_id' => $player->id, 'wallet_type' => 'lottery', 'currency_code' => $currency],
[
'balance' => 0,
'frozen_balance' => 0,
'status' => 0,
'version' => 0,
],
);
DB::table('player_credit_accounts')->updateOrInsert(
['player_id' => $player->id],
[
'credit_limit' => $creditLimit,
'used_credit' => 0,
'frozen_credit' => 0,
'updated_at' => now(),
'created_at' => now(),
],
);
DB::table('player_rebate_profiles')->updateOrInsert(
['player_id' => $player->id, 'game_type' => '*'],
[
'rebate_rate' => 0.005,
'extra_rebate_rate' => 0,
'inherit_from_agent' => true,
'updated_at' => now(),
'created_at' => now(),
],
);
return ApiResponse::success([
'player_id' => (int) $player->id,
'username' => $username,
'password' => $password,
'site_code' => $siteCode,
'admin_site_id' => (int) $site->id,
'agent_node_id' => (int) $leaf->id,
'credit_limit' => $creditLimit,
'currency' => $currency,
]);
}
public function mintSsoJwt(Request $request): JsonResponse
{
$siteCode = (string) ($request->input('site_code') ?: env('E2E_PLAYER_SITE_CODE', 'demo'));
$sitePlayerId = (string) ($request->input('site_player_id') ?: 'e2e-sso-'.bin2hex(random_bytes(4)));
$site = AdminSite::query()->where('code', $siteCode)->first();
if ($site === null) {
return ApiResponse::error('site not found', 'e2e_site_missing', null, 404);
}
$secret = $site->decryptedSsoJwtSecret();
if (! is_string($secret) || $secret === '') {
return ApiResponse::error('sso secret missing on site', 'e2e_sso_secret_missing', null, 500);
}
$now = time();
$jwt = JWT::encode([
'site_code' => $siteCode,
'site_player_id' => $sitePlayerId,
'iat' => $now,
'exp' => $now + 300,
], $secret, 'HS256');
return ApiResponse::success([
'jwt' => $jwt,
'site_code' => $siteCode,
'site_player_id' => $sitePlayerId,
]);
}
public function configureWalletApi(Request $request): JsonResponse
{
$siteCode = (string) ($request->input('site_code') ?: env('E2E_PLAYER_SITE_CODE', 'demo'));
$baseUrl = rtrim((string) $request->input('base_url', ''), '/');
if ($baseUrl === '') {
return ApiResponse::error('base_url required', 'e2e_invalid_input', null, 422);
}
$site = AdminSite::query()->where('code', $siteCode)->first();
if ($site === null) {
return ApiResponse::error('site not found', 'e2e_site_missing', null, 404);
}
$site->wallet_api_url = $baseUrl;
if ($request->filled('wallet_api_key')) {
$site->wallet_api_key_encrypted = encrypt((string) $request->input('wallet_api_key'));
}
$site->save();
app(PartnerSiteConfigResolver::class)->forgetCache($siteCode);
return ApiResponse::success([
'site_code' => $siteCode,
'wallet_api_url' => $baseUrl,
]);
}
public function resetWalletApi(Request $request): JsonResponse
{
$siteCode = (string) ($request->input('site_code') ?: env('E2E_PLAYER_SITE_CODE', 'demo'));
$site = AdminSite::query()->where('code', $siteCode)->first();
if ($site === null) {
return ApiResponse::error('site not found', 'e2e_site_missing', null, 404);
}
$site->wallet_api_url = null;
$site->save();
app(PartnerSiteConfigResolver::class)->forgetCache($siteCode);
return ApiResponse::success(['site_code' => $siteCode, 'wallet_api_url' => null]);
}
}