mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-24 08:16:19 -04:00
remove get-pdf-for-templated-letter
This commit is contained in:
@@ -257,28 +257,6 @@ def test_replay_created_notifications(notify_db_session, sample_service, mocker)
|
||||
queue="send-sms-tasks")
|
||||
|
||||
|
||||
def test_replay_created_notifications_get_pdf_for_templated_letter_tasks_for_letters_not_ready_to_send(
|
||||
sample_letter_template, mocker
|
||||
):
|
||||
mock_task = mocker.patch('app.celery.scheduled_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
create_notification(template=sample_letter_template, billable_units=0,
|
||||
created_at=datetime.utcnow() - timedelta(hours=4))
|
||||
|
||||
create_notification(template=sample_letter_template, billable_units=0,
|
||||
created_at=datetime.utcnow() - timedelta(minutes=20))
|
||||
notification_1 = create_notification(template=sample_letter_template, billable_units=0,
|
||||
created_at=datetime.utcnow() - timedelta(hours=1, minutes=20))
|
||||
notification_2 = create_notification(template=sample_letter_template, billable_units=0,
|
||||
created_at=datetime.utcnow() - timedelta(hours=5))
|
||||
|
||||
replay_created_notifications()
|
||||
|
||||
calls = [call([str(notification_1.id)], queue=QueueNames.CREATE_LETTERS_PDF),
|
||||
call([str(notification_2.id)], queue=QueueNames.CREATE_LETTERS_PDF),
|
||||
]
|
||||
mock_task.assert_has_calls(calls, any_order=True)
|
||||
|
||||
|
||||
def test_check_job_status_task_does_not_raise_error(sample_template):
|
||||
create_job(
|
||||
template=sample_template,
|
||||
|
||||
@@ -935,278 +935,6 @@ def test_save_sms_does_not_send_duplicate_and_does_not_put_in_retry_queue(sample
|
||||
assert not retry.called
|
||||
|
||||
|
||||
@pytest.mark.parametrize('personalisation, expected_to, expected_normalised', (
|
||||
({
|
||||
'addressline1': 'Foo',
|
||||
'addressline2': 'Bar',
|
||||
'addressline3': 'Baz',
|
||||
'addressline4': 'Wibble',
|
||||
'addressline5': 'Wobble',
|
||||
'addressline6': 'Wubble',
|
||||
'postcode': 'SE1 2SA',
|
||||
}, (
|
||||
'Foo\n'
|
||||
'Bar\n'
|
||||
'Baz\n'
|
||||
'Wibble\n'
|
||||
'Wobble\n'
|
||||
'Wubble\n'
|
||||
'SE1 2SA'
|
||||
), (
|
||||
'foobarbazwibblewobblewubblese12sa'
|
||||
)),
|
||||
({
|
||||
# The address isn’t normalised when we store it in the
|
||||
# `personalisation` column, but is normalised for storing in the
|
||||
# `to` column
|
||||
'addressline2': ' Foo ',
|
||||
'addressline4': 'Bar',
|
||||
'addressline6': 'se12sa',
|
||||
}, (
|
||||
'Foo\n'
|
||||
'Bar\n'
|
||||
'SE1 2SA'
|
||||
), (
|
||||
'foobarse12sa'
|
||||
)),
|
||||
))
|
||||
def test_save_letter_saves_letter_to_database(
|
||||
mocker,
|
||||
notify_db_session,
|
||||
personalisation,
|
||||
expected_to,
|
||||
expected_normalised,
|
||||
):
|
||||
service = create_service()
|
||||
contact_block = create_letter_contact(service=service, contact_block="Address contact", is_default=True)
|
||||
template = create_template(service=service, template_type=LETTER_TYPE, reply_to=contact_block.id)
|
||||
job = create_job(template=template)
|
||||
|
||||
mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life")
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
|
||||
notification_json = _notification_json(
|
||||
template=job.template,
|
||||
to='This is ignored for letters',
|
||||
personalisation=personalisation,
|
||||
job_id=job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
created_at = datetime.utcnow()
|
||||
|
||||
save_letter(
|
||||
job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification_db = Notification.query.one()
|
||||
assert notification_db.id == notification_id
|
||||
assert notification_db.to == expected_to
|
||||
assert notification_db.normalised_to == expected_normalised
|
||||
assert notification_db.job_id == job.id
|
||||
assert notification_db.template_id == job.template.id
|
||||
assert notification_db.template_version == job.template.version
|
||||
assert notification_db.status == 'created'
|
||||
assert notification_db.created_at >= created_at
|
||||
assert notification_db.notification_type == 'letter'
|
||||
assert notification_db.sent_at is None
|
||||
assert notification_db.sent_by is None
|
||||
assert notification_db.personalisation == personalisation
|
||||
assert notification_db.reference == "this-is-random-in-real-life"
|
||||
assert notification_db.reply_to_text == contact_block.contact_block
|
||||
|
||||
|
||||
@pytest.mark.parametrize('last_line_of_address, postage, expected_postage, expected_international',
|
||||
[('SW1 1AA', 'first', 'first', False),
|
||||
('SW1 1AA', 'second', 'second', False),
|
||||
('New Zealand', 'second', 'rest-of-world', True),
|
||||
('France', 'first', 'europe', True)])
|
||||
def test_save_letter_saves_letter_to_database_with_correct_postage(
|
||||
mocker, notify_db_session, last_line_of_address, postage, expected_postage, expected_international
|
||||
):
|
||||
service = create_service(service_permissions=[LETTER_TYPE])
|
||||
template = create_template(service=service, template_type=LETTER_TYPE, postage=postage)
|
||||
letter_job = create_job(template=template)
|
||||
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
notification_json = _notification_json(
|
||||
template=letter_job.template,
|
||||
to='Foo',
|
||||
personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': last_line_of_address},
|
||||
job_id=letter_job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
save_letter(
|
||||
letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification_db = Notification.query.one()
|
||||
assert notification_db.id == notification_id
|
||||
assert notification_db.postage == expected_postage
|
||||
assert notification_db.international == expected_international
|
||||
|
||||
|
||||
@pytest.mark.parametrize('reference_paceholder,', [None, 'ref2'])
|
||||
def test_save_letter_saves_letter_to_database_with_correct_client_reference(
|
||||
mocker, notify_db_session, reference_paceholder
|
||||
):
|
||||
service = create_service(service_permissions=[LETTER_TYPE])
|
||||
template = create_template(service=service, template_type=LETTER_TYPE)
|
||||
letter_job = create_job(template=template)
|
||||
|
||||
personalisation = {'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': 'SW1A 1AA'}
|
||||
if reference_paceholder:
|
||||
personalisation['reference'] = reference_paceholder
|
||||
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
notification_json = _notification_json(
|
||||
template=letter_job.template,
|
||||
to='Foo',
|
||||
personalisation=personalisation,
|
||||
job_id=letter_job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
save_letter(
|
||||
letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification_db = Notification.query.one()
|
||||
assert notification_db.id == notification_id
|
||||
assert notification_db.client_reference == reference_paceholder
|
||||
|
||||
|
||||
def test_save_letter_saves_letter_to_database_with_formatted_postcode(mocker, notify_db_session):
|
||||
service = create_service(service_permissions=[LETTER_TYPE])
|
||||
template = create_template(service=service, template_type=LETTER_TYPE)
|
||||
letter_job = create_job(template=template)
|
||||
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
notification_json = _notification_json(
|
||||
template=letter_job.template,
|
||||
to='Foo',
|
||||
personalisation={'addressline1': 'Foo', 'addressline2': 'Bar', 'postcode': 'se1 64sa'},
|
||||
job_id=letter_job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
save_letter(
|
||||
letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification_db = Notification.query.one()
|
||||
assert notification_db.id == notification_id
|
||||
assert notification_db.personalisation["postcode"] == "se1 64sa"
|
||||
|
||||
|
||||
def test_save_letter_saves_letter_to_database_right_reply_to(mocker, notify_db_session):
|
||||
service = create_service()
|
||||
create_letter_contact(service=service, contact_block="Address contact", is_default=True)
|
||||
template = create_template(service=service, template_type=LETTER_TYPE, reply_to=None)
|
||||
job = create_job(template=template)
|
||||
|
||||
mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life")
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
|
||||
personalisation = {
|
||||
'addressline1': 'Foo',
|
||||
'addressline2': 'Bar',
|
||||
'addressline3': 'Baz',
|
||||
'addressline4': 'Wibble',
|
||||
'addressline5': 'Wobble',
|
||||
'addressline6': 'Wubble',
|
||||
'postcode': 'SE1 3WS',
|
||||
}
|
||||
notification_json = _notification_json(
|
||||
template=job.template,
|
||||
to='Foo',
|
||||
personalisation=personalisation,
|
||||
job_id=job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
created_at = datetime.utcnow()
|
||||
|
||||
save_letter(
|
||||
job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification_db = Notification.query.one()
|
||||
assert notification_db.id == notification_id
|
||||
assert notification_db.to == (
|
||||
'Foo\n'
|
||||
'Bar\n'
|
||||
'Baz\n'
|
||||
'Wibble\n'
|
||||
'Wobble\n'
|
||||
'Wubble\n'
|
||||
'SE1 3WS'
|
||||
)
|
||||
assert notification_db.job_id == job.id
|
||||
assert notification_db.template_id == job.template.id
|
||||
assert notification_db.template_version == job.template.version
|
||||
assert notification_db.status == 'created'
|
||||
assert notification_db.created_at >= created_at
|
||||
assert notification_db.notification_type == 'letter'
|
||||
assert notification_db.sent_at is None
|
||||
assert notification_db.sent_by is None
|
||||
assert notification_db.personalisation == personalisation
|
||||
assert notification_db.reference == "this-is-random-in-real-life"
|
||||
assert not notification_db.reply_to_text
|
||||
|
||||
|
||||
def test_save_letter_uses_template_reply_to_text(mocker, notify_db_session):
|
||||
service = create_service()
|
||||
create_letter_contact(service=service, contact_block="Address contact", is_default=True)
|
||||
template_contact = create_letter_contact(
|
||||
service=service,
|
||||
contact_block="Template address contact",
|
||||
is_default=False
|
||||
)
|
||||
template = create_template(
|
||||
service=service,
|
||||
template_type=LETTER_TYPE,
|
||||
reply_to=template_contact.id
|
||||
)
|
||||
|
||||
job = create_job(template=template)
|
||||
|
||||
mocker.patch('app.celery.tasks.create_random_identifier', return_value="this-is-random-in-real-life")
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
|
||||
personalisation = {
|
||||
'addressline1': 'Foo',
|
||||
'addressline2': 'Bar',
|
||||
'postcode': 'Flob',
|
||||
}
|
||||
notification_json = _notification_json(
|
||||
template=job.template,
|
||||
to='Foo',
|
||||
personalisation=personalisation,
|
||||
job_id=job.id,
|
||||
row_number=1
|
||||
)
|
||||
|
||||
save_letter(
|
||||
job.service_id,
|
||||
uuid.uuid4(),
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification_db = Notification.query.one()
|
||||
assert notification_db.reply_to_text == "Template address contact"
|
||||
|
||||
|
||||
def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session):
|
||||
service = create_service_with_defined_sms_sender(sms_sender_value='07123123123')
|
||||
@@ -1246,115 +974,6 @@ def test_save_sms_uses_non_default_sms_sender_reply_to_text_if_provided(mocker,
|
||||
assert persisted_notification.reply_to_text == 'new-sender'
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Remove mail")
|
||||
@pytest.mark.parametrize('env', ['staging', 'live'])
|
||||
def test_save_letter_sets_delivered_letters_as_pdf_permission_in_research_mode_in_staging_live(
|
||||
notify_api, mocker, notify_db_session, sample_letter_job, env):
|
||||
sample_letter_job.service.research_mode = True
|
||||
sample_reference = "this-is-random-in-real-life"
|
||||
mock_create_fake_letter_response_file = mocker.patch(
|
||||
'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async')
|
||||
mocker.patch('app.celery.tasks.create_random_identifier', return_value=sample_reference)
|
||||
|
||||
personalisation = {
|
||||
'addressline1': 'Foo',
|
||||
'addressline2': 'Bar',
|
||||
'postcode': 'Flob',
|
||||
}
|
||||
notification_json = _notification_json(
|
||||
template=sample_letter_job.template,
|
||||
to='Foo',
|
||||
personalisation=personalisation,
|
||||
job_id=sample_letter_job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'NOTIFY_ENVIRONMENT': env
|
||||
}):
|
||||
save_letter(
|
||||
sample_letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
notification = Notification.query.filter(Notification.id == notification_id).one()
|
||||
assert notification.status == 'delivered'
|
||||
assert not mock_create_fake_letter_response_file.called
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Remove mail")
|
||||
@pytest.mark.parametrize('env', ['development', 'preview'])
|
||||
def test_save_letter_calls_create_fake_response_for_letters_in_research_mode_on_development_preview(
|
||||
notify_api, mocker, notify_db_session, sample_letter_job, env):
|
||||
sample_letter_job.service.research_mode = True
|
||||
sample_reference = "this-is-random-in-real-life"
|
||||
mock_create_fake_letter_response_file = mocker.patch(
|
||||
'app.celery.research_mode_tasks.create_fake_letter_response_file.apply_async')
|
||||
mocker.patch('app.celery.tasks.create_random_identifier', return_value=sample_reference)
|
||||
|
||||
personalisation = {
|
||||
'addressline1': 'Foo',
|
||||
'addressline2': 'Bar',
|
||||
'postcode': 'Flob',
|
||||
}
|
||||
notification_json = _notification_json(
|
||||
template=sample_letter_job.template,
|
||||
to='Foo',
|
||||
personalisation=personalisation,
|
||||
job_id=sample_letter_job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
with set_config_values(notify_api, {
|
||||
'NOTIFY_ENVIRONMENT': env
|
||||
}):
|
||||
save_letter(
|
||||
sample_letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
mock_create_fake_letter_response_file.assert_called_once_with(
|
||||
(sample_reference,),
|
||||
queue=QueueNames.RESEARCH_MODE
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Remove mail")
|
||||
def test_save_letter_calls_get_pdf_for_templated_letter_task_not_in_research(
|
||||
mocker, notify_db_session, sample_letter_job):
|
||||
mock_create_letters_pdf = mocker.patch('app.celery.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
|
||||
personalisation = {
|
||||
'addressline1': 'Foo',
|
||||
'addressline2': 'Bar',
|
||||
'postcode': 'Flob',
|
||||
}
|
||||
notification_json = _notification_json(
|
||||
template=sample_letter_job.template,
|
||||
to='Foo',
|
||||
personalisation=personalisation,
|
||||
job_id=sample_letter_job.id,
|
||||
row_number=1
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
save_letter(
|
||||
sample_letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
assert mock_create_letters_pdf.called
|
||||
mock_create_letters_pdf.assert_called_once_with(
|
||||
[str(notification_id)],
|
||||
queue=QueueNames.CREATE_LETTERS_PDF
|
||||
)
|
||||
|
||||
|
||||
def test_should_cancel_job_if_service_is_inactive(sample_service,
|
||||
sample_job,
|
||||
mocker):
|
||||
|
||||
Reference in New Issue
Block a user