self::API_SECRET]); } protected function tearDown(): void { Carbon::setTestNow(); parent::tearDown(); } public function test_one_time_form_submits_code_to_relay_job_once(): void { $runId = (string) Str::uuid(); $created = $this->postSignedJson('/api/hxb-verification/requests', [ 'client_run_id' => $runId, ])->assertCreated() ->assertJsonPath('run_id', $runId) ->json(); $fillToken = basename((string) parse_url($created['fill_url'], PHP_URL_PATH)); $this->assertMatchesRegularExpression('/^[a-f0-9]{64}$/', $fillToken); $this->get('/hxb-verification/' . $fillToken) ->assertOk() ->assertSee('华夏银行短信验证码') ->assertSee('6 位验证码'); $this->post('/hxb-verification/' . $fillToken, ['code' => '123456']) ->assertRedirect('/hxb-verification/' . $fillToken . '?submitted=1'); $this->get('/hxb-verification/' . $fillToken . '?submitted=1') ->assertOk() ->assertSee('验证码已提交'); $pollUrl = '/api/hxb-verification/requests/' . $runId . '/code'; $this->postJson($pollUrl, [], [ 'Authorization' => 'Bearer ' . $created['poll_token'], ])->assertOk() ->assertJsonPath('status', 'consumed') ->assertJsonPath('code', '123456'); $this->postJson($pollUrl, [], [ 'Authorization' => 'Bearer ' . $created['poll_token'], ])->assertStatus(410) ->assertJsonPath('status', 'consumed'); } public function test_request_creation_rejects_invalid_signature(): void { try { $this->withHeaders([ 'X-HXB-Timestamp' => (string) time(), 'X-HXB-Nonce' => bin2hex(random_bytes(16)), 'X-HXB-Signature' => str_repeat('0', 64), ])->postJson('/api/hxb-verification/requests', [ 'client_run_id' => (string) Str::uuid(), ]); $this->fail('无效签名不应创建验证码请求。'); } catch (HttpException $exception) { $this->assertSame(401, $exception->getStatusCode()); } } public function test_expired_link_cannot_accept_a_code(): void { Carbon::setTestNow('2026-12-02 09:00:00'); $created = $this->postSignedJson('/api/hxb-verification/requests', [ 'client_run_id' => (string) Str::uuid(), ])->assertCreated()->json(); $fillToken = basename((string) parse_url($created['fill_url'], PHP_URL_PATH)); Carbon::setTestNow('2026-12-02 09:06:00'); $this->post('/hxb-verification/' . $fillToken, ['code' => '654321']) ->assertStatus(410) ->assertSee('验证码链接已过期'); } private function postSignedJson(string $path, array $payload) { $body = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); $timestamp = (string) time(); $nonce = bin2hex(random_bytes(16)); $canonical = implode("\n", [ 'POST', ltrim($path, '/'), $timestamp, $nonce, hash('sha256', $body), ]); return $this->withHeaders([ 'X-HXB-Timestamp' => $timestamp, 'X-HXB-Nonce' => $nonce, 'X-HXB-Signature' => hash_hmac('sha256', $canonical, self::API_SECRET), ])->postJson($path, $payload); } }