Skip to content

Commit 24136f3

Browse files
committed
Tests refactor
1 parent 5bfd9fe commit 24136f3

3 files changed

Lines changed: 39 additions & 25 deletions

File tree

tests/test_encryption.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
from tests.uid2_token_generator import UID2TokenGenerator, Params
44
from uid2_client import *
55
from uid2_client.encryption import _encrypt_token
6-
from uid2_client.identity_scope import IdentityScope
7-
from uid2_client.identity_type import IdentityType
8-
from uid2_client.keys import *
6+
from test_utils import *
97

108
_master_secret = bytes([139, 37, 241, 173, 18, 92, 36, 232, 165, 168, 23, 18, 38, 195, 123, 92, 160, 136, 185, 40, 91, 173, 165, 221, 168, 16, 169, 164, 38, 139, 8, 155])
119
_site_secret = bytes([32, 251, 7, 194, 132, 154, 250, 86, 202, 116, 104, 29, 131, 192, 139, 215, 48, 164, 11, 65, 226, 110, 167, 14, 108, 51, 254, 125, 65, 24, 23, 133])
@@ -708,18 +706,9 @@ def verify_identity_type(self, raw_uid, expected_identity_type):
708706
keys = EncryptionKeysCollection([_master_key, _site_key])
709707
result = decrypt(token, keys)
710708
self.assertEqual(raw_uid, result.uid2)
711-
self.assertEqual(expected_identity_type, get_token_identity_type(token))
709+
self.assertEqual(expected_identity_type, get_identity_type(token))
712710

713711

714-
def get_token_identity_type(id):
715-
firstChar = id[0]
716-
if 'A' == firstChar or 'E' == firstChar: #from UID2-79+Token+ and +ID+format+v3
717-
return IdentityType.Email.value
718-
elif 'F' == firstChar or 'B' == firstChar:
719-
return IdentityType.Phone.value
720-
721-
raise Exception("unknown IdentityType")
722-
723712
def format_time(t):
724713
s = t.strftime('%Y-%m-%d %H:%M:%S.%f')
725714
return s[:-3]

tests/test_sharing_client.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def _create_key_collection(identity_scope):
1212
99999, 86400)
1313

1414

