From c433a7d5612052e3eb5b0498978ca2ad26bafd0e Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 15 Apr 2021 14:28:20 +0200 Subject: [PATCH] Fixup subscription test to use a VAPIDKeyset --- .../tests/tests_accept_subscription.py | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/djangoldp_webpushnotification/tests/tests_accept_subscription.py b/djangoldp_webpushnotification/tests/tests_accept_subscription.py index eaa1117..c58cd88 100644 --- a/djangoldp_webpushnotification/tests/tests_accept_subscription.py +++ b/djangoldp_webpushnotification/tests/tests_accept_subscription.py @@ -1,8 +1,11 @@ import json +from base64 import urlsafe_b64encode from django.contrib.auth import get_user_model from django.test import Client, TestCase from django.urls import reverse +from djangoldp_webpushnotification.models import VAPIDKeyset +from ecdsa import NIST256p, SigningKey from webpush.models import PushInformation, SubscriptionInfo @@ -17,12 +20,22 @@ class TestAcceptSubscription(TestCase): def tearDown(self): self.user.delete() + def gen_vapid_key(self): + generated = SigningKey.generate(curve=NIST256p) + encoded = urlsafe_b64encode(generated.to_string()).strip(b"=") + return VAPIDKeyset.objects.create(private_key=encoded) + def test_accept_sub(self): + vapid_key_set = self.gen_vapid_key() + payload = { "status_type": "subscribe", "subscription": { - "endpoint": "https://example.com", - "keys": {"auth": "barfoo", "p256dh": "foobar"}, + "endpoint": "https://hubl.example.com", + "keys": { + "auth": "front-end-generated-secret", + "p256dh": vapid_key_set.public_key.decode("utf-8"), + }, }, "browser": "firefox", } @@ -36,9 +49,9 @@ class TestAcceptSubscription(TestCase): sub_info = SubscriptionInfo.objects.get() self.assertEqual(sub_info.browser, "firefox") - self.assertEqual(sub_info.endpoint, "https://example.com") - self.assertEqual(sub_info.auth, "barfoo") - self.assertEqual(sub_info.p256dh, "foobar") + self.assertEqual(sub_info.endpoint, "https://hubl.example.com") + self.assertEqual(sub_info.auth, "front-end-generated-secret") + self.assertEqual(sub_info.p256dh, vapid_key_set.public_key.decode("utf-8")) push_info = PushInformation.objects.get() self.assertEqual(push_info.user, self.user)