diff --git a/app/celery/tasks.py b/app/celery/tasks.py index b94b2b272..04b38d847 100644 --- a/app/celery/tasks.py +++ b/app/celery/tasks.py @@ -4,7 +4,7 @@ from flask import current_app from notifications_utils.recipients import ( RecipientCSV ) -from notifications_utils.template import Template +from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate from sqlalchemy.exc import SQLAlchemyError from app import ( create_uuid, @@ -48,9 +48,10 @@ def process_job(job_id): job.job_status = 'in progress' dao_update_job(job) - template = Template( - dao_get_template_by_id(job.template_id, job.template_version).__dict__ - ) + db_template = dao_get_template_by_id(job.template_id, job.template_version) + + TemplateClass = SMSMessageTemplate if db_template.template_type == SMS_TYPE else WithSubjectTemplate + template = TemplateClass(db_template.__dict__) for row_number, recipient, personalisation in RecipientCSV( s3.get_job_from_s3(str(service.id), str(job_id)), diff --git a/tests/app/celery/test_tasks.py b/tests/app/celery/test_tasks.py index 2846fac7d..7e3cff311 100644 --- a/tests/app/celery/test_tasks.py +++ b/tests/app/celery/test_tasks.py @@ -272,33 +272,41 @@ def test_should_not_create_send_task_for_empty_file(sample_job, mocker): assert job.job_status == 'finished' +@pytest.fixture +def email_job_with_placeholders(notify_db, notify_db_session, sample_email_template_with_placeholders): + return sample_job(notify_db, notify_db_session, template=sample_email_template_with_placeholders) + + @freeze_time("2016-01-01 11:09:00.061258") -def test_should_process_email_job(sample_email_job, mocker): - mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email')) +def test_should_process_email_job(email_job_with_placeholders, mocker): + email_csv = """email_address,name + test@test.com,foo + """ + mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=email_csv) mocker.patch('app.celery.tasks.send_email.apply_async') mocker.patch('app.encryption.encrypt', return_value="something_encrypted") mocker.patch('app.celery.tasks.create_uuid', return_value="uuid") - process_job(sample_email_job.id) + process_job(email_job_with_placeholders.id) s3.get_job_from_s3.assert_called_once_with( - str(sample_email_job.service.id), - str(sample_email_job.id) + str(email_job_with_placeholders.service.id), + str(email_job_with_placeholders.id) ) assert encryption.encrypt.call_args[0][0]['to'] == 'test@test.com' - assert encryption.encrypt.call_args[0][0]['template'] == str(sample_email_job.template.id) - assert encryption.encrypt.call_args[0][0]['template_version'] == sample_email_job.template.version - assert encryption.encrypt.call_args[0][0]['personalisation'] == {'emailaddress': 'test@test.com'} + assert encryption.encrypt.call_args[0][0]['template'] == str(email_job_with_placeholders.template.id) + assert encryption.encrypt.call_args[0][0]['template_version'] == email_job_with_placeholders.template.version + assert encryption.encrypt.call_args[0][0]['personalisation'] == {'emailaddress': 'test@test.com', 'name': 'foo'} tasks.send_email.apply_async.assert_called_once_with( ( - str(sample_email_job.service_id), + str(email_job_with_placeholders.service_id), "uuid", "something_encrypted", "2016-01-01T11:09:00.061258Z" ), queue="db-email" ) - job = jobs_dao.dao_get_job_by_id(sample_email_job.id) + job = jobs_dao.dao_get_job_by_id(email_job_with_placeholders.id) assert job.job_status == 'finished' diff --git a/tests/app/conftest.py b/tests/app/conftest.py index 8e570228b..4dbf288fa 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -218,12 +218,9 @@ def sample_email_template( 'template_type': template_type, 'content': content, 'service': service, - 'created_by': user + 'created_by': user, + 'subject': subject_line } - if subject_line: - data.update({ - 'subject': subject_line - }) template = Template(**data) dao_create_template(template) return template