Compare commits

..

No commits in common. "7609c08cb41c6a398cc7cdaf6392aabe085fee9a" and "eaf6aeeccc7a590c28127a5b0a0d92a33f1fd09e" have entirely different histories.

3 changed files with 41 additions and 76 deletions

12
app.py
View file

@ -35,12 +35,10 @@ class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta):
# if not available, try git # if not available, try git
if v is None: if v is None:
try: try:
v = subprocess.check_output(["git", "describe", "--always", v = subprocess.check_output(["git", "describe", "--always", "--dirty"],
"--dirty"],
cwd=os.path.dirname(__file__)).strip().decode() cwd=os.path.dirname(__file__)).strip().decode()
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print("Checking git version lead to non-null return code ", print("Checking git version lead to non-null return code ", e.returncode)
e.returncode)
return v return v
@ -52,8 +50,7 @@ class HealthHandler(tornado.web.RequestHandler, metaclass=ABCMeta):
health['git-version'] = self.git_version health['git-version'] = self.git_version
health['timestamp'] = isodate.datetime_isoformat(datetime.now()) health['timestamp'] = isodate.datetime_isoformat(datetime.now())
health['uptime'] = isodate.duration_isoformat(datetime.now() health['uptime'] = isodate.duration_isoformat(datetime.now() - startup_timestamp)
- startup_timestamp)
self.set_header("Content-Type", "application/json") self.set_header("Content-Type", "application/json")
self.write(json.dumps(health, indent=4)) self.write(json.dumps(health, indent=4))
@ -64,8 +61,7 @@ class Oas3Handler(tornado.web.RequestHandler, metaclass=ABCMeta):
def get(self): def get(self):
self.set_header("Content-Type", "text/plain") self.set_header("Content-Type", "text/plain")
# This is the proposed content type, # This is the proposed content type,
# but browsers like Firefox try to download instead of # but browsers like Firefox try to download instead of display the content
# display the content
# self.set_header("Content-Type", "text/vnd.yml") # self.set_header("Content-Type", "text/vnd.yml")
with open('OAS3.yml', 'r') as f: with open('OAS3.yml', 'r') as f:
oas3 = f.read() oas3 = f.read()

98
test.py
View file

