mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Send Email via the API
- uses the new subject/email from fields present on the templates / service tables - brings the send email api call into line with the sms one. - same fields (to/template_id) - same rules regarding restricted services - wired in as a task into celery Requires - new celery queue - new env property (NOTIFY_EMAIL_DOMAIN)
This commit is contained in:
@@ -5,6 +5,7 @@ from tests import create_authorization_header
|
||||
from flask import json
|
||||
from app.models import Service
|
||||
from app.dao.templates_dao import get_model_templates
|
||||
from app.dao.services_dao import dao_update_service
|
||||
|
||||
|
||||
def test_get_notification_by_id(notify_api, sample_notification):
|
||||
@@ -158,21 +159,20 @@ def test_prevents_sending_to_any_mobile_on_restricted_service(notify_api, sample
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert 'Invalid phone number for restricted service' in json_resp['message']['restricted']
|
||||
assert 'Invalid phone number for restricted service' in json_resp['message']['to']
|
||||
|
||||
|
||||
def test_should_not_allow_template_from_another_service(notify_api, service_factory, mocker):
|
||||
def test_should_not_allow_template_from_another_service(notify_api, service_factory, sample_user, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_sms.apply_async')
|
||||
|
||||
service_1 = service_factory.get('service 1')
|
||||
service_2 = service_factory.get('service 2')
|
||||
service_1 = service_factory.get('service 1', user=sample_user)
|
||||
service_2 = service_factory.get('service 2', user=sample_user)
|
||||
|
||||
service_2_templates = get_model_templates(service_id=service_2.id)
|
||||
|
||||
data = {
|
||||
'to': '+441234123123',
|
||||
'to': sample_user.mobile_number,
|
||||
'template': service_2_templates[0].id
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ def test_should_not_allow_template_from_another_service(notify_api, service_fact
|
||||
app.celery.tasks.send_sms.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert 'Invalid template' in json_resp['message']['restricted']
|
||||
assert 'Template not found' in json_resp['message']['template']
|
||||
|
||||
|
||||
def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker):
|
||||
@@ -228,27 +228,14 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker
|
||||
assert notification_id
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
def test_send_email_valid_data(notify_api,
|
||||
notify_db,
|
||||
notify_db_session,
|
||||
sample_service,
|
||||
sample_admin_service_id,
|
||||
sqs_client_conn,
|
||||
mocker):
|
||||
def test_create_email_should_reject_if_missing_required_fields(notify_api, sample_api_key, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
to_address = "to@notify.com"
|
||||
from_address = "from@notify.com"
|
||||
subject = "This is the subject"
|
||||
message = "This is the message"
|
||||
data = {
|
||||
'to': to_address,
|
||||
'from': from_address,
|
||||
'subject': subject,
|
||||
'message': message
|
||||
}
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
|
||||
data = {}
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_api_key.service_id,
|
||||
request_body=json.dumps(data),
|
||||
path='/notifications/email',
|
||||
method='POST')
|
||||
@@ -258,8 +245,166 @@ def test_send_email_valid_data(notify_api,
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
assert json_resp['result'] == 'error'
|
||||
assert 'Missing data for required field.' in json_resp['message']['to'][0]
|
||||
assert 'Missing data for required field.' in json_resp['message']['template'][0]
|
||||
assert response.status_code == 400
|
||||
|
||||
|
||||
def test_should_reject_email_notification_with_bad_email(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
to_address = "bad-email"
|
||||
data = {
|
||||
'to': to_address,
|
||||
'template': sample_email_template.service.id
|
||||
}
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_email_template.service.id,
|
||||
request_body=json.dumps(data),
|
||||
path='/notifications/email',
|
||||
method='POST')
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
assert response.status_code == 400
|
||||
assert data['result'] == 'error'
|
||||
assert data['message']['to'][0] == 'Invalid email'
|
||||
|
||||
|
||||
def test_should_reject_email_notification_with_template_id_that_cant_be_found(
|
||||
notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': 1234
|
||||
}
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_email_template.service.id,
|
||||
request_body=json.dumps(data),
|
||||
path='/notifications/email',
|
||||
method='POST')
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
data = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
assert response.status_code == 400
|
||||
assert data['result'] == 'error'
|
||||
assert data['message']['template'][0] == 'Template not found'
|
||||
|
||||
|
||||
def test_should_not_allow_email_template_from_another_service(notify_api, service_factory, sample_user, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
|
||||
service_1 = service_factory.get('service 1', template_type='email', user=sample_user)
|
||||
service_2 = service_factory.get('service 2', template_type='email', user=sample_user)
|
||||
|
||||
service_2_templates = get_model_templates(service_id=service_2.id)
|
||||
|
||||
data = {
|
||||
'to': sample_user.email_address,
|
||||
'template': service_2_templates[0].id
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
service_id=service_1.id,
|
||||
request_body=json.dumps(data),
|
||||
path='/notifications/email',
|
||||
method='POST')
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert 'Template not found' in json_resp['message']['template']
|
||||
|
||||
|
||||
def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
|
||||
sample_email_template.service.restricted = True
|
||||
dao_update_service(sample_email_template)
|
||||
|
||||
data = {
|
||||
'to': "not-someone-we-trust@email-address.com",
|
||||
'template': sample_email_template.id
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
service_id=sample_email_template.service.id,
|
||||
request_body=json.dumps(data),
|
||||
path='/notifications/email',
|
||||
method='POST')
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
json_resp = json.loads(response.get_data(as_text=True))
|
||||
app.celery.tasks.send_email.apply_async.assert_not_called()
|
||||
|
||||
assert response.status_code == 400
|
||||
assert 'Email address not permitted for restricted service' in json_resp['message']['to']
|
||||
|
||||
|
||||
def test_should_allow_valid_email_notification(notify_api, sample_email_template, mocker):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
mocker.patch('app.celery.tasks.send_email.apply_async')
|
||||
mocker.patch('app.encryption.encrypt', return_value="something_encrypted")
|
||||
|
||||
data = {
|
||||
'to': 'ok@ok.com',
|
||||
'template': sample_email_template.id
|
||||
}
|
||||
|
||||
auth_header = create_authorization_header(
|
||||
request_body=json.dumps(data),
|
||||
path='/notifications/email',
|
||||
method='POST',
|
||||
service_id=sample_email_template.service_id
|
||||
)
|
||||
|
||||
response = client.post(
|
||||
path='/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
notification_id = json.loads(response.data)['notification_id']
|
||||
app.celery.tasks.send_email.apply_async.assert_called_once_with(
|
||||
(str(sample_email_template.service_id),
|
||||
notification_id,
|
||||
"Email Subject",
|
||||
"sample.service@test.notify.com",
|
||||
"something_encrypted"),
|
||||
queue="email"
|
||||
)
|
||||
assert response.status_code == 201
|
||||
assert json.loads(response.data)['notification_id'] is not None
|
||||
assert notification_id
|
||||
|
||||
|
||||
@moto.mock_sqs
|
||||
|
||||
Reference in New Issue
Block a user