mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-24 16:23:44 -04:00
fix placeholders not appearing in email subject
it now switches utils.template.Template type, since the base Template type now no longer has a subject attribute. updated test case to use `sample_email_template_with_placeholders` instead of `sample_email_template`
This commit is contained in:
@@ -4,7 +4,7 @@ from flask import current_app
|
|||||||
from notifications_utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
RecipientCSV
|
RecipientCSV
|
||||||
)
|
)
|
||||||
from notifications_utils.template import Template
|
from notifications_utils.template import SMSMessageTemplate, WithSubjectTemplate
|
||||||
from sqlalchemy.exc import SQLAlchemyError
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from app import (
|
from app import (
|
||||||
create_uuid,
|
create_uuid,
|
||||||
@@ -48,9 +48,10 @@ def process_job(job_id):
|
|||||||
job.job_status = 'in progress'
|
job.job_status = 'in progress'
|
||||||
dao_update_job(job)
|
dao_update_job(job)
|
||||||
|
|
||||||
template = Template(
|
db_template = dao_get_template_by_id(job.template_id, job.template_version)
|
||||||
dao_get_template_by_id(job.template_id, job.template_version).__dict__
|
|
||||||
)
|
TemplateClass = SMSMessageTemplate if db_template.template_type == SMS_TYPE else WithSubjectTemplate
|
||||||
|
template = TemplateClass(db_template.__dict__)
|
||||||
|
|
||||||
for row_number, recipient, personalisation in RecipientCSV(
|
for row_number, recipient, personalisation in RecipientCSV(
|
||||||
s3.get_job_from_s3(str(service.id), str(job_id)),
|
s3.get_job_from_s3(str(service.id), str(job_id)),
|
||||||
|
|||||||
@@ -272,33 +272,41 @@ def test_should_not_create_send_task_for_empty_file(sample_job, mocker):
|
|||||||
assert job.job_status == 'finished'
|
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")
|
@freeze_time("2016-01-01 11:09:00.061258")
|
||||||
def test_should_process_email_job(sample_email_job, mocker):
|
def test_should_process_email_job(email_job_with_placeholders, mocker):
|
||||||
mocker.patch('app.celery.tasks.s3.get_job_from_s3', return_value=load_example_csv('email'))
|
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.celery.tasks.send_email.apply_async')
|
||||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||||
mocker.patch('app.celery.tasks.create_uuid', return_value="uuid")
|
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(
|
s3.get_job_from_s3.assert_called_once_with(
|
||||||
str(sample_email_job.service.id),
|
str(email_job_with_placeholders.service.id),
|
||||||
str(sample_email_job.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]['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'] == str(email_job_with_placeholders.template.id)
|
||||||
assert encryption.encrypt.call_args[0][0]['template_version'] == sample_email_job.template.version
|
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'}
|
assert encryption.encrypt.call_args[0][0]['personalisation'] == {'emailaddress': 'test@test.com', 'name': 'foo'}
|
||||||
tasks.send_email.apply_async.assert_called_once_with(
|
tasks.send_email.apply_async.assert_called_once_with(
|
||||||
(
|
(
|
||||||
str(sample_email_job.service_id),
|
str(email_job_with_placeholders.service_id),
|
||||||
"uuid",
|
"uuid",
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258Z"
|
"2016-01-01T11:09:00.061258Z"
|
||||||
),
|
),
|
||||||
queue="db-email"
|
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'
|
assert job.job_status == 'finished'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -218,12 +218,9 @@ def sample_email_template(
|
|||||||
'template_type': template_type,
|
'template_type': template_type,
|
||||||
'content': content,
|
'content': content,
|
||||||
'service': service,
|
'service': service,
|
||||||
'created_by': user
|
'created_by': user,
|
||||||
|
'subject': subject_line
|
||||||
}
|
}
|
||||||
if subject_line:
|
|
||||||
data.update({
|
|
||||||
'subject': subject_line
|
|
||||||
})
|
|
||||||
template = Template(**data)
|
template = Template(**data)
|
||||||
dao_create_template(template)
|
dao_create_template(template)
|
||||||
return template
|
return template
|
||||||
|
|||||||
Reference in New Issue
Block a user