Skip to content

Commit c35abb1

Browse files
committed
Add new test case and fix checking exception in the unit tests
1 parent 0899c54 commit c35abb1

3 files changed

Lines changed: 37 additions & 44 deletions

File tree

tests/test_bidstream_client.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import unittest
22
from unittest.mock import patch
33

4-
from uid2_client import BidStreamClient, ClientType, EncryptionError
54
from test_utils import *
5+
from uid2_client import BidStreamClient, EncryptionError
66

77

88
@patch('uid2_client.bid_stream_client.refresh_bidstream_keys')
@@ -50,8 +50,9 @@ def test_token_lifetime_too_long_for_bidstream(self, mock_refresh_bidstream_keys
5050
99999, 86400,
5151
max_bidstream_lifetime_seconds=max_bidstream_lifetime)
5252
self._client.refresh_keys()
53-
with self.assertRaises(EncryptionError):
53+
with self.assertRaises(EncryptionError) as context:
5454
self._client.decrypt_ad_token_into_raw_uid(token, None)
55+
self.assertEqual('invalid token lifetime', str(context.exception))
5556

5657
def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh_bidstream_keys): # TokenGeneratedInTheFutureToSimulateClockSkew
5758
created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=31) #max allowed clock skew is 30m
@@ -62,8 +63,9 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh
6263
expected_scope, site_id, 1,
6364
99999, 86400)
6465
self._client.refresh_keys()
65-
with self.assertRaises(EncryptionError):
66+
with self.assertRaises(EncryptionError) as context:
6667
self._client.decrypt_ad_token_into_raw_uid(token, None)
68+
self.assertEqual('invalid token lifetime', str(context.exception))
6769

6870
def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refresh_bidstream_keys): # TokenGeneratedInTheFutureWithinAllowedClockSkew
6971
created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=29) #max allowed clock skew is 30m
@@ -83,23 +85,25 @@ def test_empty_keys(self, mock_refresh_bidstream_keys): # EmptyKeyContainer
8385
token = generate_uid_token(IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V3)
8486
mock_refresh_bidstream_keys.return_value = None
8587
self._client.refresh_keys()
86-
with self.assertRaises(EncryptionError):
88+
with self.assertRaises(EncryptionError) as context:
8789
self._client.decrypt_ad_token_into_raw_uid(token, None)
90+
self.assertEqual('keys not initialized', str(context.exception))
8891

8992
def test_master_key_expired(self, mock_refresh_keys_util): #ExpiredKeyContainer
9093
def get_post_refresh_keys_response_with_key_expired():
91-
master_key_expired = EncryptionKey(master_key_id, site_id, created=now, activates=YESTERDAY, expires=YESTERDAY, secret=master_secret,
94+
master_key_expired = EncryptionKey(master_key_id, -1, created=now, activates=now - dt.timedelta(hours=2), expires=now - dt.timedelta(hours=1), secret=master_secret,
9295
keyset_id=99999)
93-
expired_key = EncryptionKey(site_key_id, site_id, created=now, activates=YESTERDAY, expires=YESTERDAY, secret=site_secret,
96+
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,
9497
keyset_id=99999)
95-
return create_default_key_collection([master_key, expired_key])
98+
return create_default_key_collection([master_key_expired, site_key_expired])
9699

97100
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_key_expired()
98101
self._client.refresh_keys()
99102

100103
with self.assertRaises(EncryptionError) as context:
101104
self._client.decrypt_ad_token_into_raw_uid(example_uid, None)
102-
self.assertTrue('No Keyset Key Found' in context.exception)
105+
106+
self.assertEqual('no keys available or all keys have expired; refresh the latest keys from UID2 service', str(context.exception))
103107

104108
def test_refresh_keys(self, mock_refresh_bidstream_keys):
105109
key_collection = create_default_key_collection([master_key])

tests/test_publisher_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
from uid2_client import Uid2PublisherClient
55
from uid2_client import TokenGenerateInput
6-
from uid2_client import TokenGenerateResponse
76
from uid2_client.identity_tokens import IdentityTokens
87
from urllib.request import HTTPError
98

10-
9+
@unittest.skip
1110
class PublisherEuidIntegrationTests(unittest.TestCase):
1211

1312
EUID_SECRET_KEY = None
@@ -61,6 +60,8 @@ def test_integration_optout_generate_token(self):
6160
self.assertFalse(token_generate_response.is_success())
6261
self.assertIsNone(token_generate_response.get_identity())
6362

63+
64+
@unittest.skip
6465
class PublisherUid2IntegrationTests(unittest.TestCase):
6566

6667
UID2_SECRET_KEY = None

tests/test_sharing_client.py

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ def test_token_lifetime_too_long_for_sharing(self, mock_refresh_sharing_keys):
3434
expected_scope, site_id, 1,
3535
99999, 86400, max_sharing_lifetime)
3636
self._client.refresh_keys()
37-
with self.assertRaises(EncryptionError):
37+
with self.assertRaises(EncryptionError) as context:
3838
self._client.decrypt_sharing_token_into_raw_uid(token)
39+
self.assertEqual('invalid token lifetime', str(context.exception))
3940

