1+ import os
2+ import unittest
3+ from urllib .error import HTTPError
4+
5+ from uid2_client import SharingClient , Uid2PublisherClient , TokenGenerateInput , EncryptionStatus , BidstreamClient
6+
7+
8+ @unittest .skipIf (
9+ os .getenv ("UID2_BASE_URL" ) is None
10+ or os .getenv ("UID2_API_KEY" ) is None
11+ or os .getenv ("UID2_SECRET_KEY" ) is None ,
12+ "Environment variables UID2_BASE_URL, UID2_API_KEY, and UID2_SECRET_KEY must be set" ,
13+ )
14+ class SharingClientIntegrationTests (unittest .TestCase ):
15+ @classmethod
16+ def setUpClass (cls ):
17+ cls .UID2_BASE_URL = os .getenv ("UID2_BASE_URL" )
18+ cls .UID2_API_KEY = os .getenv ("UID2_API_KEY" )
19+ cls .UID2_SECRET_KEY = os .getenv ("UID2_SECRET_KEY" )
20+
21+ if cls .UID2_BASE_URL and cls .UID2_API_KEY and cls .UID2_SECRET_KEY :
22+ cls .sharing_client = SharingClient (cls .UID2_BASE_URL , cls .UID2_API_KEY , cls .UID2_SECRET_KEY )
23+ cls .publisher_client = Uid2PublisherClient (cls .UID2_BASE_URL , cls .UID2_API_KEY , cls .UID2_SECRET_KEY )
24+ cls .bidstream_client = BidstreamClient (cls .UID2_BASE_URL , cls .UID2_API_KEY , cls .UID2_SECRET_KEY )
25+ else :
26+ raise Exception ("set the required UID2_BASE_URL/UID2_API_KEY/UID2_SECRET_KEY environment variables first" )
27+
28+ def test_sharing_client_key_refresh (self ):
29+ refresh_response = self .sharing_client .refresh ()
30+ self .assertTrue (refresh_response .success )
31+
32+ def test_sharing_client_encrypt_decrypt_raw_uid (self ):
33+ # Get raw uid
34+ token_response = self .publisher_client .generate_token (
35+ TokenGenerateInput .
from_email (
"[email protected] " ).
do_not_generate_tokens_for_opted_out ()
36+ )
37+ identity = token_response .get_identity ()
38+
39+ self .bidstream_client .refresh ()
40+ decrypted_token = self .bidstream_client .decrypt_token_into_raw_uid (identity .get_advertising_token (), "example.com" )
41+ self .assertTrue (decrypted_token .success )
42+ raw_uid = decrypted_token .uid
43+
44+ # Refresh keys first
45+ refresh_response = self .sharing_client .refresh ()
46+ self .assertTrue (refresh_response .success )
47+
48+ # Encrypt the raw UID
49+ encryption_response = self .sharing_client .encrypt_raw_uid_into_token (raw_uid )
50+ self .assertTrue (encryption_response .success )
51+ self .assertIsNotNone (encryption_response .encrypted_data )
52+
53+ # Now decrypt the encrypted token
54+ decryption_response = self .sharing_client .decrypt_token_into_raw_uid (
55+ encryption_response .encrypted_data
56+ )
57+ self .assertTrue (decryption_response .success )
58+ self .assertEqual (decryption_response .uid , raw_uid )
59+
60+ def test_sharing_client_encrypt_with_invalid_raw_uid (self ):
61+ refresh_response = self .sharing_client .refresh ()
62+ self .assertTrue (refresh_response .success )
63+
64+ invalid_raw_uid = "invalid_raw_uid"
65+ encryption_response = self .sharing_client .encrypt_raw_uid_into_token (invalid_raw_uid )
66+
67+ self .assertFalse (encryption_response .success )
68+
69+ def test_sharing_client_decrypt_with_invalid_token (self ):
70+ refresh_response = self .sharing_client .refresh ()
71+ self .assertTrue (refresh_response .success )
72+
73+ invalid_token = "invalid-token"
74+ decryption_response = self .sharing_client .decrypt_token_into_raw_uid (invalid_token )
75+
76+ self .assertFalse (decryption_response .success )
77+
78+ def test_sharing_client_without_refresh (self ):
79+ fresh_client = SharingClient (self .UID2_BASE_URL , self .UID2_API_KEY , self .UID2_SECRET_KEY )
80+
81+ token_response = self .publisher_client .generate_token (
82+ TokenGenerateInput .
from_email (
"[email protected] " ).
do_not_generate_tokens_for_opted_out ()
83+ )
84+ identity = token_response .get_identity ()
85+ self .bidstream_client .refresh ()
86+ decrypted_token = self .bidstream_client .decrypt_token_into_raw_uid (identity .get_advertising_token (), "example.com" )
87+ self .assertTrue (decrypted_token .success )
88+ raw_uid = decrypted_token .uid
89+
90+ encryption_response = fresh_client .encrypt_raw_uid_into_token (raw_uid )
91+
92+ self .assertFalse (encryption_response .success )
93+
94+ def test_sharing_client_error_handling (self ):
95+ bad_client = SharingClient (self .UID2_BASE_URL , "bad-api-key" , self .UID2_SECRET_KEY )
96+ refresh_response = bad_client .refresh ()
97+ self .assertFalse (refresh_response .success )
98+
99+ bad_client = SharingClient (self .UID2_BASE_URL , self .UID2_API_KEY , "bad-secret-key" )
100+ refresh_response = bad_client .refresh ()
101+ self .assertFalse (refresh_response .success )
102+
103+ if __name__ == '__main__' :
104+ unittest .main ()
0 commit comments