2
2
3
3
namespace Tests \Feature \Api ;
4
4
5
- use App \Enums \LicenseSource ;
6
- use App \Enums \Subscription ;
7
- use App \Jobs \CreateAnystackLicenseJob ;
8
5
use App \Models \License ;
9
6
use App \Models \User ;
10
7
use Illuminate \Foundation \Testing \RefreshDatabase ;
11
- use Illuminate \Support \Facades \Queue ;
8
+ use Illuminate \Support \Facades \Http ;
12
9
use Tests \TestCase ;
13
10
14
11
class CreateLicenseTest extends TestCase
@@ -18,7 +15,20 @@ class CreateLicenseTest extends TestCase
18
15
protected function setUp (): void
19
16
{
20
17
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
+ ]);
22
32
}
23
33
24
34
public function test_requires_authentication ()
@@ -109,73 +119,75 @@ public function test_finds_existing_user_when_email_exists()
109
119
]);
110
120
}
111
121
112
- public function test_dispatches_create_anystack_license_job ()
122
+ public function test_creates_license_with_bifrost_source ()
113
123
{
114
124
$ user = User::factory ()->create ();
115
125
$ token = $ user ->createToken ('test-token ' )->plainTextToken ;
116
126
117
- $ this ->withHeaders ([
127
+ $ response = $ this ->withHeaders ([
118
128
'Authorization ' => 'Bearer ' .$ token ,
119
129
])->postJson ('/api/licenses ' , [
120
130
121
131
'name ' => 'Test User ' ,
122
132
'subscription ' => 'pro ' ,
123
133
]);
124
134
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
+ ]);
133
145
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 ' , [
139
148
'policy_name ' => 'pro ' ,
140
- 'source ' => LicenseSource::Bifrost,
149
+ 'source ' => 'bifrost ' ,
150
+ 'key ' => 'TEST-LICENSE-KEY ' ,
141
151
]);
142
152
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 ' , [
149
155
150
- 'name ' => 'Test User ' ,
151
- 'subscription ' => 'pro ' ,
152
156
]);
153
-
154
- $ response ->assertStatus (200 )
155
- ->assertJson ([
156
- 'id ' => $ license ->id ,
157
- 'policy_name ' => 'pro ' ,
158
- 'source ' => 'bifrost ' ,
159
- ]);
160
157
}
161
158
162
- public function test_returns_pending_response_when_license_not_found ()
159
+ public function test_creates_license_for_existing_user ()
163
160
{
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 ;
166
169
167
170
$ response = $ this ->withHeaders ([
168
171
'Authorization ' => 'Bearer ' .$ token ,
169
172
])->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 ' ,
173
176
]);
174
177
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 ' ,
179
185
]);
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 );
180
192
}
181
193
}
0 commit comments