66use App \Jobs \CreateAnystackLicenseJob ;
77use App \Models \User ;
88use App \Notifications \LicenseKeyGenerated ;
9+ use Carbon \CarbonImmutable ;
910use Illuminate \Foundation \Testing \RefreshDatabase ;
10- use Illuminate \Support \Facades \Cache ;
1111use Illuminate \Support \Facades \Http ;
1212use Illuminate \Support \Facades \Notification ;
13+ use Illuminate \Support \Str ;
1314use Tests \TestCase ;
1415
1516class CreateAnystackLicenseJobTest extends TestCase
1617{
1718 use RefreshDatabase;
1819
20+ protected CarbonImmutable $ now ;
21+
1922 protected function setUp (): void
2023 {
2124 parent ::setUp ();
2225
26+ $ this ->now = now ()->toImmutable ();
27+
2328 Http::fake ([
2429 'https://api.anystack.sh/v1/contacts ' => Http::response ([
2530 'data ' => [
2631 'id ' => 'contact-123 ' ,
2732 'email ' => 'test@example.com ' ,
2833 'first_name ' => 'John ' ,
2934 'last_name ' => 'Doe ' ,
35+ 'created_at ' => $ this ->now ->toIso8601String (),
36+ 'updated_at ' => $ this ->now ->toIso8601String (),
3037 ],
3138 ], 201 ),
3239
@@ -36,6 +43,13 @@ protected function setUp(): void
3643 'key ' => 'test-license-key-12345 ' ,
3744 'contact_id ' => 'contact-123 ' ,
3845 'policy_id ' => 'policy-123 ' ,
46+ 'name ' => null ,
47+ 'activations ' => 0 ,
48+ 'max_activations ' => 10 ,
49+ 'suspended ' => false ,
50+ 'expires_at ' => $ this ->now ->addYear ()->toIso8601String (),
51+ 'created_at ' => $ this ->now ->toIso8601String (),
52+ 'updated_at ' => $ this ->now ->toIso8601String (),
3953 ],
4054 ], 201 ),
4155 ]);
@@ -44,7 +58,7 @@ protected function setUp(): void
4458 }
4559
4660 /** @test */
47- public function it_creates_contact_and_license_on_anystack ()
61+ public function it_creates_a_contact_and_license_on_anystack_via_api ()
4862 {
4963 $ user = User::factory ()->create ([
5064 'email ' => 'test@example.com ' ,
@@ -54,6 +68,7 @@ public function it_creates_contact_and_license_on_anystack()
5468 $ job = new CreateAnystackLicenseJob (
5569 $ user ,
5670 Subscription::Max,
71+ null ,
5772 'John ' ,
5873 'Doe '
5974 );
@@ -83,7 +98,31 @@ public function it_creates_contact_and_license_on_anystack()
8398 }
8499
85100 /** @test */
86- public function it_stores_license_key_in_cache ()
101+ public function it_does_not_create_a_contact_when_the_user_already_has_a_contact_id ()
102+ {
103+ $ user = User::factory ()->create ([
104+ 'email ' => 'test@example.com ' ,
105+ 'name ' => 'John Doe ' ,
106+ 'anystack_contact_id ' => 'contact-123 ' ,
107+ ]);
108+
109+ $ job = new CreateAnystackLicenseJob (
110+ $ user ,
111+ Subscription::Max,
112+ null ,
113+ 'John ' ,
114+ 'Doe '
115+ );
116+
117+ $ job ->handle ();
118+
119+ Http::assertNotSent (function ($ request ) {
120+ return Str::contains ($ request ->url (), 'https://api.anystack.sh/v1/contacts ' );
121+ });
122+ }
123+
124+ /** @test */
125+ public function it_stores_the_license_key_in_database ()
87126 {
88127 $ user = User::factory ()->create ([
89128 'email ' => 'test@example.com ' ,
@@ -93,17 +132,55 @@ public function it_stores_license_key_in_cache()
93132 $ job = new CreateAnystackLicenseJob (
94133 $ user ,
95134 Subscription::Max,
135+ null ,
96136 'John ' ,
97137 'Doe '
98138 );
99139
100140 $ job ->handle ();
101141
102- $ this ->assertEquals ('test-license-key-12345 ' , Cache::get ('test@example.com.license_key ' ));
142+ $ this ->assertDatabaseHas ('licenses ' , [
143+ 'user_id ' => $ user ->id ,
144+ 'subscription_item_id ' => null ,
145+ 'policy_name ' => 'max ' ,
146+ 'key ' => 'test-license-key-12345 ' ,
147+ 'expires_at ' => $ this ->now ->addYear (),
148+ 'created_at ' => $ this ->now ,
149+ 'updated_at ' => $ this ->now ,
150+ ]);
151+ }
152+
153+ /** @test */
154+ public function the_subscription_item_id_is_filled_when_provided ()
155+ {
156+ $ user = User::factory ()->create ([
157+ 'email ' => 'test@example.com ' ,
158+ 'name ' => 'John Doe ' ,
159+ ]);
160+
161+ $ job = new CreateAnystackLicenseJob (
162+ $ user ,
163+ Subscription::Max,
164+ 123 ,
165+ 'John ' ,
166+ 'Doe '
167+ );
168+
169+ $ job ->handle ();
170+
171+ $ this ->assertDatabaseHas ('licenses ' , [
172+ 'user_id ' => $ user ->id ,
173+ 'subscription_item_id ' => 123 ,
174+ 'policy_name ' => 'max ' ,
175+ 'key ' => 'test-license-key-12345 ' ,
176+ 'expires_at ' => $ this ->now ->addYear (),
177+ 'created_at ' => $ this ->now ,
178+ 'updated_at ' => $ this ->now ,
179+ ]);
103180 }
104181
105182 /** @test */
106- public function it_sends_license_key_notification ()
183+ public function it_sends_a_license_key_notification ()
107184 {
108185 $ user = User::factory ()->create ([
109186 'email ' => 'test@example.com ' ,
@@ -113,6 +190,7 @@ public function it_sends_license_key_notification()
113190 $ job = new CreateAnystackLicenseJob (
114191 $ user ,
115192 Subscription::Max,
193+ null ,
116194 'John ' ,
117195 'Doe '
118196 );
0 commit comments