mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-04 18:31:13 -05:00
Working placeholders in subject with upgraded notifications-utils.
Fix test.
This commit is contained in:
@@ -6,7 +6,7 @@ from flask import Flask, _request_ctx_stack
|
|||||||
from flask.ext.sqlalchemy import SQLAlchemy
|
from flask.ext.sqlalchemy import SQLAlchemy
|
||||||
from flask_marshmallow import Marshmallow
|
from flask_marshmallow import Marshmallow
|
||||||
from werkzeug.local import LocalProxy
|
from werkzeug.local import LocalProxy
|
||||||
from utils import logging
|
from notifications_utils import logging
|
||||||
from app.celery.celery import NotifyCelery
|
from app.celery.celery import NotifyCelery
|
||||||
from app.clients.sms.mmg import MMGClient
|
from app.clients.sms.mmg import MMGClient
|
||||||
from app.clients.sms.twilio import TwilioClient
|
from app.clients.sms.twilio import TwilioClient
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ from flask import (
|
|||||||
|
|
||||||
from itsdangerous import SignatureExpired
|
from itsdangerous import SignatureExpired
|
||||||
|
|
||||||
from utils.url_safe_token import check_token
|
from notifications_utils.url_safe_token import check_token
|
||||||
|
|
||||||
from app.dao.invited_user_dao import get_invited_user_by_id
|
from app.dao.invited_user_dao import get_invited_user_by_id
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ from app.clients.sms.mmg import MMGClientException
|
|||||||
from app.dao.services_dao import dao_fetch_service_by_id
|
from app.dao.services_dao import dao_fetch_service_by_id
|
||||||
from app.dao.templates_dao import dao_get_template_by_id
|
from app.dao.templates_dao import dao_get_template_by_id
|
||||||
|
|
||||||
from utils.template import Template
|
from notifications_utils.template import Template
|
||||||
|
|
||||||
from utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
RecipientCSV,
|
RecipientCSV,
|
||||||
validate_and_format_phone_number,
|
validate_and_format_phone_number,
|
||||||
allowed_to_send_to
|
allowed_to_send_to
|
||||||
@@ -172,7 +172,6 @@ def process_job(job_id):
|
|||||||
send_email.apply_async((
|
send_email.apply_async((
|
||||||
str(job.service_id),
|
str(job.service_id),
|
||||||
str(create_uuid()),
|
str(create_uuid()),
|
||||||
template.subject,
|
|
||||||
"{}@{}".format(job.service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
"{}@{}".format(job.service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
||||||
encrypted,
|
encrypted,
|
||||||
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
datetime.utcnow().strftime(DATETIME_FORMAT)),
|
||||||
@@ -257,7 +256,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
|
|||||||
|
|
||||||
|
|
||||||
@notify_celery.task(name="send-email")
|
@notify_celery.task(name="send-email")
|
||||||
def send_email(service_id, notification_id, subject, from_address, encrypted_notification, created_at):
|
def send_email(service_id, notification_id, from_address, encrypted_notification, created_at):
|
||||||
notification = encryption.decrypt(encrypted_notification)
|
notification = encryption.decrypt(encrypted_notification)
|
||||||
client = aws_ses_client
|
client = aws_ses_client
|
||||||
service = dao_fetch_service_by_id(service_id)
|
service = dao_fetch_service_by_id(service_id)
|
||||||
@@ -293,11 +292,10 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not
|
|||||||
dao_get_template_by_id(notification['template']).__dict__,
|
dao_get_template_by_id(notification['template']).__dict__,
|
||||||
values=notification.get('personalisation', {})
|
values=notification.get('personalisation', {})
|
||||||
)
|
)
|
||||||
|
|
||||||
reference = client.send_email(
|
reference = client.send_email(
|
||||||
from_address,
|
from_address,
|
||||||
notification['to'],
|
notification['to'],
|
||||||
subject,
|
template.replaced_subject,
|
||||||
body=template.replaced,
|
body=template.replaced,
|
||||||
html_body=template.as_HTML_email,
|
html_body=template.as_HTML_email,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ def update_invited_user(service_id, invited_user_id):
|
|||||||
|
|
||||||
|
|
||||||
def _create_invitation(invited_user):
|
def _create_invitation(invited_user):
|
||||||
from utils.url_safe_token import generate_token
|
from notifications_utils.url_safe_token import generate_token
|
||||||
token = generate_token(str(invited_user.id), current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
token = generate_token(str(invited_user.id), current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
||||||
# TODO: confirm what we want to do for this - the idea is that we say expires tomorrow at midnight
|
# TODO: confirm what we want to do for this - the idea is that we say expires tomorrow at midnight
|
||||||
# and give 48 hours as the max_age
|
# and give 48 hours as the max_age
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ from flask import (
|
|||||||
url_for,
|
url_for,
|
||||||
json
|
json
|
||||||
)
|
)
|
||||||
from utils.recipients import allowed_to_send_to, first_column_heading
|
from notifications_utils.recipients import allowed_to_send_to, first_column_heading
|
||||||
from utils.template import Template
|
from notifications_utils.template import Template
|
||||||
from app.clients.email.aws_ses import get_aws_responses
|
from app.clients.email.aws_ses import get_aws_responses
|
||||||
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT
|
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, DATE_FORMAT
|
||||||
from app.authentication.auth import require_admin
|
from app.authentication.auth import require_admin
|
||||||
@@ -348,7 +348,6 @@ def send_notification(notification_type):
|
|||||||
send_email.apply_async((
|
send_email.apply_async((
|
||||||
service_id,
|
service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
template.subject,
|
|
||||||
"{}@{}".format(service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
"{}@{}".format(service.email_from, current_app.config['NOTIFY_EMAIL_DOMAIN']),
|
||||||
encryption.encrypt(notification),
|
encryption.encrypt(notification),
|
||||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from marshmallow import (
|
|||||||
|
|
||||||
from marshmallow_sqlalchemy import field_for
|
from marshmallow_sqlalchemy import field_for
|
||||||
|
|
||||||
from utils.recipients import (
|
from notifications_utils.recipients import (
|
||||||
validate_email_address,
|
validate_email_address,
|
||||||
InvalidEmailError,
|
InvalidEmailError,
|
||||||
validate_phone_number,
|
validate_phone_number,
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ def send_user_reset_password():
|
|||||||
|
|
||||||
|
|
||||||
def _create_reset_password_url(email):
|
def _create_reset_password_url(email):
|
||||||
from utils.url_safe_token import generate_token
|
from notifications_utils.url_safe_token import generate_token
|
||||||
import json
|
import json
|
||||||
data = json.dumps({'email': email, 'created_at': str(datetime.now())})
|
data = json.dumps({'email': email, 'created_at': str(datetime.now())})
|
||||||
token = generate_token(data, current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
token = generate_token(data, current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
||||||
@@ -238,7 +238,7 @@ def _create_reset_password_url(email):
|
|||||||
|
|
||||||
|
|
||||||
def _create_verification_url(user, secret_code):
|
def _create_verification_url(user, secret_code):
|
||||||
from utils.url_safe_token import generate_token
|
from notifications_utils.url_safe_token import generate_token
|
||||||
import json
|
import json
|
||||||
data = json.dumps({'user_id': str(user.id), 'email': user.email_address, 'secret_code': secret_code})
|
data = json.dumps({'user_id': str(user.id), 'email': user.email_address, 'secret_code': secret_code})
|
||||||
token = generate_token(data, current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
token = generate_token(data, current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
||||||
|
|||||||
@@ -21,4 +21,4 @@ monotonic==0.3
|
|||||||
|
|
||||||
git+https://github.com/alphagov/notifications-python-client.git@0.2.6#egg=notifications-python-client==0.2.6
|
git+https://github.com/alphagov/notifications-python-client.git@0.2.6#egg=notifications-python-client==0.2.6
|
||||||
|
|
||||||
git+https://github.com/alphagov/notifications-utils.git@3.3.1#egg=notifications-utils==3.3.1
|
git+https://github.com/alphagov/notifications-utils.git@4.1.1#egg=notifications-utils==4.1.1
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import uuid
|
import uuid
|
||||||
import pytest
|
import pytest
|
||||||
from flask import current_app
|
from flask import current_app
|
||||||
from utils.recipients import validate_phone_number, format_phone_number
|
from notifications_utils.recipients import validate_phone_number, format_phone_number
|
||||||
|
|
||||||
from app.celery.tasks import (
|
from app.celery.tasks import (
|
||||||
send_sms,
|
send_sms,
|
||||||
@@ -204,7 +204,6 @@ def test_should_process_sms_job_if_exactly_on_send_limits(notify_db,
|
|||||||
tasks.send_email.apply_async.assert_called_with(
|
tasks.send_email.apply_async.assert_called_with(
|
||||||
(str(job.service_id),
|
(str(job.service_id),
|
||||||
"uuid",
|
"uuid",
|
||||||
job.template.subject,
|
|
||||||
"{}@{}".format(job.service.email_from, "test.notify.com"),
|
"{}@{}".format(job.service.email_from, "test.notify.com"),
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258"),
|
"2016-01-01T11:09:00.061258"),
|
||||||
@@ -246,7 +245,6 @@ def test_should_process_email_job(sample_email_job, mocker, mock_celery_remove_j
|
|||||||
tasks.send_email.apply_async.assert_called_once_with(
|
tasks.send_email.apply_async.assert_called_once_with(
|
||||||
(str(sample_email_job.service_id),
|
(str(sample_email_job.service_id),
|
||||||
"uuid",
|
"uuid",
|
||||||
sample_email_job.template.subject,
|
|
||||||
"{}@{}".format(sample_email_job.service.email_from, "test.notify.com"),
|
"{}@{}".format(sample_email_job.service.email_from, "test.notify.com"),
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258"),
|
"2016-01-01T11:09:00.061258"),
|
||||||
@@ -398,7 +396,11 @@ def test_should_not_send_sms_if_restricted_service_and_invalid_number(notify_db,
|
|||||||
def test_should_send_email_if_restricted_service_and_valid_email(notify_db, notify_db_session, mocker):
|
def test_should_send_email_if_restricted_service_and_valid_email(notify_db, notify_db_session, mocker):
|
||||||
user = sample_user(notify_db, notify_db_session, email="test@restricted.com")
|
user = sample_user(notify_db, notify_db_session, email="test@restricted.com")
|
||||||
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
service = sample_service(notify_db, notify_db_session, user=user, restricted=True)
|
||||||
template = sample_template(notify_db, notify_db_session, service=service)
|
template = sample_template(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
service=service,
|
||||||
|
template_type='email')
|
||||||
|
|
||||||
notification = {
|
notification = {
|
||||||
"template": template.id,
|
"template": template.id,
|
||||||
@@ -412,7 +414,6 @@ def test_should_send_email_if_restricted_service_and_valid_email(notify_db, noti
|
|||||||
send_email(
|
send_email(
|
||||||
service.id,
|
service.id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
@@ -421,7 +422,7 @@ def test_should_send_email_if_restricted_service_and_valid_email(notify_db, noti
|
|||||||
aws_ses_client.send_email.assert_called_once_with(
|
aws_ses_client.send_email.assert_called_once_with(
|
||||||
"email_from",
|
"email_from",
|
||||||
"test@restricted.com",
|
"test@restricted.com",
|
||||||
"subject",
|
template.subject,
|
||||||
body=template.content,
|
body=template.content,
|
||||||
html_body=AnyStringWith(template.content)
|
html_body=AnyStringWith(template.content)
|
||||||
)
|
)
|
||||||
@@ -446,7 +447,6 @@ def test_should_not_send_email_if_restricted_service_and_invalid_email_address(n
|
|||||||
send_email(
|
send_email(
|
||||||
service.id,
|
service.id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
@@ -504,7 +504,6 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
|
|||||||
send_email(
|
send_email(
|
||||||
sample_email_template_with_placeholders.service_id,
|
sample_email_template_with_placeholders.service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
@@ -512,7 +511,45 @@ def test_should_use_email_template_and_persist(sample_email_template_with_placeh
|
|||||||
aws_ses_client.send_email.assert_called_once_with(
|
aws_ses_client.send_email.assert_called_once_with(
|
||||||
"email_from",
|
"email_from",
|
||||||
"my_email@my_email.com",
|
"my_email@my_email.com",
|
||||||
"subject",
|
notification['personalisation']['name'],
|
||||||
|
body="Hello Jo",
|
||||||
|
html_body=AnyStringWith("Hello Jo")
|
||||||
|
)
|
||||||
|
persisted_notification = notifications_dao.get_notification(
|
||||||
|
sample_email_template_with_placeholders.service_id, notification_id
|
||||||
|
)
|
||||||
|
assert persisted_notification.id == notification_id
|
||||||
|
assert persisted_notification.to == 'my_email@my_email.com'
|
||||||
|
assert persisted_notification.template_id == sample_email_template_with_placeholders.id
|
||||||
|
assert persisted_notification.created_at == now
|
||||||
|
assert persisted_notification.sent_at > now
|
||||||
|
assert persisted_notification.status == 'sending'
|
||||||
|
assert persisted_notification.sent_by == 'ses'
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_use_email_template_subject_placeholders(sample_email_template_with_placeholders, mocker):
|
||||||
|
notification = {
|
||||||
|
"template": sample_email_template_with_placeholders.id,
|
||||||
|
"to": "my_email@my_email.com",
|
||||||
|
"personalisation": {"name": "Jo"}
|
||||||
|
}
|
||||||
|
mocker.patch('app.encryption.decrypt', return_value=notification)
|
||||||
|
mocker.patch('app.aws_ses_client.send_email')
|
||||||
|
mocker.patch('app.aws_ses_client.get_name', return_value='ses')
|
||||||
|
|
||||||
|
notification_id = uuid.uuid4()
|
||||||
|
now = datetime.utcnow()
|
||||||
|
send_email(
|
||||||
|
sample_email_template_with_placeholders.service_id,
|
||||||
|
notification_id,
|
||||||
|
'email_from',
|
||||||
|
"encrypted-in-reality",
|
||||||
|
now.strftime(DATETIME_FORMAT)
|
||||||
|
)
|
||||||
|
aws_ses_client.send_email.assert_called_once_with(
|
||||||
|
"email_from",
|
||||||
|
"my_email@my_email.com",
|
||||||
|
notification['personalisation']['name'],
|
||||||
body="Hello Jo",
|
body="Hello Jo",
|
||||||
html_body=AnyStringWith("Hello Jo")
|
html_body=AnyStringWith("Hello Jo")
|
||||||
)
|
)
|
||||||
@@ -542,7 +579,6 @@ def test_should_use_email_template_and_persist_ses_reference(sample_email_templa
|
|||||||
send_email(
|
send_email(
|
||||||
sample_email_template_with_placeholders.service_id,
|
sample_email_template_with_placeholders.service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
@@ -568,7 +604,6 @@ def test_should_use_email_template_and_persist_without_personalisation(
|
|||||||
send_email(
|
send_email(
|
||||||
sample_email_template.service_id,
|
sample_email_template.service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
@@ -576,7 +611,7 @@ def test_should_use_email_template_and_persist_without_personalisation(
|
|||||||
aws_ses_client.send_email.assert_called_once_with(
|
aws_ses_client.send_email.assert_called_once_with(
|
||||||
"email_from",
|
"email_from",
|
||||||
"my_email@my_email.com",
|
"my_email@my_email.com",
|
||||||
"subject",
|
sample_email_template.subject,
|
||||||
body="This is a template",
|
body="This is a template",
|
||||||
html_body=AnyStringWith("This is a template")
|
html_body=AnyStringWith("This is a template")
|
||||||
)
|
)
|
||||||
@@ -631,7 +666,6 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
|||||||
send_email(
|
send_email(
|
||||||
sample_email_template.service_id,
|
sample_email_template.service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
@@ -639,7 +673,7 @@ def test_should_persist_notification_as_failed_if_email_client_fails(sample_emai
|
|||||||
aws_ses_client.send_email.assert_called_once_with(
|
aws_ses_client.send_email.assert_called_once_with(
|
||||||
"email_from",
|
"email_from",
|
||||||
"my_email@my_email.com",
|
"my_email@my_email.com",
|
||||||
"subject",
|
sample_email_template.subject,
|
||||||
body=sample_email_template.content,
|
body=sample_email_template.content,
|
||||||
html_body=AnyStringWith(sample_email_template.content)
|
html_body=AnyStringWith(sample_email_template.content)
|
||||||
)
|
)
|
||||||
@@ -692,7 +726,6 @@ def test_should_not_send_email_if_db_peristance_failed(sample_email_template, mo
|
|||||||
send_email(
|
send_email(
|
||||||
sample_email_template.service_id,
|
sample_email_template.service_id,
|
||||||
notification_id,
|
notification_id,
|
||||||
'subject',
|
|
||||||
'email_from',
|
'email_from',
|
||||||
"encrypted-in-reality",
|
"encrypted-in-reality",
|
||||||
now.strftime(DATETIME_FORMAT)
|
now.strftime(DATETIME_FORMAT)
|
||||||
|
|||||||
@@ -133,7 +133,7 @@ def sample_template(notify_db,
|
|||||||
template_name="Template Name",
|
template_name="Template Name",
|
||||||
template_type="sms",
|
template_type="sms",
|
||||||
content="This is a template",
|
content="This is a template",
|
||||||
subject_line=None,
|
subject_line='Subject',
|
||||||
service=None):
|
service=None):
|
||||||
if service is None:
|
if service is None:
|
||||||
service = sample_service(notify_db, notify_db_session)
|
service = sample_service(notify_db, notify_db_session)
|
||||||
@@ -187,7 +187,11 @@ def sample_email_template(
|
|||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
def sample_email_template_with_placeholders(notify_db, notify_db_session):
|
def sample_email_template_with_placeholders(notify_db, notify_db_session):
|
||||||
return sample_email_template(notify_db, notify_db_session, content="Hello ((name))")
|
return sample_email_template(
|
||||||
|
notify_db,
|
||||||
|
notify_db_session,
|
||||||
|
content="Hello ((name))",
|
||||||
|
subject_line="((name))")
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='function')
|
@pytest.fixture(scope='function')
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ def test_create_invited_user(notify_api, sample_service, mocker):
|
|||||||
with notify_api.test_request_context():
|
with notify_api.test_request_context():
|
||||||
with notify_api.test_client() as client:
|
with notify_api.test_client() as client:
|
||||||
mocker.patch('app.celery.tasks.email_invited_user.apply_async')
|
mocker.patch('app.celery.tasks.email_invited_user.apply_async')
|
||||||
mocker.patch('utils.url_safe_token.generate_token', return_value='the-token')
|
mocker.patch('notifications_utils.url_safe_token.generate_token', return_value='the-token')
|
||||||
email_address = 'invited_user@service.gov.uk'
|
email_address = 'invited_user@service.gov.uk'
|
||||||
invite_from = sample_service.users[0]
|
invite_from = sample_service.users[0]
|
||||||
|
|
||||||
|
|||||||
@@ -946,7 +946,6 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template
|
|||||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||||
(str(sample_email_template.service_id),
|
(str(sample_email_template.service_id),
|
||||||
notification_id,
|
notification_id,
|
||||||
"Email Subject",
|
|
||||||
"sample.service@test.notify.com",
|
"sample.service@test.notify.com",
|
||||||
"something_encrypted",
|
"something_encrypted",
|
||||||
"2016-01-01T11:09:00.061258"),
|
"2016-01-01T11:09:00.061258"),
|
||||||
|
|||||||
Reference in New Issue
Block a user