@@ -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
255280if __name__ == '__main__' :
0 commit comments