Skip to content

Commit

Permalink
Fix missing import (#2118)
Browse files Browse the repository at this point in the history
* Fix missing import

- Added tests to cover a blind spot that caused this issue to slip by in the first place

* Use service we already fetched not authenticated_service

* Remove noqa comments
  • Loading branch information
whabanks authored Feb 23, 2024
1 parent 9d1d445 commit c4489b8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
6 changes: 2 additions & 4 deletions app/job/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,10 @@ def create_job(service_id):

elif template.template_type == EMAIL_TYPE:
check_email_daily_limit(service, len(list(recipient_csv.get_rows())))
scheduled_for = datetime.fromisoformat(form.get("scheduled_for")) if form.get("scheduled_for") else None # noqa: F821
scheduled_for = datetime.fromisoformat(data.get("scheduled_for")) if data.get("scheduled_for") else None

if scheduled_for is None or not scheduled_for.date() > datetime.today().date():
increment_email_daily_count_send_warnings_if_needed(
authenticated_service, len(list(recipient_csv.get_rows())) # noqa: F821
)
increment_email_daily_count_send_warnings_if_needed(service, len(list(recipient_csv.get_rows())))

data.update({"template_version": template.version})

Expand Down
65 changes: 65 additions & 0 deletions tests/app/job/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,71 @@ def test_cancel_letter_job_does_not_call_cancel_if_can_letter_job_be_cancelled_r
assert response["message"] == "Sorry, it's too late, letters have already been sent."


def test_create_unscheduled_email_job_increments_daily_count(client, mocker, sample_email_job, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch("app.job.rest.increment_email_daily_count_send_warnings_if_needed")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": sample_email_job.template_id,
"original_file_name": sample_email_job.original_file_name,
"notification_count": "1",
"valid": "True",
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="email address\r\n[email protected]",
)
mocker.patch("app.dao.services_dao.dao_fetch_service_by_id", return_value=sample_email_job.service)
data = {
"id": fake_uuid,
"created_by": str(sample_email_job.created_by.id),
}
path = "/service/{}/job".format(sample_email_job.service_id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]

response = client.post(path, data=json.dumps(data), headers=headers)

assert response.status_code == 201

app.celery.tasks.process_job.apply_async.assert_called_once_with(([str(fake_uuid)]), queue="job-tasks")
app.job.rest.increment_email_daily_count_send_warnings_if_needed.assert_called_once_with(sample_email_job.service, 1)


def test_create_future_not_same_day_scheduled_email_job_does_not_increment_daily_count(
client, mocker, sample_email_job, fake_uuid
):
scheduled_date = (datetime.utcnow() + timedelta(hours=36, minutes=59)).isoformat()
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch("app.job.rest.increment_email_daily_count_send_warnings_if_needed")
mocker.patch(
"app.job.rest.get_job_metadata_from_s3",
return_value={
"template_id": sample_email_job.template_id,
"original_file_name": sample_email_job.original_file_name,
"notification_count": "1",
"valid": "True",
},
)
mocker.patch(
"app.job.rest.get_job_from_s3",
return_value="email address\r\n[email protected]",
)
mocker.patch("app.dao.services_dao.dao_fetch_service_by_id", return_value=sample_email_job.service)
data = {"id": fake_uuid, "created_by": str(sample_email_job.created_by.id), "scheduled_for": scheduled_date}
path = "/service/{}/job".format(sample_email_job.service_id)
auth_header = create_authorization_header()
headers = [("Content-Type", "application/json"), auth_header]

response = client.post(path, data=json.dumps(data), headers=headers)

assert response.status_code == 201

app.job.rest.increment_email_daily_count_send_warnings_if_needed.assert_not_called()


def test_create_unscheduled_job(client, sample_template, mocker, fake_uuid):
mocker.patch("app.celery.tasks.process_job.apply_async")
mocker.patch(
Expand Down

0 comments on commit c4489b8

Please sign in to comment.