mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-23 15:56:45 -04:00
Remove letters-related code (#175)
This deletes a big ol' chunk of code related to letters. It's not everything—there are still a few things that might be tied to sms/email—but it's the the heart of letters function. SMS and email function should be untouched by this. Areas affected: - Things obviously about letters - PDF tasks, used for precompiling letters - Virus scanning, used for those PDFs - FTP, used to send letters to the printer - Postage stuff
This commit is contained in:
@@ -1167,8 +1167,7 @@ def test_should_not_allow_email_notifications_if_service_permission_not_set(
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"notification_type, err_msg",
|
||||
[("letter", "letter notification type is not supported, please use the latest version of the client"),
|
||||
("apple", "apple notification type is not supported")])
|
||||
[("apple", "apple notification type is not supported")])
|
||||
def test_should_throw_exception_if_notification_type_is_invalid(client, sample_service, notification_type, err_msg):
|
||||
auth_header = create_service_authorization_header(service_id=sample_service.id)
|
||||
response = client.post(
|
||||
@@ -1208,32 +1207,7 @@ def test_post_notification_should_set_reply_to_text(client, sample_service, mock
|
||||
assert notifications[0].reply_to_text == expected_reply_to
|
||||
|
||||
|
||||
@pytest.mark.parametrize('last_line_of_address, expected_postage, expected_international',
|
||||
[('France', 'europe', True),
|
||||
('Canada', 'rest-of-world', True),
|
||||
('SW1 1AA', 'second', False)])
|
||||
def test_send_notification_should_send_international_letters(
|
||||
sample_letter_template, mocker, last_line_of_address, expected_postage, expected_international
|
||||
):
|
||||
deliver_mock = mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
data = {
|
||||
'template_id': sample_letter_template.id,
|
||||
'personalisation': {
|
||||
'address_line_1': 'Jane',
|
||||
'address_line_2': 'Rue Vert',
|
||||
'address_line_3': last_line_of_address
|
||||
},
|
||||
'to': 'Jane',
|
||||
'created_by': sample_letter_template.service.created_by_id
|
||||
}
|
||||
|
||||
notification_id = send_one_off_notification(sample_letter_template.service_id, data)
|
||||
assert deliver_mock.called
|
||||
notification = Notification.query.get(notification_id['id'])
|
||||
assert notification.postage == expected_postage
|
||||
assert notification.international == expected_international
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Rewrite without letters?")
|
||||
@pytest.mark.parametrize('reference_paceholder,', [None, 'ref2'])
|
||||
def test_send_notification_should_set_client_reference_from_placeholder(
|
||||
sample_letter_template, mocker, reference_paceholder
|
||||
|
||||
@@ -12,7 +12,6 @@ from app.dao.service_guest_list_dao import (
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
KEY_TYPE_NORMAL,
|
||||
LETTER_TYPE,
|
||||
MOBILE_TYPE,
|
||||
PRIORITY,
|
||||
SMS_TYPE,
|
||||
@@ -22,7 +21,6 @@ from app.models import (
|
||||
from app.service.send_notification import send_one_off_notification
|
||||
from app.v2.errors import BadRequestError, TooManyRequestsError
|
||||
from tests.app.db import (
|
||||
create_letter_contact,
|
||||
create_reply_to_email,
|
||||
create_service,
|
||||
create_service_sms_sender,
|
||||
@@ -100,7 +98,6 @@ def test_send_one_off_notification_calls_persist_correctly_for_sms(
|
||||
created_by_id=str(service.created_by_id),
|
||||
reply_to_text='testing',
|
||||
reference=None,
|
||||
postage=None,
|
||||
client_reference=None
|
||||
)
|
||||
|
||||
@@ -162,57 +159,6 @@ def test_send_one_off_notification_calls_persist_correctly_for_email(
|
||||
created_by_id=str(service.created_by_id),
|
||||
reply_to_text=None,
|
||||
reference=None,
|
||||
postage=None,
|
||||
client_reference=None
|
||||
)
|
||||
|
||||
|
||||
def test_send_one_off_notification_calls_persist_correctly_for_letter(
|
||||
mocker,
|
||||
persist_mock,
|
||||
celery_mock,
|
||||
notify_db_session
|
||||
):
|
||||
mocker.patch(
|
||||
'app.service.send_notification.create_random_identifier',
|
||||
return_value='this-is-random-in-real-life',
|
||||
)
|
||||
service = create_service()
|
||||
template = create_template(
|
||||
service=service,
|
||||
template_type=LETTER_TYPE,
|
||||
postage='first',
|
||||
subject="Test subject",
|
||||
content="Hello (( Name))\nYour thing is due soon",
|
||||
)
|
||||
|
||||
post_data = {
|
||||
'template_id': str(template.id),
|
||||
'to': 'First Last',
|
||||
'personalisation': {
|
||||
'name': 'foo',
|
||||
'address_line_1': 'First Last',
|
||||
'address_line_2': '1 Example Street',
|
||||
'postcode': 'SW1A 1AA',
|
||||
},
|
||||
'created_by': str(service.created_by_id)
|
||||
}
|
||||
|
||||
send_one_off_notification(service.id, post_data)
|
||||
|
||||
persist_mock.assert_called_once_with(
|
||||
template_id=template.id,
|
||||
template_version=template.version,
|
||||
recipient=post_data['to'],
|
||||
service=template.service,
|
||||
personalisation=post_data['personalisation'],
|
||||
notification_type=LETTER_TYPE,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
created_by_id=str(service.created_by_id),
|
||||
reply_to_text=None,
|
||||
reference='this-is-random-in-real-life',
|
||||
postage='first',
|
||||
client_reference=None
|
||||
)
|
||||
|
||||
@@ -360,56 +306,6 @@ def test_send_one_off_notification_should_add_email_reply_to_text_for_notificati
|
||||
assert notification.reply_to_text == reply_to_email.email_address
|
||||
|
||||
|
||||
def test_send_one_off_letter_notification_should_use_template_reply_to_text(sample_letter_template, celery_mock):
|
||||
letter_contact = create_letter_contact(sample_letter_template.service, "Edinburgh, ED1 1AA", is_default=False)
|
||||
sample_letter_template.reply_to = str(letter_contact.id)
|
||||
|
||||
data = {
|
||||
'to': 'user@example.com',
|
||||
'template_id': str(sample_letter_template.id),
|
||||
'personalisation': {
|
||||
'name': 'foo',
|
||||
'address_line_1': 'First Last',
|
||||
'address_line_2': '1 Example Street',
|
||||
'address_line_3': 'SW1A 1AA',
|
||||
},
|
||||
'created_by': str(sample_letter_template.service.created_by_id)
|
||||
}
|
||||
|
||||
notification_id = send_one_off_notification(service_id=sample_letter_template.service.id, post_data=data)
|
||||
notification = Notification.query.get(notification_id['id'])
|
||||
celery_mock.assert_called_once_with(
|
||||
notification=notification,
|
||||
research_mode=False,
|
||||
queue=None
|
||||
)
|
||||
|
||||
assert notification.reply_to_text == "Edinburgh, ED1 1AA"
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Needs updating for TTS: Remove letters")
|
||||
def test_send_one_off_letter_should_not_make_pdf_in_research_mode(sample_letter_template):
|
||||
|
||||
sample_letter_template.service.research_mode = True
|
||||
|
||||
data = {
|
||||
'to': 'A. Name',
|
||||
'template_id': str(sample_letter_template.id),
|
||||
'personalisation': {
|
||||
'name': 'foo',
|
||||
'address_line_1': 'First Last',
|
||||
'address_line_2': '1 Example Street',
|
||||
'address_line_3': 'SW1A 1AA',
|
||||
},
|
||||
'created_by': str(sample_letter_template.service.created_by_id)
|
||||
}
|
||||
|
||||
notification = send_one_off_notification(service_id=sample_letter_template.service.id, post_data=data)
|
||||
notification = Notification.query.get(notification['id'])
|
||||
|
||||
assert notification.status == "delivered"
|
||||
|
||||
|
||||
def test_send_one_off_sms_notification_should_use_sms_sender_reply_to_text(sample_service, celery_mock):
|
||||
template = create_template(service=sample_service, template_type=SMS_TYPE)
|
||||
sms_sender = create_service_sms_sender(
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
import pytest
|
||||
from freezegun import freeze_time
|
||||
from notifications_utils.s3 import S3ObjectNotFound
|
||||
|
||||
from app.dao.notifications_dao import get_notification_by_id
|
||||
from app.models import EMAIL_TYPE, LETTER_TYPE, UPLOAD_LETTERS
|
||||
from app.service.send_notification import send_pdf_letter_notification
|
||||
from app.v2.errors import BadRequestError, TooManyRequestsError
|
||||
from tests.app.db import create_service
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def post_data(sample_service_full_permissions, fake_uuid):
|
||||
return {
|
||||
'filename': 'valid.pdf',
|
||||
'created_by': sample_service_full_permissions.users[0].id,
|
||||
'file_id': fake_uuid,
|
||||
'postage': 'second',
|
||||
'recipient_address': 'Bugs%20Bunny%0A123%20Main%20Street%0ALooney%20Town'
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('permissions', [
|
||||
[EMAIL_TYPE],
|
||||
[UPLOAD_LETTERS],
|
||||
])
|
||||
def test_send_pdf_letter_notification_raises_error_if_service_does_not_have_permission(
|
||||
notify_db_session,
|
||||
permissions,
|
||||
post_data,
|
||||
):
|
||||
service = create_service(service_permissions=permissions)
|
||||
|
||||
with pytest.raises(BadRequestError):
|
||||
send_pdf_letter_notification(service.id, post_data)
|
||||
|
||||
|
||||
def test_send_pdf_letter_notification_raises_error_if_service_is_over_daily_message_limit(
|
||||
mocker,
|
||||
sample_service_full_permissions,
|
||||
post_data,
|
||||
):
|
||||
mocker.patch(
|
||||
'app.service.send_notification.check_service_over_daily_message_limit',
|
||||
side_effect=TooManyRequestsError(10))
|
||||
|
||||
with pytest.raises(TooManyRequestsError):
|
||||
send_pdf_letter_notification(sample_service_full_permissions.id, post_data)
|
||||
|
||||
|
||||
def test_send_pdf_letter_notification_validates_created_by(
|
||||
sample_service_full_permissions,
|
||||
sample_user,
|
||||
post_data
|
||||
):
|
||||
post_data['created_by'] = sample_user.id
|
||||
|
||||
with pytest.raises(BadRequestError):
|
||||
send_pdf_letter_notification(sample_service_full_permissions.id, post_data)
|
||||
|
||||
|
||||
def test_send_pdf_letter_notification_raises_error_if_service_in_trial_mode(
|
||||
mocker,
|
||||
sample_service_full_permissions,
|
||||
post_data,
|
||||
):
|
||||
sample_service_full_permissions.restricted = True
|
||||
|
||||
with pytest.raises(BadRequestError) as e:
|
||||
send_pdf_letter_notification(sample_service_full_permissions.id, post_data)
|
||||
assert 'trial mode' in e.value.message
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="Skipping letter-related functionality for now")
|
||||
def test_send_pdf_letter_notification_raises_error_when_pdf_is_not_in_transient_letter_bucket(
|
||||
mocker,
|
||||
sample_service_full_permissions,
|
||||
notify_user,
|
||||
post_data,
|
||||
):
|
||||
mocker.patch('app.service.send_notification.utils_s3download', side_effect=S3ObjectNotFound({}, ''))
|
||||
|
||||
with pytest.raises(S3ObjectNotFound):
|
||||
send_pdf_letter_notification(sample_service_full_permissions.id, post_data)
|
||||
|
||||
|
||||
def test_send_pdf_letter_notification_does_nothing_if_notification_already_exists(
|
||||
mocker,
|
||||
sample_service_full_permissions,
|
||||
notify_user,
|
||||
sample_notification,
|
||||
post_data,
|
||||
):
|
||||
post_data['file_id'] = sample_notification.id
|
||||
mocker.patch('app.service.send_notification.utils_s3download', side_effect=S3ObjectNotFound({}, ''))
|
||||
response = send_pdf_letter_notification(sample_service_full_permissions.id, post_data)
|
||||
assert response['id'] == str(sample_notification.id)
|
||||
|
||||
|
||||
@freeze_time("2019-08-02 11:00:00")
|
||||
@pytest.mark.skip(reason="Skipping letter-related functionality for now")
|
||||
def test_send_pdf_letter_notification_creates_notification_and_moves_letter(
|
||||
mocker,
|
||||
sample_service_full_permissions,
|
||||
notify_user,
|
||||
post_data,
|
||||
):
|
||||
mocker.patch('app.service.send_notification.utils_s3download')
|
||||
mocker.patch('app.service.send_notification.get_page_count', return_value=1)
|
||||
s3_mock = mocker.patch('app.service.send_notification.move_uploaded_pdf_to_letters_bucket')
|
||||
|
||||
result = send_pdf_letter_notification(sample_service_full_permissions.id, post_data)
|
||||
file_id = post_data['file_id']
|
||||
|
||||
notification = get_notification_by_id(file_id)
|
||||
|
||||
assert str(notification.id) == file_id
|
||||
assert notification.api_key_id is None
|
||||
assert notification.client_reference == post_data['filename']
|
||||
assert notification.created_by_id == post_data['created_by']
|
||||
assert notification.postage == 'second'
|
||||
assert notification.notification_type == LETTER_TYPE
|
||||
assert notification.billable_units == 1
|
||||
assert notification.to == "Bugs Bunny\n123 Main Street\nLooney Town"
|
||||
|
||||
assert notification.service_id == sample_service_full_permissions.id
|
||||
assert result == {'id': str(notification.id)}
|
||||
|
||||
s3_mock.assert_called_once_with(
|
||||
'service-{}/{}.pdf'.format(sample_service_full_permissions.id, file_id),
|
||||
'2019-08-02/NOTIFY.{}.D.2.C.20190802110000.PDF'.format(notification.reference)
|
||||
)
|
||||
Reference in New Issue
Block a user