diff --git a/app/celery/tasks.py b/app/celery/tasks.py index 14f0d6314..4dab98e9f 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -9,7 +9,7 @@ from sqlalchemy.exc import SQLAlchemyError @notify_celery.task(name="send-sms") -def send_sms(service_id, notification_id, encrypted_notification, job_id=None): +def send_sms(service_id, notification_id, encrypted_notification): notification = encryption.decrypt(encrypted_notification) template = get_model_templates(notification['template']) @@ -19,7 +19,7 @@ def send_sms(service_id, notification_id, encrypted_notification, job_id=None): template_id=notification['template'], to=notification['to'], service_id=service_id, - job_id=job_id, + job_id=notification.get('job', None), status='sent' ) save_notification(notification_db_object) diff --git a/app/job/rest.py b/app/job/rest.py index c3e4cf772..9ba4a7600 100644 --- a/app/job/rest.py +++ b/app/job/rest.py @@ -41,12 +41,12 @@ def get_job_for_service(service_id, job_id=None): if job_id: try: job = get_job(service_id, job_id) + if not job: + return jsonify(result="error", message="Job not found"), 404 data, errors = job_schema.dump(job) return jsonify(data=data) except DataError: return jsonify(result="error", message="Invalid job id"), 400 - except NoResultFound: - return jsonify(result="error", message="Job not found"), 404 else: jobs = get_jobs_by_service(service_id) data, errors = jobs_schema.dump(jobs) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index d0c8a8dae..ae06a9703 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -105,8 +105,7 @@ def create_sms_for_service(service_id): send_sms.apply_async(( api_user['client'], notification_id, - encryption.encrypt(notification), - job_id), + encryption.encrypt(notification)), queue='sms') return jsonify({'notification_id': notification_id}), 201 diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index e252cfd09..a9207ca4e 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -31,6 +31,32 @@ def test_should_send_template_to_correct_sms_provider_and_persist(sample_templat assert persisted_notification.to == '+441234123123' assert persisted_notification.template_id == sample_template.id assert persisted_notification.status == 'sent' + assert not persisted_notification.job_id + + +def test_should_send_template_to_correct_sms_provider_and_persist_with_job_id(sample_job, mocker): + notification = { + "template": sample_job.template.id, + "job": sample_job.id, + "to": "+441234123123" + } + mocker.patch('app.encryption.decrypt', return_value=notification) + mocker.patch('app.firetext_client.send_sms') + + notification_id = uuid.uuid4() + + send_sms( + sample_job.service.id, + notification_id, + "encrypted-in-reality") + + firetext_client.send_sms.assert_called_once_with("+441234123123", sample_job.template.content) + persisted_notification = notifications_dao.get_notification(sample_job.template.service_id, notification_id) + assert persisted_notification.id == notification_id + assert persisted_notification.to == '+441234123123' + assert persisted_notification.job_id == sample_job.id + assert persisted_notification.template_id == sample_job.template.id + assert persisted_notification.status == 'sent' def test_should_send_template_to_email_provider_and_persist(sample_email_template, mocker): diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index b4914a82c..9ec412eb8 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -1,6 +1,5 @@ import uuid import app.celery.tasks -import moto from tests import create_authorization_header from flask import json from app.models import Service @@ -484,8 +483,7 @@ def test_should_allow_valid_sms_notification_for_job(notify_api, sample_job, moc app.celery.tasks.send_sms.apply_async.assert_called_once_with( (str(sample_job.template.service_id), notification_id, - "something_encrypted", - str(sample_job.id)), + "something_encrypted"), queue="sms" ) assert response.status_code == 201