55import json
66import unittest
77from cryptography .hazmat .primitives import serialization
8- from nose .tools import eq_ , ok_
98from mock import patch , Mock
109
1110from py_vapid import Vapid01 , Vapid02 , VapidException
4948 ).strip ('=' ).encode ('utf8' )
5049
5150
52- def setUp (self ):
51+ def setup_module (self ):
5352 with open ('/tmp/private' , 'w' ) as ff :
5453 ff .write (TEST_KEY_PRIVATE_PEM )
5554 with open ('/tmp/public' , 'w' ) as ff :
@@ -58,16 +57,16 @@ def setUp(self):
5857 ff .write (TEST_KEY_PRIVATE_DER )
5958
6059
61- def tearDown (self ):
60+ def teardown_module (self ):
6261 os .unlink ('/tmp/private' )
6362 os .unlink ('/tmp/public' )
6463
6564
6665class VapidTestCase (unittest .TestCase ):
6766 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' )
7170
7271 def test_init (self ):
7372 v1 = Vapid01 .from_file ("/tmp/private" )
@@ -80,7 +79,7 @@ def test_init(self):
8079 self .check_keys (v4 )
8180 no_exist = '/tmp/not_exist'
8281 Vapid01 .from_file (no_exist )
83- ok_ ( os .path .isfile (no_exist ) )
82+ assert os .path .isfile (no_exist )
8483 os .unlink (no_exist )
8584
8685 def repad (self , data ):
@@ -95,8 +94,8 @@ def test_init_bad_read(self, mm):
9594 def test_gen_key (self ):
9695 v = Vapid01 ()
9796 v .generate_keys ()
98- ok_ ( v .public_key )
99- ok_ ( v .private_key )
97+ assert v .public_key
98+ assert v .private_key
10099
101100 def test_private_key (self ):
102101 v = Vapid01 ()
@@ -105,8 +104,8 @@ def test_private_key(self):
105104
106105 def test_public_key (self ):
107106 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
110109
111110 def test_save_key (self ):
112111 v = Vapid01 ()
@@ -135,7 +134,7 @@ def test_sign_01(self):
135134 claims = {"aud" : "https://example.com" ,
136135 "sub" :
"mailto:[email protected] " }
137136 result = v .sign (claims , "id=previous" )
138- eq_ ( result ['Crypto-Key' ],
137+ assert result ['Crypto-Key' ] == (
139138 'id=previous;p256ecdsa=' + TEST_KEY_PUBLIC_RAW .decode ('utf8' ))
140139 pkey = binascii .b2a_base64 (
141140 v .public_key .public_bytes (
@@ -145,16 +144,16 @@ def test_sign_01(self):
145144 ).decode ('utf8' ).replace ('+' , '-' ).replace ('/' , '_' ).strip ()
146145 items = decode (result ['Authorization' ].split (' ' )[1 ], pkey )
147146 for k in claims :
148- eq_ ( items [k ], claims [k ])
147+ assert items [k ] == claims [k ]
149148 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' ))
152151 # Verify using the same function as Integration
153152 # this should ensure that the r,s sign values are correctly formed
154- ok_ ( Vapid01 .verify (
153+ assert Vapid01 .verify (
155154 key = result ['Crypto-Key' ].split ('=' )[1 ],
156155 auth = result ['Authorization' ]
157- ))
156+ )
158157
159158 def test_sign_02 (self ):
160159 v = Vapid02 .from_file ("/tmp/private" )
@@ -164,20 +163,20 @@ def test_sign_02(self):
164163 claim_check = copy .deepcopy (claims )
165164 result = v .sign (claims , "id=previous" )
166165 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
170169 parts = auth [6 :].split (',' )
171- eq_ ( len (parts ), 2 )
170+ assert len (parts ) == 2
172171 t_val = json .loads (base64 .urlsafe_b64decode (
173172 self .repad (parts [0 ][2 :].split ('.' )[1 ])
174173 ).decode ('utf8' ))
175174 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
179178 for k in claims :
180- eq_ ( t_val [k ], claims [k ])
179+ assert t_val [k ] == claims [k ]
181180
182181 def test_sign_02_localhost (self ):
183182 v = Vapid02 .from_file ("/tmp/private" )
@@ -186,9 +185,9 @@ def test_sign_02_localhost(self):
186185 "foo" : "extra value" }
187186 result = v .sign (claims , "id=previous" )
188187 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
192191
193192 def test_integration (self ):
194193 # These values were taken from a test page. DO NOT ALTER!
@@ -199,8 +198,8 @@ def test_integration(self):
199198 "4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
200199 "W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
201200 "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 ))
204203
205204 def test_bad_integration (self ):
206205 # These values were taken from a test page. DO NOT ALTER!
@@ -211,7 +210,7 @@ def test_bad_integration(self):
211210 "4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
212211 "W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
213212 "4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZ_BAD" )
214- eq_ ( Vapid01 .verify (key = key , auth = auth ), False )
213+ assert Vapid01 .verify (key = key , auth = auth ) == False
215214
216215 def test_bad_sign (self ):
217216 v = Vapid01 .from_file ("/tmp/private" )
0 commit comments