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
This commit is contained in:
Leo Hemsted
2016-08-31 12:33:45 +01:00
parent fabbb8b01b
commit c91358673f
11 changed files with 105 additions and 7 deletions
+3 -2
View File
@@ -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),
@@ -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"]
}
@@ -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"]
}
@@ -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')
@@ -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')