-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathCreateLicenseTest.php
More file actions
187 lines (156 loc) · 5.47 KB
/
Copy pathCreateLicenseTest.php
File metadata and controls
187 lines (156 loc) · 5.47 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
<?php
namespace Tests\Feature\Api;
use App\Models\License;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
class CreateLicenseTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
// Mock the Anystack API calls
Http::fake([
'https://api.anystack.sh/v1/contacts' => Http::response(['data' => ['id' => 'contact_123']], 200),
'https://api.anystack.sh/v1/products/*/licenses' => Http::response([
'data' => [
'id' => 'license_123',
'key' => 'TEST-LICENSE-KEY',
'expires_at' => null,
'created_at' => now()->toISOString(),
'updated_at' => now()->toISOString(),
],
], 200),
]);
}
public function test_requires_authentication()
{
$response = $this->postJson('/api/licenses', [
'email' => 'test@example.com',
'name' => 'Test User',
'subscription' => 'pro',
]);
$response->assertStatus(401);
}
public function test_validates_required_fields()
{
$token = config('services.bifrost.api_key');
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
])->postJson('/api/licenses', []);
$response->assertStatus(422)
->assertJsonValidationErrors(['email', 'name', 'subscription']);
}
public function test_validates_subscription_enum()
{
$token = config('services.bifrost.api_key');
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
])->postJson('/api/licenses', [
'email' => 'test@example.com',
'name' => 'Test User',
'subscription' => 'invalid',
]);
$response->assertStatus(422)
->assertJsonValidationErrors(['subscription']);
}
public function test_creates_new_user_when_email_not_exists()
{
$token = config('services.bifrost.api_key');
$this->withHeaders([
'Authorization' => 'Bearer '.$token,
])->postJson('/api/licenses', [
'email' => 'newuser@example.com',
'name' => 'New User',
'subscription' => 'pro',
]);
$this->assertDatabaseHas('users', [
'email' => 'newuser@example.com',
'name' => 'New User',
]);
$newUser = User::where('email', 'newuser@example.com')->first();
$this->assertNotNull($newUser->password);
}
public function test_finds_existing_user_when_email_exists()
{
$existingUser = User::factory()->create([
'email' => 'existing@example.com',
'name' => 'Original Name',
]);
$token = config('services.bifrost.api_key');
$this->withHeaders([
'Authorization' => 'Bearer '.$token,
])->postJson('/api/licenses', [
'email' => 'existing@example.com',
'name' => 'New Name',
'subscription' => 'pro',
]);
// User should not be updated, original name should remain
$this->assertDatabaseHas('users', [
'email' => 'existing@example.com',
'name' => 'Original Name',
]);
}
public function test_creates_license_with_bifrost_source()
{
$token = config('services.bifrost.api_key');
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
])->postJson('/api/licenses', [
'email' => 'test@example.com',
'name' => 'Test User',
'subscription' => 'pro',
]);
$response->assertStatus(200)
->assertJsonStructure([
'id',
'user_id',
'policy_name',
'source',
'key',
'created_at',
'updated_at',
]);
// Verify the license was created with correct attributes
$this->assertDatabaseHas('licenses', [
'policy_name' => 'pro',
'source' => 'bifrost',
'key' => 'TEST-LICENSE-KEY',
]);
// Verify user was created/found
$this->assertDatabaseHas('users', [
'email' => 'test@example.com',
]);
}
public function test_creates_license_for_existing_user()
{
// Create an existing user
$existingUser = User::factory()->create([
'email' => 'existing@example.com',
'name' => 'Existing User',
]);
$token = config('services.bifrost.api_key');
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$token,
])->postJson('/api/licenses', [
'email' => 'existing@example.com',
'name' => 'Different Name', // This should be ignored
'subscription' => 'max',
]);
$response->assertStatus(200)
->assertJsonStructure([
'id',
'user_id',
'policy_name',
'source',
'key',
]);
// Verify license was created for the existing user
$license = License::where('user_id', $existingUser->id)->first();
$this->assertNotNull($license);
$this->assertEquals('max', $license->policy_name);
$this->assertEquals('bifrost', $license->source->value);
}
}