Skip to content

Commit c3b930f

Browse files
committed
refactor
1 parent aec489d commit c3b930f

File tree

2 files changed

+45
-75
lines changed

2 files changed

+45
-75
lines changed

tests/test_bidstream_client.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class TestBidStreamClient(unittest.TestCase):
6060
_CONST_BASE_URL = 'base_url'
6161
_CONST_API_KEY = 'api_key'
6262

63+
def refresh(self, refresh_json):
64+
refresh_response = self._client._refresh_json(refresh_json)
65+
self.assertTrue(refresh_response.success)
66+
6367
def assert_success(self, decryption_response, token_version, scope):
6468
self.assertTrue(decryption_response.success)
6569
self.assertEqual(decryption_response.uid, example_uid)
@@ -95,17 +99,13 @@ def test_smoke_test_for_bidstream(self): # SmokeTestForBidstream
9599
identity_established_at=now - dt.timedelta(days=120),
96100
generated_at=YESTERDAY,
97101
expires_at=IN_2_DAYS)
98-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
99-
expected_scope))
100-
self.assertTrue(refresh_response.success)
102+
self.refresh(key_bidstream_response_json_default_keys(expected_scope))
101103
self._decrypt_and_assert_success(token, expected_version, expected_scope)
102104

103105
def test_phone_uids(self): # PhoneTest
104106
for expected_scope, expected_version in test_cases_all_scopes_v3_v4_versions:
105107
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
106-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
107-
expected_scope))
108-
self.assertTrue(refresh_response.success)
108+
self.refresh(key_bidstream_response_json_default_keys(expected_scope))
109109
token = generate_uid_token(expected_scope, expected_version, phone_uid)
110110
self.assertEqual(IdentityType.Phone, get_identity_type(token))
111111
result = self._client.decrypt_token_into_raw_uid(token, None)
@@ -122,9 +122,7 @@ def test_token_lifetime_too_long_for_bidstream_but_remaining_lifetime_allowed(se
122122
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
123123
token = generate_uid_token(expected_scope, expected_version, generated_at=generated,
124124
expires_at=expires_in_sec)
125-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
126-
expected_scope))
127-
self.assertTrue(refresh_response.success)
125+
self.refresh(key_bidstream_response_json_default_keys(expected_scope))
128126
result = self._client.decrypt_token_into_raw_uid(token, None)
129127
if expected_version == AdvertisingTokenVersion.ADVERTISING_TOKEN_V2:
130128
self.assert_success(result, expected_version, expected_scope)
@@ -138,9 +136,7 @@ def test_token_remaining_lifetime_too_long_for_bidstream(self): # TokenRemainin
138136
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
139137
token = generate_uid_token(expected_scope, expected_version, generated_at=generated,
140138
expires_at=expires_in_sec)
141-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
142-
expected_scope))
143-
self.assertTrue(refresh_response.success)
139+
self.refresh(key_bidstream_response_json_default_keys(expected_scope))
144140
result = self._client.decrypt_token_into_raw_uid(token, None)
145141
self.assert_fails(result, expected_version, expected_scope)
146142

@@ -150,9 +146,7 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self): # TokenGen
150146
for expected_scope, expected_version in test_cases_all_scopes_v3_v4_versions:
151147
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
152148
token = generate_uid_token(expected_scope, expected_version, generated_at=created_at_future)
153-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
154-
expected_scope))
155-
self.assertTrue(refresh_response.success)
149+
self.refresh(key_bidstream_response_json_default_keys(expected_scope))
156150
result = self._client.decrypt_token_into_raw_uid(token, None)
157151
self.assert_fails(result, expected_version, expected_scope)
158152
self.assertEqual(result.status, DecryptionStatus.INVALID_TOKEN_LIFETIME)
@@ -162,17 +156,14 @@ def test_token_generated_in_the_future_within_allowed_clock_skew(self): # Token
162156
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
163157
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
164158
token = generate_uid_token(expected_scope, expected_version, expires_at=created_at_future)
165-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
166-
expected_scope))
167-
self.assertTrue(refresh_response.success)
159+
self.refresh(key_bidstream_response_json_default_keys(expected_scope))
168160
self._decrypt_and_assert_success(token, expected_version, expected_scope)
169161

