AgentApiRegressionTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Controllers\admin\Agent as AdminAgent;
  4. use App\Http\Controllers\AgentApiController;
  5. use App\Http\Middleware\AgentAuthMiddleware;
  6. use App\Models\Agent\Agent;
  7. use App\Models\Agent\AgentBalanceLog;
  8. use App\Models\Agent\AgentLoginLog;
  9. use App\Models\Agent\AgentRebateRecord;
  10. use App\Models\Agent\AgentToken;
  11. use App\Models\Agent\AgentWithdrawAccount;
  12. use App\Models\Agent\AgentWithdrawal;
  13. use App\Services\Agent\AgentCommissionService;
  14. use App\Services\Agent\AgentMemberService;
  15. use App\Services\Agent\AgentProfitCommissionAssignmentService;
  16. use App\Services\Agent\AgentReportService;
  17. use App\Services\Agent\AgentService;
  18. use App\Services\Agent\AgentWalletService;
  19. use App\Services\Agent\AgentWithdrawalService;
  20. use App\Services\PaymentOrderService;
  21. use Carbon\Carbon;
  22. use Illuminate\Config\Repository;
  23. use Illuminate\Contracts\Debug\ExceptionHandler;
  24. use Illuminate\Database\Capsule\Manager;
  25. use Illuminate\Database\QueryException;
  26. use Illuminate\Database\Schema\Blueprint;
  27. use Illuminate\Events\Dispatcher;
  28. use Illuminate\Foundation\Application;
  29. use Illuminate\Hashing\BcryptHasher;
  30. use Illuminate\Http\JsonResponse;
  31. use Illuminate\Http\Request;
  32. use Illuminate\Support\Facades\DB;
  33. use Illuminate\Support\Facades\Facade;
  34. use Illuminate\Support\Facades\Schema;
  35. use Illuminate\Translation\ArrayLoader;
  36. use Illuminate\Translation\Translator;
  37. use Illuminate\Validation\Factory;
  38. use PHPUnit\Framework\TestCase;
  39. use Tests\Support\AgentTestEnvironment;
  40. class AgentApiRegressionTest extends TestCase
  41. {
  42. use AgentTestEnvironment;
  43. public function test_admin_password_reset_revokes_old_login(): void
  44. {
  45. $agent = $this->agent(); $token = $this->token($agent);
  46. $this->agents->update($agent, ['password' => 'new-password']);
  47. $this->assertSame(0, AgentToken::query()->where('agent_id', $agent->id)->count());
  48. $request = Request::create('/agent/profile');
  49. $request->headers->set('Authorization', 'Bearer ' . $token);
  50. $response = (new AgentAuthMiddleware())->handle($request, fn () => new JsonResponse(['code' => 0]));
  51. $this->assertSame(101011, $response->getData(true)['code']);
  52. }
  53. public function test_disable_then_enable_does_not_restore_old_login(): void
  54. {
  55. $agent = $this->agent(); $this->token($agent);
  56. $this->agents->update($agent, ['status' => 0]);
  57. $this->agents->update($agent, ['status' => 1]);
  58. $this->assertSame(0, AgentToken::query()->count());
  59. }
  60. public function test_non_sensitive_edit_preserves_login(): void
  61. {
  62. $agent = $this->agent(); $this->token($agent);
  63. $this->agents->update($agent, ['real_name' => 'new name']);
  64. $this->assertSame(1, AgentToken::query()->count());
  65. }
  66. public function test_finance_counts_include_only_successful_in_range_provider_orders(): void
  67. {
  68. $agent = $this->agent();
  69. DB::table('users')->insert(['member_id' => 'm1', 'username' => 'member', 'agent_id' => $agent->id,
  70. 'created_at' => '2026-01-01 00:00:00']);
  71. $base = ['member_id' => 'm1', 'amount' => 10, 'created_at' => now()];
  72. DB::table('recharges')->insert($base + ['status' => 1]);
  73. DB::table('withdraws')->insert($base + ['status' => 1]);
  74. foreach ([PaymentOrderService::TYPE_PAY, PaymentOrderService::TYPE_PAYOUT, PaymentOrderService::TYPE_SELF_PAYOUT] as $type) {
  75. DB::table('payment_orders')->insert($base + ['type' => $type, 'status' => PaymentOrderService::STATUS_SUCCESS]);
  76. }
  77. DB::table('payment_orders')->insert($base + ['type' => PaymentOrderService::TYPE_PAY, 'status' => -99]);
  78. DB::table('payment_orders')->insert(array_merge($base, ['type' => PaymentOrderService::TYPE_PAY,
  79. 'status' => PaymentOrderService::STATUS_SUCCESS, 'created_at' => '2026-08-01 00:00:00']));
  80. DB::table('payment_orders')->insert(array_merge($base, ['type' => PaymentOrderService::TYPE_PAY,
  81. 'status' => PaymentOrderService::STATUS_SUCCESS, 'member_id' => 'unrelated']));
  82. $result = (new AgentMemberService(new AgentReportService()))->finance($agent, []);
  83. $member = $result['list'][0];
  84. $this->assertSame(2, $member->recharge_count);
  85. $this->assertSame(3, $member->withdraw_count);
  86. $this->assertSame('20.0000', $member->recharge_amount);
  87. $this->assertSame('30.0000', $member->withdraw_amount);
  88. }
  89. public function test_login_logs_filter_login_time_not_record_creation_time(): void
  90. {
  91. $agent = $this->agent();
  92. AgentLoginLog::query()->create(['agent_id' => $agent->id, 'username' => $agent->username,
  93. 'login_at' => '2026-09-06 23:59:59', 'created_at' => '2026-09-07 00:00:01']);
  94. $controller = new AdminAgent();
  95. $request = Request::create('/admin/agent/login-logs', 'GET', ['start_date' => '2026-09-06', 'end_date' => '2026-09-06']);
  96. $this->assertSame(1, $controller->loginLogs($request)->getData(true)['data']['total']);
  97. }
  98. private function fundedAccount(Agent $agent): AgentWithdrawAccount
  99. {
  100. Agent::query()->whereKey($agent->id)->update(['balance' => 100, 'realtime_balance' => 100]);
  101. return AgentWithdrawAccount::query()->create(['agent_id' => $agent->id, 'type' => 'usdt', 'network' => 'TRC20',
  102. 'account' => 'TQn9Y2khEsLJW1ChVWFMSMeRDow5KcbLSE', 'status' => 1]);
  103. }
  104. public function test_withdrawal_uses_current_password_not_stale_request_model(): void
  105. {
  106. $agent = $this->agent(); $account = $this->fundedAccount($agent);
  107. $this->agents->update($agent, ['withdraw_password' => 'withdraw-new']);
  108. $this->expectException(\InvalidArgumentException::class);
  109. $this->expectExceptionMessage('提现密码错误');
  110. (new AgentWithdrawalService())->request($agent, $account->id, '10', 'withdraw-old', 'stale');
  111. }
  112. public function test_disabled_agent_cannot_submit_with_stale_request_model(): void
  113. {
  114. $agent = $this->agent(); $account = $this->fundedAccount($agent);
  115. $this->agents->update($agent, ['status' => 0]);
  116. $this->expectException(\RuntimeException::class);
  117. $this->expectExceptionMessage('代理账号已禁用');
  118. (new AgentWithdrawalService())->request($agent, $account->id, '10', 'withdraw-old', 'disabled');
  119. }
  120. public function test_withdrawal_retry_and_rejection_move_funds_once(): void
  121. {
  122. $agent = $this->agent(); $account = $this->fundedAccount($agent);
  123. $service = new AgentWithdrawalService();
  124. $one = $service->request($agent, $account->id, '10', 'withdraw-old', 'retry');
  125. $two = $service->request($agent, $account->id, '10', 'withdraw-old', 'retry');
  126. $this->assertSame($one->id, $two->id);
  127. $this->assertSame('90.0000', $agent->fresh()->balance);
  128. $this->assertSame('10.0000', $agent->fresh()->frozen_balance);
  129. $service->audit($one->id, 'reject', 1);
  130. $this->assertSame('100.0000', $agent->fresh()->balance);
  131. $this->assertSame('0.0000', $agent->fresh()->frozen_balance);
  132. $this->assertSame(1, AgentWithdrawal::query()->count());
  133. $this->assertSame(2, AgentBalanceLog::query()->count());
  134. }
  135. public function test_member_finance_does_not_include_other_agent_members(): void
  136. {
  137. $agent = $this->agent(); $other = $this->agent();
  138. foreach ([$agent, $agent, $other] as $i => $owner) {
  139. DB::table('users')->insert(['member_id' => 'm' . $i, 'username' => 'member' . $i,
  140. 'agent_id' => $owner->id, 'created_at' => now()]);
  141. }
  142. $service = new AgentMemberService(new AgentReportService());
  143. $this->assertSame(2, $service->finance($agent, [])['total']);
  144. $this->assertSame(0, $service->finance($agent, ['agent_id' => $other->id])['total']);
  145. $this->assertSame(1, $service->finance($other, [])['total']);
  146. }
  147. public function test_business_errors_keep_existing_response_contract(): void
  148. {
  149. $controller = new class extends AgentApiController {
  150. public function trigger() {
  151. return $this->execute(fn () => throw new \RuntimeException('代理余额不足'));
  152. }
  153. };
  154. $data = $controller->trigger()->getData(true);
  155. $this->assertSame(['code', 'timestamp', 'msg', 'data'], array_keys($data));
  156. $this->assertSame(-3, $data['code']);
  157. $this->assertSame('代理余额不足', $data['msg']);
  158. }
  159. public function test_settlement_command_rejects_invalid_and_unfinished_dates(): void
  160. {
  161. $handler = $this->createMock(ExceptionHandler::class);
  162. $handler->expects($this->exactly(3))->method('report');
  163. $this->app->instance(ExceptionHandler::class, $handler);
  164. $reports = $this->createMock(AgentReportService::class);
  165. $reports->expects($this->never())->method('refreshDaily');
  166. $this->app->instance(AgentReportService::class, $reports);
  167. $commissions = $this->createMock(AgentCommissionService::class);
  168. $commissions->expects($this->never())->method('settle');
  169. $this->app->instance(AgentCommissionService::class, $commissions);
  170. foreach (['2026-02-30', '2026-09-07', '2026-09-08'] as $date) {
  171. $command = new \App\Console\Commands\AgentDailySettle();
  172. $command->setLaravel($this->app);
  173. $tester = new \Symfony\Component\Console\Tester\CommandTester($command);
  174. $this->assertSame(1, $tester->execute(['date' => $date]));
  175. }
  176. }
  177. public function test_sql_errors_are_logged_but_not_returned_to_frontend(): void
  178. {
  179. $handler = $this->createMock(ExceptionHandler::class);
  180. $handler->expects($this->once())->method('report');
  181. $this->app->instance(ExceptionHandler::class, $handler);
  182. $controller = new class extends AgentApiController {
  183. public function trigger() {
  184. return $this->execute(fn () => throw new QueryException('select sensitive_table', ['secret'], new \PDOException('internal database error')));
  185. }
  186. };
  187. $data = $controller->trigger()->getData(true);
  188. $this->assertSame(-3, $data['code']);
  189. $this->assertSame('系统处理失败,请稍后重试', $data['msg']);
  190. $this->assertSame([], $data['data']);
  191. }
  192. }