@ -22,16 +22,11 @@ class TestBaseAPI(tornado.testing.AsyncHTTPTestCase):
health = json.loads(response.body.decode()) health = json.loads(response.body.decode())
self.assertIn('api-version', health, self.assertIn('api-version', health, msg="api-version is not provided by health endpoint")
msg="api-version is not provided by health endpoint") self.assertEqual("v0", health['api-version'], msg="API version should be v0")
self.assertEqual("v0", health['api-version'], self.assertIn('git-version', health, msg="git-version is not provided by health endpoint")
msg="API version should be v0") self.assertIn('timestamp', health, msg="timestamp is not provided by health endpoint")
self.assertIn('git-version', health, self.assertIn('uptime', health, msg="uptime is not provided by health endpoint")
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")
def test_oas3(self): def test_oas3(self):
response = self.fetch('/v0/oas3', response = self.fetch('/v0/oas3',
@ -40,8 +35,7 @@ class TestBaseAPI(tornado.testing.AsyncHTTPTestCase):
# check contents against local OAS3.yml # check contents against local OAS3.yml
with open('OAS3.yml') as oas3f: with open('OAS3.yml') as oas3f:
self.assertEqual(response.body.decode(), oas3f.read(), self.assertEqual(response.body.decode(), oas3f.read(), "OAS3 content differs from spec file!")
"OAS3 content differs from spec file!")
class TestValidation(tornado.testing.AsyncHTTPTestCase): class TestValidation(tornado.testing.AsyncHTTPTestCase):
@ -56,90 +50,70 @@ class TestValidation(tornado.testing.AsyncHTTPTestCase):
method='POST', method='POST',
body=json.dumps(entity)) body=json.dumps(entity))
self.assertEqual(200, response.code, self.assertEqual(200, response.code, "Validation must always return 200")
"Validation must always return 200")
validation_result = json.loads(response.body.decode()) validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
"Key 'valid' expected in validation result") self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
self.assertFalse(validation_result['valid'],
"Validation result is expected to be valid==false")
def test_valid_entity(self): def test_valid_entity(self):
with open('test_cases/valid/valid.json', 'r') af f: entity_file = json.load(open('test_cases/valid/valid.json','r'))
entity_file = json.load(f)
response = self.fetch('/v0/validate', response = self.fetch('/v0/validate',
method='POST', method='POST',
body=json.dumps(entity_file)) body=json.dumps(entity_file))
self.assertEqual(200, response.code, self.assertEqual(200, response.code, "Validation must always return 200")
"Validation must always return 200")
validation_result = json.loads(response.body.decode()) validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
"Key 'valid' expected in validation result") self.assertTrue(validation_result['valid'], "Validation result is expected to be valid==true")
self.assertTrue(validation_result['valid'],
"Validation result is expected to be valid==true")
def test_invalid_iban(self): def test_invalid_iban(self):
with open('test_cases/invalid/invalid_iban.json', 'r') af f: entity_file = json.load(open('test_cases/invalid/invalid_iban.json','r'))
entity_file = json.load(f)
response = self.fetch('/v0/validate', response = self.fetch('/v0/validate',
method='POST', method='POST',
body=json.dumps(entity_file)) body=json.dumps(entity_file))
self.assertEqual(200, response.code, self.assertEqual(200, response.code, "Validation must always return 200")
"Validation must always return 200")
validation_result = json.loads(response.body.decode()) validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
"Key 'valid' expected in validation result") self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
self.assertFalse(validation_result['valid'],
"Validation result is expected to be valid==false")
def test_missing_id(self): def test_missing_id(self):
with open('test_cases/invalid/missing_id.json', 'r') af f: entity_file = json.load(open('test_cases/invalid/missing_id.json','r'))
entity_file = json.load(f)
response = self.fetch('/v0/validate', response = self.fetch('/v0/validate',
method='POST', method='POST',
body=json.dumps(entity_file)) body=json.dumps(entity_file))
self.assertEqual(200, response.code, "Validation must always return 200")
self.assertEqual(200, response.code, validation_result = json.loads(response.body.decode())
"Validation must always return 200")
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")
self.assertIn('valid', validation_result, self.assertIn('id', validation_result['errors'])
"Key 'valid' expected in validation result")
self.assertFalse(validation_result['valid'],
"Validation result is expected to be valid==false")
self.assertIn('id', validation_result['errors'])
def test_invalid_id(self): def test_invalid_id(self):
with open('test_cases/invalid/invalid_id.json', 'r') af f: entity_file = json.load(open('test_cases/invalid/invalid_id.json','r'))
entity_file = json.load(f)
response = self.fetch('/v0/validate', response = self.fetch('/v0/validate',
method='POST', method='POST',
body=json.dumps(entity_file)) body=json.dumps(entity_file))
self.assertEqual(200, response.code, self.assertEqual(200, response.code, "Validation must always return 200")
"Validation must always return 200")
validation_result = json.loads(response.body.decode()) validation_result = json.loads(response.body.decode())
self.assertIn('valid', validation_result, self.assertIn('valid', validation_result, "Key 'valid' expected in validation result")
"Key 'valid' expected in validation result") self.assertFalse(validation_result['valid'], "Validation result is expected to be valid==false")
self.assertFalse(validation_result['valid'],
"Validation result is expected to be valid==false")
self.assertIn('id', validation_result['errors']) self.assertIn('id', validation_result['errors'])
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View file

@ -2,7 +2,6 @@ from schwifty import IBAN, BIC
import datetime import datetime
from validator_collection import validators, errors from validator_collection import validators, errors
def valid_iban(field, value, error): def valid_iban(field, value, error):
try: try:
IBAN(value) IBAN(value)
@ -10,7 +9,6 @@ def valid_iban(field, value, error):
except ValueError: except ValueError:
error(field, 'not a valid IBAN') error(field, 'not a valid IBAN')
def valid_bic(field, value, error): def valid_bic(field, value, error):
try: try:
BIC(value) BIC(value)
@ -18,7 +16,6 @@ def valid_bic(field, value, error):
except ValueError: except ValueError:
error(field, 'not a valid BIC') error(field, 'not a valid BIC')
def iso_date(field, value, error): def iso_date(field, value, error):
try: try:
datetime.datetime.strptime(value, "%Y-%m-%d") datetime.datetime.strptime(value, "%Y-%m-%d")
@ -26,7 +23,6 @@ def iso_date(field, value, error):
except ValueError: except ValueError:
error(field, 'not a valid ISO 8601 date') error(field, 'not a valid ISO 8601 date')
def valid_money_amount(field, value, error): def valid_money_amount(field, value, error):
try: try:
# value is string, check formatting by parsing as float # value is string, check formatting by parsing as float
@ -35,10 +31,9 @@ def valid_money_amount(field, value, error):
except (ValueError, TypeError): except (ValueError, TypeError):
error(field, 'not a valid money value') error(field, 'not a valid money value')
def valid_email(field, value, error): def valid_email(field, value, error):
try: try:
validators.email(value) validators.email(value)
return True return True
except errors.InvalidEmailError: except errors.InvalidEmailError:
error(field, 'not a valid email') error(field, 'not a valid email')