6
6
from mock import patch , Mock
7
7
from nose .tools import eq_ , ok_ , assert_raises
8
8
import http_ece
9
- import pyelliptic
9
+ from cryptography .hazmat .primitives .asymmetric import ec
10
+ from cryptography .hazmat .backends import default_backend
10
11
11
12
from pywebpush import WebPusher , WebPushException , CaseInsensitiveDict , webpush
12
13
@@ -21,17 +22,24 @@ class WebpushTestCase(unittest.TestCase):
21
22
"M5xqEwuPM7VuQcyiLDhvovthPIXx+gsQRQ=="
22
23
)
23
24
24
- def _gen_subscription_info (self , recv_key ,
25
+ def _gen_subscription_info (self ,
26
+ recv_key = None ,
25
27
endpoint = "https://example.com/" ):
28
+ if not recv_key :
29
+ recv_key = ec .generate_private_key (ec .SECP256R1 , default_backend ())
26
30
return {
27
31
"endpoint" : endpoint ,
28
32
"keys" : {
29
33
'auth' : base64 .urlsafe_b64encode (os .urandom (16 )).strip (b'=' ),
30
- 'p256dh' : base64 .urlsafe_b64encode (
31
- recv_key .get_pubkey ()).strip (b'=' ),
34
+ 'p256dh' : self ._get_pubkey_str (recv_key ),
32
35
}
33
36
}
34
37
38
+ def _get_pubkey_str (self , priv_key ):
39
+ return base64 .urlsafe_b64encode (
40
+ priv_key .public_key ().public_numbers ().encode_point ()
41
+ ).strip (b'=' )
42
+
35
43
def test_init (self ):
36
44
# use static values so we know what to look for in the reply
37
45
subscription_info = {
@@ -72,14 +80,17 @@ def test_init(self):
72
80
73
81
def test_encode (self ):
74
82
for content_encoding in ["aesgcm" , "aes128gcm" ]:
75
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
83
+ recv_key = ec .generate_private_key (
84
+ ec .SECP256R1 , default_backend ())
76
85
subscription_info = self ._gen_subscription_info (recv_key )
77
86
data = "Mary had a little lamb, with some nice mint jelly"
78
87
push = WebPusher (subscription_info )
79
88
encoded = push .encode (data , content_encoding = content_encoding )
80
- keyid = base64 .urlsafe_b64encode (recv_key .get_pubkey ()[1 :])
81
- http_ece .keys [keyid ] = recv_key
82
- http_ece .labels [keyid ] = 'P-256'
89
+ """
90
+ crypto_key = base64.urlsafe_b64encode(
91
+ self._get_pubkey_str(recv_key)
92
+ ).strip(b'=')
93
+ """
83
94
# Convert these b64 strings into their raw, binary form.
84
95
raw_salt = None
85
96
if 'salt' in encoded :
@@ -94,15 +105,14 @@ def test_encode(self):
94
105
encoded ['body' ],
95
106
salt = raw_salt ,
96
107
dh = raw_dh ,
97
- keyid = keyid ,
98
- authSecret = raw_auth ,
108
+ private_key = recv_key ,
109
+ auth_secret = raw_auth ,
99
110
version = content_encoding
100
111
)
101
112
eq_ (decoded .decode ('utf8' ), data )
102
113
103
114
def test_bad_content_encoding (self ):
104
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
105
- subscription_info = self ._gen_subscription_info (recv_key )
115
+ subscription_info = self ._gen_subscription_info ()
106
116
data = "Mary had a little lamb, with some nice mint jelly"
107
117
push = WebPusher (subscription_info )
108
118
self .assertRaises (WebPushException ,
@@ -112,8 +122,7 @@ def test_bad_content_encoding(self):
112
122
113
123
@patch ("requests.post" )
114
124
def test_send (self , mock_post ):
115
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
116
- subscription_info = self ._gen_subscription_info (recv_key )
125
+ subscription_info = self ._gen_subscription_info ()
117
126
headers = {"Crypto-Key" : "pre-existing" ,
118
127
"Authentication" : "bearer vapid" }
119
128
data = "Mary had a little lamb"
@@ -131,9 +140,7 @@ def test_send(self, mock_post):
131
140
def test_send_vapid (self , mock_post ):
132
141
mock_post .return_value = Mock ()
133
142
mock_post .return_value .status_code = 200
134
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
135
-
136
- subscription_info = self ._gen_subscription_info (recv_key )
143
+ subscription_info = self ._gen_subscription_info ()
137
144
data = "Mary had a little lamb"
138
145
webpush (
139
146
subscription_info = subscription_info ,
@@ -165,43 +172,40 @@ def repad(str):
165
172
def test_send_bad_vapid_no_key (self , mock_post ):
166
173
mock_post .return_value = Mock ()
167
174
mock_post .return_value .status_code = 200
168
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
169
175
170
- subscription_info = self ._gen_subscription_info (recv_key )
176
+ subscription_info = self ._gen_subscription_info ()
171
177
data = "Mary had a little lamb"
172
178
assert_raises (WebPushException ,
173
179
webpush ,
174
180
subscription_info = subscription_info ,
175
181
data = data ,
176
182
vapid_claims = {
177
- "aud" : "https://example.com" ,
178
-
179
- }
183
+ "aud" : "https://example.com" ,
184
+
185
+ }
180
186
)
181
187
182
188
@patch ("requests.post" )
183
189
def test_send_bad_vapid_bad_return (self , mock_post ):
184
190
mock_post .return_value = Mock ()
185
191
mock_post .return_value .status_code = 410
186
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
187
192
188
- subscription_info = self ._gen_subscription_info (recv_key )
193
+ subscription_info = self ._gen_subscription_info ()
189
194
data = "Mary had a little lamb"
190
195
assert_raises (WebPushException ,
191
196
webpush ,
192
197
subscription_info = subscription_info ,
193
198
data = data ,
194
199
vapid_claims = {
195
- "aud" : "https://example.com" ,
196
-
197
- },
200
+ "aud" : "https://example.com" ,
201
+
202
+ },
198
203
vapid_private_key = self .vapid_key
199
204
)
200
205
201
206
@patch ("requests.post" )
202
207
def test_send_empty (self , mock_post ):
203
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
204
- subscription_info = self ._gen_subscription_info (recv_key )
208
+ subscription_info = self ._gen_subscription_info ()
205
209
headers = {"Crypto-Key" : "pre-existing" ,
206
210
"Authentication" : "bearer vapid" }
207
211
WebPusher (subscription_info ).send ('' , headers )
@@ -214,16 +218,14 @@ def test_send_empty(self, mock_post):
214
218
ok_ ('pre-existing' in ckey )
215
219
216
220
def test_encode_empty (self ):
217
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
218
- subscription_info = self ._gen_subscription_info (recv_key )
221
+ subscription_info = self ._gen_subscription_info ()
219
222
headers = {"Crypto-Key" : "pre-existing" ,
220
223
"Authentication" : "bearer vapid" }
221
224
encoded = WebPusher (subscription_info ).encode ('' , headers )
222
225
eq_ (encoded , None )
223
226
224
227
def test_encode_no_crypto (self ):
225
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
226
- subscription_info = self ._gen_subscription_info (recv_key )
228
+ subscription_info = self ._gen_subscription_info ()
227
229
del (subscription_info ['keys' ])
228
230
headers = {"Crypto-Key" : "pre-existing" ,
229
231
"Authentication" : "bearer vapid" }
@@ -236,8 +238,7 @@ def test_encode_no_crypto(self):
236
238
237
239
@patch ("requests.post" )
238
240
def test_send_no_headers (self , mock_post ):
239
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
240
- subscription_info = self ._gen_subscription_info (recv_key )
241
+ subscription_info = self ._gen_subscription_info ()
241
242
data = "Mary had a little lamb"
242
243
WebPusher (subscription_info ).send (data )
243
244
eq_ (subscription_info .get ('endpoint' ), mock_post .call_args [0 ][0 ])
@@ -248,15 +249,14 @@ def test_send_no_headers(self, mock_post):
248
249
249
250
@patch ("pywebpush.open" )
250
251
def test_as_curl (self , opener ):
251
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
252
- subscription_info = self ._gen_subscription_info (recv_key )
252
+ subscription_info = self ._gen_subscription_info ()
253
253
result = webpush (
254
254
subscription_info ,
255
255
data = "Mary had a little lamb" ,
256
256
vapid_claims = {
257
- "aud" : "https://example.com" ,
258
-
259
- },
257
+ "aud" : "https://example.com" ,
258
+
259
+ },
260
260
vapid_private_key = self .vapid_key ,
261
261
curl = True
262
262
)
@@ -281,9 +281,8 @@ def test_ci_dict(self):
281
281
282
282
@patch ("requests.post" )
283
283
def test_gcm (self , mock_post ):
284
- recv_key = pyelliptic .ECC (curve = "prime256v1" )
285
284
subscription_info = self ._gen_subscription_info (
286
- recv_key ,
285
+ None ,
287
286
endpoint = "https://android.googleapis.com/gcm/send/regid123" )
288
287
headers = {"Crypto-Key" : "pre-existing" ,
289
288
"Authentication" : "bearer vapid" }
0 commit comments