|
1 | 1 | import unittest |
2 | 2 | from unittest.mock import patch |
3 | 3 |
|
4 | | -from uid2_client import BidStreamClient, ClientType |
| 4 | +from uid2_client import BidStreamClient, ClientType, EncryptionError |
5 | 5 | from test_utils import * |
6 | 6 |
|
7 | 7 |
|
| 8 | +@patch('uid2_client.bid_stream_client.refresh_bidstream_keys') |
8 | 9 | class TestBidStreamClient(unittest.TestCase): |
9 | 10 | _CONST_BASE_URL = 'base_url' |
10 | 11 | _CONST_API_KEY = 'api_key' |
11 | 12 |
|
12 | | - @patch('uid2_client.bid_stream_client.refresh_bidstream_keys') |
13 | | - @patch('uid2_client.bid_stream_client.decrypt_token') |
14 | | - def test_decrypt_ad_token_into_raw_uid(self, mock_decrypt_token, mock_refresh_bidstream_keys): |
15 | | - key_collection = create_default_key_collection([master_key]) |
16 | | - mock_refresh_bidstream_keys.return_value = key_collection |
17 | | - mock_decrypt_token.return_value = 'decrypted_token' |
18 | | - client = BidStreamClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret) |
19 | | - client.refresh_keys() |
20 | | - decrypted_token = client.decrypt_ad_token_into_raw_uid('token', 'domain_app_name') |
21 | | - self.assertEqual(decrypted_token, 'decrypted_token') |
22 | | - mock_refresh_bidstream_keys.assert_called_once() |
23 | | - mock_decrypt_token.assert_called_once_with('token', key_collection, 'domain_app_name', ClientType.Bidstream) |
| 13 | + def setUp(self): |
| 14 | + self._key_collection = create_key_collection(IdentityScope.UID2) |
| 15 | + self._client = BidStreamClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret) |
| 16 | + |
| 17 | + def test_smoke_test(self, mock_refresh_bidstream_keys): # SmokeTest |
| 18 | + for expected_scope, expected_version in test_cases_all_scopes_all_versions: |
| 19 | + with self.subTest(expected_scope=expected_scope, expected_version=expected_version): |
| 20 | + token = generate_uid_token(expected_scope, expected_version) |
| 21 | + mock_refresh_bidstream_keys.return_value = create_key_collection(expected_scope) |
| 22 | + self._client.refresh_keys() |
| 23 | + decrypted = self._client.decrypt_ad_token_into_raw_uid(token, None) |
| 24 | + self.assertEqual(decrypted.identity_scope, expected_scope) |
| 25 | + self.assertEqual(decrypted.advertising_token_version, expected_version) |
| 26 | + |
| 27 | + def test_phone_uids(self, mock_refresh_bidstream_keys): # PhoneTest |
| 28 | + for expected_scope, expected_version in test_cases_all_scopes_v3_v4_versions: |
| 29 | + with self.subTest(expected_scope=expected_scope, expected_version=expected_version): |
| 30 | + mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key], |
| 31 | + expected_scope, site_id, 1, |
| 32 | + 99999, 86400) |
| 33 | + self._client.refresh_keys() |
| 34 | + token = generate_uid_token(expected_scope, expected_version, phone_uid) |
| 35 | + self.assertEqual(IdentityType.Phone, get_identity_type(token)) |
| 36 | + result = self._client.decrypt_ad_token_into_raw_uid(token, None) |
| 37 | + self.assertIsNotNone(result) |
| 38 | + self.assertEqual(result.uid2, phone_uid) |
| 39 | + self.assertEqual(result.identity_scope, expected_scope) |
| 40 | + self.assertEqual(result.advertising_token_version, expected_version) |
| 41 | + |
| 42 | + def test_token_lifetime_too_long_for_bidstream(self, mock_refresh_bidstream_keys): # TokenLifetimeTooLongForBidstream |
| 43 | + expires_in_sec = IN_3_DAYS + dt.timedelta(minutes=1) |
| 44 | + max_bidstream_lifetime = dt.timedelta(days=3).total_seconds() |
| 45 | + for expected_scope, expected_version in test_cases_all_scopes_all_versions: |
| 46 | + with self.subTest(expected_scope=expected_scope, expected_version=expected_version): |
| 47 | + token = generate_uid_token(expected_scope, expected_version, expires_at=expires_in_sec) |
| 48 | + mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key], |
| 49 | + expected_scope, site_id, 1, |
| 50 | + 99999, 86400, |
| 51 | + max_bidstream_lifetime_seconds=max_bidstream_lifetime) |
| 52 | + self._client.refresh_keys() |
| 53 | + with self.assertRaises(EncryptionError): |
| 54 | + self._client.decrypt_ad_token_into_raw_uid(token, None) |
| 55 | + |
| 56 | + def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh_bidstream_keys): # TokenGeneratedInTheFutureToSimulateClockSkew |
| 57 | + created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=31) #max allowed clock skew is 30m |
| 58 | + for expected_scope, expected_version in test_cases_all_scopes_all_versions: |
| 59 | + with self.subTest(expected_scope=expected_scope, expected_version=expected_version): |
| 60 | + token = generate_uid_token(expected_scope, expected_version, created_at=created_at_future) |
| 61 | + mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key], |
| 62 | + expected_scope, site_id, 1, |
| 63 | + 99999, 86400) |
| 64 | + self._client.refresh_keys() |
| 65 | + with self.assertRaises(EncryptionError): |
| 66 | + self._client.decrypt_ad_token_into_raw_uid(token, None) |
| 67 | + |
| 68 | + def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refresh_bidstream_keys): # TokenGeneratedInTheFutureWithinAllowedClockSkew |
| 69 | + created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=29) #max allowed clock skew is 30m |
| 70 | + for expected_scope, expected_version in test_cases_all_scopes_all_versions: |
| 71 | + with self.subTest(expected_scope=expected_scope, expected_version=expected_version): |
| 72 | + token = generate_uid_token(expected_scope, expected_version, expires_at=created_at_future) |
| 73 | + mock_refresh_bidstream_keys.return_value = EncryptionKeysCollection([master_key, site_key], |
| 74 | + expected_scope, site_id, 1, |
| 75 | + 99999, 86400) |
| 76 | + self._client.refresh_keys() |
| 77 | + result = self._client.decrypt_ad_token_into_raw_uid(token, None) |
| 78 | + self.assertIsNotNone(result) |
| 79 | + self.assertEqual(result.identity_scope, expected_scope) |
| 80 | + self.assertEqual(result.advertising_token_version, expected_version) |
24 | 81 |
|
25 | | - @patch('uid2_client.bid_stream_client.refresh_bidstream_keys') |
26 | 82 | def test_refresh_keys(self, mock_refresh_bidstream_keys): |
27 | 83 | key_collection = create_default_key_collection([master_key]) |
28 | 84 | mock_refresh_bidstream_keys.return_value = key_collection |
|
0 commit comments