|
7 | 7 | import requests # noqa: F401
|
8 | 8 | import requests_mock # noqa: F401
|
9 | 9 | from recap_email.app import app # pylint: disable=import-error
|
| 10 | +from recap_email.app.pacer import pacer_to_cl_ids |
| 11 | +from tests.unit.utils import MockResponse |
10 | 12 |
|
11 | 13 |
|
12 | 14 | @pytest.fixture()
|
@@ -112,6 +114,17 @@ def test_multiple_domains_success():
|
112 | 114 | assert app.check_valid_domain(email) == 1
|
113 | 115 |
|
114 | 116 |
|
| 117 | +def test_get_cl_court_id_using_mapping(): |
| 118 | + for pacer_id, expected_cl in pacer_to_cl_ids.items(): |
| 119 | + email = { |
| 120 | + "common_headers": {"from": [f"ecfnotices@{pacer_id}.uscourts.gov"]} |
| 121 | + } |
| 122 | + result = app.get_cl_court_id(email) |
| 123 | + assert ( |
| 124 | + result == expected_cl |
| 125 | + ), f"For PACER id '{pacer_id}', expected CL id '{expected_cl}' but got '{result}'." |
| 126 | + |
| 127 | + |
115 | 128 | @pytest.fixture()
|
116 | 129 | def valid_domain_failure_ses_event():
|
117 | 130 | with open("./events/ses-valid-domain-failure.json") as file:
|
@@ -208,3 +221,61 @@ def test_success(
|
208 | 221 | assert response["statusCode"] == 200
|
209 | 222 | assert "mail" in data
|
210 | 223 | assert "receipt" in data
|
| 224 | + |
| 225 | + |
| 226 | +@mock.patch.dict( |
| 227 | + os.environ, |
| 228 | + { |
| 229 | + "RECAP_EMAIL_ENDPOINT": "http://host.docker.internal:8000/api/rest/v3/recap-email/", |
| 230 | + "AUTH_TOKEN": "************************", |
| 231 | + }, |
| 232 | +) |
| 233 | +def test_request_court_field_actual_value(pacer_event_two, requests_mock): |
| 234 | + """Confirm that the court_id in the request uses the right value |
| 235 | + from map_pacer_to_cl_id""" |
| 236 | + requests_mock.post( |
| 237 | + "http://host.docker.internal:8000/api/rest/v3/recap-email/", |
| 238 | + json={"mail": {}, "receipt": {}}, |
| 239 | + ) |
| 240 | + |
| 241 | + response = app.handler(pacer_event_two, "") |
| 242 | + assert response["statusCode"] == 200 |
| 243 | + |
| 244 | + # Retrieve the request that made by send_to_court_listener |
| 245 | + request = requests_mock.request_history[0] |
| 246 | + body = json.loads(request.body) |
| 247 | + assert ( |
| 248 | + body.get("court") == "mowd" |
| 249 | + ), f"Expected 'mowd', but got '{body.get('court')}'" |
| 250 | + |
| 251 | + |
| 252 | +@mock.patch.dict( |
| 253 | + os.environ, |
| 254 | + { |
| 255 | + "RECAP_EMAIL_ENDPOINT": "http://host.docker.internal:8000/api/rest/v3/recap-email/", |
| 256 | + "AUTH_TOKEN": "************************", |
| 257 | + }, |
| 258 | +) |
| 259 | +def test_report_request_for_invalid_court(pacer_event_one, requests_mock): |
| 260 | + """Confirm that if an invalid court_id is sent to CL, an error event is |
| 261 | + sent to Sentry.""" |
| 262 | + |
| 263 | + mock_response = MockResponse( |
| 264 | + 400, {"court": ['Invalid pk "whla" - object does not exist.']} |
| 265 | + ) |
| 266 | + with ( |
| 267 | + mock.patch( |
| 268 | + "recap_email.app.app.requests.post", return_value=mock_response |
| 269 | + ), |
| 270 | + mock.patch( |
| 271 | + "recap_email.app.app.sentry_sdk.capture_message" |
| 272 | + ) as mock_sentry_capture, |
| 273 | + ): |
| 274 | + requests_mock.post( |
| 275 | + "http://host.docker.internal:8000/api/rest/v3/recap-email/", |
| 276 | + json={"mail": {}, "receipt": {}}, |
| 277 | + ) |
| 278 | + app.handler(pacer_event_one, "") |
| 279 | + # The expected error message should be sent to Sentry. |
| 280 | + expected_error = "Invalid court pk: whla" |
| 281 | + mock_sentry_capture.assert_called_with(expected_error, level="error") |
0 commit comments