mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-23 09:29:43 -04:00
Update letter processing for letter pdfs in research mode
- call create fake response file task if in research mode only on preview and development environments to not impact response files on staging and live
This commit is contained in:
@@ -29,8 +29,7 @@ from app import (
|
||||
encryption
|
||||
)
|
||||
from app.aws import s3
|
||||
from app.celery import provider_tasks
|
||||
from app.celery import letters_pdf_tasks
|
||||
from app.celery import provider_tasks, letters_pdf_tasks, research_mode_tasks
|
||||
from app.config import QueueNames
|
||||
from app.dao.inbound_sms_dao import dao_get_inbound_sms_by_id
|
||||
from app.dao.jobs_dao import (
|
||||
@@ -46,6 +45,7 @@ from app.dao.notifications_dao import (
|
||||
dao_update_notifications_by_reference,
|
||||
dao_get_last_notification_added_for_job_id,
|
||||
dao_get_notification_by_reference,
|
||||
update_notification_status_by_reference,
|
||||
)
|
||||
from app.dao.provider_details_dao import get_current_provider
|
||||
from app.dao.service_inbound_api_dao import get_service_inbound_api_for_service
|
||||
@@ -62,6 +62,7 @@ from app.models import (
|
||||
JOB_STATUS_SENT_TO_DVLA, JOB_STATUS_ERROR,
|
||||
KEY_TYPE_NORMAL,
|
||||
LETTER_TYPE,
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_DELIVERED,
|
||||
NOTIFICATION_SENDING,
|
||||
NOTIFICATION_TEMPORARY_FAILURE,
|
||||
@@ -304,6 +305,9 @@ def save_letter(
|
||||
template = dao_get_template_by_id(notification['template'], version=notification['template_version'])
|
||||
|
||||
try:
|
||||
# if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up
|
||||
status = NOTIFICATION_CREATED if not service.research_mode else NOTIFICATION_SENDING
|
||||
|
||||
saved_notification = persist_notification(
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
@@ -318,7 +322,8 @@ def save_letter(
|
||||
job_row_number=notification['row_number'],
|
||||
notification_id=notification_id,
|
||||
reference=create_random_identifier(),
|
||||
reply_to_text=template.get_reply_to_text()
|
||||
reply_to_text=template.get_reply_to_text(),
|
||||
status=status
|
||||
)
|
||||
|
||||
if service.has_permission('letters_as_pdf'):
|
||||
@@ -327,11 +332,13 @@ def save_letter(
|
||||
[str(saved_notification.id)],
|
||||
queue=QueueNames.CREATE_LETTERS_PDF
|
||||
)
|
||||
else:
|
||||
update_letter_notifications_to_sent_to_dvla.apply_async(
|
||||
kwargs={'notification_references': [saved_notification.reference]},
|
||||
elif current_app.config['NOTIFY_ENVIRONMENT'] in ['preview', 'development']:
|
||||
research_mode_tasks.create_fake_letter_response_file.apply_async(
|
||||
(saved_notification.reference,),
|
||||
queue=QueueNames.RESEARCH_MODE
|
||||
)
|
||||
else:
|
||||
update_notification_status_by_reference(saved_notification.reference, 'delivered')
|
||||
|
||||
current_app.logger.info("Letter {} created at {}".format(saved_notification.id, saved_notification.created_at))
|
||||
except SQLAlchemyError as e:
|
||||
|
||||
@@ -68,6 +68,7 @@ from tests.app.db import (
|
||||
create_reply_to_email,
|
||||
create_service_with_defined_sms_sender,
|
||||
)
|
||||
from tests.conftest import set_config_values
|
||||
|
||||
|
||||
class AnyStringWith(str):
|
||||
@@ -1149,13 +1150,15 @@ def test_save_sms_uses_sms_sender_reply_to_text(mocker, notify_db_session):
|
||||
assert persisted_notification.reply_to_text == '447123123123'
|
||||
|
||||
|
||||
def test_save_letter_calls_update_noti_to_sent_task_with_letters_as_pdf_permission_in_research_mode(
|
||||
mocker, notify_db_session, sample_letter_job):
|
||||
@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"
|
||||
service_permissions_dao.dao_add_service_permission(sample_letter_job.service.id, 'letters_as_pdf')
|
||||
mock_update_letter_noti_sent = mocker.patch(
|
||||
'app.celery.tasks.update_letter_notifications_to_sent_to_dvla.apply_async')
|
||||
mocker.patch('app.celery.tasks.create_random_identifier', return_value="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',
|
||||
@@ -1171,15 +1174,55 @@ def test_save_letter_calls_update_noti_to_sent_task_with_letters_as_pdf_permissi
|
||||
)
|
||||
notification_id = uuid.uuid4()
|
||||
|
||||
save_letter(
|
||||
sample_letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
with set_config_values(notify_api, {
|
||||
'NOTIFY_ENVIRONMENT': env
|
||||
}):
|
||||
save_letter(
|
||||
sample_letter_job.service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification_json),
|
||||
)
|
||||
|
||||
assert mock_update_letter_noti_sent.called
|
||||
mock_update_letter_noti_sent.assert_called_once_with(
|
||||
kwargs={'notification_references': ['this-is-random-in-real-life']},
|
||||
notification = Notification.query.filter(Notification.id == notification_id).one()
|
||||
assert notification.status == 'delivered'
|
||||
assert not mock_create_fake_letter_response_file.called
|
||||
|
||||
|
||||
@pytest.mark.parametrize('env', ['development', 'preview'])
|
||||
def test_save_letter_calls_create_fake_response_for_letters_as_pdf_permission_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"
|
||||
service_permissions_dao.dao_add_service_permission(sample_letter_job.service.id, 'letters_as_pdf')
|
||||
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
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user