11import json
22import unittest
3- from unittest .mock import patch
3+
4+ import responses
45
56from uid2_client import refresh_keys_util
67from test_utils import *
78from uid2_client .encryption import _encrypt_gcm , _decrypt_gcm
89
910
1011class TestRefreshKeysUtil (unittest .TestCase ):
11- class MockPostResponse :
12- def __init__ (self , return_value ):
13- self .return_value = return_value
14-
15- def read (self ):
16- return base64 .b64encode (self .return_value )
17-
1812 def _make_post_response (self , request_data , response_payload ):
1913 d = base64 .b64decode (request_data )[1 :]
2014 d = _decrypt_gcm (d , client_secret_bytes )
@@ -25,11 +19,11 @@ def _make_post_response(self, request_data, response_payload):
2519 payload += response_payload
2620 envelope = _encrypt_gcm (payload , None , client_secret_bytes )
2721
28- return self . MockPostResponse (envelope )
22+ return 200 , {}, base64 . b64encode (envelope )
2923
30- def _get_post_refresh_keys_response (self , base_url , path , headers , data ):
24+ def _get_post_refresh_keys_response (self , request ):
3125 response_payload = key_set_to_json_for_sharing ([master_key , site_key ]).encode ()
32- return self ._make_post_response (data , response_payload )
26+ return self ._make_post_response (request . body , response_payload )
3327
3428 def _validate_master_and_site_key (self , keys ):
3529 self .assertEqual (len (keys .values ()), 2 )
@@ -55,23 +49,29 @@ def _validate_master_and_site_key(self, keys):
5549 self .assertEqual (master_secret , master .secret )
5650 self .assertEqual (1 , master .keyset_id )
5751
58- @patch ('uid2_client.refresh_keys_util.post' )
59- def test_refresh_sharing_keys (self , mock_post ):
60- mock_post .side_effect = self ._get_post_refresh_keys_response
61- refresh_response = refresh_keys_util .refresh_sharing_keys ("base_url" , "auth_key" , base64 .b64decode (client_secret ))
52+ @responses .activate
53+ def test_refresh_sharing_keys (self ):
54+ responses .add_callback (
55+ responses .POST ,
56+ "https://base_url/v2/key/sharing" ,
57+ callback = self ._get_post_refresh_keys_response ,
58+ )
59+
60+ refresh_response = refresh_keys_util .refresh_sharing_keys ("https://base_url" , "auth_key" , base64 .b64decode (client_secret ))
6261 self .assertTrue (refresh_response .success )
6362 self ._validate_master_and_site_key (refresh_response .keys )
64- mock_post .assert_called_once ()
65- self .assertEqual (mock_post .call_args [0 ], ('base_url' , '/v2/key/sharing' ))
6663
67- @patch ('uid2_client.refresh_keys_util.post' )
68- def test_refresh_bidstream_keys (self , mock_post ):
69- mock_post .side_effect = self ._get_post_refresh_keys_response
70- refresh_response = refresh_keys_util .refresh_bidstream_keys ("base_url" , "auth_key" , base64 .b64decode (client_secret ))
64+ @responses .activate
65+ def test_refresh_bidstream_keys (self ):
66+ responses .add_callback (
67+ responses .POST ,
68+ "https://base_url/v2/key/bidstream" ,
69+ callback = self ._get_post_refresh_keys_response ,
70+ )
71+
72+ refresh_response = refresh_keys_util .refresh_bidstream_keys ("https://base_url" , "auth_key" , base64 .b64decode (client_secret ))
7173 self .assertTrue (refresh_response .success )
7274 self ._validate_master_and_site_key (refresh_response .keys )
73- mock_post .assert_called_once ()
74- self .assertEqual (mock_post .call_args [0 ], ('base_url' , '/v2/key/bidstream' ))
7575
7676 def test_parse_keys_json_identity (self ):
7777 response_body_str = key_set_to_json_for_sharing ([master_key , site_key ])
0 commit comments