From 74b271269937eb5752133e8a91cd957f1663dc29 Mon Sep 17 00:00:00 2001 From: decentral1se Date: Thu, 8 Apr 2021 16:28:49 +0200 Subject: [PATCH] Test VAPIDKeyset can generate a public key --- djangoldp_webpushnotification/models.py | 2 +- .../tests/settings_default.py | 1 + .../tests/tests_vapidkeyset.py | 13 +++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/djangoldp_webpushnotification/models.py b/djangoldp_webpushnotification/models.py index 08cf378..47a418f 100644 --- a/djangoldp_webpushnotification/models.py +++ b/djangoldp_webpushnotification/models.py @@ -15,7 +15,7 @@ class VAPIDKeyset(models.Model): @property def public_key(self): - key_str = self.private_key.tobytes() + key_str = self.private_key padding = len(key_str) % 4 key_str += b"=" * padding key = SigningKey.from_string( diff --git a/djangoldp_webpushnotification/tests/settings_default.py b/djangoldp_webpushnotification/tests/settings_default.py index 7a3bc08..8d81b9e 100644 --- a/djangoldp_webpushnotification/tests/settings_default.py +++ b/djangoldp_webpushnotification/tests/settings_default.py @@ -4,6 +4,7 @@ yaml_config = """ dependencies: ldppackages: + - djangoldp_webpushnotification - djangoldp_webpushnotification.tests server: diff --git a/djangoldp_webpushnotification/tests/tests_vapidkeyset.py b/djangoldp_webpushnotification/tests/tests_vapidkeyset.py index 006d74a..af71f55 100644 --- a/djangoldp_webpushnotification/tests/tests_vapidkeyset.py +++ b/djangoldp_webpushnotification/tests/tests_vapidkeyset.py @@ -1,6 +1,15 @@ +from base64 import urlsafe_b64encode + +import ecdsa from django.test import TestCase +from djangoldp_webpushnotification.models import VAPIDKeyset +from ecdsa import SigningKey class TestRSAKey(TestCase): - def test_foobar(self): - assert 2 == 2 + def test_vapidkeyset_public_key(self): + priv_key = SigningKey.generate(curve=ecdsa.NIST256p) + vapid_key_set = VAPIDKeyset.objects.create( + private_key=urlsafe_b64encode(priv_key.to_string()).strip(b"=") + ) + assert isinstance(vapid_key_set.public_key, bytes)