Skip to content

Commit

Permalink
Deploy 2023.12.1 to test (#1806)
Browse files Browse the repository at this point in the history
  • Loading branch information
angela-tran committed Dec 1, 2023
2 parents 10f8a0b + ce953c9 commit 36fd3ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
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
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "benefits"
version = "2023.09.1"
version = "2023.12.1"
description = "Cal-ITP Benefits is an application that enables automated eligibility verification and enrollment for transit benefits onto customers’ existing contactless bank (credit/debit) cards."
readme = "README.md"
license = { file = "LICENSE" }
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)

0 comments on commit 36fd3ba

Please sign in to comment.