Skip to content

Commit aec489d

Browse files
committed
Add unit tests
1 parent 22c17e8 commit aec489d

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

tests/test_bidstream_client.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@ def _decrypt_and_assert_success(self, token, token_version, scope):
8888
def setUp(self):
8989
self._client = BidstreamClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
9090

91-
def test_smoke_test(self): # SmokeTest
91+
def test_smoke_test_for_bidstream(self): # SmokeTestForBidstream
9292
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
9393
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
94-
token = generate_uid_token(expected_scope, expected_version)
94+
token = generate_uid_token(expected_scope, expected_version,
95+
identity_established_at=now - dt.timedelta(days=120),
96+
generated_at=YESTERDAY,
97+
expires_at=IN_2_DAYS)
9598
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
9699
expected_scope))
97100
self.assertTrue(refresh_response.success)
@@ -112,11 +115,29 @@ def test_phone_uids(self): # PhoneTest
112115
self.assertEqual(result.identity_scope, expected_scope)
113116
self.assertEqual(result.advertising_token_version, expected_version)
114117

115-
def test_token_lifetime_too_long_for_bidstream(self): # TokenLifetimeTooLongForBidstream
116-
expires_in_sec = IN_3_DAYS + dt.timedelta(minutes=1)
118+
def test_token_lifetime_too_long_for_bidstream_but_remaining_lifetime_allowed(self): # TokenLifetimeTooLongForBidstreamButRemainingLifetimeAllowed
119+
generated = YESTERDAY
120+
expires_in_sec = generated + dt.timedelta(days=3) + dt.timedelta(minutes=1)
117121
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
118122
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
119-
token = generate_uid_token(expected_scope, expected_version, expires_at=expires_in_sec)
123+
token = generate_uid_token(expected_scope, expected_version, generated_at=generated,
124+
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)
128+
result = self._client.decrypt_token_into_raw_uid(token, None)
129+
if expected_version == AdvertisingTokenVersion.ADVERTISING_TOKEN_V2:
130+
self.assert_success(result, expected_version, expected_scope)
131+
else:
132+
self.assert_fails(result, expected_version, expected_scope)
133+
134+
def test_token_remaining_lifetime_too_long_for_bidstream(self): # TokenRemainingLifetimeTooLongForBidstream
135+
generated = now
136+
expires_in_sec = generated + dt.timedelta(days=3) + dt.timedelta(minutes=1)
137+
for expected_scope, expected_version in test_cases_all_scopes_v3_v4_versions:
138+
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
139+
token = generate_uid_token(expected_scope, expected_version, generated_at=generated,
140+
expires_at=expires_in_sec)
120141
refresh_response = self._client._refresh_json(key_bidstream_response_json_default_keys(
121142
expected_scope))
122143
self.assertTrue(refresh_response.success)

tests/test_sharing_client.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,35 @@ def decrypt_and_assert_success(self, token, token_version, scope):
7777
decrypted = self._client.decrypt_token_into_raw_uid(token)
7878
self._test_bidstream_client.assert_success(decrypted, token_version, scope)
7979

80-
def test_smoke_test(self): # SmokeTest
80+
def test_smoke_test_for_sharing(self): # SmokeTestForSharing
8181
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
8282
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
83-
token = generate_uid_token(expected_scope, expected_version)
83+
token = generate_uid_token(expected_scope, expected_version,
84+
identity_established_at=now - dt.timedelta(days=120),
85+
generated_at=YESTERDAY, expires_at=now + dt.timedelta(days=29))
8486
refresh_response = self._client._refresh_json(key_sharing_response_json_default_keys(
8587
expected_scope))
8688
self.assertTrue(refresh_response.success)
8789
self.decrypt_and_assert_success(token, expected_version, expected_scope)
8890

91+
def test_token_lifetime_too_long_for_sharing_but_remaining_lifetime_allowed(self): # TokenLifetimeTooLongForSharingButRemainingLifetimeAllowed
92+
generated = YESTERDAY
93+
expires_in_sec = generated + dt.timedelta(days=31)
94+
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
95+
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
96+
token = generate_uid_token(expected_scope, expected_version, generated_at=generated,
97+
expires_at=expires_in_sec)
98+
refresh_response = self._client._refresh_json(key_sharing_response_json_default_keys(
99+
expected_scope))
100+
self.assertTrue(refresh_response.success)
101+
result = self._client.decrypt_token_into_raw_uid(token)
102+
if expected_version == AdvertisingTokenVersion.ADVERTISING_TOKEN_V2:
103+
self._test_bidstream_client.assert_success(result, expected_version, expected_scope)
104+
else:
105+
self._test_bidstream_client.assert_fails(result, expected_version, expected_scope)
106+
89107
def test_token_lifetime_too_long_for_sharing(self): # TokenLifetimeTooLongForSharing
90-
expires_in_sec = dt.datetime.now(tz=timezone.utc) + dt.timedelta(days=31)
108+
expires_in_sec = now + dt.timedelta(days=30) + dt.timedelta(minutes=1)
91109
for expected_scope, expected_version in test_cases_all_scopes_all_versions:
92110
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
93111
token = generate_uid_token(expected_scope, expected_version, expires_at=expires_in_sec)
@@ -277,7 +295,8 @@ def test_expiry_in_token_matches_expiry_in_response(self): # ExpiryInTokenMatch
277295
self.assertTrue(result.status)
278296
self.assertEqual(example_uid, result.uid)
279297

280-
future_decryption_result = self._client._decrypt_token_into_raw_uid(encryption_data_response.encrypted_data, now + dt.timedelta(seconds=3))
298+
future_decryption_result = self._client._decrypt_token_into_raw_uid(encryption_data_response.encrypted_data,
299+
now + dt.timedelta(seconds=3))
281300
self.assertFalse(future_decryption_result.success)
282301
self.assertEqual(DecryptionStatus.EXPIRED_TOKEN, future_decryption_result.status)
283302
self.assertEqual(now + dt.timedelta(seconds=2), future_decryption_result.expiry)

0 commit comments

Comments
 (0)