|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from urllib.error import URLError, HTTPError |
| 4 | + |
| 5 | +from uid2_client import IdentityMapClient, IdentityMapInput, normalize_and_hash_email, normalize_and_hash_phone |
| 6 | + |
| 7 | + |
| 8 | +class IdentityMapIntegrationTests(unittest.TestCase): |
| 9 | + UID2_BASE_URL = None |
| 10 | + UID2_API_KEY = None |
| 11 | + UID2_SECRET_KEY = None |
| 12 | + |
| 13 | + identity_map_client = None |
| 14 | + |
| 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.identity_map_client = IdentityMapClient(cls.UID2_BASE_URL, cls.UID2_API_KEY, cls.UID2_SECRET_KEY) |
| 23 | + else: |
| 24 | + raise Exception("set the required UID2_BASE_URL/UID2_API_KEY/UID2_SECRET_KEY environment variables first") |
| 25 | + |
| 26 | + def test_identity_map_emails(self): |
| 27 | + identity_map_input = IdentityMapInput.from_emails( |
| 28 | + ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) |
| 29 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 30 | + self.assert_mapped(response, "hopefully-not-opted-out@example.com") |
| 31 | + self.assert_mapped(response, "somethingelse@example.com") |
| 32 | + |
| 33 | + self.assert_unmapped(response, "optout", "optout@example.com") |
| 34 | + |
| 35 | + def test_identity_map_nothing_unmapped(self): |
| 36 | + identity_map_input = IdentityMapInput.from_emails( |
| 37 | + ["hopefully-not-opted-out@example.com", "somethingelse@example.com"]) |
| 38 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 39 | + self.assert_mapped(response, "hopefully-not-opted-out@example.com") |
| 40 | + self.assert_mapped(response, "somethingelse@example.com") |
| 41 | + |
| 42 | + def test_identity_map_nothing_mapped(self): |
| 43 | + identity_map_input = IdentityMapInput.from_emails(["optout@example.com"]) |
| 44 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 45 | + self.assert_unmapped(response, "optout", "optout@example.com") |
| 46 | + |
| 47 | + def test_identity_map_invalid_email(self): |
| 48 | + self.assertRaises(ValueError, IdentityMapInput.from_emails, |
| 49 | + ["email@example.com", "this is not an email"]) |
| 50 | + |
| 51 | + def test_identity_map_invalid_phone(self): |
| 52 | + self.assertRaises(ValueError, IdentityMapInput.from_phones, |
| 53 | + ["+12345678901", "this is not a phone number"]) |
| 54 | + |
| 55 | + def test_identity_map_invalid_hashed_email(self): |
| 56 | + identity_map_input = IdentityMapInput.from_hashed_emails(["this is not a hashed email"]) |
| 57 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 58 | + self.assert_unmapped(response, "invalid identifier", "this is not a hashed email") |
| 59 | + |
| 60 | + def test_identity_map_invalid_hashed_phone(self): |
| 61 | + identity_map_input = IdentityMapInput.from_hashed_emails(["this is not a hashed phone"]) |
| 62 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 63 | + self.assert_unmapped(response, "invalid identifier", "this is not a hashed phone") |
| 64 | + |
| 65 | + def test_identity_map_hashed_emails(self): |
| 66 | + hashed_email1 = normalize_and_hash_email("hopefully-not-opted-out@example.com") |
| 67 | + hashed_email2 = normalize_and_hash_email("somethingelse@example.com") |
| 68 | + hashed_opted_out_email = normalize_and_hash_email("optout@example.com") |
| 69 | + identity_map_input = IdentityMapInput.from_hashed_emails([hashed_email1, hashed_email2, hashed_opted_out_email]) |
| 70 | + |
| 71 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 72 | + |
| 73 | + self.assert_mapped(response, hashed_email1) |
| 74 | + self.assert_mapped(response, hashed_email2) |
| 75 | + |
| 76 | + self.assert_unmapped(response, "optout", hashed_opted_out_email) |
| 77 | + |
| 78 | + def test_identity_map_duplicate_emails(self): |
| 79 | + identity_map_input = IdentityMapInput.from_emails( |
| 80 | + ["JANE.SAOIRSE@gmail.com", "Jane.Saoirse@gmail.com", "JaneSaoirse+UID2@gmail.com", "janesaoirse@gmail.com", |
| 81 | + "JANE.SAOIRSE@gmail.com"]) |
| 82 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 83 | + |
| 84 | + mapped_identities = response.mapped_identities |
| 85 | + self.assertEqual(4, len(mapped_identities)) |
| 86 | + |
| 87 | + raw_uid = mapped_identities.get("JANE.SAOIRSE@gmail.com").get_raw_uid() |
| 88 | + self.assertEqual(raw_uid, mapped_identities.get("Jane.Saoirse@gmail.com").get_raw_uid()) |
| 89 | + self.assertEqual(raw_uid, mapped_identities.get("JaneSaoirse+UID2@gmail.com").get_raw_uid()) |
| 90 | + self.assertEqual(raw_uid, mapped_identities.get("janesaoirse@gmail.com").get_raw_uid()) |
| 91 | + |
| 92 | + def test_identity_map_duplicate_hashed_emails(self): |
| 93 | + hashed_email = normalize_and_hash_email("hopefully-not-opted-out@example.com") |
| 94 | + duplicate_hashed_email = hashed_email |
| 95 | + hashed_opted_out_email = normalize_and_hash_email("optout@example.com") |
| 96 | + duplicate_hashed_opted_out_email = hashed_opted_out_email |
| 97 | + |
| 98 | + identity_map_input = IdentityMapInput.from_hashed_emails( |
| 99 | + [hashed_email, duplicate_hashed_email, hashed_opted_out_email, duplicate_hashed_opted_out_email]) |
| 100 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 101 | + |
| 102 | + self.assert_mapped(response, hashed_email) |
| 103 | + self.assert_mapped(response, duplicate_hashed_email) |
| 104 | + |
| 105 | + self.assert_unmapped(response, "optout", hashed_opted_out_email) |
| 106 | + self.assert_unmapped(response, "optout", duplicate_hashed_opted_out_email) |
| 107 | + |
| 108 | + def test_identity_map_empty_input(self): |
| 109 | + identity_map_input = IdentityMapInput.from_emails([]) |
| 110 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 111 | + self.assertTrue(len(response.mapped_identities) == 0) |
| 112 | + self.assertTrue(len(response.unmapped_identities) == 0) |
| 113 | + |
| 114 | + def test_identity_map_phones(self): |
| 115 | + identity_map_input = IdentityMapInput.from_phones(["+12345678901", "+98765432109", "+00000000000"]) |
| 116 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 117 | + self.assert_mapped(response, "+12345678901") |
| 118 | + self.assert_mapped(response, "+98765432109") |
| 119 | + |
| 120 | + self.assert_unmapped(response, "optout", "+00000000000") |
| 121 | + |
| 122 | + def test_identity_map_hashed_phones(self): |
| 123 | + hashed_phone1 = normalize_and_hash_phone("+12345678901") |
| 124 | + hashed_phone2 = normalize_and_hash_phone("+98765432109") |
| 125 | + hashed_opted_out_phone = normalize_and_hash_phone("+00000000000") |
| 126 | + identity_map_input = IdentityMapInput.from_hashed_phones([hashed_phone1, hashed_phone2, hashed_opted_out_phone]) |
| 127 | + response = self.identity_map_client.generate_identity_map(identity_map_input) |
| 128 | + self.assert_mapped(response, hashed_phone1) |
| 129 | + self.assert_mapped(response, hashed_phone2) |
| 130 | + |
| 131 | + self.assert_unmapped(response, "optout", hashed_opted_out_phone) |
| 132 | + |
| 133 | + def test_identity_map_bad_url(self): |
| 134 | + identity_map_input = IdentityMapInput.from_emails( |
| 135 | + ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) |
| 136 | + client = IdentityMapClient("https://operator-bad-url.uidapi.com", os.getenv("UID2_API_KEY"), os.getenv("UID2_SECRET_KEY")) |
| 137 | + self.assertRaises(URLError, client.generate_identity_map, identity_map_input) |
| 138 | + |
| 139 | + def test_identity_map_bad_api_key(self): |
| 140 | + identity_map_input = IdentityMapInput.from_emails( |
| 141 | + ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) |
| 142 | + client = IdentityMapClient(os.getenv("UID2_BASE_URL"), "bad-api-key", os.getenv("UID2_SECRET_KEY")) |
| 143 | + self.assertRaises(HTTPError, client.generate_identity_map,identity_map_input) |
| 144 | + |
| 145 | + def test_identity_map_bad_secret(self): |
| 146 | + identity_map_input = IdentityMapInput.from_emails( |
| 147 | + ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) |
| 148 | + client = IdentityMapClient(os.getenv("UID2_BASE_URL"), os.getenv("UID2_API_KEY"), "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=") |
| 149 | + self.assertRaises(HTTPError, client.generate_identity_map, |
| 150 | + identity_map_input) |
| 151 | + |
| 152 | + def assert_mapped(self, response, dii): |
| 153 | + mapped_identity = response.mapped_identities.get(dii) |
| 154 | + self.assertIsNotNone(mapped_identity) |
| 155 | + self.assertIsNotNone(mapped_identity.get_raw_uid()) |
| 156 | + self.assertIsNotNone(mapped_identity.get_bucket_id()) |
| 157 | + |
| 158 | + unmapped_identity = response.unmapped_identities.get(dii) |
| 159 | + self.assertIsNone(unmapped_identity) |
| 160 | + |
| 161 | + def assert_unmapped(self, response, reason, dii): |
| 162 | + unmapped_identity = response.unmapped_identities.get(dii) |
| 163 | + self.assertEqual(reason, unmapped_identity.get_reason()) |
| 164 | + |
| 165 | + mapped_identity = response.mapped_identities.get(dii) |
| 166 | + self.assertIsNone(mapped_identity) |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + unittest.main() |
0 commit comments