1
1
import json
2
2
import unittest
3
-
4
- import responses
3
+ from unittest .mock import patch
5
4
6
5
from uid2_client import refresh_keys_util
7
6
from test_utils import *
8
7
from uid2_client .encryption import _encrypt_gcm , _decrypt_gcm
9
8
10
9
11
10
class 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
+
12
18
def _make_post_response (self , request_data , response_payload ):
13
19
d = base64 .b64decode (request_data )[1 :]
14
20
d = _decrypt_gcm (d , client_secret_bytes )
@@ -19,11 +25,11 @@ def _make_post_response(self, request_data, response_payload):
19
25
payload += response_payload
20
26
envelope = _encrypt_gcm (payload , None , client_secret_bytes )
21
27
22
- return 200 , {}, base64 . b64encode (envelope )
28
+ return self . MockPostResponse (envelope )
23
29
24
- def _get_post_refresh_keys_response (self , request ):
30
+ def _get_post_refresh_keys_response (self , base_url , path , headers , data ):
25
31
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 )
27
33
28
34
def _validate_master_and_site_key (self , keys ):
29
35
self .assertEqual (len (keys .values ()), 2 )
@@ -49,29 +55,23 @@ def _validate_master_and_site_key(self, keys):
49
55
self .assertEqual (master_secret , master .secret )
50
56
self .assertEqual (1 , master .keyset_id )
51
57
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 ))
61
62
self .assertTrue (refresh_response .success )
62
63
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' ))
63
66
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 ))
73
71
self .assertTrue (refresh_response .success )
74
72
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' ))
75
75
76
76
def test_parse_keys_json_identity (self ):
77
77
response_body_str = key_set_to_json_for_sharing ([master_key , site_key ])
0 commit comments