Skip to content

Commit 3d6403b

Browse files
committed
Return refresh response instead of throwing exception
1 parent 12de071 commit 3d6403b

13 files changed

+134
-86
lines changed

examples/sample_bidstream_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def _usage():
2121
ad_token = sys.argv[5]
2222

2323
client = BidstreamClient(base_url, auth_key, secret_key)
24-
client.refresh()
24+
refresh_response = client.refresh()
25+
if not refresh_response.success:
26+
print('Failed to refresh keys due to =', refresh_response.reason)
27+
sys.exit(1)
28+
2529
decrypt_result = client.decrypt_token_into_raw_uid(ad_token, domain_name)
2630

2731
print('Status =', decrypt_result.status)

examples/sample_sharing_client.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ def _usage():
2020
raw_uid = sys.argv[4]
2121

2222
client = SharingClient(base_url, auth_key, secret_key)
23-
client.refresh()
23+
refresh_response = client.refresh()
24+
if not refresh_response.success:
25+
print('Failed to refresh keys due to =', refresh_response.reason)
26+
sys.exit(1)
27+
2428
encryption_data_response = client.encrypt_raw_uid_into_token(raw_uid)
2529

2630
print('New Sharing Token =', encryption_data_response.encrypted_data)

tests/test_bidstream_client.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
from test_utils import *
55
from uid2_client import BidstreamClient, EncryptionError, Uid2Base64UrlCoder, DecryptionStatus
6+
from uid2_client.refresh_response import RefreshResponse
7+
8+
9+
def create_default_refresh_keys_success():
10+
return RefreshResponse.make_success(create_default_key_collection([master_key, site_key]))
611

712

813
@patch('uid2_client.bid_stream_client.refresh_bidstream_keys')
@@ -34,16 +39,17 @@ def test_smoke_test(self, mock_refresh_bidstream_keys): # SmokeTest
3439
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
3540
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
3641
token = generate_uid_token(expected_scope, expected_version)
37-
mock_refresh_bidstream_keys.return_value = create_key_collection(expected_scope)
42+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_success(
43+
create_key_collection(expected_scope))
3844
self._client.refresh()
3945
self.decrypt_and_assert_success(token, expected_version, expected_scope)
4046

4147
def test_phone_uids(self, mock_refresh_bidstream_keys): # PhoneTest
4248
for expected_scope, expected_version in test_cases_all_scopes_v3_v4_versions:
4349
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
44-
mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key],
50+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_success(EncryptionKeysCollection([master_key, site_key],
4551
expected_scope, site_id, 1,
46-
99999, 86400)
52+
99999, 86400))
4753
self._client.refresh()
4854
token = generate_uid_token(expected_scope, expected_version, phone_uid)
4955
self.assertEqual(IdentityType.Phone, get_identity_type(token))
@@ -60,10 +66,10 @@ def test_token_lifetime_too_long_for_bidstream(self, mock_refresh_bidstream_keys
6066
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
6167
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
6268
token = generate_uid_token(expected_scope, expected_version, expires_at=expires_in_sec)
63-
mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key],
69+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_success(EncryptionKeysCollection([master_key, site_key],
6470
expected_scope, site_id, 1,
6571
99999, 86400,
66-
max_bidstream_lifetime_seconds=max_bidstream_lifetime)
72+
max_bidstream_lifetime_seconds=max_bidstream_lifetime))
6773
self._client.refresh()
6874
result = self._client.decrypt_token_into_raw_uid(token, None)
6975
self.assertFalse(result.success)
@@ -74,9 +80,8 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh
7480
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
7581
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
7682
token = generate_uid_token(expected_scope, expected_version, created_at=created_at_future)
77-
mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key],
78-
expected_scope, site_id, 1,
79-
99999, 86400)
83+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_success(EncryptionKeysCollection(
84+
[master_key, site_key], expected_scope, site_id, 1,99999, 86400))
8085
self._client.refresh()
8186
result = self._client.decrypt_token_into_raw_uid(token, None)
8287
self.assertFalse(result.success)
@@ -87,9 +92,9 @@ def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refr
8792
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
8893
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
8994
token = generate_uid_token(expected_scope, expected_version, expires_at=created_at_future)
90-
mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key],
91-
expected_scope, site_id, 1,
92-
99999, 86400)
95+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_success(
96+
EncryptionKeysCollection([master_key, site_key], expected_scope, site_id, 1,
97+
99999, 86400))
9398
self._client.refresh()
9499
result = self._client.decrypt_token_into_raw_uid(token, None)
95100
self.assertIsNotNone(result)
@@ -98,7 +103,7 @@ def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refr
98103

