33import os
44import unittest
55
6- from mock import patch , Mock
6+ from mock import patch
77from nose .tools import eq_ , ok_ , assert_raises
88import http_ece
99from cryptography .hazmat .primitives .asymmetric import ec
1010from cryptography .hazmat .backends import default_backend
11+ import py_vapid
1112
1213from pywebpush import WebPusher , WebPushException , CaseInsensitiveDict , webpush
1314
@@ -138,7 +139,6 @@ def test_send(self, mock_post):
138139
139140 @patch ("requests.post" )
140141 def test_send_vapid (self , mock_post ):
141- mock_post .return_value = Mock ()
142142 mock_post .return_value .status_code = 200
143143 subscription_info = self ._gen_subscription_info ()
144144 data = "Mary had a little lamb"
@@ -168,9 +168,25 @@ def repad(str):
168168 ok_ ('dh=' in ckey )
169169 eq_ (pheaders .get ('content-encoding' ), 'aesgcm' )
170170
171+ @patch .object (WebPusher , "send" )
172+ @patch .object (py_vapid .Vapid , "sign" )
173+ def test_webpush_vapid_instance (self , vapid_sign , pusher_send ):
174+ pusher_send .return_value .status_code = 200
175+ subscription_info = self ._gen_subscription_info ()
176+ data = "Mary had a little lamb"
177+ vapid_key = py_vapid .Vapid .from_string (self .vapid_key )
178+ claims = dict (
sub = "mailto:[email protected] " ,
aud = "https://example.com" )
179+ webpush (
180+ subscription_info = subscription_info ,
181+ data = data ,
182+ vapid_private_key = vapid_key ,
183+ vapid_claims = claims ,
184+ )
185+ vapid_sign .assert_called_once_with (claims )
186+ pusher_send .assert_called_once ()
187+
171188 @patch ("requests.post" )
172189 def test_send_bad_vapid_no_key (self , mock_post ):
173- mock_post .return_value = Mock ()
174190 mock_post .return_value .status_code = 200
175191
176192 subscription_info = self ._gen_subscription_info ()
@@ -187,7 +203,6 @@ def test_send_bad_vapid_no_key(self, mock_post):
187203
188204 @patch ("requests.post" )
189205 def test_send_bad_vapid_bad_return (self , mock_post ):
190- mock_post .return_value = Mock ()
191206 mock_post .return_value .status_code = 410
192207
193208 subscription_info = self ._gen_subscription_info ()
@@ -297,7 +312,6 @@ def test_gcm(self, mock_post):
297312
298313 @patch ("requests.post" )
299314 def test_timeout (self , mock_post ):
300- mock_post .return_value = Mock ()
301315 mock_post .return_value .status_code = 200
302316 subscription_info = self ._gen_subscription_info ()
303317 WebPusher (subscription_info ).send (timeout = 5.2 )
0 commit comments