-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_identity_map_client.py
More file actions
191 lines (150 loc) · 9.73 KB
/
test_identity_map_client.py
File metadata and controls
191 lines (150 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import datetime as dt
import os
import unittest
from urllib.error import URLError, HTTPError
from uid2_client import IdentityMapClient, IdentityMapInput, normalize_and_hash_email, normalize_and_hash_phone
@unittest.skipIf(
os.getenv("UID2_BASE_URL") == None
or os.getenv("UID2_API_KEY") == None
or os.getenv("UID2_SECRET_KEY") == None,
reason="Environment variables UID2_BASE_URL, UID2_API_KEY, and UID2_SECRET_KEY must be set",
)
class IdentityMapIntegrationTests(unittest.TestCase):
UID2_BASE_URL = None
UID2_API_KEY = None
UID2_SECRET_KEY = None
identity_map_client = None
@classmethod
def setUpClass(cls):
cls.UID2_BASE_URL = os.getenv("UID2_BASE_URL")
cls.UID2_API_KEY = os.getenv("UID2_API_KEY")
cls.UID2_SECRET_KEY = os.getenv("UID2_SECRET_KEY")
if cls.UID2_BASE_URL and cls.UID2_API_KEY and cls.UID2_SECRET_KEY:
cls.identity_map_client = IdentityMapClient(cls.UID2_BASE_URL, cls.UID2_API_KEY, cls.UID2_SECRET_KEY)
else:
raise Exception("set the required UID2_BASE_URL/UID2_API_KEY/UID2_SECRET_KEY environment variables first")
def test_identity_map_emails(self):
identity_map_input = IdentityMapInput.from_emails(
["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_mapped(response, "hopefully-not-opted-out@example.com")
self.assert_mapped(response, "somethingelse@example.com")
self.assert_unmapped(response, "optout", "optout@example.com")
def test_identity_map_nothing_unmapped(self):
identity_map_input = IdentityMapInput.from_emails(
["hopefully-not-opted-out@example.com", "somethingelse@example.com"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_mapped(response, "hopefully-not-opted-out@example.com")
self.assert_mapped(response, "somethingelse@example.com")
def test_identity_map_nothing_mapped(self):
identity_map_input = IdentityMapInput.from_emails(["optout@example.com"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_unmapped(response, "optout", "optout@example.com")
def test_identity_map_invalid_email(self):
self.assertRaises(ValueError, IdentityMapInput.from_emails,
["email@example.com", "this is not an email"])
def test_identity_map_invalid_phone(self):
self.assertRaises(ValueError, IdentityMapInput.from_phones,
["+12345678901", "this is not a phone number"])
def test_identity_map_invalid_hashed_email(self):
identity_map_input = IdentityMapInput.from_hashed_emails(["this is not a hashed email"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_unmapped(response, "invalid identifier", "this is not a hashed email")
def test_identity_map_invalid_hashed_phone(self):
identity_map_input = IdentityMapInput.from_hashed_emails(["this is not a hashed phone"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_unmapped(response, "invalid identifier", "this is not a hashed phone")
def test_identity_map_hashed_emails(self):
hashed_email1 = normalize_and_hash_email("hopefully-not-opted-out@example.com")
hashed_email2 = normalize_and_hash_email("somethingelse@example.com")
hashed_opted_out_email = normalize_and_hash_email("optout@example.com")
identity_map_input = IdentityMapInput.from_hashed_emails([hashed_email1, hashed_email2, hashed_opted_out_email])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_mapped(response, hashed_email1)
self.assert_mapped(response, hashed_email2)
self.assert_unmapped(response, "optout", hashed_opted_out_email)
def test_identity_map_duplicate_emails(self):
identity_map_input = IdentityMapInput.from_emails(
["JANE.SAOIRSE@gmail.com", "Jane.Saoirse@gmail.com", "JaneSaoirse+UID2@gmail.com", "janesaoirse@gmail.com",
"JANE.SAOIRSE@gmail.com"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
mapped_identities = response.mapped_identities
self.assertEqual(4, len(mapped_identities))
raw_uid = mapped_identities.get("JANE.SAOIRSE@gmail.com").get_raw_uid()
self.assertEqual(raw_uid, mapped_identities.get("Jane.Saoirse@gmail.com").get_raw_uid())
self.assertEqual(raw_uid, mapped_identities.get("JaneSaoirse+UID2@gmail.com").get_raw_uid())
self.assertEqual(raw_uid, mapped_identities.get("janesaoirse@gmail.com").get_raw_uid())
def test_identity_map_duplicate_hashed_emails(self):
hashed_email = normalize_and_hash_email("hopefully-not-opted-out@example.com")
duplicate_hashed_email = hashed_email
hashed_opted_out_email = normalize_and_hash_email("optout@example.com")
duplicate_hashed_opted_out_email = hashed_opted_out_email
identity_map_input = IdentityMapInput.from_hashed_emails(
[hashed_email, duplicate_hashed_email, hashed_opted_out_email, duplicate_hashed_opted_out_email])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_mapped(response, hashed_email)
self.assert_mapped(response, duplicate_hashed_email)
self.assert_unmapped(response, "optout", hashed_opted_out_email)
self.assert_unmapped(response, "optout", duplicate_hashed_opted_out_email)
def test_identity_map_empty_input(self):
identity_map_input = IdentityMapInput.from_emails([])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assertTrue(len(response.mapped_identities) == 0)
self.assertTrue(len(response.unmapped_identities) == 0)
def test_identity_map_phones(self):
identity_map_input = IdentityMapInput.from_phones(["+12345678901", "+98765432109", "+00000000000"])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_mapped(response, "+12345678901")
self.assert_mapped(response, "+98765432109")
self.assert_unmapped(response, "optout", "+00000000000")
def test_identity_map_hashed_phones(self):
hashed_phone1 = normalize_and_hash_phone("+12345678901")
hashed_phone2 = normalize_and_hash_phone("+98765432109")
hashed_opted_out_phone = normalize_and_hash_phone("+00000000000")
identity_map_input = IdentityMapInput.from_hashed_phones([hashed_phone1, hashed_phone2, hashed_opted_out_phone])
response = self.identity_map_client.generate_identity_map(identity_map_input)
self.assert_mapped(response, hashed_phone1)
self.assert_mapped(response, hashed_phone2)
self.assert_unmapped(response, "optout", hashed_opted_out_phone)
def test_identity_map_client_bad_url(self):
identity_map_input = IdentityMapInput.from_emails(
["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"])
client = IdentityMapClient("https://operator-bad-url.uidapi.com", os.getenv("UID2_API_KEY"), os.getenv("UID2_SECRET_KEY"))
self.assertRaises(URLError, client.generate_identity_map, identity_map_input)
self.assertRaises(URLError, client.get_identity_buckets, dt.datetime.now())
def test_identity_map_client_bad_api_key(self):
identity_map_input = IdentityMapInput.from_emails(
["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"])
client = IdentityMapClient(os.getenv("UID2_BASE_URL"), "bad-api-key", os.getenv("UID2_SECRET_KEY"))
self.assertRaises(HTTPError, client.generate_identity_map,identity_map_input)
self.assertRaises(HTTPError, client.get_identity_buckets, dt.datetime.now())
def test_identity_map_client_bad_secret(self):
identity_map_input = IdentityMapInput.from_emails(
["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"])
client = IdentityMapClient(os.getenv("UID2_BASE_URL"), os.getenv("UID2_API_KEY"), "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=")
self.assertRaises(HTTPError, client.generate_identity_map,
identity_map_input)
self.assertRaises(HTTPError, client.get_identity_buckets,
dt.datetime.now())
def assert_mapped(self, response, dii):
mapped_identity = response.mapped_identities.get(dii)
self.assertIsNotNone(mapped_identity)
self.assertIsNotNone(mapped_identity.get_raw_uid())
self.assertIsNotNone(mapped_identity.get_bucket_id())
unmapped_identity = response.unmapped_identities.get(dii)
self.assertIsNone(unmapped_identity)
def assert_unmapped(self, response, reason, dii):
unmapped_identity = response.unmapped_identities.get(dii)
self.assertEqual(reason, unmapped_identity.get_reason())
mapped_identity = response.mapped_identities.get(dii)
self.assertIsNone(mapped_identity)
def test_identity_buckets(self):
response = self.identity_map_client.get_identity_buckets(dt.datetime.now() - dt.timedelta(days=90))
self.assertTrue(len(response.buckets) > 0)
self.assertTrue(response.is_success)
def test_identity_buckets_empty_response(self):
response = self.identity_map_client.get_identity_buckets(dt.datetime.now() + dt.timedelta(days=1))
self.assertTrue(len(response.buckets) == 0)
self.assertTrue(response.is_success)
if __name__ == '__main__':
unittest.main()