44from test_utils import *
55from uid2_client import *
66from uid2_client .euid_client_factory import EuidClientFactory
7+ from uid2_client .refresh_response import RefreshResponse
78from uid2_client .uid2_client_factory import Uid2ClientFactory
89
910
@@ -19,12 +20,19 @@ def setUp(self):
1920 99999 , 86400 )
2021
2122 def test_refresh (self , mock_refresh_keys_util ):
22- mock_refresh_keys_util .return_value = self ._key_collection
23+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
2324 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
2425 client .refresh_keys ()
2526 mock_refresh_keys_util .assert_called_once_with (self ._CONST_BASE_URL , self ._CONST_API_KEY , base64 .b64decode (client_secret ))
2627 self .assertEqual (client ._keys , self ._key_collection )
2728
29+ def test_refresh_fail (self , mock_refresh_keys_util ):
30+ mock_refresh_keys_util .return_value = RefreshResponse .make_error ('Exception msg' )
31+ client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
32+ with self .assertRaises (Exception ) as context :
33+ client .refresh_keys ()
34+ self .assertEqual (str (context .exception ), 'Exception msg' )
35+
2836 @patch ('uid2_client.client.parse_keys_json' )
2937 def test_refresh_json (self , mock_refresh_keys , mock_parse_keys ):
3038 mock_parse_keys .return_value = self ._key_collection
@@ -33,7 +41,7 @@ def test_refresh_json(self, mock_refresh_keys, mock_parse_keys):
3341 self .assertIsNotNone (keys )
3442
3543 def test_encrypt_decrypt (self , mock_refresh_keys_util ):
36- mock_refresh_keys_util .return_value = self ._key_collection
44+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
3745 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
3846 client .refresh_keys ()
3947
@@ -45,7 +53,7 @@ def test_encrypt_decrypt(self, mock_refresh_keys_util):
4553 self .assertEqual (example_uid , result .uid )
4654
4755 def test_can_decrypt_another_clients_encrypted_token (self , mock_refresh_keys_util ):
48- mock_refresh_keys_util .return_value = self ._key_collection
56+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
4957 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
5058 client .refresh_keys ()
5159
@@ -58,7 +66,7 @@ def test_can_decrypt_another_clients_encrypted_token(self, mock_refresh_keys_uti
5866 self .assertEqual (example_uid , result .uid )
5967
6068 def test_sharing_token_is_v4 (self , mock_refresh_keys_util ):
61- mock_refresh_keys_util .return_value = self ._key_collection
69+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
6270 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
6371 client .refresh_keys ()
6472
@@ -67,23 +75,23 @@ def test_sharing_token_is_v4(self, mock_refresh_keys_util):
6775 self .assertFalse (contains_base_64_special_chars )
6876
6977 def test_uid2_client_produces_uid2_token (self , mock_refresh_keys_util ):
70- mock_refresh_keys_util .return_value = self ._key_collection
78+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
7179 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
7280 client .refresh_keys ()
7381
7482 ad_token = client .encrypt (example_uid )
7583 self .assertEqual ("A" , ad_token [0 ])
7684
7785 def test_euid_client_produces_euid_token (self , mock_refresh_keys_util ):
78- mock_refresh_keys_util .return_value = self ._key_collection
86+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
7987 client = EuidClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
8088 client .refresh_keys ()
8189
8290 ad_token = client .encrypt (example_uid )
8391 self .assertEqual ("E" , ad_token [0 ])
8492
8593 def test_raw_uid_produces_correct_identity_type_in_token (self , mock_refresh_keys_util ):
86- mock_refresh_keys_util .return_value = self ._key_collection
94+ mock_refresh_keys_util .return_value = RefreshResponse . make_success ( self ._key_collection )
8795 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
8896 client .refresh_keys ()
8997
@@ -102,7 +110,8 @@ def test_raw_uid_produces_correct_identity_type_in_token(self, mock_refresh_keys
102110
103111 def test_multiple_keys_per_keyset (self , mock_refresh_keys_util ):
104112 def get_post_refresh_keys_response_with_multiple_keys ():
105- return create_default_key_collection ([master_key , site_key , master_key2 , site_key2 ])
113+ return RefreshResponse .make_success (
114+ create_default_key_collection ([master_key , site_key , master_key2 , site_key2 ]))
106115
107116 mock_refresh_keys_util .return_value = get_post_refresh_keys_response_with_multiple_keys ()
108117 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
@@ -115,7 +124,7 @@ def get_post_refresh_keys_response_with_multiple_keys():
115124
116125 def test_cannot_encrypt_if_no_key_from_default_keyset (self , mock_refresh_keys_util ):
117126 def get_post_refresh_keys_response_with_no_default_keyset_key ():
118- return create_default_key_collection ([master_key ])
127+ return RefreshResponse . make_success ( create_default_key_collection ([master_key ]) )
119128
120129 mock_refresh_keys_util .return_value = get_post_refresh_keys_response_with_no_default_keyset_key ()
121130 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
@@ -128,8 +137,8 @@ def get_post_refresh_keys_response_with_no_default_keyset_key():
128137 def test_cannot_encrypt_if_theres_no_default_keyset_header (self , mock_refresh_keys_util ):
129138 def get_post_refresh_keys_response_with_no_default_keyset_header ():
130139 key_set = [master_key , site_key ]
131- return EncryptionKeysCollection (key_set , IdentityScope .UID2 , 9000 , 1 ,
132- "" , 86400 )
140+ return RefreshResponse . make_success ( EncryptionKeysCollection (key_set , IdentityScope .UID2 , 9000 , 1 ,
141+ "" , 86400 ))
133142
134143 mock_refresh_keys_util .return_value = get_post_refresh_keys_response_with_no_default_keyset_header ()
135144 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
@@ -141,7 +150,7 @@ def get_post_refresh_keys_response_with_no_default_keyset_header():
141150
142151 def test_expiry_in_token_matches_expiry_in_response (self , mock_refresh_keys_util ):
143152 def get_post_refresh_keys_response_with_token_expiry ():
144- return create_default_key_collection ([master_key , site_key ])
153+ return RefreshResponse . make_success ( create_default_key_collection ([master_key , site_key ]) )
145154
146155 mock_refresh_keys_util .return_value = get_post_refresh_keys_response_with_token_expiry ()
147156 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
@@ -168,7 +177,7 @@ def test_encrypt_key_inactive(self, mock_refresh_keys_util):
168177 def get_post_refresh_keys_response_with_key_inactive ():
169178 inactive_key = EncryptionKey (245 , site_id , now , now + dt .timedelta (days = 1 ), now + dt .timedelta (days = 2 ),
170179 site_secret , keyset_id = 99999 )
171- return create_default_key_collection ([inactive_key ])
180+ return RefreshResponse . make_success ( create_default_key_collection ([inactive_key ]) )
172181
173182 mock_refresh_keys_util .return_value = get_post_refresh_keys_response_with_key_inactive ()
174183 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
@@ -182,7 +191,7 @@ def test_encrypt_key_expired(self, mock_refresh_keys_util):
182191 def get_post_refresh_keys_response_with_key_expired ():
183192 expired_key = EncryptionKey (245 , site_id , now , now , now - dt .timedelta (days = 1 ), site_secret ,
184193 keyset_id = 99999 )
185- return create_default_key_collection ([expired_key ])
194+ return RefreshResponse . make_success ( create_default_key_collection ([expired_key ]) )
186195
187196 mock_refresh_keys_util .return_value = get_post_refresh_keys_response_with_key_expired ()
188197 client = Uid2ClientFactory .create (self ._CONST_BASE_URL , self ._CONST_API_KEY , client_secret )
0 commit comments