mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-31 23:26:23 -05:00
Implemented the post email notifications endpoint for v2
This commit is contained in:
@@ -3,8 +3,10 @@ import uuid
|
||||
import pytest
|
||||
from flask import json
|
||||
from jsonschema import ValidationError
|
||||
from notifications_utils.recipients import InvalidPhoneError, InvalidEmailError
|
||||
|
||||
from app.v2.notifications.notification_schemas import post_sms_request, post_sms_response
|
||||
from app.v2.notifications.notification_schemas import post_sms_request, post_sms_response, post_email_request, \
|
||||
post_email_response
|
||||
from app.schema_validation import validate
|
||||
|
||||
valid_json = {"phone_number": "07515111111",
|
||||
@@ -54,6 +56,15 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
||||
assert len(error.keys()) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid_phone_number',
|
||||
['notaphoneumber', '08515111111', '07515111*11'])
|
||||
def test_post_sms_request_invalid_phone_number(invalid_phone_number):
|
||||
j = {"phone_number": invalid_phone_number,
|
||||
"template_id": str(uuid.uuid4())
|
||||
}
|
||||
with pytest.raises(InvalidPhoneError):
|
||||
validate(j, post_sms_request)
|
||||
|
||||
valid_response = {
|
||||
"id": str(uuid.uuid4()),
|
||||
"content": {"body": "contents of message",
|
||||
@@ -90,3 +101,58 @@ def test_post_sms_response_schema_missing_uri():
|
||||
assert error['status_code'] == 400
|
||||
assert error['errors'] == [{'error': 'ValidationError',
|
||||
'message': "uri is a required property"}]
|
||||
|
||||
|
||||
valid_post_email_json = {"email_address": "test@example.gov.uk",
|
||||
"template_id": str(uuid.uuid4())
|
||||
}
|
||||
valid_post_emaiL_json_with_optionals = {
|
||||
"email_address": "test@example.gov.uk",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"reference": "reference from caller",
|
||||
"personalisation": {"key": "value"}
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input", [valid_post_email_json, valid_post_emaiL_json_with_optionals])
|
||||
def test_post_email_schema_is_valid(input):
|
||||
assert validate(input, post_email_request) == input
|
||||
|
||||
|
||||
def test_post_email_schema_bad_uuid_and_missing_email_address():
|
||||
j = {"template_id": "bad_template"}
|
||||
with pytest.raises(ValidationError):
|
||||
validate(j, post_email_request)
|
||||
|
||||
|
||||
def test_post_email_schema_invalid_email_address():
|
||||
j = {"template_id": str(uuid.uuid4()),
|
||||
"email_address": "notavalidemail@address"}
|
||||
with pytest.raises(InvalidEmailError):
|
||||
validate(j, post_email_request)
|
||||
|
||||
|
||||
valid_email_response = {"id": str(uuid.uuid4()),
|
||||
"content": {"body": "the body of the message",
|
||||
"subject": "subject of the message",
|
||||
"from_email": "service@dig.gov.uk"},
|
||||
"uri": "/v2/notifications/id",
|
||||
"template": {"id": str(uuid.uuid4()),
|
||||
"version": 1,
|
||||
"uri": "/v2/template/id"}
|
||||
}
|
||||
valid_email_response_with_optionals = {"id": str(uuid.uuid4()),
|
||||
"reference": "some reference",
|
||||
"content": {"body": "the body of the message",
|
||||
"subject": "subject of the message",
|
||||
"from_email": "service@dig.gov.uk"},
|
||||
"uri": "/v2/notifications/id",
|
||||
"template": {"id": str(uuid.uuid4()),
|
||||
"version": 1,
|
||||
"uri": "/v2/template/id"}
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input", [valid_email_response, valid_email_response_with_optionals])
|
||||
def test_post_email_response(input):
|
||||
assert validate(input, post_email_response) == input
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import uuid
|
||||
|
||||
import pytest
|
||||
from flask import json
|
||||
|
||||
from app.models import Notification
|
||||
from tests import create_authorization_header
|
||||
|
||||
@@ -102,3 +102,55 @@ def test_post_sms_notification_returns_400_and_for_schema_problems(notify_api, s
|
||||
assert error_resp['errors'] == [{'error': 'ValidationError',
|
||||
'message': "template_id is a required property"
|
||||
}]
|
||||
|
||||
|
||||
def test_post_email_notification_returns_201(client, sample_email_template, mocker):
|
||||
mocked = mocker.patch('app.celery.provider_tasks.deliver_email.apply_async')
|
||||
data = {
|
||||
"reference": "reference from caller",
|
||||
"email_address": sample_email_template.service.users[0].email_address,
|
||||
"template_id": sample_email_template.id,
|
||||
}
|
||||
auth_header = create_authorization_header(service_id=sample_email_template.service_id)
|
||||
response = client.post(
|
||||
path="v2/notifications/email",
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
assert response.status_code == 201
|
||||
resp_json = json.loads(response.get_data(as_text=True))
|
||||
notifications = Notification.query.all()
|
||||
assert len(notifications) == 1
|
||||
notification_id = notifications[0].id
|
||||
assert resp_json['id'] is not None
|
||||
assert resp_json['reference'] is None
|
||||
assert resp_json['content']['body'] == sample_email_template.content
|
||||
assert resp_json['content']['subject'] == sample_email_template.subject
|
||||
assert resp_json['content']['from_email'] == sample_email_template.service.email_from
|
||||
assert 'v2/notifications/{}'.format(notification_id) in resp_json['uri']
|
||||
assert resp_json['template']['id'] == str(sample_email_template.id)
|
||||
assert resp_json['template']['version'] == sample_email_template.version
|
||||
assert 'v2/templates/{}'.format(sample_email_template.id) in resp_json['template']['uri']
|
||||
assert mocked.called
|
||||
|
||||
|
||||
def test_post_email_notification_returns_404_and_missing_template(notify_api, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
data = {
|
||||
"email_address": sample_service.users[0].email_address,
|
||||
'template_id': str(uuid.uuid4())
|
||||
}
|
||||
auth_header = create_authorization_header(service_id=sample_service.id)
|
||||
|
||||
response = client.post(
|
||||
path='/v2/notifications/email',
|
||||
data=json.dumps(data),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
|
||||
error_json = json.loads(response.get_data(as_text=True))
|
||||
assert error_json['status_code'] == 400
|
||||
assert error_json['errors'] == [{"error": "BadRequestError",
|
||||
"message": 'Template not found'}]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
import pytest
|
||||
from flask import url_for
|
||||
from notifications_utils.recipients import InvalidPhoneError
|
||||
from sqlalchemy.exc import DataError
|
||||
|
||||
|
||||
@@ -39,6 +40,10 @@ def app_for_test(mocker):
|
||||
def raising_data_error():
|
||||
raise DataError("There was a db problem", "params", "orig")
|
||||
|
||||
@blue.route("raise_phone_error", methods=["GET"])
|
||||
def raising_invalid_phone_error():
|
||||
raise InvalidPhoneError("The phone number is wrong")
|
||||
|
||||
@blue.route("raise_exception", methods=["GET"])
|
||||
def raising_exception():
|
||||
raise AssertionError("Raising any old exception")
|
||||
@@ -116,3 +121,13 @@ def test_internal_server_error_handler(app_for_test):
|
||||
error = json.loads(response.get_data(as_text=True))
|
||||
assert error == {"status_code": 500,
|
||||
"errors": [{"error": "AssertionError", "message": "Internal server error"}]}
|
||||
|
||||
|
||||
def test_invalid_phone_error_handler(app_for_test):
|
||||
with app_for_test.test_request_context():
|
||||
with app_for_test.test_client() as client:
|
||||
response = client.get(url_for("v2_under_test.raising_invalid_phone_error"))
|
||||
assert response.status_code == 400
|
||||
error = json.loads(response.get_data(as_text=True))
|
||||
assert error == {"status_code": 400,
|
||||
"errors": [{"error": "InvalidPhoneError", "message": "The phone number is wrong"}]}
|
||||
|
||||
Reference in New Issue
Block a user