['care' => $this->apiKey]]); $this->user = User::create([ 'public_id' => 'api-user-001', 'name' => 'API User', 'email' => 'api@example.com', ]); $this->organization = Organization::create([ 'owner_ref' => $this->user->public_id, 'name' => 'API Org', 'slug' => 'api-org', 'settings' => ['onboarded' => true], ]); } public function test_api_requires_service_key(): void { $this->getJson('/api/visits?owner='.$this->user->public_id) ->assertUnauthorized(); } public function test_api_can_create_visit(): void { $response = $this->withHeaders(['Authorization' => 'Bearer '.$this->apiKey]) ->postJson('/api/visits', [ 'owner' => $this->user->public_id, 'organization_id' => $this->organization->id, 'full_name' => 'API Visitor', 'visitor_type' => 'visitor', ]); $response->assertCreated(); $this->assertDatabaseHas('frontdesk_visits', ['visitor_type' => 'visitor']); } public function test_api_can_list_visits(): void { $visitor = Visitor::create([ 'owner_ref' => $this->user->public_id, 'organization_id' => $this->organization->id, 'full_name' => 'Listed Visitor', ]); Visit::create([ 'owner_ref' => $this->user->public_id, 'organization_id' => $this->organization->id, 'visitor_id' => $visitor->id, 'visitor_type' => 'visitor', 'status' => Visit::STATUS_CHECKED_IN, 'checked_in_at' => now(), ]); $this->withHeaders(['Authorization' => 'Bearer '.$this->apiKey]) ->getJson('/api/visits?owner='.$this->user->public_id) ->assertOk() ->assertJsonPath('total', 1); } public function test_api_scopes_to_owner(): void { $other = User::create([ 'public_id' => 'other-user', 'name' => 'Other', 'email' => 'other@example.com', ]); $visitor = Visitor::create([ 'owner_ref' => $other->public_id, 'organization_id' => Organization::create([ 'owner_ref' => $other->public_id, 'name' => 'Other Org', 'slug' => 'other', ])->id, 'full_name' => 'Secret Visitor', ]); Visit::create([ 'owner_ref' => $other->public_id, 'organization_id' => $visitor->organization_id, 'visitor_id' => $visitor->id, 'visitor_type' => 'visitor', 'status' => Visit::STATUS_CHECKED_IN, 'checked_in_at' => now(), ]); $this->withHeaders(['Authorization' => 'Bearer '.$this->apiKey]) ->getJson('/api/visits?owner='.$this->user->public_id) ->assertOk() ->assertJsonPath('total', 0); } }