mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 17:31:14 -05:00
Add postage for send-one-off letters.
The postage is set to europe or rest-of-world for international letters, otherwise the template postage is used. Also set international for letters.
This commit is contained in:
@@ -646,6 +646,54 @@ def test_process_sanitised_letter_with_valid_letter(
|
||||
assert file_contents == 'sanitised_pdf_content'
|
||||
|
||||
|
||||
@mock_s3
|
||||
@pytest.mark.parametrize('address, expected_postage, expected_international',
|
||||
[('Lady Lou, 123 Main Street, SW1 1AA', 'second', False),
|
||||
('Lady Lou, 123 Main Street, France', 'europe', True),
|
||||
('Lady Lou, 123 Main Street, New Zealand', 'rest-of-world', True),
|
||||
])
|
||||
def test_process_sanitised_letter_sets_postage_international(
|
||||
sample_letter_notification,
|
||||
expected_postage,
|
||||
expected_international,
|
||||
address
|
||||
):
|
||||
filename = 'NOTIFY.{}'.format(sample_letter_notification.reference)
|
||||
|
||||
scan_bucket_name = current_app.config['LETTERS_SCAN_BUCKET_NAME']
|
||||
template_preview_bucket_name = current_app.config['LETTER_SANITISE_BUCKET_NAME']
|
||||
destination_bucket_name = current_app.config['LETTERS_PDF_BUCKET_NAME']
|
||||
conn = boto3.resource('s3', region_name='eu-west-1')
|
||||
conn.create_bucket(Bucket=scan_bucket_name)
|
||||
conn.create_bucket(Bucket=template_preview_bucket_name)
|
||||
conn.create_bucket(Bucket=destination_bucket_name)
|
||||
|
||||
s3 = boto3.client('s3', region_name='eu-west-1')
|
||||
s3.put_object(Bucket=scan_bucket_name, Key=filename, Body=b'original_pdf_content')
|
||||
s3.put_object(Bucket=template_preview_bucket_name, Key=filename, Body=b'sanitised_pdf_content')
|
||||
|
||||
sample_letter_notification.status = NOTIFICATION_PENDING_VIRUS_CHECK
|
||||
sample_letter_notification.billable_units = 1
|
||||
sample_letter_notification.created_at = datetime(2018, 7, 1, 12)
|
||||
|
||||
encrypted_data = encryption.encrypt({
|
||||
'page_count': 2,
|
||||
'message': None,
|
||||
'invalid_pages': None,
|
||||
'validation_status': 'passed',
|
||||
'filename': filename,
|
||||
'notification_id': str(sample_letter_notification.id),
|
||||
'address': address
|
||||
})
|
||||
process_sanitised_letter(encrypted_data)
|
||||
|
||||
assert sample_letter_notification.status == 'created'
|
||||
assert sample_letter_notification.billable_units == 1
|
||||
assert sample_letter_notification.to == address
|
||||
assert sample_letter_notification.postage == expected_postage
|
||||
assert sample_letter_notification.international == expected_international
|
||||
|
||||
|
||||
@mock_s3
|
||||
@pytest.mark.parametrize('key_type', [KEY_TYPE_NORMAL, KEY_TYPE_TEST])
|
||||
def test_process_sanitised_letter_with_invalid_letter(sample_letter_notification, key_type):
|
||||
|
||||
@@ -18,6 +18,7 @@ from app.dao.services_dao import dao_update_service
|
||||
from app.dao.api_key_dao import save_model_api_key
|
||||
from app.errors import InvalidRequest
|
||||
from app.models import Template
|
||||
from app.service.send_notification import send_one_off_notification
|
||||
from app.v2.errors import RateLimitError
|
||||
|
||||
from tests import create_authorization_header
|
||||
@@ -1214,3 +1215,29 @@ def test_post_notification_should_set_reply_to_text(client, sample_service, mock
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
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
|
||||
|
||||
@@ -541,3 +541,23 @@ def test_persist_notification_with_billable_units_stores_correct_info(
|
||||
persisted_notification = Notification.query.all()[0]
|
||||
|
||||
assert persisted_notification.billable_units == 3
|
||||
|
||||
|
||||
@pytest.mark.parametrize('postage', ['europe', 'rest-of-world'])
|
||||
def test_persist_notification_for_international_letter(sample_letter_template, postage):
|
||||
notification = persist_notification(
|
||||
template_id=sample_letter_template.id,
|
||||
template_version=sample_letter_template.version,
|
||||
recipient="123 Main Street",
|
||||
service=sample_letter_template.service,
|
||||
personalisation=None,
|
||||
notification_type=sample_letter_template.template_type,
|
||||
api_key_id=None,
|
||||
key_type="normal",
|
||||
billable_units=3,
|
||||
postage=postage,
|
||||
template_postage='second'
|
||||
)
|
||||
persisted_notification = Notification.query.get(notification.id)
|
||||
assert persisted_notification.postage == postage
|
||||
assert persisted_notification.international
|
||||
|
||||
@@ -5,7 +5,12 @@ from notifications_utils import SMS_CHAR_COUNT_LIMIT
|
||||
|
||||
import app
|
||||
from app.dao import templates_dao
|
||||
from app.models import SMS_TYPE, EMAIL_TYPE, LETTER_TYPE
|
||||
from app.models import (
|
||||
EMAIL_TYPE,
|
||||
INTERNATIONAL_LETTERS,
|
||||
LETTER_TYPE,
|
||||
SMS_TYPE,
|
||||
)
|
||||
from app.notifications.process_notifications import create_content_for_notification
|
||||
from app.notifications.validators import (
|
||||
check_content_char_count,
|
||||
@@ -20,6 +25,7 @@ from app.notifications.validators import (
|
||||
check_service_letter_contact_id,
|
||||
check_reply_to,
|
||||
service_can_send_to_recipient,
|
||||
validate_address,
|
||||
validate_and_format_recipient,
|
||||
validate_template,
|
||||
)
|
||||
@@ -619,3 +625,19 @@ def test_check_if_service_can_send_files_by_email_passes_if_contact_link_set(sam
|
||||
service_contact_link=sample_service.contact_link,
|
||||
service_id=sample_service.id
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('key, address_line_3, expected_postage',
|
||||
[('address_line_3', 'SW1 1AA', None),
|
||||
('address_line_5', 'CANADA', 'rest-of-world'),
|
||||
('address_line_3', 'GERMANY', 'europe')
|
||||
])
|
||||
def test_validate_address(notify_db_session, key, address_line_3, expected_postage):
|
||||
service = create_service(service_permissions=[LETTER_TYPE, INTERNATIONAL_LETTERS])
|
||||
data = {
|
||||
'address_line_1': 'Prince Harry',
|
||||
'address_line_2': 'Toronto',
|
||||
key: address_line_3,
|
||||
}
|
||||
postage = validate_address(service, data)
|
||||
assert postage == expected_postage
|
||||
|
||||
@@ -100,6 +100,7 @@ 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
|
||||
)
|
||||
|
||||
|
||||
@@ -161,6 +162,7 @@ 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
|
||||
)
|
||||
|
||||
|
||||
@@ -188,8 +190,8 @@ def test_send_one_off_notification_calls_persist_correctly_for_letter(
|
||||
'to': 'First Last',
|
||||
'personalisation': {
|
||||
'name': 'foo',
|
||||
'address line 1': 'First Last',
|
||||
'address line 2': '1 Example Street',
|
||||
'address_line_1': 'First Last',
|
||||
'address_line_2': '1 Example Street',
|
||||
'postcode': 'SW1A 1AA',
|
||||
},
|
||||
'created_by': str(service.created_by_id)
|
||||
@@ -210,6 +212,7 @@ def test_send_one_off_notification_calls_persist_correctly_for_letter(
|
||||
created_by_id=str(service.created_by_id),
|
||||
reply_to_text=None,
|
||||
reference='this-is-random-in-real-life',
|
||||
postage=None
|
||||
)
|
||||
|
||||
|
||||
@@ -362,6 +365,12 @@ def test_send_one_off_letter_notification_should_use_template_reply_to_text(samp
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -383,6 +392,12 @@ def test_send_one_off_letter_should_not_make_pdf_in_research_mode(sample_letter_
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -175,29 +175,30 @@ def test_post_letter_notification_stores_country(
|
||||
'Germany'
|
||||
)
|
||||
assert notification.postage == 'europe'
|
||||
assert notification.international
|
||||
|
||||
|
||||
def test_post_letter_notification_international_sets_rest_of_world(
|
||||
client, notify_db_session, mocker
|
||||
):
|
||||
service = create_service(service_permissions=[LETTER_TYPE, INTERNATIONAL_LETTERS])
|
||||
template = create_template(service, template_type="letter")
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
data = {
|
||||
'template_id': str(template.id),
|
||||
'personalisation': {
|
||||
'address_line_1': 'Prince Harry',
|
||||
'address_line_2': 'Toronto',
|
||||
'address_line_5': 'Canada',
|
||||
}
|
||||
client, notify_db_session, mocker
|
||||
):
|
||||
service = create_service(service_permissions=[LETTER_TYPE, INTERNATIONAL_LETTERS])
|
||||
template = create_template(service, template_type="letter")
|
||||
mocker.patch('app.celery.tasks.letters_pdf_tasks.get_pdf_for_templated_letter.apply_async')
|
||||
data = {
|
||||
'template_id': str(template.id),
|
||||
'personalisation': {
|
||||
'address_line_1': 'Prince Harry',
|
||||
'address_line_2': 'Toronto',
|
||||
'address_line_5': 'Canada',
|
||||
}
|
||||
}
|
||||
|
||||
resp_json = letter_request(client, data, service_id=service.id)
|
||||
resp_json = letter_request(client, data, service_id=service.id)
|
||||
|
||||
assert validate(resp_json, post_letter_response) == resp_json
|
||||
notification = Notification.query.one()
|
||||
assert validate(resp_json, post_letter_response) == resp_json
|
||||
notification = Notification.query.one()
|
||||
|
||||
assert notification.postage == 'rest-of-world'
|
||||
assert notification.postage == 'rest-of-world'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('permissions, personalisation, expected_error', (
|
||||
|
||||
Reference in New Issue
Block a user