@@ -14,7 +14,6 @@ def test_parse_unsafe_webhook_payload
1414 } . to_json
1515
1616 event = @lithic . webhooks . parse_unsafe ( payload )
17- # Verify the event was parsed successfully
1817 assert ( event )
1918 assert_equal ( "card.created" , event . event_type )
2019 end
@@ -28,7 +27,6 @@ def test_parse_with_valid_signature
2827 payload = '{"event_type":"card.created","card_token":"test_card_token_123"}'
2928 timestamp = Time . now . to_i . to_s
3029
31- # Generate a valid signature
3230 wh = StandardWebhooks ::Webhook . new ( secret )
3331 signature = wh . sign ( "test_msg_id" , timestamp , payload )
3432
@@ -38,12 +36,10 @@ def test_parse_with_valid_signature
3836 "webhook-signature" => signature
3937 }
4038
41- # Should verify and return typed event
4239 event = @lithic . webhooks . parse ( payload , headers : headers , secret : secret )
4340 assert ( event )
4441 assert_equal ( :"card.created" , event . event_type )
4542
46- # Verify we can pattern match on the typed event
4743 case event
4844 when Lithic ::Models ::CardCreatedWebhookEvent
4945 assert_equal ( "test_card_token_123" , event . card_token )
@@ -52,6 +48,36 @@ def test_parse_with_valid_signature
5248 end
5349 end
5450
51+ def test_parse_with_invalid_signatures
52+ skip ( "standardwebhooks gem not installed" ) unless gem_installed? ( "standardwebhooks" )
53+
54+ require ( "standardwebhooks" )
55+
56+ secret = "whsec_test_secret"
57+ payload = '{"event_type":"card.created"}'
58+ timestamp = Time . now . to_i . to_s
59+
60+ wh = StandardWebhooks ::Webhook . new ( secret )
61+ signature = wh . sign ( "test_msg_id" , timestamp , payload )
62+
63+ headers = {
64+ "webhook-id" => "test_msg_id" ,
65+ "webhook-timestamp" => timestamp ,
66+ "webhook-signature" => signature
67+ }
68+
69+ bad_headers = [
70+ headers . merge ( "webhook-id" => "bad" ) ,
71+ headers . merge ( "webhook-timestamp" => "0" ) ,
72+ headers . merge ( "webhook-signature" => wh . sign ( "test_msg_id" , timestamp , "xxx" ) )
73+ ]
74+ bad_headers . each do |bad_header |
75+ assert_raises ( StandardWebhooks ::WebhookVerificationError ) do
76+ @lithic . webhooks . parse ( payload , headers : bad_header , secret : secret )
77+ end
78+ end
79+ end
80+
5581 def test_parse_with_env_var_secret
5682 skip ( "standardwebhooks gem not installed" ) unless gem_installed? ( "standardwebhooks" )
5783
@@ -61,7 +87,6 @@ def test_parse_with_env_var_secret
6187 payload = '{"event_type":"card.created","data":{"token":"test_token"}}'
6288 timestamp = Time . now . to_i . to_s
6389
64- # Generate a valid signature
6590 wh = StandardWebhooks ::Webhook . new ( secret )
6691 signature = wh . sign ( "test_msg_id" , timestamp , payload )
6792
@@ -71,10 +96,8 @@ def test_parse_with_env_var_secret
7196 "webhook-signature" => signature
7297 }
7398
74- # Set env var
7599 ENV [ "LITHIC_WEBHOOK_SECRET" ] = secret
76100 begin
77- # Should use env var when secret not provided
78101 event = @lithic . webhooks . parse ( payload , headers : headers )
79102 assert ( event )
80103 assert_equal ( "card.created" , event . event_type )
@@ -93,7 +116,6 @@ def test_parse_raises_when_secret_missing
93116 "webhook-signature" => "v1,invalid"
94117 }
95118
96- # Ensure env var is not set
97119 ENV . delete ( "LITHIC_WEBHOOK_SECRET" )
98120
99121 error = assert_raises ( ArgumentError ) do
@@ -112,7 +134,6 @@ def test_verify_signature_with_valid_signature
112134 payload = '{"event_type":"card.created"}'
113135 timestamp = Time . now . to_i . to_s
114136
115- # Generate a valid signature
116137 wh = StandardWebhooks ::Webhook . new ( secret )
117138 signature = wh . sign ( "test_msg_id" , timestamp , payload )
118139
@@ -122,14 +143,12 @@ def test_verify_signature_with_valid_signature
122143 "webhook-signature" => signature
123144 }
124145
125- # Should not raise an error and returns the parsed JSON
126146 result = @lithic . webhooks . verify_signature (
127147 payload : payload ,
128148 headers : headers ,
129149 secret : secret
130150 )
131151
132- # verify returns the parsed JSON content with symbolized keys
133152 assert_equal ( "card.created" , result [ :event_type ] )
134153 end
135154
@@ -165,7 +184,6 @@ def test_verify_signature_with_env_var
165184 payload = '{"event_type":"card.created"}'
166185 timestamp = Time . now . to_i . to_s
167186
168- # Generate a valid signature
169187 wh = StandardWebhooks ::Webhook . new ( secret )
170188 signature = wh . sign ( "test_msg_id" , timestamp , payload )
171189
@@ -175,10 +193,8 @@ def test_verify_signature_with_env_var
175193 "webhook-signature" => signature
176194 }
177195
178- # Set env var
179196 ENV [ "LITHIC_WEBHOOK_SECRET" ] = secret
180197 begin
181- # Should use env var when secret not provided
182198 result = @lithic . webhooks . verify_signature ( payload : payload , headers : headers )
183199 assert_equal ( "card.created" , result [ :event_type ] )
184200 ensure
0 commit comments