Skip to content
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

Block PO Boxes #549

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions microsetta_private_api/util/melissa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand Down Expand Up @@ -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"]
Expand Down
76 changes: 76 additions & 0 deletions microsetta_private_api/util/tests/test_melissa.py
Original file line number Diff line number Diff line change
@@ -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()
Loading