-
Notifications
You must be signed in to change notification settings - Fork 3
sch-UID2-5486 added identity map v3 client and binary support #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
68254a1
added identity map v3 client
sophia-chen-ttd b660acb
clean up
sophia-chen-ttd 23659df
fixed typing
sophia-chen-ttd 97bf9b7
added validation to api response
sophia-chen-ttd fcd1cf4
added validation to api identity
sophia-chen-ttd 80b6d26
enabling tests in pipeline
sophia-chen-ttd 647ecea
adding back vulnerability_scan_only true
sophia-chen-ttd f84941c
fix indentation
sophia-chen-ttd 3c41f5b
styling
sophia-chen-ttd 09aa42a
added e2e tests for bidstream client and sharing client
sophia-chen-ttd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| import os | ||
| import unittest | ||
|
|
||
| from datetime import datetime, timedelta, timezone | ||
| from urllib.error import URLError, HTTPError | ||
|
|
||
| from uid2_client import IdentityMapV3Client, IdentityMapV3Input, IdentityMapV3Response, normalize_and_hash_email, normalize_and_hash_phone | ||
| from uid2_client.unmapped_identity_reason import UnmappedIdentityReason | ||
|
|
||
|
|
||
| @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 IdentityMapV3IntegrationTests(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 = IdentityMapV3Client(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 = IdentityMapV3Input.from_emails( | ||
| ["[email protected]", "[email protected]", "[email protected]"]) | ||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
| self.assert_mapped(response, "[email protected]") | ||
| self.assert_mapped(response, "[email protected]") | ||
|
|
||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, "[email protected]") | ||
|
|
||
| def test_identity_map_nothing_unmapped(self): | ||
| identity_map_input = IdentityMapV3Input.from_emails( | ||
| ["[email protected]", "[email protected]"]) | ||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
| self.assert_mapped(response, "[email protected]") | ||
| self.assert_mapped(response, "[email protected]") | ||
|
|
||
| def test_identity_map_nothing_mapped(self): | ||
| identity_map_input = IdentityMapV3Input.from_emails(["[email protected]"]) | ||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, "[email protected]") | ||
|
|
||
| def test_identity_map_invalid_email(self): | ||
| self.assertRaises(ValueError, IdentityMapV3Input.from_emails, | ||
| ["[email protected]", "this is not an email"]) | ||
|
|
||
| def test_identity_map_invalid_phone(self): | ||
| self.assertRaises(ValueError, IdentityMapV3Input.from_phones, | ||
| ["+12345678901", "this is not a phone number"]) | ||
|
|
||
| def test_identity_map_invalid_hashed_email(self): | ||
| identity_map_input = IdentityMapV3Input.from_hashed_emails(["this is not a hashed email"]) | ||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.INVALID_IDENTIFIER, "this is not a hashed email") | ||
|
|
||
| def test_identity_map_invalid_hashed_phone(self): | ||
| identity_map_input = IdentityMapV3Input.from_hashed_phones(["this is not a hashed phone"]) | ||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.INVALID_IDENTIFIER, "this is not a hashed phone") | ||
|
|
||
| def test_identity_map_hashed_emails(self): | ||
| hashed_email1 = normalize_and_hash_email("[email protected]") | ||
| hashed_email2 = normalize_and_hash_email("[email protected]") | ||
| hashed_opted_out_email = normalize_and_hash_email("[email protected]") | ||
| identity_map_input = IdentityMapV3Input.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, UnmappedIdentityReason.OPTOUT, hashed_opted_out_email) | ||
|
|
||
| def test_identity_map_duplicate_emails(self): | ||
| identity_map_input = IdentityMapV3Input.from_emails( | ||
| ["[email protected]", "[email protected]", "[email protected]", "[email protected]", | ||
| "[email protected]"]) | ||
| 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("[email protected]").current_raw_uid | ||
| self.assertEqual(raw_uid, mapped_identities.get("[email protected]").current_raw_uid) | ||
| self.assertEqual(raw_uid, mapped_identities.get("[email protected]").current_raw_uid) | ||
| self.assertEqual(raw_uid, mapped_identities.get("[email protected]").current_raw_uid) | ||
|
|
||
| def test_identity_map_duplicate_hashed_emails(self): | ||
| hashed_email = normalize_and_hash_email("[email protected]") | ||
| duplicate_hashed_email = hashed_email | ||
| hashed_opted_out_email = normalize_and_hash_email("[email protected]") | ||
| duplicate_hashed_opted_out_email = hashed_opted_out_email | ||
|
|
||
| identity_map_input = IdentityMapV3Input.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, UnmappedIdentityReason.OPTOUT, hashed_opted_out_email) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, duplicate_hashed_opted_out_email) | ||
|
|
||
| def test_identity_map_empty_input(self): | ||
| identity_map_input = IdentityMapV3Input.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 = IdentityMapV3Input.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, UnmappedIdentityReason.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 = IdentityMapV3Input.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, UnmappedIdentityReason.OPTOUT, hashed_opted_out_phone) | ||
|
|
||
| def test_identity_map_all_identity_types_in_one_request(self): | ||
| mapped_email = "[email protected]" | ||
| optout_email = "[email protected]" | ||
| mapped_phone = "+12345678901" | ||
| optout_phone = "+00000000000" | ||
|
|
||
| mapped_email_hash = normalize_and_hash_email("[email protected]") | ||
| optout_email_hash = normalize_and_hash_email(optout_email) | ||
| mapped_phone_hash = normalize_and_hash_phone(mapped_phone) | ||
| optout_phone_hash = normalize_and_hash_phone(optout_phone) | ||
|
|
||
| identity_map_input = (IdentityMapV3Input.from_emails([mapped_email, optout_email]) | ||
| .with_hashed_emails([mapped_email_hash, optout_email_hash]) | ||
| .with_phones([mapped_phone, optout_phone]) | ||
| .with_hashed_phones([mapped_phone_hash, optout_phone_hash])) | ||
|
|
||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
|
|
||
| # Test mapped identities | ||
| self.assert_mapped(response, mapped_email) | ||
| self.assert_mapped(response, mapped_email_hash) | ||
| self.assert_mapped(response, mapped_phone) | ||
| self.assert_mapped(response, mapped_phone_hash) | ||
|
|
||
| # Test unmapped identities | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, optout_email) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, optout_email_hash) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, optout_phone) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, optout_phone_hash) | ||
|
|
||
| def test_identity_map_all_identity_types_added_one_by_one(self): | ||
| mapped_email = "[email protected]" | ||
| optout_phone = "+00000000000" | ||
| mapped_phone_hash = normalize_and_hash_phone("+12345678901") | ||
| optout_email_hash = normalize_and_hash_email("[email protected]") | ||
|
|
||
| identity_map_input = IdentityMapV3Input() | ||
| identity_map_input.with_email(mapped_email) | ||
| identity_map_input.with_phone(optout_phone) | ||
| identity_map_input.with_hashed_phone(mapped_phone_hash) | ||
| identity_map_input.with_hashed_email(optout_email_hash) | ||
|
|
||
| response = self.identity_map_client.generate_identity_map(identity_map_input) | ||
|
|
||
| # Test mapped identities | ||
| self.assert_mapped(response, mapped_email) | ||
| self.assert_mapped(response, mapped_phone_hash) | ||
|
|
||
| # Test unmapped identities | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, optout_phone) | ||
| self.assert_unmapped(response, UnmappedIdentityReason.OPTOUT, optout_email_hash) | ||
|
|
||
| def test_identity_map_client_bad_url(self): | ||
| identity_map_input = IdentityMapV3Input.from_emails( | ||
| ["[email protected]", "[email protected]", "[email protected]"]) | ||
| client = IdentityMapV3Client("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) | ||
|
|
||
| def test_identity_map_client_bad_api_key(self): | ||
| identity_map_input = IdentityMapV3Input.from_emails( | ||
| ["[email protected]", "[email protected]", "[email protected]"]) | ||
| client = IdentityMapV3Client(os.getenv("UID2_BASE_URL"), "bad-api-key", os.getenv("UID2_SECRET_KEY")) | ||
| self.assertRaises(HTTPError, client.generate_identity_map, identity_map_input) | ||
|
|
||
| def test_identity_map_client_bad_secret(self): | ||
| identity_map_input = IdentityMapV3Input.from_emails( | ||
| ["[email protected]", "[email protected]", "[email protected]"]) | ||
|
|
||
| client = IdentityMapV3Client(os.getenv("UID2_BASE_URL"), os.getenv("UID2_API_KEY"), "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=") | ||
| self.assertRaises(HTTPError, client.generate_identity_map, identity_map_input) | ||
|
|
||
| def assert_mapped(self, response: IdentityMapV3Response, dii): | ||
| mapped_identity = response.mapped_identities.get(dii) | ||
| self.assertIsNotNone(mapped_identity) | ||
| self.assertIsNotNone(mapped_identity.current_raw_uid) | ||
|
|
||
| # Refresh from should be now or in the future, allow some slack for time between request and this assertion | ||
| one_minute_ago = datetime.now(timezone.utc) - timedelta(seconds=60) | ||
| self.assertTrue(mapped_identity.refresh_from > one_minute_ago) | ||
|
|
||
| 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.reason) | ||
|
|
||
| mapped_identity = response.mapped_identities.get(dii) | ||
| self.assertIsNone(mapped_identity) | ||
|
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is a good idea, we should break if those are missing instead. Otherwise we may have those tests skipped permanently in the pipeline without anyone noticing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these are integration tests, are they running in the pipeline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it doesn't look like they've been running in the pipeline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious, what prevents them from running in the pipeline? (if not this
skipIf) ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed all the @unittest.skipIf(), now the tests require the env variables to set
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah its because we've set the 'vulnerability_scan_only' param to true in our workflow so it will only run the vulnerability scan. I think if we want to turn this off and run the tests in the pipeline, we'll need to keep the unittest.skipIf or set the env variables in the workflow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we try to turn off
vulnerability_scan_only? (and keepskipIf)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just tried it but it didn't work because the shared testing pipeline only works for java tests. I'm thinking to keep the vulnerability_scan_only on but then write a custom testing job in this repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah right I found UID2-3648 for that so we can leave it for now (skipIf should remain though)