@@ -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