Use jsonschema validation + remove 'v2' schema

Emualated the validation methods that exist [in the python-client](620e5f7014/integration_test/__init__.py).

The `validate_v0` function loads json schemas from a local
`/schemas` directory, whereas the new `validate` function (which
we're going to use for our v2 API calls) uses the common
`get_notification_response` python schema defined in
"app/v2/notifications/notification_schemas.py".

Removed the new `v2` schema from the last commit as it's no longer
being used.

Also, refactored common code in the GET and POST contract files
so that making requests and converting responses to JSON are
pulled out into common functions.
This commit is contained in:
Paul Craig
2016-11-22 11:17:28 +00:00
parent fb50bb6325
commit 7ae427c4ef
12 changed files with 64 additions and 113 deletions

View File

@@ -2,15 +2,25 @@ import os
from flask import json
import jsonschema
from jsonschema import Draft4Validator
def validate(json_string, schema_filename):
schema_dir = os.path.join(os.path.dirname(__file__), 'schemas')
def return_json_from_response(response):
return json.loads(response.get_data(as_text=True))
def validate_v0(json_to_validate, schema_filename):
schema_dir = os.path.join(os.path.dirname(__file__), 'schemas/v0')
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_to_validate,
json.load(schema),
format_checker=jsonschema.FormatChecker(),
resolver=resolver
)
def validate(json_to_validate, schema):
validator = Draft4Validator(schema)
validator.validate(json_to_validate, schema)

View File

@@ -1,67 +0,0 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "GET notification response schema",
"type": "object",
"title": "response v2/notification",
"oneOf": [
{"properties": {
"email_address": {"type": "string", "format": "email_address"},
"type": {"enum": ["email"]},
"phone_number": {"type": "null"},
"line_1": {"type": "null"},
"postcode": {"type": "null"}
}},
{"properties": {
"phone_number": {"type": "string", "format": "phone_number"},
"type": {"enum": ["sms"]},
"email_address": {"type": "null"},
"line_1": {"type": "null"},
"postcode": {"type": "null"}
}},
{"properties": {
"line_1": {"type": "string", "minLength": 1},
"postcode": {"type": "string", "minLength": 1},
"type": {"enum": ["letter"]},
"email_address": {"type": "null"},
"phone_number": {"type": "null"}
}}
],
"properties": {
"id": {"$ref": "definitions.json#/uuid"},
"reference": {"type": ["string", "null"]},
"email_address": {"type": ["string", "null"]},
"phone_number": {"type": ["string", "null"]},
"line_1": {"type": ["string", "null"]},
"line_2": {"type": ["string", "null"]},
"line_3": {"type": ["string", "null"]},
"line_4": {"type": ["string", "null"]},
"line_5": {"type": ["string", "null"]},
"line_6": {"type": ["string", "null"]},
"postcode": {"type": ["string", "null"]},
"cost": {"type": "number"},
"type": {"enum": ["sms", "letter", "email"]},
"status": {"type": "string"},
"template": {
"type": "object",
"properties": {
"id": {"$ref": "definitions.json#/uuid"},
"uri": {"type": "string"},
"version": {"type": "number"}
},
"additionalProperties": false,
"required": ["id", "uri", "version"]
},
"created_at": {"type": "string"},
"sent_at": {"type": ["string", "null"]},
"completed_at": {"type": ["string", "null"]}
},
"required": [
"id", "reference", "email_address", "phone_number",
"line_1", "line_2", "line_3", "line_4", "line_5", "line_6", "postcode",
"cost", "type", "status", "template",
"created_at", "sent_at", "completed_at"
]
}

View File

@@ -1,10 +1,11 @@
from . import validate
from . import return_json_from_response, validate_v0, validate
from app.models import ApiKey, KEY_TYPE_NORMAL
from app.dao.api_key_dao import save_model_api_key
from app.v2.notifications.notification_schemas import get_notification_response
from tests import create_authorization_header
def _get(client, notification, url):
def _get_notification(client, notification, url):
save_model_api_key(ApiKey(
service=notification.service,
name='api_key',
@@ -16,30 +17,42 @@ def _get(client, notification, url):
def test_get_v2_notification(client, sample_notification):
response = _get(client, sample_notification, '/v2/notifications/{}'.format(sample_notification.id))
validate(response.get_data(as_text=True), 'GET_notification_return_email_v2.json')
response_json = return_json_from_response(_get_notification(
client, sample_notification, '/v2/notifications/{}'.format(sample_notification.id)
))
validate(response_json, get_notification_response)
def test_get_api_sms_contract(client, sample_notification):
response = _get(client, sample_notification, '/notifications/{}'.format(sample_notification.id))
validate(response.get_data(as_text=True), 'GET_notification_return_sms.json')
response_json = return_json_from_response(_get_notification(
client, sample_notification, '/notifications/{}'.format(sample_notification.id)
))
validate_v0(response_json, 'GET_notification_return_sms.json')
def test_get_api_email_contract(client, sample_email_notification):
response = _get(client, sample_email_notification, '/notifications/{}'.format(sample_email_notification.id))
validate(response.get_data(as_text=True), 'GET_notification_return_email.json')
response_json = return_json_from_response(_get_notification(
client, sample_email_notification, '/notifications/{}'.format(sample_email_notification.id)
))
validate_v0(response_json, 'GET_notification_return_email.json')
def test_get_job_sms_contract(client, sample_notification):
response = _get(client, sample_notification, '/notifications/{}'.format(sample_notification.id))
validate(response.get_data(as_text=True), 'GET_notification_return_sms.json')
response_json = return_json_from_response(_get_notification(
client, sample_notification, '/notifications/{}'.format(sample_notification.id)
))
validate_v0(response_json, 'GET_notification_return_sms.json')
def test_get_job_email_contract(client, sample_email_notification):
response = _get(client, sample_email_notification, '/notifications/{}'.format(sample_email_notification.id))
validate(response.get_data(as_text=True), 'GET_notification_return_email.json')
response_json = return_json_from_response(_get_notification(
client, sample_email_notification, '/notifications/{}'.format(sample_email_notification.id)
))
validate_v0(response_json, 'GET_notification_return_email.json')
def test_get_notifications_contract(client, sample_notification, sample_email_notification):
response = _get(client, sample_notification, '/notifications')
validate(response.get_data(as_text=True), 'GET_notifications_return.json')
response_json = return_json_from_response(_get_notification(
client, sample_notification, '/notifications'
))
validate_v0(response_json, 'GET_notifications_return.json')

View File

@@ -1,44 +1,39 @@
from flask import json
from . import validate
from . import return_json_from_response, validate_v0
from tests import create_authorization_header
def _post_notification(client, template, url, to):
data = {
'to': to,
'template': str(template.id)
}
auth_header = create_authorization_header(service_id=template.service_id)
return client.post(
path=url,
data=json.dumps(data),
headers=[('Content-Type', 'application/json'), auth_header]
)
def test_post_sms_contract(client, mocker, sample_template):
mocker.patch('app.celery.provider_tasks.deliver_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')
response_json = return_json_from_response(_post_notification(
client, sample_template, url='/notifications/sms', to='07700 900 855'
))
validate_v0(response_json, 'POST_notification_return_sms.json')
def test_post_email_contract(client, mocker, sample_email_template):
mocker.patch('app.celery.provider_tasks.deliver_email.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')
response_json = return_json_from_response(_post_notification(
client, sample_email_template, url='/notifications/email', to='foo@bar.com'
))
validate_v0(response_json, 'POST_notification_return_email.json')