From c91358673fa410f2000eff20327044a239ffefc4 Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Wed, 31 Aug 2016 12:22:05 +0100 Subject: [PATCH] add POST response tests assert that the response for emails and sms is formatted as expected also moved schemas into subdirectory to make folder more legible --- tests/app/public_contracts/__init__.py | 5 ++- .../GET_notification_return_email.json | 0 .../GET_notification_return_sms.json | 0 .../GET_notifications_return.json | 0 .../POST_notification_return_email.json | 27 ++++++++++++ .../schemas/POST_notification_return_sms.json | 26 +++++++++++ .../{ => schemas}/definitions.json | 0 .../{ => schemas}/email_notification.json | 0 .../{ => schemas}/sms_notification.json | 0 .../public_contracts/test_GET_notification.py | 10 ++--- .../test_POST_notification.py | 44 +++++++++++++++++++ 11 files changed, 105 insertions(+), 7 deletions(-) rename tests/app/public_contracts/{ => schemas}/GET_notification_return_email.json (100%) rename tests/app/public_contracts/{ => schemas}/GET_notification_return_sms.json (100%) rename tests/app/public_contracts/{ => schemas}/GET_notifications_return.json (100%) create mode 100644 tests/app/public_contracts/schemas/POST_notification_return_email.json create mode 100644 tests/app/public_contracts/schemas/POST_notification_return_sms.json rename tests/app/public_contracts/{ => schemas}/definitions.json (100%) rename tests/app/public_contracts/{ => schemas}/email_notification.json (100%) rename tests/app/public_contracts/{ => schemas}/sms_notification.json (100%) create mode 100644 tests/app/public_contracts/test_POST_notification.py diff --git a/tests/app/public_contracts/__init__.py b/tests/app/public_contracts/__init__.py index cf57c3295..c6f7eee80 100644 --- a/tests/app/public_contracts/__init__.py +++ b/tests/app/public_contracts/__init__.py @@ -4,8 +4,9 @@ from flask import json import jsonschema def validate(json_string, schema_filename): - resolver = jsonschema.RefResolver('file://' + os.path.dirname(__file__) + '/', None) - with open(os.path.join(os.path.dirname(__file__), schema_filename)) as schema: + schema_dir = os.path.join(os.path.dirname(__file__), 'schemas') + resolver = jsonschema.RefResolver('file://' + schema_dir + '/', None) + with open(os.path.join(schema_dir, schema_filename)) as schema: jsonschema.validate( json.loads(json_string), json.load(schema), diff --git a/tests/app/public_contracts/GET_notification_return_email.json b/tests/app/public_contracts/schemas/GET_notification_return_email.json similarity index 100% rename from tests/app/public_contracts/GET_notification_return_email.json rename to tests/app/public_contracts/schemas/GET_notification_return_email.json diff --git a/tests/app/public_contracts/GET_notification_return_sms.json b/tests/app/public_contracts/schemas/GET_notification_return_sms.json similarity index 100% rename from tests/app/public_contracts/GET_notification_return_sms.json rename to tests/app/public_contracts/schemas/GET_notification_return_sms.json diff --git a/tests/app/public_contracts/GET_notifications_return.json b/tests/app/public_contracts/schemas/GET_notifications_return.json similarity index 100% rename from tests/app/public_contracts/GET_notifications_return.json rename to tests/app/public_contracts/schemas/GET_notifications_return.json diff --git a/tests/app/public_contracts/schemas/POST_notification_return_email.json b/tests/app/public_contracts/schemas/POST_notification_return_email.json new file mode 100644 index 000000000..bd84f798a --- /dev/null +++ b/tests/app/public_contracts/schemas/POST_notification_return_email.json @@ -0,0 +1,27 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "POST notification return schema - for email notifications", + "type" : "object", + "properties": { + "data": { + "type": "object", + "properties": { + "notification": { + "type": "object", + "properties": { + "id": {"$ref": "definitions.json#/uuid"} + }, + "additionalProperties": false, + "required": ["id"] + }, + "body": {"type": "string"}, + "template_version": {"type": "number"}, + "subject": {"type": "string"} + }, + "additionalProperties": false, + "required": ["notification", "body", "template_version", "subject"] + } + }, + "additionalProperties": false, + "required": ["data"] +} diff --git a/tests/app/public_contracts/schemas/POST_notification_return_sms.json b/tests/app/public_contracts/schemas/POST_notification_return_sms.json new file mode 100644 index 000000000..3119c6d73 --- /dev/null +++ b/tests/app/public_contracts/schemas/POST_notification_return_sms.json @@ -0,0 +1,26 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "POST notification return schema - for sms notifications", + "type" : "object", + "properties": { + "data": { + "type": "object", + "properties": { + "notification": { + "type": "object", + "properties": { + "id": {"$ref": "definitions.json#/uuid"} + }, + "additionalProperties": false, + "required": ["id"] + }, + "body": {"type": "string"}, + "template_version": {"type": "number"} + }, + "additionalProperties": false, + "required": ["notification", "body", "template_version"] + } + }, + "additionalProperties": false, + "required": ["data"] +} diff --git a/tests/app/public_contracts/definitions.json b/tests/app/public_contracts/schemas/definitions.json similarity index 100% rename from tests/app/public_contracts/definitions.json rename to tests/app/public_contracts/schemas/definitions.json diff --git a/tests/app/public_contracts/email_notification.json b/tests/app/public_contracts/schemas/email_notification.json similarity index 100% rename from tests/app/public_contracts/email_notification.json rename to tests/app/public_contracts/schemas/email_notification.json diff --git a/tests/app/public_contracts/sms_notification.json b/tests/app/public_contracts/schemas/sms_notification.json similarity index 100% rename from tests/app/public_contracts/sms_notification.json rename to tests/app/public_contracts/schemas/sms_notification.json diff --git a/tests/app/public_contracts/test_GET_notification.py b/tests/app/public_contracts/test_GET_notification.py index e9a3c44d5..fdc13e078 100644 --- a/tests/app/public_contracts/test_GET_notification.py +++ b/tests/app/public_contracts/test_GET_notification.py @@ -18,7 +18,7 @@ def test_get_api_sms_contract(client, sample_notification): auth_header = create_authorization_header(service_id=sample_notification.service_id) response = client.get('/notifications/{}'.format(sample_notification.id), headers=[auth_header]) - validate(response.get_data(as_text=True), './GET_notification_return_sms.json') + validate(response.get_data(as_text=True), 'GET_notification_return_sms.json') def test_get_api_email_contract(client, sample_email_notification): @@ -35,25 +35,25 @@ def test_get_api_email_contract(client, sample_email_notification): auth_header = create_authorization_header(service_id=sample_email_notification.service_id) response = client.get('/notifications/{}'.format(sample_email_notification.id), headers=[auth_header]) - validate(response.get_data(as_text=True), './GET_notification_return_email.json') + validate(response.get_data(as_text=True), 'GET_notification_return_email.json') def test_get_job_sms_contract(client, sample_notification): auth_header = create_authorization_header(service_id=sample_notification.service_id) response = client.get('/notifications/{}'.format(sample_notification.id), headers=[auth_header]) - validate(response.get_data(as_text=True), './GET_notification_return_sms.json') + validate(response.get_data(as_text=True), 'GET_notification_return_sms.json') def test_get_job_email_contract(client, sample_email_notification): auth_header = create_authorization_header(service_id=sample_email_notification.service_id) response = client.get('/notifications/{}'.format(sample_email_notification.id), headers=[auth_header]) - validate(response.get_data(as_text=True), './GET_notification_return_email.json') + validate(response.get_data(as_text=True), 'GET_notification_return_email.json') def test_get_notifications_contract(client, sample_notification, sample_email_notification): auth_header = create_authorization_header(service_id=sample_notification.service_id) response = client.get('/notifications', headers=[auth_header]) - validate(response.get_data(as_text=True), './GET_notifications_return.json') + validate(response.get_data(as_text=True), 'GET_notifications_return.json') diff --git a/tests/app/public_contracts/test_POST_notification.py b/tests/app/public_contracts/test_POST_notification.py new file mode 100644 index 000000000..ffb91210c --- /dev/null +++ b/tests/app/public_contracts/test_POST_notification.py @@ -0,0 +1,44 @@ +from flask import json + +from . import validate +from tests import create_authorization_header + + +def test_post_sms_contract(client, mocker, sample_template): + mocker.patch('app.celery.tasks.send_sms.apply_async') + mocker.patch('app.encryption.encrypt', return_value="something_encrypted") + + data = { + 'to': '07700 900 855', + 'template': str(sample_template.id) + } + + auth_header = create_authorization_header(service_id=sample_template.service_id) + + response = client.post( + path='/notifications/sms', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header] + ) + + validate(response.get_data(as_text=True), 'POST_notification_return_sms.json') + + +def test_post_email_contract(client, mocker, sample_email_template): + mocker.patch('app.celery.tasks.send_sms.apply_async') + mocker.patch('app.encryption.encrypt', return_value="something_encrypted") + + data = { + 'to': 'foo@bar.com', + 'template': str(sample_email_template.id) + } + + auth_header = create_authorization_header(service_id=sample_email_template.service_id) + + response = client.post( + path='/notifications/email', + data=json.dumps(data), + headers=[('Content-Type', 'application/json'), auth_header] + ) + + validate(response.get_data(as_text=True), 'POST_notification_return_email.json')