Test invalid older_than, template_types, and bad ids

Come up with some simple tests in the routes, just to see we get
back what we expect as errors.
This commit is contained in:
Paul Craig
2016-11-28 15:12:03 +00:00
parent 2ca675eb73
commit 4082d38c0c
3 changed files with 71 additions and 8 deletions

View File

@@ -8,7 +8,7 @@ from itertools import groupby
from flask import current_app
from werkzeug.datastructures import MultiDict
from sqlalchemy import (desc, func, or_, and_, asc, cast, Text)
from sqlalchemy import (desc, func, or_, and_, asc)
from sqlalchemy.orm import joinedload
from app import db, create_uuid

View File

@@ -1,5 +1,4 @@
from flask import jsonify, request, url_for
from app import api_user
from app.dao import notifications_dao
from app.schema_validation import validate

View File

@@ -60,6 +60,44 @@ def test_get_notification_by_id_returns_200(
assert json_response == expected_response
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
}
def test_get_notification_by_id_invalid_id(client, sample_notification):
auth_header = create_authorization_header(service_id=sample_notification.service_id)
response = client.get(
path='/v2/notifications/1234-badly-formatted-id-7890',
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"
}
def test_get_all_notifications_returns_200(client, notify_db, notify_db_session):
notifications = [create_sample_notification(notify_db, notify_db_session) for _ in range(2)]
notification = notifications[-1]
@@ -88,7 +126,7 @@ def test_get_all_notifications_returns_200(client, notify_db, notify_db_session)
assert json_response['notifications'][0]['type'] == "sms"
def test_get_all_notifications_no_notifications_if_no_notificatons(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)
response = client.get(
path='/v2/notifications',
@@ -135,6 +173,22 @@ def test_get_all_notifications_filter_by_template_type(client, notify_db, notify
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_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
assert json_response['errors'][0]['message'] == "orange is not one of [sms, email, letter]"
def test_get_all_notifications_filter_by_single_status(client, notify_db, notify_db_session):
notification = create_sample_notification(notify_db, notify_db_session, status="pending")
create_sample_notification(notify_db, notify_db_session)
@@ -156,11 +210,8 @@ def test_get_all_notifications_filter_by_single_status(client, notify_db, notify
assert json_response['notifications'][0]['status'] == "pending"
def test_get_all_notifications_filter_by_status_invalid_status(client, notify_db, notify_db_session):
notification = create_sample_notification(notify_db, notify_db_session, status="pending")
create_sample_notification(notify_db, notify_db_session)
auth_header = create_authorization_header(service_id=notification.service_id)
def test_get_all_notifications_filter_by_status_invalid_status(client, sample_notification):
auth_header = create_authorization_header(service_id=sample_notification.service_id)
response = client.get(
path='/v2/notifications?status=elephant',
headers=[('Content-Type', 'application/json'), auth_header])
@@ -250,6 +301,19 @@ def test_get_all_notifications_filter_by_id(client, notify_db, notify_db_session
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_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"
def test_get_all_notifications_filter_by_id_no_notifications_if_nonexistent_id(client, notify_db, notify_db_session):
notification = create_sample_notification(notify_db, notify_db_session)