22
33namespace PalauaAndSons \RevenueCatWebhooks \Tests ;
44
5- use Illuminate \Auth \Access \AuthorizationException ;
65use Illuminate \Support \Facades \Event ;
76use Illuminate \Support \Facades \Route ;
87use Spatie \WebhookClient \Exceptions \InvalidWebhookSignature ;
@@ -19,7 +18,7 @@ public function setUp(): void
1918 Route::revenueCatWebhooks ('revenuecat-webhooks ' );
2019 Route::revenueCatWebhooks ('revenuecat-webhooks/{configKey} ' );
2120
22- config (['revenuecat-webhooks.jobs ' => ['my_type ' => DummyJob::class]]);
21+ config (['revenuecat-webhooks.jobs ' => ['INITIAL_PURCHASE ' => DummyJob::class]]);
2322 cache ()->clear ();
2423 }
2524
@@ -29,8 +28,11 @@ public function it_can_handle_a_valid_request()
2928 $ this ->withoutExceptionHandling ();
3029
3130 $ payload = [
32- 'event_type ' => 'my_type ' ,
33- 'key ' => 'value ' ,
31+ 'api_version ' => '1.0 ' ,
32+ 'event ' => [
33+ 'type ' => 'INITIAL_PURCHASE ' ,
34+ 'key ' => 'value ' ,
35+ ],
3436 ];
3537
3638 $ this ->postJson ('revenuecat-webhooks ' , $ payload )
@@ -40,16 +42,17 @@ public function it_can_handle_a_valid_request()
4042
4143 $ webhookCall = WebhookCall::first ();
4244
43- $ this ->assertEquals ('my_type ' , $ webhookCall ->payload ['event_type ' ]);
45+ $ this ->assertEquals ('INITIAL_PURCHASE ' , $ webhookCall ->payload ['event ' ][ ' type ' ]);
4446 $ this ->assertEquals ($ payload , $ webhookCall ->payload );
4547 $ this ->assertNull ($ webhookCall ->exception );
4648
47- Event::assertDispatched ('revenuecat-webhooks::my_type ' , function ($ event , $ eventPayload ) use ($ webhookCall ) {
48- $ this ->assertInstanceOf (WebhookCall::class, $ eventPayload );
49- $ this ->assertEquals ($ webhookCall ->id , $ eventPayload ->id );
49+ Event::assertDispatched ('revenuecat-webhooks::INITIAL_PURCHASE ' ,
50+ function ($ event , $ eventPayload ) use ($ webhookCall ) {
51+ $ this ->assertInstanceOf (WebhookCall::class, $ eventPayload );
52+ $ this ->assertEquals ($ webhookCall ->id , $ eventPayload ->id );
5053
51- return true ;
52- });
54+ return true ;
55+ });
5356
5457 $ this ->assertEquals ($ webhookCall ->id , cache ('dummyjob ' )->id );
5558 }
@@ -66,12 +69,13 @@ public function a_request_with_an_invalid_payload_will_be_logged_but_events_and_
6669
6770 $ webhookCall = WebhookCall::first ();
6871
69- $ this ->assertFalse (isset ($ webhookCall ->payload ['event_type ' ]));
72+ $ this ->assertFalse (isset ($ webhookCall ->payload ['event ' ][ ' type ' ]));
7073 $ this ->assertEquals (['invalid_payload ' ], $ webhookCall ->payload );
7174
72- $ this ->assertEquals ('Webhook call id `1` did not contain a type. Valid RevenueCat webhook calls should always contain a type. ' , $ webhookCall ->exception ['message ' ]);
75+ $ this ->assertEquals ('Webhook call id `1` did not contain a type. Valid RevenueCat webhook calls should always contain a type. ' ,
76+ $ webhookCall ->exception ['message ' ]);
7377
74- Event::assertNotDispatched ('revenuecat-webhooks::my_type ' );
78+ Event::assertNotDispatched ('revenuecat-webhooks::INITIAL_PURCHASE ' );
7579
7680 $ this ->assertNull (cache ('dummyjob ' ));
7781 }
@@ -84,8 +88,11 @@ public function it_can_handle_a_valid_request_with_authorization_header()
8488 $ this ->withoutExceptionHandling ();
8589
8690 $ payload = [
87- 'event_type ' => 'my_type ' ,
88- 'key ' => 'value ' ,
91+ 'api_version ' => '1.0 ' ,
92+ 'event ' => [
93+ 'type ' => 'INITIAL_PURCHASE ' ,
94+ 'key ' => 'value ' ,
95+ ],
8996 ];
9097
9198 $ this ->postJson ('revenuecat-webhooks ' , $ payload , ['Authorization ' => 'Bearer ABC ' ])
@@ -95,16 +102,17 @@ public function it_can_handle_a_valid_request_with_authorization_header()
95102
96103 $ webhookCall = WebhookCall::first ();
97104
98- $ this ->assertEquals ('my_type ' , $ webhookCall ->payload ['event_type ' ]);
105+ $ this ->assertEquals ('INITIAL_PURCHASE ' , $ webhookCall ->payload ['event ' ][ ' type ' ]);
99106 $ this ->assertEquals ($ payload , $ webhookCall ->payload );
100107 $ this ->assertNull ($ webhookCall ->exception );
101108
102- Event::assertDispatched ('revenuecat-webhooks::my_type ' , function ($ event , $ eventPayload ) use ($ webhookCall ) {
103- $ this ->assertInstanceOf (WebhookCall::class, $ eventPayload );
104- $ this ->assertEquals ($ webhookCall ->id , $ eventPayload ->id );
109+ Event::assertDispatched ('revenuecat-webhooks::INITIAL_PURCHASE ' ,
110+ function ($ event , $ eventPayload ) use ($ webhookCall ) {
111+ $ this ->assertInstanceOf (WebhookCall::class, $ eventPayload );
112+ $ this ->assertEquals ($ webhookCall ->id , $ eventPayload ->id );
105113
106- return true ;
107- });
114+ return true ;
115+ });
108116
109117 $ this ->assertEquals ($ webhookCall ->id , cache ('dummyjob ' )->id );
110118 }
@@ -118,16 +126,19 @@ public function it_rejects_a_request_with_invalid_authorization_header()
118126 $ this ->expectException (InvalidWebhookSignature::class);
119127
120128 $ payload = [
121- 'event_type ' => 'my_type ' ,
122- 'key ' => 'value ' ,
129+ 'api_version ' => '1.0 ' ,
130+ 'event ' => [
131+ 'type ' => 'INITIAL_PURCHASE ' ,
132+ 'key ' => 'value ' ,
133+ ],
123134 ];
124135
125136 $ this ->postJson ('revenuecat-webhooks ' , $ payload , ['Authorization ' => 'Invalid Bearer Token ' ])
126137 ->assertStatus (500 );
127138
128139 $ this ->assertCount (0 , WebhookCall::get ());
129140
130- Event::assertNotDispatched ('revenuecat-webhooks::my_type ' );
141+ Event::assertNotDispatched ('revenuecat-webhooks::INITIAL_PURCHASE ' );
131142
132143 $ this ->assertNull (cache ('dummyjob ' ));
133144 }
0 commit comments