15-
def _generate_uid_token(identity_scope, version, created_at=None, expires_at=None):
16-
return UID2TokenGenerator.generate_uid_token(example_id, master_key, site_id, site_key,
15+
def _generate_uid_token(identity_scope, version, raw_uid=example_uid, created_at=None, expires_at=None):
16+
return UID2TokenGenerator.generate_uid_token(raw_uid, master_key, site_id, site_key,
1717
identity_scope, version, created_at, expires_at)
1818

1919

@@ -24,7 +24,7 @@ class TestSharingClient(unittest.TestCase):
2424

2525
def setUp(self):
2626
self._key_collection = _create_key_collection(IdentityScope.UID2)
27-
self._test_cases = [
27+
self._test_cases_all_scopes_all_versions = [
2828
[IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V2],
2929
[IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V3],
3030
[IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4],
@@ -33,6 +33,13 @@ def setUp(self):
3333
[IdentityScope.EUID, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4]
3434
]
3535

36+
self._test_cases_all_scopes_v3_v4_versions = [
37+
[IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V3],
38+
[IdentityScope.UID2, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4],
39+
[IdentityScope.EUID, AdvertisingTokenVersion.ADVERTISING_TOKEN_V3],
40+
[IdentityScope.EUID, AdvertisingTokenVersion.ADVERTISING_TOKEN_V4]
41+
]
42+
3643
def test_encrypt_decrypt(self, mock_refresh_sharing_keys): #CanEncryptAndDecryptForSharing
3744
key_collection = self._key_collection
3845
mock_refresh_sharing_keys.return_value = key_collection
@@ -201,9 +208,9 @@ def test_refresh_keys(self, mock_refresh_sharing_keys):
201208

202209
def test_smoke_test(self, mock_refresh_sharing_keys): # SmokeTest
203210
client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
204-
for expected_scope, expected_version in self._test_cases:
211+
for expected_scope, expected_version in self._test_cases_all_scopes_all_versions:
205212
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
206-
token = _generate_uid_token(expected_scope, expected_version, None)
213+
token = _generate_uid_token(expected_scope, expected_version)
207214
mock_refresh_sharing_keys.return_value = _create_key_collection(expected_scope)
208215
client.refresh_keys()
209216
decrypted = client.decrypt_sharing_token_into_raw_uid(token)
@@ -214,9 +221,9 @@ def test_token_lifetime_too_long_for_sharing(self, mock_refresh_sharing_keys):
214221
client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
215222
expires_in_sec = dt.datetime.now(tz=timezone.utc) + dt.timedelta(days=31)
216223
max_sharing_lifetime = dt.timedelta(days=30).total_seconds()
217-
for expected_scope, expected_version in self._test_cases:
224+
for expected_scope, expected_version in self._test_cases_all_scopes_all_versions:
218225
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
219-
token = _generate_uid_token(expected_scope, expected_version, expires_in_sec)
226+
token = _generate_uid_token(expected_scope, expected_version, expires_at=expires_in_sec)
220227
mock_refresh_sharing_keys.return_value = EncryptionKeysCollection([master_key, site_key],
221228
expected_scope, site_id, 1,
222229
99999, 86400, max_sharing_lifetime)
@@ -227,9 +234,9 @@ def test_token_lifetime_too_long_for_sharing(self, mock_refresh_sharing_keys):
227234
def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh_sharing_keys): # TokenGeneratedInTheFutureToSimulateClockSkew
228235
client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
229236
created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=31) #max allowed clock skew is 30m
230-
for expected_scope, expected_version in self._test_cases:
237+
for expected_scope, expected_version in self._test_cases_all_scopes_all_versions:
231238
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
232-
token = _generate_uid_token(expected_scope, expected_version, created_at_future)
239+
token = _generate_uid_token(expected_scope, expected_version, created_at=created_at_future)
233240
mock_refresh_sharing_keys.return_value = EncryptionKeysCollection([master_key, site_key],
234241
expected_scope, site_id, 1,
235242
99999, 86400)
@@ -240,16 +247,34 @@ def test_token_generated_in_the_future_to_simulate_clock_skew(self, mock_refresh
240247
def test_token_generated_in_the_future_within_allowed_clock_skew(self, mock_refresh_sharing_keys): # TokenGeneratedInTheFutureWithinAllowedClockSkew
241248
client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
242249
created_at_future = dt.datetime.now(tz=timezone.utc) + dt.timedelta(minutes=29) #max allowed clock skew is 30m
243-
for expected_scope, expected_version in self._test_cases:
250+
for expected_scope, expected_version in self._test_cases_all_scopes_all_versions:
244251
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
245-
token = _generate_uid_token(expected_scope, expected_version, created_at_future)
252+
token = _generate_uid_token(expected_scope, expected_version, expires_at=created_at_future)
246253
mock_refresh_sharing_keys.return_value = EncryptionKeysCollection([master_key, site_key],
247254
expected_scope, site_id, 1,
248255
99999, 86400)
249256
client.refresh_keys()
250257
result = client.decrypt_sharing_token_into_raw_uid(token)
251258
self.assertIsNotNone(result)
259+
self.assertEqual(result.identity_scope, expected_scope)
260+
self.assertEqual(result.advertising_token_version, expected_version)
252261

262+
def test_phone_uids(self, mock_refresh_sharing_keys): # PhoneTest
263+
client = SharingClient(self._CONST_BASE_URL, self._CONST_API_KEY, client_secret)
264+
phone_uid = "BEOGxroPLdcY7LrSiwjY52+X05V0ryELpJmoWAyXiwbZ"
265+
for expected_scope, expected_version in self._test_cases_all_scopes_v3_v4_versions:
266+
with self.subTest(expected_scope=expected_scope, expected_version=expected_version):
267+
mock_refresh_sharing_keys.return_value = EncryptionKeysCollection([master_key, site_key],
268+
expected_scope, site_id, 1,
269+
99999, 86400)
270+
client.refresh_keys()
271+
token = _generate_uid_token(expected_scope, expected_version, phone_uid)
272+
self.assertEqual(IdentityType.Phone, get_identity_type(token))
273+
result = client.decrypt_sharing_token_into_raw_uid(token)
274+
self.assertIsNotNone(result)
275+
self.assertEqual(result.uid2, phone_uid)
276+
self.assertEqual(result.identity_scope, expected_scope)
277+
self.assertEqual(result.advertising_token_version, expected_version)
253278

254279

255280
if __name__ == '__main__':

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_identity_type(token):
4242
if ('F' == first_char or 'B' == first_char):
4343
return IdentityType.Phone
4444

45-
return None
45+
raise Exception("unknown IdentityType")
4646

4747

4848
def get_token_identity_type(uid2, keys):

0 commit comments

Comments
 (0)