22
33namespace Tests \Feature \Api ;
44
5- use App \Enums \LicenseSource ;
6- use App \Enums \Subscription ;
7- use App \Jobs \CreateAnystackLicenseJob ;
85use App \Models \License ;
96use App \Models \User ;
107use Illuminate \Foundation \Testing \RefreshDatabase ;
11- use Illuminate \Support \Facades \Queue ;
8+ use Illuminate \Support \Facades \Http ;
129use Tests \TestCase ;
1310
1411class CreateLicenseTest extends TestCase
@@ -18,7 +15,20 @@ class CreateLicenseTest extends TestCase
1815 protected function setUp (): void
1916 {
2017 parent ::setUp ();
21- Queue::fake ();
18+
19+ // Mock the Anystack API calls
20+ Http::fake ([
21+ 'https://api.anystack.sh/v1/contacts ' => Http::response (['data ' => ['id ' => 'contact_123 ' ]], 200 ),
22+ 'https://api.anystack.sh/v1/products/*/licenses ' => Http::response ([
23+ 'data ' => [
24+ 'id ' => 'license_123 ' ,
25+ 'key ' => 'TEST-LICENSE-KEY ' ,
26+ 'expires_at ' => null ,
27+ 'created_at ' => now ()->toISOString (),
28+ 'updated_at ' => now ()->toISOString (),
29+ ],
30+ ], 200 ),
31+ ]);
2232 }
2333
2434 public function test_requires_authentication ()
@@ -109,73 +119,75 @@ public function test_finds_existing_user_when_email_exists()
109119 ]);
110120 }
111121
112- public function test_dispatches_create_anystack_license_job ()
122+ public function test_creates_license_with_bifrost_source ()
113123 {
114124 $ user = User::factory ()->create ();
115125 $ token = $ user ->createToken ('test-token ' )->plainTextToken ;
116126
117- $ this ->withHeaders ([
127+ $ response = $ this ->withHeaders ([
118128 'Authorization ' => 'Bearer ' .$ token ,
119129 ])->postJson ('/api/licenses ' , [
120130121131 'name ' => 'Test User ' ,
122132 'subscription ' => 'pro ' ,
123133 ]);
124134
125- Queue::assertPushed (CreateAnystackLicenseJob::class, function ($ job ) {
126- return $ job ->subscription === Subscription::Pro
127- && $ job ->firstName === null
128- && $ job ->lastName === null
129- && $ job ->source === LicenseSource::Bifrost
130- && $ job ->subscriptionItemId === null ;
131- });
132- }
135+ $ response ->assertStatus (200 )
136+ ->assertJsonStructure ([
137+ 'id ' ,
138+ 'user_id ' ,
139+ 'policy_name ' ,
140+ 'source ' ,
141+ 'key ' ,
142+ 'created_at ' ,
143+ 'updated_at ' ,
144+ ]);
133145
134- public function test_returns_existing_license_when_found ()
135- {
136- $ targetUser = User::
factory ()->
create ([
'email ' =>
'[email protected] ' ]);
137- $ license = License::factory ()->create ([
138- 'user_id ' => $ targetUser ->id ,
146+ // Verify the license was created with correct attributes
147+ $ this ->assertDatabaseHas ('licenses ' , [
139148 'policy_name ' => 'pro ' ,
140- 'source ' => LicenseSource::Bifrost,
149+ 'source ' => 'bifrost ' ,
150+ 'key ' => 'TEST-LICENSE-KEY ' ,
141151 ]);
142152
143- $ user = User::factory ()->create ();
144- $ token = $ user ->createToken ('test-token ' )->plainTextToken ;
145-
146- $ response = $ this ->withHeaders ([
147- 'Authorization ' => 'Bearer ' .$ token ,
148- ])->postJson ('/api/licenses ' , [
153+ // Verify user was created/found
154+ $ this ->assertDatabaseHas ('users ' , [
149155150- 'name ' => 'Test User ' ,
151- 'subscription ' => 'pro ' ,
152156 ]);
153-
154- $ response ->assertStatus (200 )
155- ->assertJson ([
156- 'id ' => $ license ->id ,
157- 'policy_name ' => 'pro ' ,
158- 'source ' => 'bifrost ' ,
159- ]);
160157 }
161158
162- public function test_returns_pending_response_when_license_not_found ()
159+ public function test_creates_license_for_existing_user ()
163160 {
164- $ user = User::factory ()->create ();
165- $ token = $ user ->createToken ('test-token ' )->plainTextToken ;
161+ // Create an existing user
162+ $ existingUser = User::factory ()->create ([
163+ 164+ 'name ' => 'Existing User ' ,
165+ ]);
166+
167+ $ authUser = User::factory ()->create ();
168+ $ token = $ authUser ->createToken ('test-token ' )->plainTextToken ;
166169
167170 $ response = $ this ->withHeaders ([
168171 'Authorization ' => 'Bearer ' .$ token ,
169172 ])->postJson ('/api/licenses ' , [
170- 'email ' => 'newuser @example.com ' ,
171- 'name ' => 'New User ' ,
172- 'subscription ' => 'mini ' ,
173+ 'email ' => 'existing @example.com ' ,
174+ 'name ' => 'Different Name ' , // This should be ignored
175+ 'subscription ' => 'max ' ,
173176 ]);
174177
175- $ response ->assertStatus (202 )
176- ->assertJson ([
177- 'message ' => 'License creation initiated. Please check back shortly. ' ,
178- 'subscription ' => 'mini ' ,
178+ $ response ->assertStatus (200 )
179+ ->assertJsonStructure ([
180+ 'id ' ,
181+ 'user_id ' ,
182+ 'policy_name ' ,
183+ 'source ' ,
184+ 'key ' ,
179185 ]);
186+
187+ // Verify license was created for the existing user
188+ $ license = License::where ('user_id ' , $ existingUser ->id )->first ();
189+ $ this ->assertNotNull ($ license );
190+ $ this ->assertEquals ('max ' , $ license ->policy_name );
191+ $ this ->assertEquals ('bifrost ' , $ license ->source ->value );
180192 }
181193}
0 commit comments