Merge pull request #1032 from GSA/notify-api-769b

convert one-off sends to jobs
This commit is contained in:
Carlo Costino
2023-12-21 17:39:20 -05:00
committed by GitHub
6 changed files with 222 additions and 71 deletions

View File

@@ -30,6 +30,63 @@ from tests.conftest import (
normalize_spaces,
)
FAKE_ONE_OFF_NOTIFICATION = {
"links": {},
"notifications": [
{
"api_key": None,
"billable_units": 0,
"carrier": None,
"client_reference": None,
"created_at": "2023-12-14T20:35:55+00:00",
"created_by": {
"email_address": "grsrbsrgsrf@fake.gov",
"id": "de059e0a-42e5-48bb-939e-4f76804ab739",
"name": "grsrbsrgsrf",
},
"document_download_count": None,
"id": "a3442b43-0ba1-4854-9e0a-d2fba1cc9b81",
"international": False,
"job": {
"id": "55b242b5-9f62-4271-aff7-039e9c320578",
"original_file_name": "1127b78e-a4a8-4b70-8f4f-9f4fbf03ece2.csv",
},
"job_row_number": 0,
"key_name": None,
"key_type": "normal",
"normalised_to": "+16615555555",
"notification_type": "sms",
"personalisation": {
"dayofweek": "2",
"favecolor": "3",
"phonenumber": "+16615555555",
},
"phone_prefix": "1",
"provider_response": None,
"rate_multiplier": 1.0,
"reference": None,
"reply_to_text": "development",
"sent_at": None,
"sent_by": None,
"service": "f62d840f-8bcb-4b36-b959-4687e16dd1a1",
"status": "created",
"template": {
"content": "((day of week)) and ((fave color))",
"id": "bd9caa7e-00ee-4c5a-839e-10ae1a7e6f73",
"name": "personalized",
"redact_personalisation": False,
"subject": None,
"template_type": "sms",
"version": 1,
},
"to": "+16615555555",
"updated_at": None,
}
],
"page_size": 50,
"total": 1,
}
template_types = ["email", "sms"]
unchanging_fake_uuid = uuid.uuid4()
@@ -2060,10 +2117,19 @@ def test_route_permissions_send_check_notifications(
route,
response_code,
method,
mock_create_job,
mock_s3_upload,
):
with client_request.session_transaction() as session:
session["recipient"] = "2028675301"
session["placeholders"] = {"name": "a"}
mocker.patch("app.main.views.send.check_messages")
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=FAKE_ONE_OFF_NOTIFICATION,
)
validate_route_permission_with_client(
mocker,
client_request,
@@ -2578,28 +2644,31 @@ def test_check_notification_shows_preview(
def test_send_notification_submits_data(
client_request,
fake_uuid,
mock_send_notification,
mock_get_service_template,
template,
recipient,
placeholders,
expected_personalisation,
mocker,
mock_create_job,
mock_s3_upload,
):
with client_request.session_transaction() as session:
session["recipient"] = recipient
session["placeholders"] = placeholders
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=FAKE_ONE_OFF_NOTIFICATION,
)
mocker.patch("app.main.views.send.check_messages", return_value="")
client_request.post(
"main.send_notification", service_id=SERVICE_ONE_ID, template_id=fake_uuid
)
mock_send_notification.assert_called_once_with(
SERVICE_ONE_ID,
template_id=fake_uuid,
recipient=recipient,
personalisation=expected_personalisation,
sender_id=None,
)
mock_create_job.assert_called_once()
def test_send_notification_clears_session(
@@ -2608,11 +2677,20 @@ def test_send_notification_clears_session(
fake_uuid,
mock_send_notification,
mock_get_service_template,
mocker,
mock_create_job,
mock_s3_upload,
):
with client_request.session_transaction() as session:
session["recipient"] = "2028675301"
session["placeholders"] = {"a": "b"}
mocker.patch("app.main.views.send.check_messages")
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=FAKE_ONE_OFF_NOTIFICATION,
)
client_request.post(
"main.send_notification", service_id=service_one["id"], template_id=fake_uuid
)
@@ -2661,22 +2739,26 @@ def test_send_notification_redirects_to_view_page(
mock_get_service_template,
extra_args,
extra_redirect_args,
mocker,
mock_create_job,
mock_s3_upload,
):
with client_request.session_transaction() as session:
session["recipient"] = "2028675301"
session["placeholders"] = {"a": "b"}
mocker.patch("app.main.views.send.check_messages")
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=FAKE_ONE_OFF_NOTIFICATION,
)
client_request.post(
"main.send_notification",
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
_expected_status=302,
_expected_redirect=url_for(
".view_notification",
service_id=SERVICE_ONE_ID,
notification_id=fake_uuid,
**extra_redirect_args,
),
**extra_args,
)
@@ -2715,13 +2797,24 @@ def test_send_notification_shows_error_if_400(
fake_uuid,
mocker,
mock_get_service_template_with_placeholders,
mock_create_job,
exception_msg,
expected_h1,
expected_err_details,
mock_s3_upload,
):
class MockHTTPError(HTTPError):
message = exception_msg
mocker.patch(
"app.main.views.send.check_messages",
)
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=FAKE_ONE_OFF_NOTIFICATION,
)
mocker.patch(
"app.notification_api_client.send_notification",
side_effect=MockHTTPError(),
@@ -2730,18 +2823,14 @@ def test_send_notification_shows_error_if_400(
session["recipient"] = "2028675301"
session["placeholders"] = {"name": "a" * 900}
# This now redirects to the jobs results page
page = client_request.post(
"main.send_notification",
service_id=service_one["id"],
template_id=fake_uuid,
_expected_status=200,
_expected_status=302,
)
assert normalize_spaces(page.select(".banner-dangerous h1")[0].text) == expected_h1
assert (
normalize_spaces(page.select(".banner-dangerous p")[0].text)
== expected_err_details
)
assert not page.find("input[type=submit]")
@@ -2750,11 +2839,19 @@ def test_send_notification_shows_email_error_in_trial_mode(
fake_uuid,
mocker,
mock_get_service_email_template,
mock_create_job,
mock_s3_upload,
):
class MockHTTPError(HTTPError):
message = TRIAL_MODE_MSG
status_code = 400
mocker.patch("app.main.views.send.check_messages")
mocker.patch(
"app.notification_api_client.get_notifications_for_service",
return_value=FAKE_ONE_OFF_NOTIFICATION,
)
mocker.patch(
"app.notification_api_client.send_notification",
side_effect=MockHTTPError(),
@@ -2763,18 +2860,12 @@ def test_send_notification_shows_email_error_in_trial_mode(
session["recipient"] = "test@example.com"
session["placeholders"] = {"date": "foo", "thing": "bar"}
page = client_request.post(
# Calling this means we successful ran a job so we will be redirect to the jobs page
client_request.post(
"main.send_notification",
service_id=SERVICE_ONE_ID,
template_id=fake_uuid,
_expected_status=200,
)
assert normalize_spaces(page.select(".banner-dangerous h1")[0].text) == (
"You cannot send to this email address"
)
assert normalize_spaces(page.select(".banner-dangerous p")[0].text) == (
"In trial mode you can only send to yourself and members of your team"
_expected_status=302,
)

View File

@@ -1396,7 +1396,15 @@ def mock_check_verify_code_code_expired(mocker):
@pytest.fixture()
def mock_create_job(mocker, api_user_active):
def _create(job_id, service_id, scheduled_for=None):
def _create(
job_id,
service_id,
scheduled_for=None,
template_id=None,
original_file_name=None,
notification_count=None,
valid=None,
):
return job_json(
service_id,
api_user_active,