MemberTokenIdleTimeoutTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 \Tests\Support\JsonResponseFactory());
  34. if (!function_exists('create_token') || !function_exists('lang')) require_once dirname(__DIR__, 2) . '/app/Helpers/helpers.php';
  35. $db->schema()->create('users', function (Blueprint $t) { $t->id(); $t->string('user_id'); $t->string('member_id'); $t->integer('is_banned')->default(0); $t->bigInteger('delete_time')->nullable(); });
  36. $db->schema()->create('user_session', function (Blueprint $t) {
  37. $t->id(); $t->string('user_id'); $t->string('token'); $t->bigInteger('expire_time');
  38. $t->bigInteger('create_time'); $t->bigInteger('update_time')->nullable();
  39. });
  40. DB::table('users')->insert(['user_id' => '100001', 'member_id' => '100001']);
  41. }
  42. protected function tearDown(): void
  43. {
  44. Carbon::setTestNow(); DB::disconnect(); Facade::clearResolvedInstances(); parent::tearDown();
  45. }
  46. private function request(?string $token): JsonResponse
  47. {
  48. $request = Request::create('/api/wallet/test', 'GET');
  49. if ($token !== null) $request->headers->set('Authorization', $token);
  50. return (new CheckToken())->handle($request, static fn($r) => new JsonResponse(['code' => 0, 'user_id' => $r->user->user_id]));
  51. }
  52. private function clock(int $time): void { Carbon::setTestNow(Carbon::createFromTimestamp($time, 'Asia/Shanghai')); }
  53. public function test_login_and_authenticated_activity_use_a_full_sliding_day(): void
  54. {
  55. $token = UserSession::setToken('100001');
  56. $session = DB::table('user_session')->where('token', $token)->first();
  57. $this->assertSame($this->start + 86400, (int)$session->expire_time);
  58. $this->assertSame($this->start, (int)$session->update_time);
  59. $this->clock($this->start + 3600);
  60. $this->assertSame(0, $this->request($token)->getData(true)['code']);
  61. $this->assertSame($this->start + 3600 + 86400, (int)DB::table('user_session')->value('expire_time'));
  62. $lastActivity = $this->start + 25 * 3600 - 1;
  63. $this->clock($lastActivity);
  64. $this->assertSame(0, $this->request($token)->getData(true)['code']);
  65. $this->assertSame($lastActivity + 86400, (int)DB::table('user_session')->value('expire_time'));
  66. $this->clock($lastActivity + 86400);
  67. $before = (array)DB::table('user_session')->first();
  68. $this->assertSame(401, $this->request($token)->getStatusCode());
  69. $this->assertSame($before, (array)DB::table('user_session')->first());
  70. $this->assertNull(UserSession::renewActiveToken($token, now()->getTimestamp()));
  71. $this->assertSame($before, (array)DB::table('user_session')->first());
  72. }
  73. public function test_legacy_long_expiry_cannot_bypass_idle_limit_or_be_renewed(): void
  74. {
  75. DB::table('user_session')->insert(['user_id' => '100001', 'token' => 'legacy',
  76. 'expire_time' => $this->start + 7 * 86400, 'create_time' => $this->start, 'update_time' => $this->start]);
  77. $this->clock($this->start + 86400);
  78. $before = (array)DB::table('user_session')->first();
  79. $this->assertSame(401, $this->request('legacy')->getData(true)['code']);
  80. $this->assertNull(UserSession::renewActiveToken('legacy', now()->getTimestamp()));
  81. $this->assertSame($before, (array)DB::table('user_session')->first());
  82. $this->assertSame(401, $this->request(null)->getStatusCode());
  83. $this->assertNull(UserSession::renewActiveToken('', now()->getTimestamp()));
  84. }
  85. public function test_replaced_revoked_missing_or_orphaned_sessions_cannot_be_reactivated(): void
  86. {
  87. $token = UserSession::setToken('100001');
  88. DB::table('user_session')->where('token', $token)->update(['token' => 'replacement']);
  89. $this->assertSame(401, $this->request($token)->getStatusCode());
  90. $this->assertNull(UserSession::renewActiveToken($token, $this->start));
  91. $this->assertNull(UserSession::renewActiveToken('missing', $this->start));
  92. DB::table('user_session')->update(['expire_time' => $this->start]);
  93. $before = (array)DB::table('user_session')->first();
  94. $this->assertSame(401, $this->request('replacement')->getStatusCode());
  95. $this->assertSame($before, (array)DB::table('user_session')->first());
  96. DB::table('user_session')->update(['expire_time' => $this->start + 86400]);
  97. DB::table('users')->update(['is_banned' => 1]);
  98. $before = (array)DB::table('user_session')->first();
  99. $this->assertSame(401, $this->request('replacement')->getStatusCode());
  100. $this->assertSame($before, (array)DB::table('user_session')->first());
  101. DB::table('users')->update(['is_banned' => 0, 'delete_time' => $this->start]);
  102. $this->assertSame(401, $this->request('replacement')->getStatusCode());
  103. $this->assertSame($before, (array)DB::table('user_session')->first());
  104. DB::table('users')->delete();
  105. $before = (array)DB::table('user_session')->first();
  106. $this->assertSame(401, $this->request('replacement')->getStatusCode());
  107. $this->assertSame($before, (array)DB::table('user_session')->first());
  108. }
  109. public function test_missing_activity_falls_back_to_creation_and_concurrent_renewals_are_monotonic(): void
  110. {
  111. foreach ([null, 0] as $index => $activity) {
  112. $token = 'fallback-' . $index;
  113. DB::table('user_session')->insert(['user_id' => '100001', 'token' => $token,
  114. 'expire_time' => $this->start + 7 * 86400, 'create_time' => $this->start - 3600, 'update_time' => $activity]);
  115. $session = UserSession::where('token', $token)->first();
  116. $this->assertSame($this->start + 82800, UserSession::validUntil($session->getAttributes()));
  117. $this->assertSame(0, $this->request($token)->getData(true)['code']);
  118. $this->assertSame($this->start + 86400, (int)DB::table('user_session')->where('token', $token)->value('expire_time'));
  119. }
  120. DB::table('user_session')->where('token', 'fallback-0')->update(['update_time' => $this->start + 10, 'expire_time' => $this->start + 10 + 86400]);
  121. $before = (array)DB::table('user_session')->where('token', 'fallback-0')->first();
  122. $this->assertNotNull(UserSession::renewActiveToken('fallback-0', $this->start));
  123. $this->assertSame($before, (array)DB::table('user_session')->where('token', 'fallback-0')->first());
  124. }
  125. public function test_renewal_stays_bound_to_the_original_session_identity(): void
  126. {
  127. $token = UserSession::setToken('100001');
  128. $id = (int)DB::table('user_session')->value('id');
  129. DB::table('user_session')->where('id', $id)->update(['token' => 'replacement']);
  130. DB::table('user_session')->insert(['user_id' => 'other-user', 'token' => $token,
  131. 'expire_time' => $this->start + 86400, 'create_time' => $this->start, 'update_time' => $this->start]);
  132. $before = DB::table('user_session')->orderBy('id')->get()->toJson();
  133. $this->assertNull(UserSession::renewActiveToken($token, $this->start + 1, $id));
  134. $this->assertSame($before, DB::table('user_session')->orderBy('id')->get()->toJson());
  135. }
  136. }