Merge pull request #1400 from alphagov/ken-allow-include_jobs-arg-get_all_notis

Allow include jobs request param on v2 get all notifications
This commit is contained in:
kentsanggds
2017-11-16 16:41:10 +00:00
committed by GitHub
4 changed files with 59 additions and 4 deletions

View File

@@ -35,6 +35,9 @@ def get_notifications():
if 'reference' in _data: if 'reference' in _data:
_data['reference'] = _data['reference'][0] _data['reference'] = _data['reference'][0]
if 'include_jobs' in _data:
_data['include_jobs'] = _data['include_jobs'][0]
data = validate(_data, get_notifications_request) data = validate(_data, get_notifications_request)
paginated_notifications = notifications_dao.get_notifications_for_service( paginated_notifications = notifications_dao.get_notifications_for_service(
@@ -44,7 +47,8 @@ def get_notifications():
personalisation=True, personalisation=True,
older_than=data.get('older_than'), older_than=data.get('older_than'),
client_reference=data.get('reference'), client_reference=data.get('reference'),
page_size=current_app.config.get('API_PAGE_SIZE') page_size=current_app.config.get('API_PAGE_SIZE'),
include_jobs=data.get('include_jobs')
) )
def _build_links(notifications): def _build_links(notifications):

View File

@@ -74,6 +74,7 @@ get_notifications_request = {
"enum": TEMPLATE_TYPES "enum": TEMPLATE_TYPES
} }
}, },
"include_jobs": {"enum": ["true", "True"]},
"older_than": uuid "older_than": uuid
}, },
"additionalProperties": False, "additionalProperties": False,
@@ -88,7 +89,7 @@ get_notifications_response = {
"type": "array", "type": "array",
"items": { "items": {
"type": "object", "type": "object",
"ref": get_notification_response "$ref": "#/definitions/notification"
} }
}, },
"links": { "links": {
@@ -106,7 +107,11 @@ get_notifications_response = {
} }
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["notifications", "links"] "required": ["notifications", "links"],
"definitions": {
"notification": get_notification_response
},
} }
post_sms_request = { post_sms_request = {

View File

@@ -266,7 +266,8 @@ def test_get_notification_doesnt_have_delivery_estimate_for_non_letters(
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_returns_200(client, sample_template): 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)] notifications = [create_notification(template=sample_template) for _ in range(2)]
notification = notifications[-1] notification = notifications[-1]
@@ -295,6 +296,34 @@ def test_get_all_notifications_returns_200(client, sample_template):
assert not json_response['notifications'][0]['scheduled_for'] assert not json_response['notifications'][0]['scheduled_for']
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']
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_authorization_header(service_id=sample_service.id) auth_header = create_authorization_header(service_id=sample_service.id)
response = client.get( response = client.get(

View File

@@ -5,6 +5,7 @@ from flask import json
from freezegun import freeze_time from freezegun import freeze_time
from jsonschema import ValidationError from jsonschema import ValidationError
from app.models import NOTIFICATION_CREATED, EMAIL_TYPE
from app.schema_validation import validate from app.schema_validation import validate
from app.v2.notifications.notification_schemas import ( from app.v2.notifications.notification_schemas import (
get_notifications_request, get_notifications_request,
@@ -13,6 +14,22 @@ from app.v2.notifications.notification_schemas import (
) )
valid_get_json = {}
valid_get_with_optionals_json = {
"reference": "test reference",
"status": [NOTIFICATION_CREATED],
"template_type": [EMAIL_TYPE],
"include_jobs": "true",
"older_than": "a5149c32-f03b-4711-af49-ad6993797d45"
}
@pytest.mark.parametrize("input", [valid_get_json, valid_get_with_optionals_json])
def test_get_notifications_valid_json(input):
assert validate(input, get_notifications_request) == input
@pytest.mark.parametrize('invalid_statuses, valid_statuses', [ @pytest.mark.parametrize('invalid_statuses, valid_statuses', [
# one invalid status # one invalid status
(["elephant"], []), (["elephant"], []),