4041
def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh_sharing_keys): # TokenGeneratedInTheFutureToSimulateClockSkew
4142
created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=31) #max allowed clock skew is 30m
@@ -46,8 +47,9 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh
4647
expected_scope, site_id, 1,
4748
99999, 86400)
4849
self._client.refresh_keys()
49-
with self.assertRaises(EncryptionError):
50+
with self.assertRaises(EncryptionError) as context:
5051
self._client.decrypt_sharing_token_into_raw_uid(token)
52+
self.assertEqual('invalid token lifetime', str(context.exception))
5153

5254
def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refresh_sharing_keys): # TokenGeneratedInTheFutureWithinAllowedClockSkew
5355
created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=29) #max allowed clock skew is 30m
@@ -84,15 +86,15 @@ def test_sharing_client_produces_uid2_token(self, mock_refresh_keys_util): #Cli
8486
mock_refresh_keys_util.return_value = self._key_collection
8587
self._client.refresh_keys()
8688

87-
ad_token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
88-
self.assertEqual("A", ad_token[0])
89+
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
90+
self.assertEqual("A", token[0])
8991

9092
def test_sharing_client_produces_euid_token(self, mock_refresh_keys_util): #ClientProducesTokenWithCorrectPrefix
9193
mock_refresh_keys_util.return_value = create_key_collection(IdentityScope.EUID)
9294
self._client.refresh_keys()
9395

94-
ad_token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
95-
self.assertEqual("E", ad_token[0])
96+
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
97+
self.assertEqual("E", token[0])
9698

