|
| 1 | +import hashlib |
| 2 | +import hmac |
| 3 | +import json |
| 4 | +import time |
1 | 5 | from unittest import mock |
2 | 6 |
|
3 | 7 | import stripe |
@@ -433,3 +437,75 @@ def test_idempotent_checkout_processing(self, mock_construct, mock_retrieve): |
433 | 437 | self.assertEqual(response.status_code, 200) |
434 | 438 | # Should still only have one subscription |
435 | 439 | self.assertEqual(UserSubscription.objects.filter(user=self.user).count(), 1) |
| 440 | + |
| 441 | + |
| 442 | +def _signed_subscription_updated(secret, subscription_id, quantity): |
| 443 | + payload = json.dumps( |
| 444 | + { |
| 445 | + "id": "evt_test", |
| 446 | + "object": "event", |
| 447 | + "type": "customer.subscription.updated", |
| 448 | + "data": { |
| 449 | + "object": { |
| 450 | + "id": subscription_id, |
| 451 | + "status": "active", |
| 452 | + "cancel_at_period_end": False, |
| 453 | + "items": {"data": [{"quantity": quantity}]}, |
| 454 | + } |
| 455 | + }, |
| 456 | + } |
| 457 | + ) |
| 458 | + timestamp = int(time.time()) |
| 459 | + signature = hmac.new( |
| 460 | + secret.encode(), f"{timestamp}.{payload}".encode(), hashlib.sha256 |
| 461 | + ).hexdigest() |
| 462 | + return payload, f"t={timestamp},v1={signature}" |
| 463 | + |
| 464 | + |
| 465 | +class StripeWebhookSignatureTest(StudioAPITestCase): |
| 466 | + """Exercise real signature verification, without mocking construct_event.""" |
| 467 | + |
| 468 | + def setUp(self): |
| 469 | + self.user = testdata.user(email="webhook-signature@test.com") |
| 470 | + self.subscription = UserSubscription.objects.create( |
| 471 | + user=self.user, |
| 472 | + stripe_customer_id="cus_test123", |
| 473 | + stripe_subscription_id="sub_test123", |
| 474 | + stripe_subscription_status="active", |
| 475 | + subscription_disk_space=0, |
| 476 | + ) |
| 477 | + self.webhook_url = reverse("stripe_webhook") |
| 478 | + |
| 479 | + def _post(self, secret): |
| 480 | + payload, signature = _signed_subscription_updated(secret, "sub_test123", 50) |
| 481 | + return self.client.post( |
| 482 | + self.webhook_url, |
| 483 | + data=payload, |
| 484 | + content_type="application/json", |
| 485 | + HTTP_STRIPE_SIGNATURE=signature, |
| 486 | + ) |
| 487 | + |
| 488 | + @override_settings(STRIPE_WEBHOOK_SECRET="") |
| 489 | + def test_rejects_event_signed_with_empty_secret_when_none_configured(self): |
| 490 | + with self.assertLogs("contentcuration.views.subscription", "ERROR"): |
| 491 | + response = self._post("") |
| 492 | + |
| 493 | + self.assertEqual(response.status_code, 400) |
| 494 | + self.subscription.refresh_from_db() |
| 495 | + self.assertEqual(self.subscription.subscription_disk_space, 0) |
| 496 | + |
| 497 | + @override_settings(STRIPE_WEBHOOK_SECRET="whsec_test_fake") |
| 498 | + def test_handles_correctly_signed_event_when_secret_configured(self): |
| 499 | + response = self._post("whsec_test_fake") |
| 500 | + |
| 501 | + self.assertEqual(response.status_code, 200) |
| 502 | + self.subscription.refresh_from_db() |
| 503 | + self.assertEqual(self.subscription.subscription_disk_space, 50 * 10 ** 9) |
| 504 | + |
| 505 | + @override_settings(STRIPE_WEBHOOK_SECRET="whsec_test_fake") |
| 506 | + def test_rejects_event_signed_with_wrong_secret(self): |
| 507 | + response = self._post("") |
| 508 | + |
| 509 | + self.assertEqual(response.status_code, 400) |
| 510 | + self.subscription.refresh_from_db() |
| 511 | + self.assertEqual(self.subscription.subscription_disk_space, 0) |
0 commit comments