Skip to content

Commit

Permalink
fix placeholders not appearing in email subject
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
leohemsted committed Dec 22, 2016
1 parent 6bf0245 commit 3ed9715
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
9 changes: 5 additions & 4 deletions app/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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,
Expand Down Expand Up @@ -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)),
Expand Down
28 changes: 18 additions & 10 deletions tests/app/celery/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'


Expand Down
7 changes: 2 additions & 5 deletions tests/app/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3ed9715

Please sign in to comment.