Fixup subscription test to use a VAPIDKeyset
This commit is contained in:
parent
59ce5900e0
commit
c433a7d561
@ -1,8 +1,11 @@
|
|||||||
import json
|
import json
|
||||||
|
from base64 import urlsafe_b64encode
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from djangoldp_webpushnotification.models import VAPIDKeyset
|
||||||
|
from ecdsa import NIST256p, SigningKey
|
||||||
from webpush.models import PushInformation, SubscriptionInfo
|
from webpush.models import PushInformation, SubscriptionInfo
|
||||||
|
|
||||||
|
|
||||||
@ -17,12 +20,22 @@ class TestAcceptSubscription(TestCase):
|
|||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
self.user.delete()
|
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):
|
def test_accept_sub(self):
|
||||||
|
vapid_key_set = self.gen_vapid_key()
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
"status_type": "subscribe",
|
"status_type": "subscribe",
|
||||||
"subscription": {
|
"subscription": {
|
||||||
"endpoint": "https://example.com",
|
"endpoint": "https://hubl.example.com",
|
||||||
"keys": {"auth": "barfoo", "p256dh": "foobar"},
|
"keys": {
|
||||||
|
"auth": "front-end-generated-secret",
|
||||||
|
"p256dh": vapid_key_set.public_key.decode("utf-8"),
|
||||||
|
},
|
||||||
},
|
},
|
||||||
"browser": "firefox",
|
"browser": "firefox",
|
||||||
}
|
}
|
||||||
@ -36,9 +49,9 @@ class TestAcceptSubscription(TestCase):
|
|||||||
|
|
||||||
sub_info = SubscriptionInfo.objects.get()
|
sub_info = SubscriptionInfo.objects.get()
|
||||||
self.assertEqual(sub_info.browser, "firefox")
|
self.assertEqual(sub_info.browser, "firefox")
|
||||||
self.assertEqual(sub_info.endpoint, "https://example.com")
|
self.assertEqual(sub_info.endpoint, "https://hubl.example.com")
|
||||||
self.assertEqual(sub_info.auth, "barfoo")
|
self.assertEqual(sub_info.auth, "front-end-generated-secret")
|
||||||
self.assertEqual(sub_info.p256dh, "foobar")
|
self.assertEqual(sub_info.p256dh, vapid_key_set.public_key.decode("utf-8"))
|
||||||
|
|
||||||
push_info = PushInformation.objects.get()
|
push_info = PushInformation.objects.get()
|
||||||
self.assertEqual(push_info.user, self.user)
|
self.assertEqual(push_info.user, self.user)
|
||||||
|
Reference in New Issue
Block a user