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

Fix: Re-enrollment #1804

Merged
merged 2 commits into from
Dec 1, 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
16 changes: 5 additions & 11 deletions benefits/enrollment/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, response):
class GroupResponse:
"""Benefits Enrollment Customer Group API response."""

def __init__(self, response, requested_id, payload=None):
def __init__(self, response, requested_id, group_id, payload=None):
if payload is None:
try:
payload = response.json()
Expand All @@ -74,18 +74,12 @@ def __init__(self, response, requested_id, payload=None):
else:
try:
# Group API uses an error response (500) to indicate that the customer already exists in the group (!!!)
# The error message should contain the customer ID we sent via payload and start with "Duplicate"
# The error message should contain the customer ID and group ID we sent via payload
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

error = response.json()["errors"][0]
customer_id = payload[0]
detail = error["detail"]

failure = (
customer_id is None
or detail is None
or customer_id not in detail
or customer_id in detail
and not detail.startswith("Duplicate")
)
failure = customer_id is None or detail is None or not (customer_id in detail and group_id in detail)

if failure:
raise ApiError("Invalid response format")
Expand Down Expand Up @@ -269,10 +263,10 @@ def enroll(self, customer_token, group_id):

if r.status_code in (200, 201):
logger.info("Customer enrolled in group")
return GroupResponse(r, customer.id)
return GroupResponse(r, customer.id, group_id)
elif r.status_code == 500:
logger.info("Customer already exists in group")
return GroupResponse(r, customer.id, payload=payload)
return GroupResponse(r, customer.id, group_id, payload=payload)
else:
r.raise_for_status()
except requests.ConnectionError:
Expand Down
24 changes: 12 additions & 12 deletions tests/pytest/enrollment/test_api_GroupResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def test_no_payload_invalid_response(mocker):
mock_response.json.side_effect = ValueError

with pytest.raises(ApiError, match=r"response"):
GroupResponse(mock_response, 0)
GroupResponse(mock_response, "customer", "group")


def test_no_payload_valid_response_single_matching_id(mocker):
mock_response = mocker.Mock()
mock_response.json.return_value = ["0"]

response = GroupResponse(mock_response, "0")
response = GroupResponse(mock_response, "0", "group")

assert response.customer_ids == ["0"]
assert response.updated_customer_id == "0"
Expand All @@ -27,7 +27,7 @@ def test_no_payload_valid_response_single_unmatching_id(mocker):
mock_response = mocker.Mock()
mock_response.json.return_value = ["1"]

response = GroupResponse(mock_response, "0")
response = GroupResponse(mock_response, "0", "group")

assert response.customer_ids == ["1"]
assert response.updated_customer_id == "1"
Expand All @@ -39,7 +39,7 @@ def test_no_payload_valid_response_multiple_ids(mocker):
mock_response = mocker.Mock()
mock_response.json.return_value = ["0", "1"]

response = GroupResponse(mock_response, "0")
response = GroupResponse(mock_response, "0", "group")

assert response.customer_ids == ["0", "1"]
assert not response.updated_customer_id
Expand All @@ -53,14 +53,14 @@ def test_payload_invalid_response(mocker, exception):
mock_response.json.side_effect = exception

with pytest.raises(ApiError, match=r"response"):
GroupResponse(mock_response, "0", [])
GroupResponse(mock_response, "0", "group", [])


def test_payload_valid_response(mocker):
mock_response = mocker.Mock()
mock_response.json.return_value = {"errors": [{"detail": "Duplicate id 0"}]}
mock_response.json.return_value = {"errors": [{"detail": "0 group"}]}

response = GroupResponse(mock_response, "0", ["0"])
response = GroupResponse(mock_response, "0", "group", ["0"])

assert response.customer_ids == ["0"]
assert response.updated_customer_id == "0"
Expand All @@ -69,13 +69,13 @@ def test_payload_valid_response(mocker):


failure_conditions = [
# customer_id is None
({"detail": "Duplicate"}, [None]),
# detail is None
({"detail": None}, ["0"]),
# customer_id is None
({"detail": "0 group"}, [None]),
# customer_id not in detail
({"detail": "1"}, ["0"]),
# customer_id in detail, detail doesn't start with Duplicate
({"detail": "1 group"}, ["0"]),
# group_id not in detail
({"detail": "0"}, ["0"]),
]

Expand All @@ -86,4 +86,4 @@ def test_payload_failure_response(mocker, error, payload):
mock_response.json.return_value = {"errors": [error]}

with pytest.raises(ApiError, match=r"response"):
GroupResponse(mock_response, "0", payload)
GroupResponse(mock_response, "0", "group", payload)
Loading