5
5
import json
6
6
import unittest
7
7
from cryptography .hazmat .primitives import serialization
8
- from nose .tools import eq_ , ok_
9
8
from mock import patch , Mock
10
9
11
10
from py_vapid import Vapid01 , Vapid02 , VapidException
49
48
).strip ('=' ).encode ('utf8' )
50
49
51
50
52
- def setUp (self ):
51
+ def setup_module (self ):
53
52
with open ('/tmp/private' , 'w' ) as ff :
54
53
ff .write (TEST_KEY_PRIVATE_PEM )
55
54
with open ('/tmp/public' , 'w' ) as ff :
@@ -58,16 +57,16 @@ def setUp(self):
58
57
ff .write (TEST_KEY_PRIVATE_DER )
59
58
60
59
61
- def tearDown (self ):
60
+ def teardown_module (self ):
62
61
os .unlink ('/tmp/private' )
63
62
os .unlink ('/tmp/public' )
64
63
65
64
66
65
class VapidTestCase (unittest .TestCase ):
67
66
def check_keys (self , v ):
68
- eq_ ( v .private_key .private_numbers ().private_value , key .get ('d' ) )
69
- eq_ ( v .public_key .public_numbers ().x , key .get ('x' ) )
70
- eq_ ( v .public_key .public_numbers ().y , key .get ('y' ) )
67
+ assert v .private_key .private_numbers ().private_value == key .get ('d' )
68
+ assert v .public_key .public_numbers ().x == key .get ('x' )
69
+ assert v .public_key .public_numbers ().y == key .get ('y' )
71
70
72
71
def test_init (self ):
73
72
v1 = Vapid01 .from_file ("/tmp/private" )
@@ -80,7 +79,7 @@ def test_init(self):
80
79
self .check_keys (v4 )
81
80
no_exist = '/tmp/not_exist'
82
81
Vapid01 .from_file (no_exist )
83
- ok_ ( os .path .isfile (no_exist ) )
82
+ assert os .path .isfile (no_exist )
84
83
os .unlink (no_exist )
85
84
86
85
def repad (self , data ):
@@ -95,8 +94,8 @@ def test_init_bad_read(self, mm):
95
94
def test_gen_key (self ):
96
95
v = Vapid01 ()
97
96
v .generate_keys ()
98
- ok_ ( v .public_key )
99
- ok_ ( v .private_key )
97
+ assert v .public_key
98
+ assert v .private_key
100
99
101
100
def test_private_key (self ):
102
101
v = Vapid01 ()
@@ -105,8 +104,8 @@ def test_private_key(self):
105
104
106
105
def test_public_key (self ):
107
106
v = Vapid01 ()
108
- eq_ ( v ._private_key , None )
109
- eq_ ( v ._public_key , None )
107
+ assert v ._private_key is None
108
+ assert v ._public_key is None
110
109
111
110
def test_save_key (self ):
112
111
v = Vapid01 ()
@@ -135,7 +134,7 @@ def test_sign_01(self):
135
134
claims = {"aud" : "https://example.com" ,
136
135
"sub" :
"mailto:[email protected] " }
137
136
result = v .sign (claims , "id=previous" )
138
- eq_ ( result ['Crypto-Key' ],
137
+ assert result ['Crypto-Key' ] == (
139
138
'id=previous;p256ecdsa=' + TEST_KEY_PUBLIC_RAW .decode ('utf8' ))
140
139
pkey = binascii .b2a_base64 (
141
140
v .public_key .public_bytes (
@@ -145,16 +144,16 @@ def test_sign_01(self):
145
144
).decode ('utf8' ).replace ('+' , '-' ).replace ('/' , '_' ).strip ()
146
145
items = decode (result ['Authorization' ].split (' ' )[1 ], pkey )
147
146
for k in claims :
148
- eq_ ( items [k ], claims [k ])
147
+ assert items [k ] == claims [k ]
149
148
result = v .sign (claims )
150
- eq_ ( result ['Crypto-Key' ],
151
- 'p256ecdsa=' + TEST_KEY_PUBLIC_RAW .decode ('utf8' ))
149
+ assert result ['Crypto-Key' ] == ( 'p256ecdsa=' +
150
+ TEST_KEY_PUBLIC_RAW .decode ('utf8' ))
152
151
# Verify using the same function as Integration
153
152
# this should ensure that the r,s sign values are correctly formed
154
- ok_ ( Vapid01 .verify (
153
+ assert Vapid01 .verify (
155
154
key = result ['Crypto-Key' ].split ('=' )[1 ],
156
155
auth = result ['Authorization' ]
157
- ))
156
+ )
158
157
159
158
def test_sign_02 (self ):
160
159
v = Vapid02 .from_file ("/tmp/private" )
@@ -164,20 +163,20 @@ def test_sign_02(self):
164
163
claim_check = copy .deepcopy (claims )
165
164
result = v .sign (claims , "id=previous" )
166
165
auth = result ['Authorization' ]
167
- eq_ ( auth [:6 ], 'vapid ' )
168
- ok_ ( ' t=' in auth )
169
- ok_ ( ',k=' in auth )
166
+ assert auth [:6 ] == 'vapid '
167
+ assert ' t=' in auth
168
+ assert ',k=' in auth
170
169
parts = auth [6 :].split (',' )
171
- eq_ ( len (parts ), 2 )
170
+ assert len (parts ) == 2
172
171
t_val = json .loads (base64 .urlsafe_b64decode (
173
172
self .repad (parts [0 ][2 :].split ('.' )[1 ])
174
173
).decode ('utf8' ))
175
174
k_val = binascii .a2b_base64 (self .repad (parts [1 ][2 :]))
176
- eq_ ( binascii .hexlify (k_val )[:2 ], b'04' )
177
- eq_ ( len (k_val ), 65 )
178
- eq_ ( claims , claim_check )
175
+ assert binascii .hexlify (k_val )[:2 ] == b'04'
176
+ assert len (k_val ) == 65
177
+ assert claims == claim_check
179
178
for k in claims :
180
- eq_ ( t_val [k ], claims [k ])
179
+ assert t_val [k ] == claims [k ]
181
180
182
181
def test_sign_02_localhost (self ):
183
182
v = Vapid02 .from_file ("/tmp/private" )
@@ -186,9 +185,9 @@ def test_sign_02_localhost(self):
186
185
"foo" : "extra value" }
187
186
result = v .sign (claims , "id=previous" )
188
187
auth = result ['Authorization' ]
189
- eq_ ( auth [:6 ], 'vapid ' )
190
- ok_ ( ' t=' in auth )
191
- ok_ ( ',k=' in auth )
188
+ assert auth [:6 ] == 'vapid '
189
+ assert ' t=' in auth
190
+ assert ',k=' in auth
192
191
193
192
def test_integration (self ):
194
193
# These values were taken from a test page. DO NOT ALTER!
@@ -199,8 +198,8 @@ def test_integration(self):
199
198
"4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
200
199
"W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
201
200
"4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZolQA" )
202
- ok_ ( Vapid01 .verify (key = key , auth = "webpush {}" .format (auth ) ))
203
- ok_ ( Vapid02 .verify (auth = "vapid t={},k={}" .format (auth , key ) ))
201
+ assert Vapid01 .verify (key = key , auth = "webpush {}" .format (auth ))
202
+ assert Vapid02 .verify (auth = "vapid t={},k={}" .format (auth , key ))
204
203
205
204
def test_bad_integration (self ):
206
205
# These values were taken from a test page. DO NOT ALTER!
@@ -211,7 +210,7 @@ def test_bad_integration(self):
211
210
"4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
212
211
"W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
213
212
"4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZ_BAD" )
214
- eq_ ( Vapid01 .verify (key = key , auth = auth ), False )
213
+ assert Vapid01 .verify (key = key , auth = auth ) == False
215
214
216
215
def test_bad_sign (self ):
217
216
v = Vapid01 .from_file ("/tmp/private" )
0 commit comments