1+ <?php
2+
3+ namespace Tests \Feature ;
4+
5+ use App \Services \ApiKeyService ;
6+ use Mockery ;
7+ use Tests \TestCase ;
8+
9+ class ApiKeyStoreTest extends TestCase
10+ {
11+
12+ protected function setUp (): void
13+ {
14+ parent ::setUp ();
15+ }
16+
17+ protected function tearDown (): void
18+ {
19+ Mockery::close ();
20+ parent ::tearDown ();
21+ }
22+
23+ public function test_stores_valid_api_key_successfully (): void
24+ {
25+ $ mockApiKeyService = Mockery::mock (ApiKeyService::class);
26+ $ this ->app ->instance (ApiKeyService::class, $ mockApiKeyService );
27+
28+ $ validApiKey = 'sk-1234567890abcdef1234567890abcdef1234567890abcdef ' ;
29+
30+ $ mockApiKeyService ->shouldReceive ('validateApiKey ' )
31+ ->once ()
32+ ->with ($ validApiKey )
33+ ->andReturn (true );
34+
35+ $ mockApiKeyService ->shouldReceive ('setApiKey ' )
36+ ->once ()
37+ ->with ($ validApiKey );
38+
39+ $ response = $ this ->postJson ('/api/openai/api-key ' , [
40+ 'api_key ' => $ validApiKey
41+ ]);
42+
43+ $ response ->assertStatus (200 )
44+ ->assertJson ([
45+ 'success ' => true ,
46+ 'message ' => 'API key saved successfully. '
47+ ]);
48+ }
49+
50+ public function test_rejects_invalid_api_key (): void
51+ {
52+ $ mockApiKeyService = Mockery::mock (ApiKeyService::class);
53+ $ this ->app ->instance (ApiKeyService::class, $ mockApiKeyService );
54+
55+ $ invalidApiKey = 'sk-1234567890abcdef1234567890abcdef1234567890invalid ' ;
56+
57+ $ mockApiKeyService ->shouldReceive ('validateApiKey ' )
58+ ->once ()
59+ ->with ($ invalidApiKey )
60+ ->andReturn (false );
61+
62+ $ mockApiKeyService ->shouldNotReceive ('setApiKey ' );
63+
64+ $ response = $ this ->postJson ('/api/openai/api-key ' , [
65+ 'api_key ' => $ invalidApiKey
66+ ]);
67+
68+ $ response ->assertStatus (422 )
69+ ->assertJson ([
70+ 'success ' => false ,
71+ 'message ' => 'The provided API key is invalid. Please check and try again. '
72+ ]);
73+ }
74+
75+ public function test_validates_required_api_key_field (): void
76+ {
77+ $ response = $ this ->postJson ('/api/openai/api-key ' , []);
78+
79+ $ response ->assertStatus (422 )
80+ ->assertJsonValidationErrors (['api_key ' ]);
81+ }
82+
83+ public function test_validates_api_key_minimum_length (): void
84+ {
85+ $ response = $ this ->postJson ('/api/openai/api-key ' , [
86+ 'api_key ' => 'sk-short '
87+ ]);
88+
89+ $ response ->assertStatus (422 )
90+ ->assertJsonValidationErrors (['api_key ' ]);
91+ }
92+
93+ public function test_validates_api_key_is_string (): void
94+ {
95+ $ response = $ this ->postJson ('/api/openai/api-key ' , [
96+ 'api_key ' => 123456
97+ ]);
98+
99+ $ response ->assertStatus (422 )
100+ ->assertJsonValidationErrors (['api_key ' ]);
101+ }
102+
103+ public function test_handles_api_key_service_exception (): void
104+ {
105+ $ mockApiKeyService = Mockery::mock (ApiKeyService::class);
106+ $ this ->app ->instance (ApiKeyService::class, $ mockApiKeyService );
107+
108+ $ validApiKey = 'sk-1234567890abcdef1234567890abcdef1234567890abcdef ' ;
109+
110+ $ mockApiKeyService ->shouldReceive ('validateApiKey ' )
111+ ->once ()
112+ ->with ($ validApiKey )
113+ ->andThrow (new \Exception ('Service error ' ));
114+
115+ $ response = $ this ->postJson ('/api/openai/api-key ' , [
116+ 'api_key ' => $ validApiKey
117+ ]);
118+
119+ // Controller doesn't handle exceptions, so it returns 500
120+ $ response ->assertStatus (500 );
121+ }
122+
123+ public function test_api_key_endpoint_accessible_without_existing_api_key (): void
124+ {
125+ // This test ensures the middleware allows access to the API key store endpoint
126+ // even when no API key is configured
127+
128+ $ mockApiKeyService = Mockery::mock (ApiKeyService::class);
129+ $ this ->app ->instance (ApiKeyService::class, $ mockApiKeyService );
130+
131+ $ validApiKey = 'sk-1234567890abcdef1234567890abcdef1234567890abcdef ' ;
132+
133+ $ mockApiKeyService ->shouldReceive ('validateApiKey ' )
134+ ->once ()
135+ ->andReturn (true );
136+
137+ $ mockApiKeyService ->shouldReceive ('setApiKey ' )
138+ ->once ();
139+
140+ $ response = $ this ->postJson ('/api/openai/api-key ' , [
141+ 'api_key ' => $ validApiKey
142+ ]);
143+
144+ $ response ->assertStatus (200 );
145+ }
146+ }
0 commit comments