2017-09-11 14:16:04 +01:00
|
|
|
import datetime
|
2016-11-22 17:50:29 +00:00
|
|
|
import pytest
|
2017-09-08 16:38:36 +01:00
|
|
|
from flask import json, url_for
|
2016-11-18 17:36:11 +00:00
|
|
|
|
|
|
|
|
from app import DATETIME_FORMAT
|
|
|
|
|
from tests import create_authorization_header
|
2017-05-25 11:41:07 +01:00
|
|
|
from tests.app.db import (
|
|
|
|
|
create_notification,
|
|
|
|
|
create_template,
|
|
|
|
|
create_service)
|
2016-11-18 17:36:11 +00:00
|
|
|
|
2017-09-18 11:00:21 +01:00
|
|
|
from tests.app.conftest import (
|
|
|
|
|
sample_notification,
|
|
|
|
|
sample_email_notification,
|
|
|
|
|
)
|
|
|
|
|
|
2016-11-18 17:36:11 +00:00
|
|
|
|
2016-11-22 17:50:29 +00:00
|
|
|
@pytest.mark.parametrize('billable_units, provider', [
|
|
|
|
|
(1, 'mmg'),
|
|
|
|
|
(0, 'mmg'),
|
|
|
|
|
(1, None)
|
|
|
|
|
])
|
|
|
|
|
def test_get_notification_by_id_returns_200(
|
2017-05-25 11:41:07 +01:00
|
|
|
client, billable_units, provider, sample_template
|
2016-11-22 17:50:29 +00:00
|
|
|
):
|
2017-09-08 16:38:36 +01:00
|
|
|
sample_notification = create_notification(
|
|
|
|
|
template=sample_template,
|
|
|
|
|
billable_units=billable_units,
|
|
|
|
|
sent_by=provider,
|
|
|
|
|
scheduled_for="2017-05-12 15:15"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
another = create_notification(
|
|
|
|
|
template=sample_template,
|
|
|
|
|
billable_units=billable_units,
|
|
|
|
|
sent_by=provider,
|
|
|
|
|
scheduled_for="2017-06-12 15:15"
|
|
|
|
|
)
|
2017-05-25 11:41:07 +01:00
|
|
|
|
2016-11-22 17:50:29 +00:00
|
|
|
auth_header = create_authorization_header(service_id=sample_notification.service_id)
|
2016-11-18 17:36:11 +00:00
|
|
|
response = client.get(
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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),
|
2016-12-15 16:19:55 +00:00
|
|
|
'body': sample_notification.template.content,
|
|
|
|
|
"subject": None,
|
|
|
|
|
'sent_at': sample_notification.sent_at,
|
2017-05-15 17:27:38 +01:00
|
|
|
'completed_at': sample_notification.completed_at(),
|
2017-05-24 16:27:15 +01:00
|
|
|
'scheduled_for': '2017-05-12T14:15:00.000000Z'
|
2016-12-15 16:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert json_response == expected_response
|
|
|
|
|
|
|
|
|
|
|
2016-12-15 16:30:38 +00:00
|
|
|
def test_get_notification_by_id_with_placeholders_returns_200(
|
2017-05-25 11:41:07 +01:00
|
|
|
client, sample_email_template_with_placeholders
|
2016-12-15 16:19:55 +00:00
|
|
|
):
|
2017-09-08 16:38:36 +01:00
|
|
|
sample_notification = create_notification(
|
|
|
|
|
template=sample_email_template_with_placeholders,
|
|
|
|
|
personalisation={"name": "Bob"}
|
|
|
|
|
)
|
2016-12-15 16:19:55 +00:00
|
|
|
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
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']
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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),
|
|
|
|
|
'body': "Hello Bob\nThis is an email from GOV.\u200bUK",
|
|
|
|
|
"subject": "Bob",
|
2016-11-18 17:36:11 +00:00
|
|
|
'sent_at': sample_notification.sent_at,
|
2017-05-15 17:27:38 +01:00
|
|
|
'completed_at': sample_notification.completed_at(),
|
|
|
|
|
'scheduled_for': None
|
2016-11-18 17:36:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert json_response == expected_response
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
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')
|
2016-12-07 14:09:56 +00:00
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_notification_with_reference.service_id)
|
|
|
|
|
response = client.get(
|
2016-12-12 18:06:14 +00:00
|
|
|
path='/v2/notifications?reference={}'.format(sample_notification_with_reference.client_reference),
|
2016-12-07 14:09:56 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == 'application/json'
|
|
|
|
|
|
2016-12-12 18:06:14 +00:00
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert len(json_response['notifications']) == 1
|
2016-12-07 14:09:56 +00:00
|
|
|
|
2016-12-12 18:06:14 +00:00
|
|
|
assert json_response['notifications'][0]['id'] == str(sample_notification_with_reference.id)
|
|
|
|
|
assert json_response['notifications'][0]['reference'] == "some-client-reference"
|
|
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
def test_get_notifications_returns_scheduled_for(client, sample_template):
|
|
|
|
|
sample_notification_with_reference = create_notification(template=sample_template,
|
|
|
|
|
client_reference='some-client-reference',
|
|
|
|
|
scheduled_for='2017-05-23 17:15')
|
2017-05-15 17:27:38 +01:00
|
|
|
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == 'application/json'
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert len(json_response['notifications']) == 1
|
|
|
|
|
|
|
|
|
|
assert json_response['notifications'][0]['id'] == str(sample_notification_with_reference.id)
|
2017-05-24 16:27:15 +01:00
|
|
|
assert json_response['notifications'][0]['scheduled_for'] == "2017-05-23T16:15:00.000000Z"
|
2017-05-15 17:27:38 +01:00
|
|
|
|
|
|
|
|
|
2016-12-12 18:06:14 +00:00
|
|
|
def test_get_notification_by_reference_nonexistent_reference_returns_no_notifications(client, sample_service):
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
2016-12-07 14:09:56 +00:00
|
|
|
response = client.get(
|
2016-12-12 18:06:14 +00:00
|
|
|
path='/v2/notifications?reference={}'.format('nonexistent-reference'),
|
2016-12-07 14:09:56 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
2016-12-12 18:06:14 +00:00
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == "application/json"
|
|
|
|
|
assert len(json_response['notifications']) == 0
|
2016-12-07 14:09:56 +00:00
|
|
|
|
|
|
|
|
|
2016-11-28 15:12:03 +00:00
|
|
|
def test_get_notification_by_id_nonexistent_id(client, sample_notification):
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-18 09:56:26 +00:00
|
|
|
@pytest.mark.parametrize("id", ["1234-badly-formatted-id-7890", "0"])
|
|
|
|
|
def test_get_notification_by_id_invalid_id(client, sample_notification, id):
|
2016-11-28 15:12:03 +00:00
|
|
|
auth_header = create_authorization_header(service_id=sample_notification.service_id)
|
|
|
|
|
response = client.get(
|
2017-01-18 09:56:26 +00:00
|
|
|
path='/v2/notifications/{}'.format(id),
|
2016-11-28 15:12:03 +00:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header])
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 404
|
|
|
|
|
assert response.headers['Content-type'] == 'application/json'
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert json_response == {
|
|
|
|
|
"message": "The requested URL was not found on the server. "
|
|
|
|
|
"If you entered the URL manually please check your spelling and try again.",
|
|
|
|
|
"result": "error"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-11 14:16:04 +01:00
|
|
|
@pytest.mark.parametrize('created_at_month, estimated_delivery', [
|
|
|
|
|
(
|
|
|
|
|
12, '2000-12-06T16:00:00.000000Z', # 4pm GMT in winter
|
|
|
|
|
),
|
|
|
|
|
(
|
|
|
|
|
6, '2000-06-05T15:00:00.000000Z', # 4pm BST in summer
|
|
|
|
|
),
|
|
|
|
|
])
|
|
|
|
|
def test_get_notification_adds_delivery_estimate_for_letters(
|
|
|
|
|
client,
|
|
|
|
|
sample_letter_notification,
|
|
|
|
|
created_at_month,
|
|
|
|
|
estimated_delivery,
|
|
|
|
|
):
|
|
|
|
|
sample_letter_notification.created_at = datetime.date(2000, created_at_month, 1)
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_letter_notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
path='/v2/notifications/{}'.format(sample_letter_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 json_response['estimated_delivery'] == estimated_delivery
|
|
|
|
|
|
|
|
|
|
|
2017-09-18 11:00:21 +01:00
|
|
|
@pytest.mark.parametrize('notification_mock', [
|
|
|
|
|
sample_notification,
|
|
|
|
|
sample_email_notification,
|
|
|
|
|
])
|
|
|
|
|
def test_get_notification_doesnt_have_delivery_estimate_for_non_letters(
|
|
|
|
|
client,
|
|
|
|
|
notify_db,
|
|
|
|
|
notify_db_session,
|
|
|
|
|
notification_mock,
|
|
|
|
|
):
|
|
|
|
|
mocked_notification = notification_mock(notify_db, notify_db_session)
|
|
|
|
|
auth_header = create_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]
|
|
|
|
|
)
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert 'estimated_delivery' not in json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
|
2017-11-16 16:02:31 +00:00
|
|
|
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
|
2017-05-25 11:41:07 +01:00
|
|
|
notifications = [create_notification(template=sample_template) for _ in range(2)]
|
2016-11-24 10:31:14 +00:00
|
|
|
notification = notifications[-1]
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications")
|
2016-11-24 10:31:14 +00:00
|
|
|
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]['phone_number'] == "+447700900855"
|
|
|
|
|
assert json_response['notifications'][0]['type'] == "sms"
|
2017-05-15 17:27:38 +01:00
|
|
|
assert not json_response['notifications'][0]['scheduled_for']
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
|
2017-11-16 14:45:02 +00:00
|
|
|
def test_get_all_notifications_with_include_jobs_arg_returns_200(
|
|
|
|
|
client, sample_template, sample_job
|
|
|
|
|
):
|
|
|
|
|
notifications = [
|
|
|
|
|
create_notification(template=sample_template, job=sample_job),
|
|
|
|
|
create_notification(template=sample_template)
|
|
|
|
|
]
|
|
|
|
|
notification = notifications[-1]
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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['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']
|
|
|
|
|
|
|
|
|
|
|
2016-11-28 15:12:03 +00:00
|
|
|
def test_get_all_notifications_no_notifications_if_no_notifications(client, sample_service):
|
2016-11-24 10:31:14 +00:00
|
|
|
auth_header = create_authorization_header(service_id=sample_service.id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications")
|
2016-11-24 10:31:14 +00:00
|
|
|
assert 'next' not in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 0
|
|
|
|
|
|
|
|
|
|
|
2017-11-28 10:42:54 +00:00
|
|
|
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")
|
2016-11-24 10:31:14 +00:00
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
notification = create_notification(template=email_template, to_field="don.draper@scdp.biz")
|
|
|
|
|
create_notification(template=sms_template)
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications?template_type=email")
|
2016-11-24 10:31:14 +00:00
|
|
|
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),
|
2017-11-09 14:48:27 +00:00
|
|
|
'uri': notification.template.get_link(),
|
2016-11-24 10:31:14 +00:00
|
|
|
'version': 1
|
|
|
|
|
}
|
|
|
|
|
assert json_response['notifications'][0]['email_address'] == "don.draper@scdp.biz"
|
|
|
|
|
assert json_response['notifications'][0]['type'] == "email"
|
|
|
|
|
|
|
|
|
|
|
2016-11-28 15:12:03 +00:00
|
|
|
def test_get_all_notifications_filter_by_template_type_invalid_template_type(client, sample_notification):
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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 json_response['status_code'] == 400
|
|
|
|
|
assert len(json_response['errors']) == 1
|
2016-12-15 16:35:27 +00:00
|
|
|
assert json_response['errors'][0]['message'] == "template_type orange is not one of [sms, email, letter]"
|
2016-11-28 15:12:03 +00:00
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
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)
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications?status=pending")
|
2016-11-24 10:31:14 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
|
2016-11-28 15:12:03 +00:00
|
|
|
def test_get_all_notifications_filter_by_status_invalid_status(client, sample_notification):
|
|
|
|
|
auth_header = create_authorization_header(service_id=sample_notification.service_id)
|
2016-11-28 14:22:51 +00:00
|
|
|
response = client.get(
|
2016-11-28 15:10:13 +00:00
|
|
|
path='/v2/notifications?status=elephant',
|
2016-11-28 14:22:51 +00:00
|
|
|
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 json_response['status_code'] == 400
|
2016-11-28 15:10:13 +00:00
|
|
|
assert len(json_response['errors']) == 1
|
2017-04-25 17:08:29 +01:00
|
|
|
assert json_response['errors'][0]['message'] == "status elephant is not one of [created, sending, sent, " \
|
2017-10-23 16:24:55 +01:00
|
|
|
"delivered, pending, failed, technical-failure, temporary-failure, permanent-failure, accepted, received]"
|
2016-11-28 14:22:51 +00:00
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
def test_get_all_notifications_filter_by_multiple_statuses(client, sample_template):
|
2016-11-24 10:31:14 +00:00
|
|
|
notifications = [
|
2017-05-25 11:41:07 +01:00
|
|
|
create_notification(template=sample_template, status=_status)
|
2016-11-24 10:31:14 +00:00
|
|
|
for _status in ["created", "pending", "sending"]
|
|
|
|
|
]
|
2017-05-25 11:41:07 +01:00
|
|
|
failed_notification = create_notification(template=sample_template, status="permanent-failure")
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == "application/json"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications?status=created&status=pending&status=sending")
|
2016-11-24 10:31:14 +00:00
|
|
|
assert 'next' in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 3
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
assert failed_notification.id not in returned_notification_ids
|
|
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
def test_get_all_notifications_filter_by_failed_status(client, sample_template):
|
|
|
|
|
created_notification = create_notification(template=sample_template, status="created")
|
2016-11-25 14:55:45 +00:00
|
|
|
failed_notifications = [
|
2017-05-25 11:41:07 +01:00
|
|
|
create_notification(template=sample_template, status=_status)
|
2016-11-25 14:55:45 +00:00
|
|
|
for _status in ["technical-failure", "temporary-failure", "permanent-failure"]
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=created_notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications?status=failed")
|
2016-11-25 14:55:45 +00:00
|
|
|
assert 'next' in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 3
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
assert created_notification.id not in returned_notification_ids
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
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)
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == "application/json"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications?older_than={}".format(newer_notification.id))
|
2016-11-24 10:31:14 +00:00
|
|
|
assert 'next' in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 1
|
|
|
|
|
|
|
|
|
|
assert json_response['notifications'][0]['id'] == str(older_notification.id)
|
|
|
|
|
|
|
|
|
|
|
2016-11-28 15:12:03 +00:00
|
|
|
def test_get_all_notifications_filter_by_id_invalid_id(client, sample_notification):
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, sample_template):
|
|
|
|
|
notification = create_notification(template=sample_template)
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == "application/json"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith(
|
|
|
|
|
"/v2/notifications?older_than=dd4b8b9d-d414-4a83-9256-580046bf18f9")
|
2016-11-24 10:31:14 +00:00
|
|
|
assert 'next' not in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 0
|
|
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
def test_get_all_notifications_filter_by_id_no_notifications_if_last_notification(client, sample_template):
|
|
|
|
|
notification = create_notification(template=sample_template)
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_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])
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
|
|
|
|
|
assert response.status_code == 200
|
|
|
|
|
assert response.headers['Content-type'] == "application/json"
|
2016-11-28 11:13:11 +00:00
|
|
|
assert json_response['links']['current'].endswith("/v2/notifications?older_than={}".format(notification.id))
|
2016-11-24 10:31:14 +00:00
|
|
|
assert 'next' not in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 0
|
|
|
|
|
|
|
|
|
|
|
2017-05-25 11:41:07 +01:00
|
|
|
def test_get_all_notifications_filter_multiple_query_parameters(client, sample_email_template):
|
2016-11-24 10:31:14 +00:00
|
|
|
# this is the notification we are looking for
|
2017-05-25 11:41:07 +01:00
|
|
|
older_notification = create_notification(
|
|
|
|
|
template=sample_email_template, status="pending")
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
# wrong status
|
2017-05-25 11:41:07 +01:00
|
|
|
create_notification(template=sample_email_template)
|
|
|
|
|
wrong_template = create_template(sample_email_template.service, template_type='sms')
|
2016-11-24 10:31:14 +00:00
|
|
|
# wrong template
|
2017-05-25 11:41:07 +01:00
|
|
|
create_notification(template=wrong_template, status="pending")
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
# we only want notifications created before this one
|
2017-05-25 11:41:07 +01:00
|
|
|
newer_notification = create_notification(template=sample_email_template)
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
# this notification was created too recently
|
2017-05-25 11:41:07 +01:00
|
|
|
create_notification(template=sample_email_template, status="pending")
|
2016-11-24 10:31:14 +00:00
|
|
|
|
|
|
|
|
auth_header = create_authorization_header(service_id=newer_notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
path='/v2/notifications?status=pending&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"
|
|
|
|
|
# query parameters aren't returned in order
|
|
|
|
|
for url_part in [
|
|
|
|
|
"/v2/notifications?",
|
|
|
|
|
"template_type=email",
|
|
|
|
|
"status=pending",
|
|
|
|
|
"older_than={}".format(newer_notification.id)
|
|
|
|
|
]:
|
|
|
|
|
assert url_part in json_response['links']['current']
|
|
|
|
|
|
|
|
|
|
assert 'next' in json_response['links'].keys()
|
|
|
|
|
assert len(json_response['notifications']) == 1
|
|
|
|
|
|
|
|
|
|
assert json_response['notifications'][0]['id'] == str(older_notification.id)
|
2017-09-08 16:38:36 +01:00
|
|
|
|
|
|
|
|
|
2017-09-13 15:28:23 +01:00
|
|
|
def test_get_all_notifications_renames_letter_statuses(
|
|
|
|
|
client,
|
|
|
|
|
sample_letter_notification,
|
|
|
|
|
sample_notification,
|
|
|
|
|
sample_email_notification,
|
|
|
|
|
):
|
2017-09-08 16:38:36 +01:00
|
|
|
auth_header = create_authorization_header(service_id=sample_letter_notification.service_id)
|
|
|
|
|
response = client.get(
|
|
|
|
|
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']:
|
2017-09-13 15:28:23 +01:00
|
|
|
if noti['type'] == 'sms' or noti['type'] == 'email':
|
2017-09-08 16:38:36 +01:00
|
|
|
assert noti['status'] == 'created'
|
|
|
|
|
elif noti['type'] == 'letter':
|
2017-09-11 11:57:33 +01:00
|
|
|
assert noti['status'] == 'accepted'
|
2017-09-08 16:38:36 +01:00
|
|
|
else:
|
|
|
|
|
pytest.fail()
|
|
|
|
|
|
|
|
|
|
|
2017-10-23 16:24:55 +01:00
|
|
|
@pytest.mark.parametrize('db_status,expected_status', [
|
|
|
|
|
('created', 'accepted'),
|
|
|
|
|
('sending', 'accepted'),
|
|
|
|
|
('delivered', 'received'),
|
|
|
|
|
('pending', 'pending'),
|
|
|
|
|
('technical-failure', 'technical-failure')
|
|
|
|
|
])
|
|
|
|
|
def test_get_notifications_renames_letter_statuses(client, sample_letter_template, db_status, expected_status):
|
|
|
|
|
letter_noti = create_notification(
|
|
|
|
|
sample_letter_template,
|
|
|
|
|
status=db_status,
|
|
|
|
|
personalisation={'address_line_1': 'Mr Foo', 'address_line_2': '1 Bar Street', 'postcode': 'N1'}
|
|
|
|
|
)
|
|
|
|
|
auth_header = create_authorization_header(service_id=letter_noti.service_id)
|
2017-09-08 16:38:36 +01:00
|
|
|
response = client.get(
|
2017-10-23 16:24:55 +01:00
|
|
|
path=url_for('v2_notifications.get_notification_by_id', id=letter_noti.id),
|
2017-09-08 16:38:36 +01:00
|
|
|
headers=[('Content-Type', 'application/json'), auth_header]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
json_response = json.loads(response.get_data(as_text=True))
|
|
|
|
|
assert response.status_code == 200
|
2017-10-23 16:24:55 +01:00
|
|
|
assert json_response['status'] == expected_status
|