summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristopher Viel <viel.christopher@gmail.com>2018-01-03 19:03:04 (GMT)
committerChristopher Viel <viel.christopher@gmail.com>2018-01-03 19:58:23 (GMT)
commitc289cdbfe677b524ab97e5a54b8b421a1cd54f15 (patch)
treedb84fdb8e6ad5fc946f6f2276ebaf7f897a37fa5
parent1db719e153ae980a60dbef9ca68171027bf9f170 (diff)
downloadpywebpush-c289cdbfe677b524ab97e5a54b8b421a1cd54f15.zip
pywebpush-c289cdbfe677b524ab97e5a54b8b421a1cd54f15.tar.gz
pywebpush-c289cdbfe677b524ab97e5a54b8b421a1cd54f15.tar.bz2
Copy subscription before altering the keys.
Altering subscriptions reference by changing the keys from string to byte causes hard to debug issues in libraries. For exemple, trying to call `json.dumps()` after initializing `WebPusher` throws an error because json cannot handle bytes.
-rw-r--r--pywebpush/__init__.py3
-rw-r--r--pywebpush/tests/test_webpush.py7
2 files changed, 7 insertions, 3 deletions
diff --git a/pywebpush/__init__.py b/pywebpush/__init__.py
index 3312745..d093bf8 100644
--- a/pywebpush/__init__.py
+++ b/pywebpush/__init__.py
@@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import base64
+from copy import deepcopy
import json
import os
@@ -116,7 +117,7 @@ class WebPusher:
if 'endpoint' not in subscription_info:
raise WebPushException("subscription_info missing endpoint URL")
- self.subscription_info = subscription_info
+ self.subscription_info = deepcopy(subscription_info)
self.auth_key = self.receiver_key = None
if 'keys' in subscription_info:
keys = self.subscription_info['keys']
diff --git a/pywebpush/tests/test_webpush.py b/pywebpush/tests/test_webpush.py
index 136f749..ded6a69 100644
--- a/pywebpush/tests/test_webpush.py
+++ b/pywebpush/tests/test_webpush.py
@@ -4,7 +4,7 @@ import os
import unittest
from mock import patch
-from nose.tools import eq_, ok_, assert_raises
+from nose.tools import eq_, ok_, assert_is_not, assert_raises
import http_ece
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.backends import default_backend
@@ -75,7 +75,10 @@ class WebpushTestCase(unittest.TestCase):
"keys": {'p256dh': 'AAA=', 'auth': 'AAA='}})
push = WebPusher(subscription_info)
- eq_(push.subscription_info, subscription_info)
+ assert_is_not(push.subscription_info, subscription_info)
+ assert_is_not(push.subscription_info['keys'],
+ subscription_info['keys'])
+ eq_(push.subscription_info['endpoint'], subscription_info['endpoint'])
eq_(push.receiver_key, rk_decode)
eq_(push.auth_key, b'\x93\xc2U\xea\xc8\xddn\x10"\xd6}\xff,0K\xbc')