mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-26 01:03:41 -04:00
reformat
This commit is contained in:
@@ -6,13 +6,11 @@ from tests import create_service_authorization_header
|
||||
from tests.app.db import create_notification, create_template
|
||||
|
||||
|
||||
@pytest.mark.parametrize('billable_units, provider', [
|
||||
(1, 'sns'),
|
||||
(0, 'sns'),
|
||||
(1, None)
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"billable_units, provider", [(1, "sns"), (0, "sns"), (1, None)]
|
||||
)
|
||||
def test_get_notification_by_id_returns_200(
|
||||
client, billable_units, provider, sample_template
|
||||
client, billable_units, provider, sample_template
|
||||
):
|
||||
sample_notification = create_notification(
|
||||
template=sample_template,
|
||||
@@ -27,119 +25,133 @@ def test_get_notification_by_id_returns_200(
|
||||
sent_by=provider,
|
||||
)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications/{}'.format(sample_notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications/{}".format(sample_notification.id),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
expected_template_response = {
|
||||
'id': '{}'.format(sample_notification.serialize()['template']['id']),
|
||||
'version': sample_notification.serialize()['template']['version'],
|
||||
'uri': sample_notification.serialize()['template']['uri']
|
||||
"id": "{}".format(sample_notification.serialize()["template"]["id"]),
|
||||
"version": sample_notification.serialize()["template"]["version"],
|
||||
"uri": sample_notification.serialize()["template"]["uri"],
|
||||
}
|
||||
|
||||
expected_response = {
|
||||
'id': '{}'.format(sample_notification.id),
|
||||
'reference': None,
|
||||
'email_address': None,
|
||||
'phone_number': '{}'.format(sample_notification.to),
|
||||
'line_1': None,
|
||||
'line_2': None,
|
||||
'line_3': None,
|
||||
'line_4': None,
|
||||
'line_5': None,
|
||||
'line_6': None,
|
||||
'postcode': None,
|
||||
'type': '{}'.format(sample_notification.notification_type),
|
||||
'status': '{}'.format(sample_notification.status),
|
||||
'template': expected_template_response,
|
||||
'created_at': sample_notification.created_at.strftime(DATETIME_FORMAT),
|
||||
'created_by_name': None,
|
||||
'body': sample_notification.template.content,
|
||||
"id": "{}".format(sample_notification.id),
|
||||
"reference": None,
|
||||
"email_address": None,
|
||||
"phone_number": "{}".format(sample_notification.to),
|
||||
"line_1": None,
|
||||
"line_2": None,
|
||||
"line_3": None,
|
||||
"line_4": None,
|
||||
"line_5": None,
|
||||
"line_6": None,
|
||||
"postcode": None,
|
||||
"type": "{}".format(sample_notification.notification_type),
|
||||
"status": "{}".format(sample_notification.status),
|
||||
"template": expected_template_response,
|
||||
"created_at": sample_notification.created_at.strftime(DATETIME_FORMAT),
|
||||
"created_by_name": None,
|
||||
"body": sample_notification.template.content,
|
||||
"subject": None,
|
||||
'sent_at': sample_notification.sent_at,
|
||||
'completed_at': sample_notification.completed_at(),
|
||||
'scheduled_for': None,
|
||||
'provider_response': None
|
||||
"sent_at": sample_notification.sent_at,
|
||||
"completed_at": sample_notification.completed_at(),
|
||||
"scheduled_for": None,
|
||||
"provider_response": None,
|
||||
}
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
def test_get_notification_by_id_with_placeholders_returns_200(
|
||||
client, sample_email_template_with_placeholders
|
||||
client, sample_email_template_with_placeholders
|
||||
):
|
||||
sample_notification = create_notification(
|
||||
template=sample_email_template_with_placeholders,
|
||||
personalisation={"name": "Bob"}
|
||||
personalisation={"name": "Bob"},
|
||||
)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications/{}'.format(sample_notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications/{}".format(sample_notification.id),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
expected_template_response = {
|
||||
'id': '{}'.format(sample_notification.serialize()['template']['id']),
|
||||
'version': sample_notification.serialize()['template']['version'],
|
||||
'uri': sample_notification.serialize()['template']['uri']
|
||||
"id": "{}".format(sample_notification.serialize()["template"]["id"]),
|
||||
"version": sample_notification.serialize()["template"]["version"],
|
||||
"uri": sample_notification.serialize()["template"]["uri"],
|
||||
}
|
||||
|
||||
expected_response = {
|
||||
'id': '{}'.format(sample_notification.id),
|
||||
'reference': None,
|
||||
'email_address': '{}'.format(sample_notification.to),
|
||||
'phone_number': None,
|
||||
'line_1': None,
|
||||
'line_2': None,
|
||||
'line_3': None,
|
||||
'line_4': None,
|
||||
'line_5': None,
|
||||
'line_6': None,
|
||||
'postcode': None,
|
||||
'type': '{}'.format(sample_notification.notification_type),
|
||||
'status': '{}'.format(sample_notification.status),
|
||||
'template': expected_template_response,
|
||||
'created_at': sample_notification.created_at.strftime(DATETIME_FORMAT),
|
||||
'created_by_name': None,
|
||||
'body': "Hello Bob\nThis is an email from GOV.UK",
|
||||
"id": "{}".format(sample_notification.id),
|
||||
"reference": None,
|
||||
"email_address": "{}".format(sample_notification.to),
|
||||
"phone_number": None,
|
||||
"line_1": None,
|
||||
"line_2": None,
|
||||
"line_3": None,
|
||||
"line_4": None,
|
||||
"line_5": None,
|
||||
"line_6": None,
|
||||
"postcode": None,
|
||||
"type": "{}".format(sample_notification.notification_type),
|
||||
"status": "{}".format(sample_notification.status),
|
||||
"template": expected_template_response,
|
||||
"created_at": sample_notification.created_at.strftime(DATETIME_FORMAT),
|
||||
"created_by_name": None,
|
||||
"body": "Hello Bob\nThis is an email from GOV.UK",
|
||||
"subject": "Bob",
|
||||
'sent_at': sample_notification.sent_at,
|
||||
'completed_at': sample_notification.completed_at(),
|
||||
'scheduled_for': None,
|
||||
'provider_response': None
|
||||
"sent_at": sample_notification.sent_at,
|
||||
"completed_at": sample_notification.completed_at(),
|
||||
"scheduled_for": None,
|
||||
"provider_response": None,
|
||||
}
|
||||
|
||||
assert json_response == expected_response
|
||||
|
||||
|
||||
def test_get_notification_by_reference_returns_200(client, sample_template):
|
||||
sample_notification_with_reference = create_notification(template=sample_template,
|
||||
client_reference='some-client-reference')
|
||||
sample_notification_with_reference = create_notification(
|
||||
template=sample_template, client_reference="some-client-reference"
|
||||
)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification_with_reference.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification_with_reference.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?reference={}'.format(sample_notification_with_reference.client_reference),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?reference={}".format(
|
||||
sample_notification_with_reference.client_reference
|
||||
),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
assert len(json_response['notifications']) == 1
|
||||
assert len(json_response["notifications"]) == 1
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(sample_notification_with_reference.id)
|
||||
assert json_response['notifications'][0]['reference'] == "some-client-reference"
|
||||
assert json_response["notifications"][0]["id"] == str(
|
||||
sample_notification_with_reference.id
|
||||
)
|
||||
assert json_response["notifications"][0]["reference"] == "some-client-reference"
|
||||
|
||||
|
||||
def test_get_notification_by_id_returns_created_by_name_if_notification_created_by_id(
|
||||
@@ -150,110 +162,134 @@ def test_get_notification_by_id_returns_created_by_name_if_notification_created_
|
||||
sms_notification = create_notification(template=sample_template)
|
||||
sms_notification.created_by_id = sample_user.id
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=sms_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sms_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path=url_for('v2_notifications.get_notification_by_id', notification_id=sms_notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
path=url_for(
|
||||
"v2_notifications.get_notification_by_id",
|
||||
notification_id=sms_notification.id,
|
||||
),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = response.get_json()
|
||||
assert json_response['created_by_name'] == 'Test User'
|
||||
assert json_response["created_by_name"] == "Test User"
|
||||
|
||||
|
||||
def test_get_notification_by_reference_nonexistent_reference_returns_no_notifications(client, sample_service):
|
||||
def test_get_notification_by_reference_nonexistent_reference_returns_no_notifications(
|
||||
client, sample_service
|
||||
):
|
||||
auth_header = create_service_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/notifications?reference={}'.format('nonexistent-reference'),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?reference={}".format("nonexistent-reference"),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert len(json_response['notifications']) == 0
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert len(json_response["notifications"]) == 0
|
||||
|
||||
|
||||
def test_get_notification_by_id_nonexistent_id(client, sample_notification):
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications/dd4b8b9d-d414-4a83-9256-580046bf18f9',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications/dd4b8b9d-d414-4a83-9256-580046bf18f9",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 404
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
assert json_response == {
|
||||
"errors": [
|
||||
{
|
||||
"error": "NoResultFound",
|
||||
"message": "No result found"
|
||||
}
|
||||
],
|
||||
"status_code": 404
|
||||
"errors": [{"error": "NoResultFound", "message": "No result found"}],
|
||||
"status_code": 404,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("id", ["1234-badly-formatted-id-7890", "0"])
|
||||
def test_get_notification_by_id_invalid_id(client, sample_notification, id):
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications/{}'.format(id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications/{}".format(id),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.headers['Content-type'] == 'application/json'
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
assert json_response == {"errors": [
|
||||
{"error": "ValidationError",
|
||||
"message": "notification_id is not a valid UUID"
|
||||
}],
|
||||
"status_code": 400}
|
||||
assert json_response == {
|
||||
"errors": [
|
||||
{
|
||||
"error": "ValidationError",
|
||||
"message": "notification_id is not a valid UUID",
|
||||
}
|
||||
],
|
||||
"status_code": 400,
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize('template_type', ['sms', 'email'])
|
||||
def test_get_notification_doesnt_have_delivery_estimate_for_non_letters(client, sample_service, template_type):
|
||||
@pytest.mark.parametrize("template_type", ["sms", "email"])
|
||||
def test_get_notification_doesnt_have_delivery_estimate_for_non_letters(
|
||||
client, sample_service, template_type
|
||||
):
|
||||
template = create_template(service=sample_service, template_type=template_type)
|
||||
mocked_notification = create_notification(template=template)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=mocked_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=mocked_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications/{}'.format(mocked_notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
path="/v2/notifications/{}".format(mocked_notification.id),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert 'estimated_delivery' not in json.loads(response.get_data(as_text=True))
|
||||
assert "estimated_delivery" not in json.loads(response.get_data(as_text=True))
|
||||
|
||||
|
||||
def test_get_all_notifications_except_job_notifications_returns_200(client, sample_template, sample_job):
|
||||
create_notification(template=sample_template, job=sample_job) # should not return this job notification
|
||||
def test_get_all_notifications_except_job_notifications_returns_200(
|
||||
client, sample_template, sample_job
|
||||
):
|
||||
create_notification(
|
||||
template=sample_template, job=sample_job
|
||||
) # should not return this job notification
|
||||
notifications = [create_notification(template=sample_template) for _ in range(2)]
|
||||
notification = notifications[-1]
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications")
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 2
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith("/v2/notifications")
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 2
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(notification.id)
|
||||
assert json_response['notifications'][0]['status'] == "created"
|
||||
assert json_response['notifications'][0]['template'] == {
|
||||
'id': str(notification.template.id),
|
||||
'uri': notification.template.get_link(),
|
||||
'version': 1
|
||||
assert json_response["notifications"][0]["id"] == str(notification.id)
|
||||
assert json_response["notifications"][0]["status"] == "created"
|
||||
assert json_response["notifications"][0]["template"] == {
|
||||
"id": str(notification.template.id),
|
||||
"uri": notification.template.get_link(),
|
||||
"version": 1,
|
||||
}
|
||||
assert json_response['notifications'][0]['phone_number'] == "+447700900855"
|
||||
assert json_response['notifications'][0]['type'] == "sms"
|
||||
assert not json_response['notifications'][0]['scheduled_for']
|
||||
assert json_response["notifications"][0]["phone_number"] == "+447700900855"
|
||||
assert json_response["notifications"][0]["type"] == "sms"
|
||||
assert not json_response["notifications"][0]["scheduled_for"]
|
||||
|
||||
|
||||
def test_get_all_notifications_with_include_jobs_arg_returns_200(
|
||||
@@ -261,128 +297,166 @@ def test_get_all_notifications_with_include_jobs_arg_returns_200(
|
||||
):
|
||||
notifications = [
|
||||
create_notification(template=sample_template, job=sample_job),
|
||||
create_notification(template=sample_template)
|
||||
create_notification(template=sample_template),
|
||||
]
|
||||
notification = notifications[-1]
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?include_jobs=true',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?include_jobs=true",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?include_jobs=true")
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 2
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?include_jobs=true"
|
||||
)
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 2
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(notification.id)
|
||||
assert json_response['notifications'][0]['status'] == notification.status
|
||||
assert json_response['notifications'][0]['phone_number'] == notification.to
|
||||
assert json_response['notifications'][0]['type'] == notification.template.template_type
|
||||
assert not json_response['notifications'][0]['scheduled_for']
|
||||
assert json_response["notifications"][0]["id"] == str(notification.id)
|
||||
assert json_response["notifications"][0]["status"] == notification.status
|
||||
assert json_response["notifications"][0]["phone_number"] == notification.to
|
||||
assert (
|
||||
json_response["notifications"][0]["type"] == notification.template.template_type
|
||||
)
|
||||
assert not json_response["notifications"][0]["scheduled_for"]
|
||||
|
||||
|
||||
def test_get_all_notifications_no_notifications_if_no_notifications(client, sample_service):
|
||||
def test_get_all_notifications_no_notifications_if_no_notifications(
|
||||
client, sample_service
|
||||
):
|
||||
auth_header = create_service_authorization_header(service_id=sample_service.id)
|
||||
response = client.get(
|
||||
path='/v2/notifications',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications")
|
||||
assert 'next' not in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 0
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith("/v2/notifications")
|
||||
assert "next" not in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 0
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_template_type(client, sample_service):
|
||||
email_template = create_template(service=sample_service, template_type="email")
|
||||
sms_template = create_template(service=sample_service, template_type="sms")
|
||||
|
||||
notification = create_notification(template=email_template, to_field="don.draper@scdp.biz")
|
||||
notification = create_notification(
|
||||
template=email_template, to_field="don.draper@scdp.biz"
|
||||
)
|
||||
create_notification(template=sms_template)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?template_type=email',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?template_type=email",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?template_type=email")
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 1
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?template_type=email"
|
||||
)
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 1
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(notification.id)
|
||||
assert json_response['notifications'][0]['status'] == "created"
|
||||
assert json_response['notifications'][0]['template'] == {
|
||||
'id': str(email_template.id),
|
||||
'uri': notification.template.get_link(),
|
||||
'version': 1
|
||||
assert json_response["notifications"][0]["id"] == str(notification.id)
|
||||
assert json_response["notifications"][0]["status"] == "created"
|
||||
assert json_response["notifications"][0]["template"] == {
|
||||
"id": str(email_template.id),
|
||||
"uri": notification.template.get_link(),
|
||||
"version": 1,
|
||||
}
|
||||
assert json_response['notifications'][0]['email_address'] == "don.draper@scdp.biz"
|
||||
assert json_response['notifications'][0]['type'] == "email"
|
||||
assert json_response["notifications"][0]["email_address"] == "don.draper@scdp.biz"
|
||||
assert json_response["notifications"][0]["type"] == "email"
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_template_type_invalid_template_type(client, sample_notification):
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
def test_get_all_notifications_filter_by_template_type_invalid_template_type(
|
||||
client, sample_notification
|
||||
):
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?template_type=orange',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?template_type=orange",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
assert json_response['status_code'] == 400
|
||||
assert len(json_response['errors']) == 1
|
||||
assert json_response['errors'][0]['message'] == "template_type orange is not one of [sms, email]"
|
||||
assert json_response["status_code"] == 400
|
||||
assert len(json_response["errors"]) == 1
|
||||
assert (
|
||||
json_response["errors"][0]["message"]
|
||||
== "template_type orange is not one of [sms, email]"
|
||||
)
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_single_status(client, sample_template):
|
||||
notification = create_notification(template=sample_template, status="pending")
|
||||
create_notification(template=sample_template)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?status=pending',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?status=pending",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?status=pending")
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 1
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?status=pending"
|
||||
)
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 1
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(notification.id)
|
||||
assert json_response['notifications'][0]['status'] == "pending"
|
||||
assert json_response["notifications"][0]["id"] == str(notification.id)
|
||||
assert json_response["notifications"][0]["status"] == "pending"
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_status_invalid_status(client, sample_notification):
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
def test_get_all_notifications_filter_by_status_invalid_status(
|
||||
client, sample_notification
|
||||
):
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?status=elephant',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?status=elephant",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 400
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
|
||||
assert json_response['status_code'] == 400
|
||||
assert len(json_response['errors']) == 1
|
||||
assert json_response['errors'][0]['message'] == "status elephant is not one of [cancelled, created, sending, " \
|
||||
"sent, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, " \
|
||||
assert json_response["status_code"] == 400
|
||||
assert len(json_response["errors"]) == 1
|
||||
assert (
|
||||
json_response["errors"][0]["message"]
|
||||
== "status elephant is not one of [cancelled, created, sending, "
|
||||
"sent, delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, "
|
||||
"pending-virus-check, validation-failed, virus-scan-failed]"
|
||||
)
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_multiple_statuses(client, sample_template):
|
||||
@@ -390,22 +464,29 @@ def test_get_all_notifications_filter_by_multiple_statuses(client, sample_templa
|
||||
create_notification(template=sample_template, status=_status)
|
||||
for _status in ["created", "pending", "sending"]
|
||||
]
|
||||
failed_notification = create_notification(template=sample_template, status="permanent-failure")
|
||||
failed_notification = create_notification(
|
||||
template=sample_template, status="permanent-failure"
|
||||
)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notifications[0].service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notifications[0].service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?status=created&status=pending&status=sending',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?status=created&status=pending&status=sending",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?status=created&status=pending&status=sending")
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 3
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?status=created&status=pending&status=sending"
|
||||
)
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 3
|
||||
|
||||
returned_notification_ids = [_n['id'] for _n in json_response['notifications']]
|
||||
returned_notification_ids = [_n["id"] for _n in json_response["notifications"]]
|
||||
for _id in [_notification.id for _notification in notifications]:
|
||||
assert str(_id) in returned_notification_ids
|
||||
|
||||
@@ -413,22 +494,29 @@ def test_get_all_notifications_filter_by_multiple_statuses(client, sample_templa
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_failed_status(client, sample_template):
|
||||
created_notification = create_notification(template=sample_template, status="created")
|
||||
failed_notifications = [create_notification(template=sample_template, status='failed')]
|
||||
auth_header = create_service_authorization_header(service_id=created_notification.service_id)
|
||||
created_notification = create_notification(
|
||||
template=sample_template, status="created"
|
||||
)
|
||||
failed_notifications = [
|
||||
create_notification(template=sample_template, status="failed")
|
||||
]
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=created_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?status=failed',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?status=failed",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?status=failed")
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 1
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith("/v2/notifications?status=failed")
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 1
|
||||
|
||||
returned_notification_ids = [n['id'] for n in json_response['notifications']]
|
||||
returned_notification_ids = [n["id"] for n in json_response["notifications"]]
|
||||
for _id in [_notification.id for _notification in failed_notifications]:
|
||||
assert str(_id) in returned_notification_ids
|
||||
|
||||
@@ -439,79 +527,103 @@ def test_get_all_notifications_filter_by_id(client, sample_template):
|
||||
older_notification = create_notification(template=sample_template)
|
||||
newer_notification = create_notification(template=sample_template)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=newer_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=newer_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?older_than={}'.format(newer_notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?older_than={}".format(newer_notification.id),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?older_than={}".format(newer_notification.id))
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 1
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?older_than={}".format(newer_notification.id)
|
||||
)
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 1
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(older_notification.id)
|
||||
assert json_response["notifications"][0]["id"] == str(older_notification.id)
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_id_invalid_id(client, sample_notification):
|
||||
auth_header = create_service_authorization_header(service_id=sample_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?older_than=1234-badly-formatted-id-7890',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?older_than=1234-badly-formatted-id-7890",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert json_response['status_code'] == 400
|
||||
assert len(json_response['errors']) == 1
|
||||
assert json_response['errors'][0]['message'] == "older_than is not a valid UUID"
|
||||
assert json_response["status_code"] == 400
|
||||
assert len(json_response["errors"]) == 1
|
||||
assert json_response["errors"][0]["message"] == "older_than is not a valid UUID"
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, sample_template):
|
||||
def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(
|
||||
client, sample_template
|
||||
):
|
||||
notification = create_notification(template=sample_template)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?older_than=dd4b8b9d-d414-4a83-9256-580046bf18f9',
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?older_than=dd4b8b9d-d414-4a83-9256-580046bf18f9",
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith(
|
||||
"/v2/notifications?older_than=dd4b8b9d-d414-4a83-9256-580046bf18f9")
|
||||
assert 'next' not in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 0
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?older_than=dd4b8b9d-d414-4a83-9256-580046bf18f9"
|
||||
)
|
||||
assert "next" not in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 0
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(client, sample_template):
|
||||
def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(
|
||||
client, sample_template
|
||||
):
|
||||
notification = create_notification(template=sample_template)
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?older_than={}'.format(notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?older_than={}".format(notification.id),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert json_response['links']['current'].endswith("/v2/notifications?older_than={}".format(notification.id))
|
||||
assert 'next' not in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 0
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
assert json_response["links"]["current"].endswith(
|
||||
"/v2/notifications?older_than={}".format(notification.id)
|
||||
)
|
||||
assert "next" not in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 0
|
||||
|
||||
|
||||
def test_get_all_notifications_filter_multiple_query_parameters(client, sample_email_template):
|
||||
def test_get_all_notifications_filter_multiple_query_parameters(
|
||||
client, sample_email_template
|
||||
):
|
||||
# TODO had to change pending to sending. Is that correct?
|
||||
# this is the notification we are looking for
|
||||
older_notification = create_notification(
|
||||
template=sample_email_template, status="sending")
|
||||
template=sample_email_template, status="sending"
|
||||
)
|
||||
|
||||
# wrong status
|
||||
create_notification(template=sample_email_template)
|
||||
wrong_template = create_template(sample_email_template.service, template_type='sms')
|
||||
wrong_template = create_template(sample_email_template.service, template_type="sms")
|
||||
# wrong template
|
||||
create_notification(template=wrong_template, status="sending")
|
||||
|
||||
@@ -521,28 +633,33 @@ def test_get_all_notifications_filter_multiple_query_parameters(client, sample_e
|
||||
# this notification was created too recently
|
||||
create_notification(template=sample_email_template, status="sending")
|
||||
|
||||
auth_header = create_service_authorization_header(service_id=newer_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=newer_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path='/v2/notifications?status=sending&template_type=email&older_than={}'.format(newer_notification.id),
|
||||
headers=[('Content-Type', 'application/json'), auth_header])
|
||||
path="/v2/notifications?status=sending&template_type=email&older_than={}".format(
|
||||
newer_notification.id
|
||||
),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers['Content-type'] == "application/json"
|
||||
assert response.headers["Content-type"] == "application/json"
|
||||
# query parameters aren't returned in order
|
||||
for url_part in [
|
||||
"/v2/notifications?",
|
||||
"template_type=email",
|
||||
"status=sending",
|
||||
"older_than={}".format(newer_notification.id)
|
||||
"older_than={}".format(newer_notification.id),
|
||||
]:
|
||||
assert url_part in json_response['links']['current']
|
||||
assert url_part in json_response["links"]["current"]
|
||||
|
||||
assert 'next' in json_response['links'].keys()
|
||||
assert len(json_response['notifications']) == 1
|
||||
assert "next" in json_response["links"].keys()
|
||||
assert len(json_response["notifications"]) == 1
|
||||
|
||||
assert json_response['notifications'][0]['id'] == str(older_notification.id)
|
||||
assert json_response["notifications"][0]["id"] == str(older_notification.id)
|
||||
|
||||
|
||||
def test_get_all_notifications_renames_letter_statuses(
|
||||
@@ -550,17 +667,19 @@ def test_get_all_notifications_renames_letter_statuses(
|
||||
sample_notification,
|
||||
sample_email_notification,
|
||||
):
|
||||
auth_header = create_service_authorization_header(service_id=sample_email_notification.service_id)
|
||||
auth_header = create_service_authorization_header(
|
||||
service_id=sample_email_notification.service_id
|
||||
)
|
||||
response = client.get(
|
||||
path=url_for('v2_notifications.get_notifications'),
|
||||
headers=[('Content-Type', 'application/json'), auth_header]
|
||||
path=url_for("v2_notifications.get_notifications"),
|
||||
headers=[("Content-Type", "application/json"), auth_header],
|
||||
)
|
||||
|
||||
json_response = json.loads(response.get_data(as_text=True))
|
||||
assert response.status_code == 200
|
||||
|
||||
for noti in json_response['notifications']:
|
||||
if noti['type'] == 'sms' or noti['type'] == 'email':
|
||||
assert noti['status'] == 'created'
|
||||
for noti in json_response["notifications"]:
|
||||
if noti["type"] == "sms" or noti["type"] == "email":
|
||||
assert noti["status"] == "created"
|
||||
else:
|
||||
pytest.fail()
|
||||
|
||||
@@ -22,7 +22,7 @@ valid_get_with_optionals_json = {
|
||||
"status": [NOTIFICATION_CREATED],
|
||||
"template_type": [EMAIL_TYPE],
|
||||
"include_jobs": "true",
|
||||
"older_than": "a5149c32-f03b-4711-af49-ad6993797d45"
|
||||
"older_than": "a5149c32-f03b-4711-af49-ad6993797d45",
|
||||
}
|
||||
|
||||
|
||||
@@ -31,85 +31,106 @@ def test_get_notifications_valid_json(input):
|
||||
assert validate(input, get_notifications_request) == input
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid_statuses, valid_statuses', [
|
||||
# one invalid status
|
||||
(["elephant"], []),
|
||||
# multiple invalid statuses
|
||||
(["elephant", "giraffe", "cheetah"], []),
|
||||
# one bad status and one good status
|
||||
(["elephant"], ["created"]),
|
||||
])
|
||||
def test_get_notifications_request_invalid_statuses(
|
||||
invalid_statuses, valid_statuses
|
||||
):
|
||||
partial_error_status = "is not one of " \
|
||||
"[cancelled, created, sending, sent, delivered, pending, failed, " \
|
||||
"technical-failure, temporary-failure, permanent-failure, pending-virus-check, " \
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_statuses, valid_statuses",
|
||||
[
|
||||
# one invalid status
|
||||
(["elephant"], []),
|
||||
# multiple invalid statuses
|
||||
(["elephant", "giraffe", "cheetah"], []),
|
||||
# one bad status and one good status
|
||||
(["elephant"], ["created"]),
|
||||
],
|
||||
)
|
||||
def test_get_notifications_request_invalid_statuses(invalid_statuses, valid_statuses):
|
||||
partial_error_status = (
|
||||
"is not one of "
|
||||
"[cancelled, created, sending, sent, delivered, pending, failed, "
|
||||
"technical-failure, temporary-failure, permanent-failure, pending-virus-check, "
|
||||
"validation-failed, virus-scan-failed]"
|
||||
)
|
||||
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate({'status': invalid_statuses + valid_statuses}, get_notifications_request)
|
||||
validate(
|
||||
{"status": invalid_statuses + valid_statuses}, get_notifications_request
|
||||
)
|
||||
|
||||
errors = json.loads(str(e.value)).get('errors')
|
||||
errors = json.loads(str(e.value)).get("errors")
|
||||
assert len(errors) == len(invalid_statuses)
|
||||
for index, value in enumerate(invalid_statuses):
|
||||
assert errors[index]['message'] == "status {} {}".format(value, partial_error_status)
|
||||
assert errors[index]["message"] == "status {} {}".format(
|
||||
value, partial_error_status
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid_template_types, valid_template_types', [
|
||||
# one invalid template_type
|
||||
(["orange"], []),
|
||||
# multiple invalid template_types
|
||||
(["orange", "avocado", "banana"], []),
|
||||
# one bad template_type and one good template_type
|
||||
(["orange"], ["sms"]),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_template_types, valid_template_types",
|
||||
[
|
||||
# one invalid template_type
|
||||
(["orange"], []),
|
||||
# multiple invalid template_types
|
||||
(["orange", "avocado", "banana"], []),
|
||||
# one bad template_type and one good template_type
|
||||
(["orange"], ["sms"]),
|
||||
],
|
||||
)
|
||||
def test_get_notifications_request_invalid_template_types(
|
||||
invalid_template_types, valid_template_types
|
||||
invalid_template_types, valid_template_types
|
||||
):
|
||||
partial_error_template_type = "is not one of [sms, email]"
|
||||
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate({'template_type': invalid_template_types + valid_template_types}, get_notifications_request)
|
||||
validate(
|
||||
{"template_type": invalid_template_types + valid_template_types},
|
||||
get_notifications_request,
|
||||
)
|
||||
|
||||
errors = json.loads(str(e.value)).get('errors')
|
||||
errors = json.loads(str(e.value)).get("errors")
|
||||
assert len(errors) == len(invalid_template_types)
|
||||
for index, value in enumerate(invalid_template_types):
|
||||
assert errors[index]['message'] == "template_type {} {}".format(value, partial_error_template_type)
|
||||
assert errors[index]["message"] == "template_type {} {}".format(
|
||||
value, partial_error_template_type
|
||||
)
|
||||
|
||||
|
||||
def test_get_notifications_request_invalid_statuses_and_template_types():
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate({
|
||||
'status': ["created", "elephant", "giraffe"],
|
||||
'template_type': ["sms", "orange", "avocado"]
|
||||
}, get_notifications_request)
|
||||
validate(
|
||||
{
|
||||
"status": ["created", "elephant", "giraffe"],
|
||||
"template_type": ["sms", "orange", "avocado"],
|
||||
},
|
||||
get_notifications_request,
|
||||
)
|
||||
|
||||
errors = json.loads(str(e.value)).get('errors')
|
||||
errors = json.loads(str(e.value)).get("errors")
|
||||
|
||||
assert len(errors) == 4
|
||||
|
||||
error_messages = [error['message'] for error in errors]
|
||||
error_messages = [error["message"] for error in errors]
|
||||
for invalid_status in ["elephant", "giraffe"]:
|
||||
assert "status {} is not one of [cancelled, created, sending, sent, delivered, " \
|
||||
"pending, failed, technical-failure, temporary-failure, permanent-failure, " \
|
||||
assert (
|
||||
"status {} is not one of [cancelled, created, sending, sent, delivered, "
|
||||
"pending, failed, technical-failure, temporary-failure, permanent-failure, "
|
||||
"pending-virus-check, validation-failed, virus-scan-failed]".format(
|
||||
invalid_status
|
||||
) in error_messages
|
||||
)
|
||||
in error_messages
|
||||
)
|
||||
|
||||
for invalid_template_type in ["orange", "avocado"]:
|
||||
assert "template_type {} is not one of [sms, email]" \
|
||||
.format(invalid_template_type) in error_messages
|
||||
assert (
|
||||
"template_type {} is not one of [sms, email]".format(invalid_template_type)
|
||||
in error_messages
|
||||
)
|
||||
|
||||
|
||||
valid_json = {"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4())
|
||||
}
|
||||
valid_json = {"phone_number": "2028675309", "template_id": str(uuid.uuid4())}
|
||||
valid_json_with_optionals = {
|
||||
"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"reference": "reference from caller",
|
||||
"personalisation": {"key": "value"}
|
||||
"personalisation": {"key": "value"},
|
||||
}
|
||||
|
||||
|
||||
@@ -118,28 +139,29 @@ def test_post_sms_schema_is_valid(input):
|
||||
assert validate(input, post_sms_request_schema) == input
|
||||
|
||||
|
||||
@pytest.mark.parametrize("template_id",
|
||||
['2ebe4da8-17be-49fe-b02f-dff2760261a0' + "\n",
|
||||
'2ebe4da8-17be-49fe-b02f-dff2760261a0' + " ",
|
||||
'2ebe4da8-17be-49fe-b02f-dff2760261a0' + "\r",
|
||||
"\t" + '2ebe4da8-17be-49fe-b02f-dff2760261a0',
|
||||
'2ebe4da8-17be-49fe-b02f-dff2760261a0'[4:],
|
||||
"bad_uuid"
|
||||
]
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"template_id",
|
||||
[
|
||||
"2ebe4da8-17be-49fe-b02f-dff2760261a0" + "\n",
|
||||
"2ebe4da8-17be-49fe-b02f-dff2760261a0" + " ",
|
||||
"2ebe4da8-17be-49fe-b02f-dff2760261a0" + "\r",
|
||||
"\t" + "2ebe4da8-17be-49fe-b02f-dff2760261a0",
|
||||
"2ebe4da8-17be-49fe-b02f-dff2760261a0"[4:],
|
||||
"bad_uuid",
|
||||
],
|
||||
)
|
||||
def test_post_sms_json_schema_bad_uuid(template_id):
|
||||
j = {
|
||||
"template_id": template_id,
|
||||
"phone_number": "2028675309"
|
||||
}
|
||||
j = {"template_id": template_id, "phone_number": "2028675309"}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_sms_request_schema)
|
||||
error = json.loads(str(e.value))
|
||||
assert len(error.keys()) == 2
|
||||
assert error.get('status_code') == 400
|
||||
assert len(error.get('errors')) == 1
|
||||
assert {'error': 'ValidationError',
|
||||
'message': "template_id is not a valid UUID"} in error['errors']
|
||||
assert error.get("status_code") == 400
|
||||
assert len(error.get("errors")) == 1
|
||||
assert {
|
||||
"error": "ValidationError",
|
||||
"message": "template_id is not a valid UUID",
|
||||
} in error["errors"]
|
||||
|
||||
|
||||
def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
|
||||
@@ -148,12 +170,16 @@ def test_post_sms_json_schema_bad_uuid_and_missing_phone_number():
|
||||
validate(j, post_sms_request_schema)
|
||||
error = json.loads(str(e.value))
|
||||
assert len(error.keys()) == 2
|
||||
assert error.get('status_code') == 400
|
||||
assert len(error.get('errors')) == 2
|
||||
assert {'error': 'ValidationError',
|
||||
'message': "phone_number is a required property"} in error['errors']
|
||||
assert {'error': 'ValidationError',
|
||||
'message': "template_id is not a valid UUID"} in error['errors']
|
||||
assert error.get("status_code") == 400
|
||||
assert len(error.get("errors")) == 2
|
||||
assert {
|
||||
"error": "ValidationError",
|
||||
"message": "phone_number is a required property",
|
||||
} in error["errors"]
|
||||
assert {
|
||||
"error": "ValidationError",
|
||||
"message": "template_id is not a valid UUID",
|
||||
} in error["errors"]
|
||||
|
||||
|
||||
def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
||||
@@ -161,61 +187,79 @@ def test_post_sms_schema_with_personalisation_that_is_not_a_dict():
|
||||
"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"reference": "reference from caller",
|
||||
"personalisation": "not_a_dict"
|
||||
"personalisation": "not_a_dict",
|
||||
}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_sms_request_schema)
|
||||
error = json.loads(str(e.value))
|
||||
assert len(error.get('errors')) == 1
|
||||
assert error['errors'] == [{'error': 'ValidationError',
|
||||
'message': "personalisation not_a_dict is not of type object"}]
|
||||
assert error.get('status_code') == 400
|
||||
assert len(error.get("errors")) == 1
|
||||
assert error["errors"] == [
|
||||
{
|
||||
"error": "ValidationError",
|
||||
"message": "personalisation not_a_dict is not of type object",
|
||||
}
|
||||
]
|
||||
assert error.get("status_code") == 400
|
||||
assert len(error.keys()) == 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize('invalid_phone_number, err_msg', [
|
||||
('08515111111', 'phone_number Phone number is not possible'),
|
||||
('07515111*11', 'phone_number Not enough digits'),
|
||||
('notaphoneumber', 'phone_number The string supplied did not seem to be a phone number.'),
|
||||
(7700900001, 'phone_number 7700900001 is not of type string'),
|
||||
(None, 'phone_number None is not of type string'),
|
||||
([], 'phone_number [] is not of type string'),
|
||||
({}, 'phone_number {} is not of type string'),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_phone_number, err_msg",
|
||||
[
|
||||
("08515111111", "phone_number Phone number is not possible"),
|
||||
("07515111*11", "phone_number Not enough digits"),
|
||||
(
|
||||
"notaphoneumber",
|
||||
"phone_number The string supplied did not seem to be a phone number.",
|
||||
),
|
||||
(7700900001, "phone_number 7700900001 is not of type string"),
|
||||
(None, "phone_number None is not of type string"),
|
||||
([], "phone_number [] is not of type string"),
|
||||
({}, "phone_number {} is not of type string"),
|
||||
],
|
||||
)
|
||||
def test_post_sms_request_schema_invalid_phone_number(invalid_phone_number, err_msg):
|
||||
j = {"phone_number": invalid_phone_number,
|
||||
"template_id": str(uuid.uuid4())
|
||||
}
|
||||
j = {"phone_number": invalid_phone_number, "template_id": str(uuid.uuid4())}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_sms_request_schema)
|
||||
errors = json.loads(str(e.value)).get('errors')
|
||||
errors = json.loads(str(e.value)).get("errors")
|
||||
assert len(errors) == 1
|
||||
assert {"error": "ValidationError", "message": err_msg} == errors[0]
|
||||
|
||||
|
||||
def test_post_sms_request_schema_invalid_phone_number_and_missing_template():
|
||||
j = {"phone_number": '5558675309',
|
||||
}
|
||||
j = {
|
||||
"phone_number": "5558675309",
|
||||
}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_sms_request_schema)
|
||||
errors = json.loads(str(e.value)).get('errors')
|
||||
errors = json.loads(str(e.value)).get("errors")
|
||||
assert len(errors) == 2
|
||||
assert {"error": "ValidationError", "message": "phone_number Phone number range is not in use"} in errors
|
||||
assert {"error": "ValidationError", "message": "template_id is a required property"} in errors
|
||||
assert {
|
||||
"error": "ValidationError",
|
||||
"message": "phone_number Phone number range is not in use",
|
||||
} in errors
|
||||
assert {
|
||||
"error": "ValidationError",
|
||||
"message": "template_id is a required property",
|
||||
} in errors
|
||||
|
||||
|
||||
valid_post_email_json = {"email_address": "test@example.gov.uk",
|
||||
"template_id": str(uuid.uuid4())
|
||||
}
|
||||
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"}
|
||||
"personalisation": {"key": "value"},
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("input", [valid_post_email_json, valid_post_email_json_with_optionals])
|
||||
@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_schema) == input
|
||||
|
||||
@@ -226,20 +270,23 @@ def test_post_email_schema_bad_uuid_and_missing_email_address():
|
||||
validate(j, post_email_request_schema)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('email_address, err_msg', [
|
||||
('example', 'email_address Not a valid email address'),
|
||||
(12345, 'email_address 12345 is not of type string'),
|
||||
('with(brackets)@example.com', 'email_address Not a valid email address'),
|
||||
(None, 'email_address None is not of type string'),
|
||||
([], 'email_address [] is not of type string'),
|
||||
({}, 'email_address {} is not of type string'),
|
||||
])
|
||||
@pytest.mark.parametrize(
|
||||
"email_address, err_msg",
|
||||
[
|
||||
("example", "email_address Not a valid email address"),
|
||||
(12345, "email_address 12345 is not of type string"),
|
||||
("with(brackets)@example.com", "email_address Not a valid email address"),
|
||||
(None, "email_address None is not of type string"),
|
||||
([], "email_address [] is not of type string"),
|
||||
({}, "email_address {} is not of type string"),
|
||||
],
|
||||
)
|
||||
def test_post_email_schema_invalid_email_address(email_address, err_msg):
|
||||
j = {"template_id": str(uuid.uuid4()), "email_address": email_address}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_email_request_schema)
|
||||
|
||||
errors = json.loads(str(e.value)).get('errors')
|
||||
errors = json.loads(str(e.value)).get("errors")
|
||||
assert len(errors) == 1
|
||||
assert {"error": "ValidationError", "message": err_msg} == errors[0]
|
||||
|
||||
@@ -247,25 +294,25 @@ def test_post_email_schema_invalid_email_address(email_address, err_msg):
|
||||
def valid_email_response():
|
||||
return {
|
||||
"id": str(uuid.uuid4()),
|
||||
"content": {"body": "the body of the message",
|
||||
"subject": "subject of the message",
|
||||
"from_email": "service@dig.gov.uk"},
|
||||
"content": {
|
||||
"body": "the body of the message",
|
||||
"subject": "subject of the message",
|
||||
"from_email": "service@dig.gov.uk",
|
||||
},
|
||||
"uri": "http://notify.api/v2/notifications/id",
|
||||
"template": {
|
||||
"id": str(uuid.uuid4()),
|
||||
"version": 1,
|
||||
"uri": "http://notify.api/v2/template/id"
|
||||
"uri": "http://notify.api/v2/template/id",
|
||||
},
|
||||
"scheduled_for": ""
|
||||
"scheduled_for": "",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("schema",
|
||||
[post_email_request_schema, post_sms_request_schema])
|
||||
@pytest.mark.parametrize("schema", [post_email_request_schema, post_sms_request_schema])
|
||||
@freeze_time("2017-05-12 13:00:00")
|
||||
def test_post_schema_valid_scheduled_for(schema):
|
||||
j = {"template_id": str(uuid.uuid4()),
|
||||
"scheduled_for": "2017-05-12 13:15"}
|
||||
j = {"template_id": str(uuid.uuid4()), "scheduled_for": "2017-05-12 13:15"}
|
||||
if schema == post_email_request_schema:
|
||||
j.update({"email_address": "joe@gmail.com"})
|
||||
else:
|
||||
@@ -273,16 +320,13 @@ def test_post_schema_valid_scheduled_for(schema):
|
||||
assert validate(j, schema) == j
|
||||
|
||||
|
||||
@pytest.mark.parametrize("invalid_datetime",
|
||||
["13:00:00 2017-01-01",
|
||||
"2017-31-12 13:00:00",
|
||||
"01-01-2017T14:00:00.0000Z"
|
||||
])
|
||||
@pytest.mark.parametrize("schema",
|
||||
[post_email_request_schema, post_sms_request_schema])
|
||||
@pytest.mark.parametrize(
|
||||
"invalid_datetime",
|
||||
["13:00:00 2017-01-01", "2017-31-12 13:00:00", "01-01-2017T14:00:00.0000Z"],
|
||||
)
|
||||
@pytest.mark.parametrize("schema", [post_email_request_schema, post_sms_request_schema])
|
||||
def test_post_email_schema_invalid_scheduled_for(invalid_datetime, schema):
|
||||
j = {"template_id": str(uuid.uuid4()),
|
||||
"scheduled_for": invalid_datetime}
|
||||
j = {"template_id": str(uuid.uuid4()), "scheduled_for": invalid_datetime}
|
||||
if schema == post_email_request_schema:
|
||||
j.update({"email_address": "joe@gmail.com"})
|
||||
else:
|
||||
@@ -290,34 +334,50 @@ def test_post_email_schema_invalid_scheduled_for(invalid_datetime, schema):
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, schema)
|
||||
error = json.loads(str(e.value))
|
||||
assert error['status_code'] == 400
|
||||
assert error['errors'] == [{'error': 'ValidationError',
|
||||
'message': "scheduled_for datetime format is invalid. "
|
||||
"It must be a valid ISO8601 date time format, "
|
||||
"https://en.wikipedia.org/wiki/ISO_8601"}]
|
||||
assert error["status_code"] == 400
|
||||
assert error["errors"] == [
|
||||
{
|
||||
"error": "ValidationError",
|
||||
"message": "scheduled_for datetime format is invalid. "
|
||||
"It must be a valid ISO8601 date time format, "
|
||||
"https://en.wikipedia.org/wiki/ISO_8601",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@freeze_time("2017-05-12 13:00:00")
|
||||
def test_scheduled_for_raises_validation_error_when_in_the_past():
|
||||
j = {"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"scheduled_for": "2017-05-12 10:00"}
|
||||
j = {
|
||||
"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"scheduled_for": "2017-05-12 10:00",
|
||||
}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_sms_request_schema)
|
||||
error = json.loads(str(e.value))
|
||||
assert error['status_code'] == 400
|
||||
assert error['errors'] == [{'error': 'ValidationError',
|
||||
'message': "scheduled_for datetime can not be in the past"}]
|
||||
assert error["status_code"] == 400
|
||||
assert error["errors"] == [
|
||||
{
|
||||
"error": "ValidationError",
|
||||
"message": "scheduled_for datetime can not be in the past",
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@freeze_time("2017-05-12 13:00:00")
|
||||
def test_scheduled_for_raises_validation_error_when_more_than_24_hours_in_the_future():
|
||||
j = {"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"scheduled_for": "2017-05-13 14:00"}
|
||||
j = {
|
||||
"phone_number": "2028675309",
|
||||
"template_id": str(uuid.uuid4()),
|
||||
"scheduled_for": "2017-05-13 14:00",
|
||||
}
|
||||
with pytest.raises(ValidationError) as e:
|
||||
validate(j, post_sms_request_schema)
|
||||
error = json.loads(str(e.value))
|
||||
assert error['status_code'] == 400
|
||||
assert error['errors'] == [{'error': 'ValidationError',
|
||||
'message': "scheduled_for datetime can only be 24 hours in the future"}]
|
||||
assert error["status_code"] == 400
|
||||
assert error["errors"] == [
|
||||
{
|
||||
"error": "ValidationError",
|
||||
"message": "scheduled_for datetime can only be 24 hours in the future",
|
||||
}
|
||||
]
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user