11import json
22import unittest
3-
4- import responses
3+ from unittest .mock import patch
54
65from uid2_client import refresh_keys_util
76from test_utils import *
87from uid2_client .encryption import _encrypt_gcm , _decrypt_gcm
98
109
1110class 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+
1218 def _make_post_response (self , request_data , response_payload ):
1319 d = base64 .b64decode (request_data )[1 :]
1420 d = _decrypt_gcm (d , client_secret_bytes )
@@ -19,11 +25,11 @@ def _make_post_response(self, request_data, response_payload):
1925 payload += response_payload
2026 envelope = _encrypt_gcm (payload , None , client_secret_bytes )
2127
22- return 200 , {}, base64 . b64encode (envelope )
28+ return self . MockPostResponse (envelope )
2329
24- def _get_post_refresh_keys_response (self , request ):
30+ def _get_post_refresh_keys_response (self , base_url , path , headers , data ):
2531 response_payload = key_set_to_json_for_sharing ([master_key , site_key ]).encode ()
26- return self ._make_post_response (request . body , response_payload )
32+ return self ._make_post_response (data , response_payload )
2733
2834 def _validate_master_and_site_key (self , keys ):
2935 self .assertEqual (len (keys .values ()), 2 )
@@ -49,29 +55,23 @@ def _validate_master_and_site_key(self, keys):
4955 self .assertEqual (master_secret , master .secret )
5056 self .assertEqual (1 , master .keyset_id )
5157
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 ))
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 ))
6162 self .assertTrue (refresh_response .success )
6263 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' ))
6366
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 ))
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 ))
7371 self .assertTrue (refresh_response .success )
7472 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