MemberTokenIdleTimeoutTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Tests\Integration;
  3. use App\Http\Middleware\CheckToken;
  4. use App\Models\UserSession;
  5. use Carbon\Carbon;
  6. use Illuminate\Config\Repository;
  7. use Illuminate\Database\Capsule\Manager;
  8. use Illuminate\Database\Schema\Blueprint;
  9. use Illuminate\Foundation\Application;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Facade;
  14. use Illuminate\Translation\ArrayLoader;
  15. use Illuminate\Translation\Translator;
  16. use PHPUnit\Framework\TestCase;
  17. /** Actual member-token middleware and sessions, isolated SQLite and a fixed server clock. */
  18. class MemberTokenIdleTimeoutTest extends TestCase
  19. {
  20. private int $start;
  21. protected function setUp(): void
  22. {
  23. Facade::clearResolvedInstances();
  24. Carbon::setTestNow(Carbon::parse('2026-10-25 15:18:55', 'Asia/Shanghai'));
  25. $this->start = now()->getTimestamp();
  26. $app = new Application(dirname(__DIR__, 2));
  27. $app->instance('config', new Repository(['app' => ['locale' => 'zh', 'timezone' => 'Asia/Shanghai']]));
  28. Facade::setFacadeApplication($app);
  29. $db = new Manager($app);
  30. $db->addConnection(['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => 'bot_']);
  31. $db->setAsGlobal(); $db->bootEloquent(); $app->instance('db', $db->getDatabaseManager());
  32. $app->instance('translator', new Translator(new ArrayLoader(), 'zh'));
  33. $app->instance(\Illuminate\Contracts\Routing\ResponseFactory::class, new class {
  34. public function json($data, $status = 200, $headers = [], $options = 0) { return new JsonResponse($data, $status, $headers, $options); }
  35. });
  36. if (!function_exists('create_token') || !function_exists('lang')) require_once dirname(__DIR__, 2) . '/app/Helpers/helpers.php';
  37. $db->schema()->create('users', function (Blueprint $t) { $t->id(); $t->string('user_id'); $t->string('member_id'); });
  38. $db->schema()->create('user_session', function (Blueprint $t) {
  39. $t->id(); $t->string('user_id'); $t->string('token'); $t->bigInteger('expire_time');
  40. $t->bigInteger('create_time'); $t->bigInteger('update_time')->nullable();
  41. });
  42. DB::table('users')->insert(['user_id' => '100001', 'member_id' => '100001']);
  43. }
  44. protected function tearDown(): void
  45. {
  46. Carbon::setTestNow(); DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
  47. }
  48. private function request(?string $token): JsonResponse
  49. {
  50. $request = Request::create('/api/wallet/test', 'GET');
  51. if ($token !== null) $request->headers->set('Authorization', $token);
  52. return (new CheckToken())->handle($request, static fn($r) => new JsonResponse(['code' => 0, 'user_id' => $r->user->user_id]));
  53. }
  54. private function clock(int $time): void { Carbon::setTestNow(Carbon::createFromTimestamp($time, 'Asia/Shanghai')); }
  55. public function test_login_and_authenticated_activity_use_a_full_sliding_day(): void
  56. {
  57. $token = UserSession::setToken('100001');
  58. $session = DB::table('user_session')->where('token', $token)->first();
  59. $this->assertSame($this->start + 86400, (int)$session->expire_time);
  60. $this->assertSame($this->start, (int)$session->update_time);
  61. $this->clock($this->start + 3600);
  62. $this->assertSame(0, $this->request($token)->getData(true)['code']);
  63. $this->assertSame($this->start + 3600 + 86400, (int)DB::table('user_session')->value('expire_time'));
  64. $lastActivity = $this->start + 25 * 3600 - 1;
  65. $this->clock($lastActivity);
  66. $this->assertSame(0, $this->request($token)->getData(true)['code']);
  67. $this->assertSame($lastActivity + 86400, (int)DB::table('user_session')->value('expire_time'));
  68. $this->clock($lastActivity + 86400);
  69. $before = (array)DB::table('user_session')->first();
  70. $this->assertSame(401, $this->request($token)->getStatusCode());
  71. $this->assertSame($before, (array)DB::table('user_session')->first());
  72. $this->assertNull(UserSession::renewActiveToken($token, now()->getTimestamp()));
  73. $this->assertSame($before, (array)DB::table('user_session')->first());
  74. }
  75. public function test_legacy_long_expiry_cannot_bypass_idle_limit_or_be_renewed(): void
  76. {
  77. DB::table('user_session')->insert(['user_id' => '100001', 'token' => 'legacy',
  78. 'expire_time' => $this->start + 7 * 86400, 'create_time' => $this->start, 'update_time' => $this->start]);
  79. $this->clock($this->start + 86400);
  80. $before = (array)DB::table('user_session')->first();
  81. $this->assertSame(401, $this->request('legacy')->getData(true)['code']);
  82. $this->assertNull(UserSession::renewActiveToken('legacy', now()->getTimestamp()));
  83. $this->assertSame($before, (array)DB::table('user_session')->first());
  84. $this->assertSame(401, $this->request(null)->getStatusCode());
  85. $this->assertNull(UserSession::renewActiveToken('', now()->getTimestamp()));
  86. }
  87. public function test_replaced_revoked_missing_or_orphaned_sessions_cannot_be_reactivated(): void
  88. {
  89. $token = UserSession::setToken('100001');
  90. DB::table('user_session')->where('token', $token)->update(['token' => 'replacement']);
  91. $this->assertSame(401, $this->request($token)->getStatusCode());
  92. $this->assertNull(UserSession::renewActiveToken($token, $this->start));
  93. $this->assertNull(UserSession::renewActiveToken('missing', $this->start));
  94. DB::table('user_session')->update(['expire_time' => $this->start]);
  95. $before = (array)DB::table('user_session')->first();
  96. $this->assertSame(401, $this->request('replacement')->getStatusCode());
  97. $this->assertSame($before, (array)DB::table('user_session')->first());
  98. DB::table('user_session')->update(['expire_time' => $this->start + 86400]);
  99. DB::table('users')->delete();
  100. $before = (array)DB::table('user_session')->first();
  101. $this->assertSame(401, $this->request('replacement')->getStatusCode());
  102. $this->assertSame($before, (array)DB::table('user_session')->first());
  103. }
  104. public function test_missing_activity_falls_back_to_creation_and_concurrent_renewals_are_monotonic(): void
  105. {
  106. foreach ([null, 0] as $index => $activity) {
  107. $token = 'fallback-' . $index;
  108. DB::table('user_session')->insert(['user_id' => '100001', 'token' => $token,
  109. 'expire_time' => $this->start + 7 * 86400, 'create_time' => $this->start - 3600, 'update_time' => $activity]);
  110. $session = UserSession::where('token', $token)->first();
  111. $this->assertSame($this->start + 82800, UserSession::validUntil($session->getAttributes()));
  112. $this->assertSame(0, $this->request($token)->getData(true)['code']);
  113. $this->assertSame($this->start + 86400, (int)DB::table('user_session')->where('token', $token)->value('expire_time'));
  114. }
  115. DB::table('user_session')->where('token', 'fallback-0')->update(['update_time' => $this->start + 10, 'expire_time' => $this->start + 10 + 86400]);
  116. $before = (array)DB::table('user_session')->where('token', 'fallback-0')->first();
  117. $this->assertNotNull(UserSession::renewActiveToken('fallback-0', $this->start));
  118. $this->assertSame($before, (array)DB::table('user_session')->where('token', 'fallback-0')->first());
  119. }
  120. public function test_renewal_stays_bound_to_the_original_session_identity(): void
  121. {
  122. $token = UserSession::setToken('100001');
  123. $id = (int)DB::table('user_session')->value('id');
  124. DB::table('user_session')->where('id', $id)->update(['token' => 'replacement']);
  125. DB::table('user_session')->insert(['user_id' => 'other-user', 'token' => $token,
  126. 'expire_time' => $this->start + 86400, 'create_time' => $this->start, 'update_time' => $this->start]);
  127. $before = DB::table('user_session')->orderBy('id')->get()->toJson();
  128. $this->assertNull(UserSession::renewActiveToken($token, $this->start + 1, $id));
  129. $this->assertSame($before, DB::table('user_session')->orderBy('id')->get()->toJson());
  130. }
  131. }