170162
def test_legacy_response_from_old_operator(self):
171163
test_cases = [AdvertisingTokenVersion.ADVERTISING_TOKEN_V2,
172164
AdvertisingTokenVersion.ADVERTISING_TOKEN_V3,
173165
AdvertisingTokenVersion.ADVERTISING_TOKEN_V4]
174-
refresh_response = self._client._refresh_json(key_set_to_json_for_sharing([master_key, site_key]))
175-
self.assertTrue(refresh_response.success)
166+
self.refresh(key_set_to_json_for_sharing([master_key, site_key]))
176167
for token_version in test_cases:
177168
with self.subTest(token_version=token_version):
178169
token = generate_uid_token(IdentityScope.UID2, token_version)
@@ -212,9 +203,7 @@ def test_identity_scope_and_types(self): # IdentityScopeAndType_TestCases
212203
for uid, identity_scope, identity_type in test_cases:
213204
with self.subTest(identity_scope=identity_scope, identity_type=identity_type):
214205
token = generate_uid_token(identity_scope, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
215-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
216-
identity_scope))
217-
self.assertTrue(refresh_response.success)
206+
self.refresh(key_bidstream_response_json_default_keys(identity_scope))
218207
self._decrypt_and_assert_success(token, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4, identity_scope)
219208

220209
def test_empty_keys(self): # EmptyKeyContainer
@@ -229,9 +218,7 @@ def test_master_key_expired(self): #ExpiredKeyContainer
229218
site_key_expired = EncryptionKey(site_key_id, site_id, created=now, activates=now - dt.timedelta(hours=2),
230219
expires=now - dt.timedelta(hours=1), secret=site_secret, keyset_id=99999)
231220

232-
refresh_response = self._client._refresh_json(key_bidstream_response_json(
233-
[master_key_expired, site_key_expired]))
234-
self.assertTrue(refresh_response.success)
221+
self.refresh(key_bidstream_response_json([master_key_expired, site_key_expired]))
235222

236223
result = self._client.decrypt_token_into_raw_uid(example_uid, None)
237224
self.assertFalse(result.success)
@@ -241,18 +228,15 @@ def test_not_authorized_for_master_key(self): #NotAuthorizedForMasterKey
241228

242229
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)
243230
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)
244-
refresh_response = self._client._refresh_json(key_bidstream_response_json(
245-
[another_master_key, another_site_key]))
246-
self.assertTrue(refresh_response.success)
231+
self.refresh(key_bidstream_response_json([another_master_key, another_site_key]))
247232
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
248233

249234
result = self._client.decrypt_token_into_raw_uid(token, None)
250235
self.assertFalse(result.success)
251236
self.assertEqual(result.status, DecryptionStatus.NOT_AUTHORIZED_FOR_MASTER_KEY)
252237

253238
def test_invalid_payload(self): #InvalidPayload
254-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys())
255-
self.assertTrue(refresh_response.success)
239+
self.refresh(key_bidstream_response_json_default_keys())
256240
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
257241
payload = Uid2Base64UrlCoder.decode(token)
258242
bad_token = base64.urlsafe_b64encode(payload[:0])
@@ -262,8 +246,7 @@ def test_invalid_payload(self): #InvalidPayload
262246
self.assertEqual(result.status, DecryptionStatus.INVALID_PAYLOAD)
263247

264248
def test_token_expiry_custom_decryption_time(self): #TokenExpiryAndCustomNow
265-
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys())
266-
self.assertTrue(refresh_response.success)
249+
self.refresh(key_bidstream_response_json_default_keys())
267250

268251
expires_at = now - dt.timedelta(days=60)
269252
created_at = expires_at - dt.timedelta(minutes=1)

0 commit comments

Comments
 (0)