entities_validation_svc/test.py

126 lines
4.7 KiB
Python
Raw Permalink Normal View History

2020-11-06 22:41:07 +01:00
#!/usr/bin/python3
from app import make_app
import util
import unittest
import tornado.testing
import json
util.platform_setup()
class TestBaseAPI(tornado.testing.AsyncHTTPTestCase):
"""Example test case"""
def get_app(self):
return make_app()
def test_health_endpoint(self):
response = self.fetch('/v0/health',
method='GET')
self.assertEqual(200, response.code, "GET /health must be available")
health = json.loads(response.body.decode())
self.assertIn('api-version', health, msg="api-version is not provided by health endpoint")
self.assertEqual("v0", health['api-version'], msg="API version should be v0")
self.assertIn('git-version', health, msg="git-version is not provided by health endpoint")
self.assertIn('timestamp', health, msg="timestamp is not provided by health endpoint")
self.assertIn('uptime', health, msg="uptime is not provided by health endpoint")
2020-11-06 22:41:07 +01:00
def test_oas3(self):
response = self.fetch('/v0/oas3',
method='GET')
self.assertEqual(200, response.code, "GET /oas3 must be available")
# check contents against local OAS3.yml
with open('OAS3.yml') as oas3f:
self.assertEqual(response.body.decode(), oas3f.read(), "OAS3 content differs from spec file!")
2020-11-06 22:41:07 +01:00
2020-12-01 21:03:11 +01:00
class TestValidation(tornado.testing.AsyncHTTPTestCase):
"""Validation test cases"""
def get_app(self):
return make_app()
def test_null_validation(self):
entity = {}
response = self.fetch('/v0/validate',
method='POST',
body=json.dumps(entity))
self.assertEqual(200, response.code, "Validation must always return 200")
2020-12-01 21:03:11 +01:00
validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
2020-12-04 21:18:56 +01:00
def test_valid_entity(self):
2021-01-05 14:32:12 +01:00
with open('test_cases/valid/valid.json', 'r') as f:
2021-01-04 22:01:24 +01:00
entity_file = json.load(f)
2020-12-04 21:18:56 +01:00
response = self.fetch('/v0/validate',
method='POST',
2020-12-06 21:35:39 +01:00
body=json.dumps(entity_file))
2021-01-04 22:14:36 +01:00
self.assertEqual(200, response.code, "Validation must always return 200")
2020-12-04 21:18:56 +01:00
validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
self.assertTrue(validation_result['valid'], "Validation result is expected to be valid==true")
2020-12-01 21:03:11 +01:00
2020-12-06 22:10:52 +01:00
def test_invalid_iban(self):
2021-01-05 14:32:12 +01:00
with open('test_cases/invalid/invalid_iban.json', 'r') as f:
2021-01-04 22:01:24 +01:00
entity_file = json.load(f)
2020-12-06 22:06:53 +01:00
response = self.fetch('/v0/validate',
method='POST',
body=json.dumps(entity_file))
2021-01-04 22:14:36 +01:00
self.assertEqual(200, response.code, "Validation must always return 200")
2020-12-06 22:06:53 +01:00
validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
2020-12-06 22:06:53 +01:00
2020-12-06 22:10:52 +01:00
def test_missing_id(self):
2021-01-05 14:32:12 +01:00
with open('test_cases/invalid/missing_id.json', 'r') as f:
2021-01-04 22:01:24 +01:00
entity_file = json.load(f)
2020-12-06 22:10:52 +01:00
2021-01-04 22:01:24 +01:00
response = self.fetch('/v0/validate',
2021-01-04 22:14:36 +01:00
method='POST',
body=json.dumps(entity_file))
self.assertEqual(200, response.code, "Validation must always return 200")
2020-12-06 22:10:52 +01:00
2021-01-04 22:01:24 +01:00
validation_result = json.loads(response.body.decode())
2020-12-06 22:10:52 +01:00
self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
2021-01-04 22:01:24 +01:00
self.assertIn('id', validation_result['errors'])
2020-12-06 22:10:52 +01:00
def test_invalid_id(self):
2021-01-05 14:32:12 +01:00
with open('test_cases/invalid/invalid_id.json', 'r') as f:
2021-01-04 22:01:24 +01:00
entity_file = json.load(f)
2021-01-04 22:14:36 +01:00
2020-12-06 22:10:52 +01:00
response = self.fetch('/v0/validate',
2021-01-04 22:14:36 +01:00
method='POST',
body=json.dumps(entity_file))
self.assertEqual(200, response.code,
"Validation must always return 200")
2020-12-06 22:10:52 +01:00
validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
2020-12-06 22:10:52 +01:00
self.assertIn('id', validation_result['errors'])
2020-12-01 21:03:11 +01:00
2021-01-04 22:14:36 +01:00
2020-11-06 22:41:07 +01:00
if __name__ == "__main__":
unittest.main()