9799
def test_encrypt_decrypt(self, mock_refresh_sharing_keys): #CanEncryptAndDecryptForSharing
98100
key_collection = self._key_collection
@@ -111,20 +113,20 @@ def test_can_decrypt_another_clients_encrypted_token(self, mock_refresh_keys_uti
111113
sending_client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
112114
sending_client.refresh_keys()
113115

114-
ad_token = sending_client.encrypt_raw_uid_into_sharing_token(example_uid)
116+
token = sending_client.encrypt_raw_uid_into_sharing_token(example_uid)
115117

116118
receiving_client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
117119
receiving_client.refresh_keys()
118120

119-
result = receiving_client.decrypt_sharing_token_into_raw_uid(ad_token)
121+
result = receiving_client.decrypt_sharing_token_into_raw_uid(token)
120122
self.assertEqual(example_uid, result.uid2)
121123

122124
def test_sharing_token_is_v4(self, mock_refresh_keys_util):
123125
mock_refresh_keys_util.return_value = self._key_collection
124126
self._client.refresh_keys()
125127

126-
ad_token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
127-
contains_base_64_special_chars = "+" in ad_token or "/" in ad_token or "=" in ad_token
128+
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
129+
contains_base_64_special_chars = "+" in token or "/" in token or "=" in token
128130
self.assertFalse(contains_base_64_special_chars)
129131

130132
def test_raw_uid_produces_correct_identity_type_in_token(self, mock_refresh_keys_util): #RawUidProducesCorrectIdentityTypeInToken
@@ -163,7 +165,7 @@ def get_post_refresh_keys_response_with_no_default_keyset_key():
163165

164166
with self.assertRaises(EncryptionError) as context:
165167
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
166-
self.assertTrue('No Site ID in keys' in context.exception)
168+
self.assertEqual('No Keyset Key Found', str(context.exception))
167169

168170
def test_cannot_encrypt_if_theres_no_default_keyset_header(self, mock_refresh_keys_util): #CannotEncryptIfTheresNoDefaultKeysetHeader
169171
def get_post_refresh_keys_response_with_no_default_keyset_header():
@@ -176,31 +178,17 @@ def get_post_refresh_keys_response_with_no_default_keyset_header():
176178

177179
with self.assertRaises(EncryptionError) as context:
178180
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
179-
self.assertTrue('No Keyset Key Found' in context.exception)
181+
self.assertEqual('No Keyset Key Found', str(context.exception))
180182

181183
def test_expiry_in_token_matches_expiry_in_response(self, mock_refresh_keys_util): # ExpiryInTokenMatchesExpiryInResponse
182-
def get_post_refresh_keys_response_with_token_expiry():
183-
return create_default_key_collection([master_key, site_key])
184184

185-
mock_refresh_keys_util.return_value = get_post_refresh_keys_response_with_token_expiry()
185+
mock_refresh_keys_util.return_value = create_default_key_collection([master_key, site_key])
186186
self._client.refresh_keys()
187-
188-
ad_token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
189-
190-
result = self._client.decrypt_sharing_token_into_raw_uid(ad_token)
191-
self.assertEqual(example_uid, result.uid2)
192-
193-
real_decrypt_v3 = encryption._decrypt_token_v3
194-
195-
with patch('uid2_client.encryption._decrypt_token_v3') as mock_decrypt:
196-
def decrypt_side_effect(token_bytes, keys):
197-
return real_decrypt_v3(token_bytes, keys, '', ClientType.Sharing, IN_3_DAYS, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4)
198-
199-
mock_decrypt.side_effect = decrypt_side_effect
200-
201-
with self.assertRaises(EncryptionError) as context:
202-
self._client.decrypt_sharing_token_into_raw_uid(ad_token)
203-
self.assertTrue('token expired' in context.exception)
187+
token = self._client.encrypt_raw_uid_into_sharing_token(example_uid)
188+
self._client.decrypt_sharing_token_into_raw_uid(token, now + dt.timedelta(seconds=1))
189+
with self.assertRaises(EncryptionError) as context:
190+
self._client.decrypt_sharing_token_into_raw_uid(token, now + dt.timedelta(seconds=3))
191+
self.assertEqual('token expired', str(context.exception))
204192

205193
def test_encrypt_key_inactive(self, mock_refresh_keys_util): #EncryptKeyInactive
206194
def get_post_refresh_keys_response_with_key_inactive():
@@ -213,7 +201,7 @@ def get_post_refresh_keys_response_with_key_inactive():
213201

214202
with self.assertRaises(EncryptionError) as context:
215203
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
216-
self.assertTrue('No Keyset Key Found' in context.exception)
204+
self.assertEqual('No Keyset Key Found', str(context.exception))
217205

218206
def test_encrypt_key_expired(self, mock_refresh_keys_util): #EncryptKeyExpired
219207
def get_post_refresh_keys_response_with_key_expired():
@@ -226,7 +214,7 @@ def get_post_refresh_keys_response_with_key_expired():
226214

227215
with self.assertRaises(EncryptionError) as context:
228216
self._client.encrypt_raw_uid_into_sharing_token(example_uid)
229-
self.assertTrue('No Keyset Key Found' in context.exception)
217+
self.assertEqual('No Keyset Key Found', str(context.exception))
230218

231219
def test_refresh_keys(self, mock_refresh_sharing_keys):
232220
key_collection = create_default_key_collection([master_key])

0 commit comments

Comments
 (0)