-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathCustomerLicenseManagementTest.php
More file actions
420 lines (341 loc) · 13.3 KB
/
Copy pathCustomerLicenseManagementTest.php
File metadata and controls
420 lines (341 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
namespace Tests\Feature;
use App\Features\ShowAuthButtons;
use App\Livewire\Customer\Licenses\Show;
use App\Models\License;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Laravel\Pennant\Feature;
use Livewire\Livewire;
use Tests\TestCase;
class CustomerLicenseManagementTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Enable the auth feature flag for all license management tests
Feature::define(ShowAuthButtons::class, true);
}
public function test_customer_can_view_licenses_page(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/dashboard/licenses');
$response->assertStatus(200);
$response->assertSee('Your Licenses');
$response->assertSee('Manage your NativePHP licenses');
}
public function test_customer_sees_no_licenses_message_when_no_licenses_exist(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/dashboard/licenses');
$response->assertStatus(200);
$response->assertSee('No licenses found');
$response->assertSee('believe this is an error');
}
public function test_customer_can_view_their_licenses(): void
{
$user = User::factory()->create();
$license1 = License::factory()->create([
'user_id' => $user->id,
'policy_name' => 'Standard License',
'key' => 'test-key-1',
]);
$license2 = License::factory()->create([
'user_id' => $user->id,
'policy_name' => 'Premium License',
'key' => 'test-key-2',
]);
$response = $this->actingAs($user)->get('/dashboard/licenses');
$response->assertStatus(200);
$response->assertSee('Standard License');
$response->assertSee('Premium License');
$response->assertSee('test-key-1');
$response->assertSee('test-key-2');
}
public function test_customer_cannot_view_other_customers_licenses(): void
{
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$license1 = License::factory()->create([
'user_id' => $user1->id,
'policy_name' => 'User 1 License',
]);
$license2 = License::factory()->create([
'user_id' => $user2->id,
'policy_name' => 'User 2 License',
]);
$response = $this->actingAs($user1)->get('/dashboard/licenses');
$response->assertStatus(200);
$response->assertSee('User 1 License');
$response->assertDontSee('User 2 License');
}
public function test_customer_can_view_individual_license_details(): void
{
$user = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user->id,
'policy_name' => 'pro',
'key' => 'test-license-key-123',
'expires_at' => now()->addDays(30),
]);
$response = $this->actingAs($user)->get('/dashboard/licenses/'.$license->key);
$response->assertStatus(200);
$response->assertSee('pro');
$response->assertSee('test-license-key-123');
$response->assertSee('License Information');
$response->assertDontSee('Active');
$response->assertDontSee('Expires');
}
public function test_customer_cannot_view_other_customers_license_details(): void
{
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user2->id,
'key' => 'other-user-license',
]);
$response = $this->actingAs($user1)->get('/dashboard/licenses/'.$license->key);
$response->assertStatus(404);
}
public function test_license_index_does_not_display_status_or_expiry(): void
{
$user = User::factory()->create();
// Active license
$activeLicense = License::factory()->create([
'user_id' => $user->id,
'key' => 'license-key-one',
'expires_at' => now()->addDays(30),
'is_suspended' => false,
]);
// Expired license
$expiredLicense = License::factory()->create([
'user_id' => $user->id,
'key' => 'license-key-two',
'expires_at' => now()->subDays(1),
'is_suspended' => false,
]);
// Suspended license
$suspendedLicense = License::factory()->create([
'user_id' => $user->id,
'key' => 'license-key-three',
'is_suspended' => true,
]);
$response = $this->actingAs($user)->get('/dashboard/licenses');
$response->assertStatus(200);
$response->assertSee('license-key-one');
$response->assertSee('license-key-two');
$response->assertSee('license-key-three');
$response->assertDontSee('Status');
$response->assertDontSee('Expires');
$response->assertDontSee('Expired');
$response->assertDontSee('Suspended');
}
public function test_customer_can_update_license_name(): void
{
$user = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user->id,
'key' => 'test-license-key',
'name' => null,
]);
$response = $this->actingAs($user)
->patch('/dashboard/licenses/'.$license->key, [
'name' => 'My Production License',
]);
$response->assertRedirect('/dashboard/licenses/'.$license->key);
$response->assertSessionHas('success', 'License name updated successfully!');
$this->assertDatabaseHas('licenses', [
'id' => $license->id,
'name' => 'My Production License',
]);
}
public function test_customer_can_clear_license_name(): void
{
$user = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user->id,
'key' => 'test-license-key',
'name' => 'Old Name',
]);
$response = $this->actingAs($user)
->patch('/dashboard/licenses/'.$license->key, [
'name' => '',
]);
$response->assertRedirect('/dashboard/licenses/'.$license->key);
$response->assertSessionHas('success', 'License name updated successfully!');
$this->assertDatabaseHas('licenses', [
'id' => $license->id,
'name' => null,
]);
}
public function test_license_name_validation(): void
{
$user = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user->id,
'key' => 'test-license-key',
]);
$response = $this->actingAs($user)
->patch('/dashboard/licenses/'.$license->key, [
'name' => str_repeat('a', 256), // Too long
]);
$response->assertSessionHasErrors(['name']);
}
public function test_customer_cannot_update_other_customers_license_name(): void
{
$user1 = User::factory()->create();
$user2 = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user2->id,
'key' => 'other-user-license',
]);
$response = $this->actingAs($user1)
->patch('/dashboard/licenses/'.$license->key, [
'name' => 'Hacked Name',
]);
$response->assertStatus(404);
}
public function test_license_names_display_on_list_page(): void
{
$user = User::factory()->create();
$namedLicense = License::factory()->create([
'user_id' => $user->id,
'policy_name' => 'pro',
'name' => 'My Custom License Name',
]);
$unnamedLicense = License::factory()->create([
'user_id' => $user->id,
'policy_name' => 'starter',
'name' => null,
]);
$response = $this->actingAs($user)->get('/dashboard/licenses');
$response->assertStatus(200);
// Named license should show custom name prominently
$response->assertSee('My Custom License Name');
$response->assertSee('pro');
// Unnamed license should show policy name
$response->assertSee('starter');
}
public function test_license_name_displays_on_show_page(): void
{
$user = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user->id,
'key' => 'test-license-key',
'name' => 'My Custom License',
]);
$response = $this->actingAs($user)->get('/dashboard/licenses/'.$license->key);
$response->assertStatus(200);
$response->assertSee('My Custom License');
$response->assertSee('License Name');
}
public function test_license_show_page_displays_no_name_set_when_name_is_null(): void
{
$user = User::factory()->create();
$license = License::factory()->create([
'user_id' => $user->id,
'key' => 'test-license-key',
'name' => null,
]);
$response = $this->actingAs($user)->get('/dashboard/licenses/'.$license->key);
$response->assertStatus(200);
$response->assertSee('No name set');
}
public function test_dashboard_shows_license_count(): void
{
$user = User::factory()->create();
License::factory()->count(3)->create(['user_id' => $user->id]);
$response = $this->actingAs($user)->get('/dashboard');
$response->assertStatus(200);
$response->assertSee('View licenses');
}
public function test_dashboard_hides_licenses_card_when_user_has_no_licenses(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)->get('/dashboard');
$response->assertStatus(200);
$response->assertDontSee('View licenses');
}
public function test_customer_can_rotate_license_key(): void
{
Http::fake([
'https://api.anystack.sh/v1/products/*/licenses' => Http::response([
'data' => [
'id' => 'new-anystack-id',
'key' => 'new-rotated-key',
'expires_at' => now()->addYear()->toIso8601String(),
'created_at' => now()->toIso8601String(),
'updated_at' => now()->toIso8601String(),
],
], 201),
'https://api.anystack.sh/v1/products/*/licenses/*' => Http::response([
'data' => ['suspended' => true],
], 200),
]);
$user = User::factory()->create(['anystack_contact_id' => 'contact-123']);
$license = License::factory()->active()->create([
'user_id' => $user->id,
'policy_name' => 'mini',
'key' => 'old-key-to-rotate',
'anystack_id' => 'old-anystack-id',
]);
Livewire::actingAs($user)
->test(Show::class, ['licenseKey' => 'old-key-to-rotate'])
->call('rotateLicenseKey')
->assertRedirect(route('customer.licenses.show', 'new-rotated-key'));
$license->refresh();
$this->assertEquals('new-rotated-key', $license->key);
$this->assertEquals('new-anystack-id', $license->anystack_id);
$this->assertFalse($license->is_suspended);
}
public function test_customer_cannot_rotate_suspended_license_key(): void
{
$user = User::factory()->create();
$license = License::factory()->suspended()->create([
'user_id' => $user->id,
'key' => 'suspended-license-key',
]);
Livewire::actingAs($user)
->test(Show::class, ['licenseKey' => 'suspended-license-key'])
->call('rotateLicenseKey')
->assertNoRedirect();
Http::assertNothingSent();
}
public function test_customer_cannot_rotate_expired_license_key(): void
{
$user = User::factory()->create();
$license = License::factory()->expired()->create([
'user_id' => $user->id,
'key' => 'expired-license-key',
]);
Livewire::actingAs($user)
->test(Show::class, ['licenseKey' => 'expired-license-key'])
->call('rotateLicenseKey')
->assertNoRedirect();
Http::assertNothingSent();
}
public function test_active_license_shows_rotate_button(): void
{
$user = User::factory()->create();
$license = License::factory()->active()->create([
'user_id' => $user->id,
'key' => 'active-license-key',
]);
$response = $this->actingAs($user)->get('/dashboard/licenses/active-license-key');
$response->assertStatus(200);
$response->assertSee('Rotate key');
}
public function test_suspended_license_does_not_show_rotate_button(): void
{
$user = User::factory()->create();
$license = License::factory()->suspended()->create([
'user_id' => $user->id,
'key' => 'suspended-license-key',
]);
$response = $this->actingAs($user)->get('/dashboard/licenses/suspended-license-key');
$response->assertStatus(200);
$response->assertDontSee('Rotate key');
}
}