#!/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") 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!") 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") 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") def test_valid_entity(self): entity = { 'finanzdaten': { 'bic': 'PBNKDEFFXXX', 'holder': '', 'iban': 'DE89370400440532013000', 'issuance': '2012-01-01', 'reference': '0042', 'scan-sepa-mandate': '' }, 'mitgliederdaten': { 'bis': '', 'mitgliedsbeitrag': '30', 'scan-antrag': '', 'schliessberechtigung': 'Ja', 'spendenbeitrag': '0', 'status': 'V', 'von': '2012-01-01' }, 'stammdaten': { 'address_code': '39104', 'address_country': 'DE', 'address_label': 'Max Hackerberg\nLeibnizstr. 32\n39104 Magdeburg', 'address_locality': 'Magdeburg', 'address_region': '', 'address_street': 'Leibnizstr. 32', 'birth_date': '1970-01-01', 'birth_location': 'Hackstadt', 'email': 'max.hackerberg@netz39.de', 'fullname': 'Max Hackerberg', 'nickname': 'maxH', 'pgp-key': '', 'ssh-key': '' }, 'timestamp': '2020-03-25T23:58:11', 'id': '6af68' } response = self.fetch('/v0/validate', method='POST', body=json.dumps(entity)) self.assertEqual(200, response.code, "Validation must always return 200") 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") if __name__ == "__main__": unittest.main()