From c54ce5c04096785eedcef04b9ea94c74e575734d Mon Sep 17 00:00:00 2001 From: Cassidy Symons Date: Thu, 30 Nov 2023 12:35:33 -0800 Subject: [PATCH] Block PO boxes + add Melissa unit tests --- microsetta_private_api/util/melissa.py | 26 +++++-- .../util/tests/test_melissa.py | 76 +++++++++++++++++++ 2 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 microsetta_private_api/util/tests/test_melissa.py diff --git a/microsetta_private_api/util/melissa.py b/microsetta_private_api/util/melissa.py index bef1f8ae1..e8235f32a 100644 --- a/microsetta_private_api/util/melissa.py +++ b/microsetta_private_api/util/melissa.py @@ -16,15 +16,21 @@ GOOD_CODES_NO_ERROR = ["AV14"] -def verify_address(address_1, address_2=None, address_3=None, city=None, - state=None, postal=None, country=None): +def verify_address( + address_1, address_2=None, address_3=None, city=None, state=None, + postal=None, country=None, block_po_boxes=True +): """ Required parameters: address_1, postal, country - Optional parameters: address_2, address_3, city, state + Optional parameters: address_2, address_3, city, state, block_po_boxes Note - postal and country default to None as you can't have non-default - arguments after default arguments, and preserving structural order - makes sense for addresses + arguments after default arguments, and preserving structural order + makes sense for addresses + Note 2 - block_po_boxes defaults to True because our only current use for + Melissa is verifying shipping addresses. If a future use arises + where PO boxes are acceptable, pass block_po_boxes=False into + this function """ if address_1 is None or len(address_1) < 1 or postal is None or\ @@ -122,6 +128,16 @@ def verify_address(address_1, address_2=None, address_3=None, city=None, if r_good_conditional and not r_errors_present: r_good = True + # We can't ship to PO boxes, so we need to block them even if + # the address is otherwise valid. We check for the AddressType + # key, as it's only applicable to US addresses + if block_po_boxes and "AddressType" in record_obj: + if record_obj["AddressType"] == "P": + # Mark the record bad + r_good = False + # Inject a custom error code to indicate why + r_codes += ",AEPOBOX" + r_address_1 = record_obj["AddressLine1"] r_address_2 = record_obj["AddressLine2"] r_address_3 = record_obj["AddressLine3"] diff --git a/microsetta_private_api/util/tests/test_melissa.py b/microsetta_private_api/util/tests/test_melissa.py new file mode 100644 index 000000000..3a23abc07 --- /dev/null +++ b/microsetta_private_api/util/tests/test_melissa.py @@ -0,0 +1,76 @@ +import unittest +from unittest import skipIf + +from microsetta_private_api.config_manager import SERVER_CONFIG +from microsetta_private_api.util.melissa import verify_address + + +class MelissaTests(unittest.TestCase): + @skipIf(SERVER_CONFIG['melissa_license_key'] in + ('', 'qwerty123456'), + "Melissa secrets not provided") + def test_verify_address_valid(self): + # UC San Diego's address is a known and stable valid address + obs = verify_address( + address_1="9500 Gilman Dr", + address_2="", + address_3="", + city="La Jolla", + state="CA", + postal="92093", + country="US" + ) + self.assertTrue(obs['valid']) + + @skipIf(SERVER_CONFIG['melissa_license_key'] in + ('', 'qwerty123456'), + "Melissa secrets not provided") + def test_verify_address_invalid(self): + # Non-existent street address in San Diego + obs = verify_address( + address_1="1234 NotAReal St", + address_2="", + address_3="", + city="San Diego", + state="CA", + postal="92116", + country="US" + ) + self.assertFalse(obs['valid']) + + @skipIf(SERVER_CONFIG['melissa_license_key'] in + ('', 'qwerty123456'), + "Melissa secrets not provided") + def test_verify_address_po_box_good(self): + # Assert that PO boxes will return valid when block_po_boxes=False + obs = verify_address( + address_1="PO Box 9001", + address_2="", + address_3="", + city="San Diego", + state="CA", + postal="92169", + country="US", + block_po_boxes=False + ) + self.assertTrue(obs['valid']) + + @skipIf(SERVER_CONFIG['melissa_license_key'] in + ('', 'qwerty123456'), + "Melissa secrets not provided") + def test_verify_address_po_box_bad(self): + # Assert that PO boxes will return invalid when we omit block_po_boxes + obs = verify_address( + address_1="PO Box 9002", + address_2="", + address_3="", + city="San Diego", + state="CA", + postal="92169", + country="US" + ) + self.assertFalse(obs['valid']) + + +if __name__ == '__main__': + unittest.main()