99104
def test_empty_keys(self, mock_refresh_bidstream_keys): # EmptyKeyContainer
100105
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V3)
101-
mock_refresh_bidstream_keys.return_value = None
106+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_error('Exception')
102107
self._client.refresh()
103108
result = self._client.decrypt_token_into_raw_uid(token, None)
104109
self.assertFalse(result.success)
@@ -110,7 +115,7 @@ def get_post_refresh_keys_response_with_key_expired():
110115
keyset_id=99999)
111116
site_key_expired = EncryptionKey(site_key_id, site_id, created=now, activates=now - dt.timedelta(hours=2), expires=now - dt.timedelta(hours=1), secret=site_secret,
112117
keyset_id=99999)
113-
return create_default_key_collection([master_key_expired, site_key_expired])
118+
return RefreshResponse.make_success(create_default_key_collection([master_key_expired, site_key_expired]))
114119

115120
mock_refresh_bidstream_keys.return_value = get_post_refresh_keys_response_with_key_expired()
116121
self._client.refresh()
@@ -123,7 +128,7 @@ def test_not_authorized_for_master_key(self, mock_refresh_bidstream_keys): #Not
123128
def get_post_refresh_keys_response_with_key_expired():
124129
another_master_key = EncryptionKey(master_key_id + site_key_id + 1, -1, created=now, activates=now, expires=now + dt.timedelta(hours=1), secret=master_secret)
125130
another_site_key = EncryptionKey(master_key_id + site_key_id + 2, site_id, created=now, activates=now, expires=now + dt.timedelta(hours=1), secret=site_secret)
126-
return create_default_key_collection([another_master_key, another_site_key])
131+
return RefreshResponse.make_success(create_default_key_collection([another_master_key, another_site_key]))
127132

128133
mock_refresh_bidstream_keys.return_value = get_post_refresh_keys_response_with_key_expired()
129134
self._client.refresh()
@@ -134,7 +139,7 @@ def get_post_refresh_keys_response_with_key_expired():
134139
self.assertEqual(result.status, DecryptionStatus.NOT_AUTHORIZED_FOR_MASTER_KEY)
135140

136141
def test_invalid_payload(self, mock_refresh_bidstream_keys): #InvalidPayload
137-
mock_refresh_bidstream_keys.return_value = create_default_key_collection([master_key, site_key])
142+
mock_refresh_bidstream_keys.return_value = create_default_refresh_keys_success()
138143
self._client.refresh()
139144
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
140145
payload = Uid2Base64UrlCoder.decode(token)
@@ -145,7 +150,7 @@ def test_invalid_payload(self, mock_refresh_bidstream_keys): #InvalidPayload
145150
self.assertEqual(result.status, DecryptionStatus.INVALID_PAYLOAD)
146151

147152
def test_token_expiry_custom_decryption_time(self, mock_refresh_bidstream_keys): #TokenExpiryAndCustomNow
148-
mock_refresh_bidstream_keys.return_value = create_default_key_collection([master_key, site_key])
153+
mock_refresh_bidstream_keys.return_value = create_default_refresh_keys_success()
149154
self._client.refresh()
150155

151156
expires_at = now - dt.timedelta(days=60)
@@ -163,7 +168,7 @@ def test_token_expiry_custom_decryption_time(self, mock_refresh_bidstream_keys):
163168
self.assertEqual(result.advertising_token_version, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
164169

165170
def test_refresh_keys(self, mock_refresh_bidstream_keys):
166-
mock_refresh_bidstream_keys.return_value = create_default_key_collection([master_key])
171+
mock_refresh_bidstream_keys.return_value = RefreshResponse.make_success(create_default_key_collection([master_key]))
167172
self._client.refresh()
168173
mock_refresh_bidstream_keys.assert_called_once_with(self._CONST_BASE_URL, self._CONST_API_KEY,
169174
client_secret_bytes)

tests/test_client.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from test_utils import *
55
from uid2_client import *
66
from uid2_client.euid_client_factory import EuidClientFactory
7+
from uid2_client.refresh_response import RefreshResponse
78
from 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

Comments
 (0)