#!/usr/bin/env python3 """Validate real API exchanges against OpenAPI; public tests stay unchanged.""" import argparse from collections import Counter import json import re import sys from urllib.parse import parse_qs, urlsplit from jsonschema import Draft202012Validator, FormatChecker from referencing import Registry, Resource from common import Client, ROOT, check SPEC = json.loads((ROOT/'contracts/openapi.json').read_text()) RESOURCE = dict(SPEC, **{'$schema':'https://json-schema.org/draft/2020-12/schema'}) REGISTRY = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(RESOURCE)) OBSERVED = Counter() def validate(value, schema): # Put the schema under the same base URI so local component references resolve. wrapped = dict(RESOURCE) wrapped['$id'] = 'urn:mtusi:openapi' wrapped.update(schema) Draft202012Validator(wrapped, registry=REGISTRY, format_checker=FormatChecker()).validate(value) original_call = Client.call def checked_call(self, method, path, *args, **kwargs): split = urlsplit(path) matches = [(template, operations) for template, operations in SPEC['paths'].items() if re.fullmatch(re.sub(r'\{[^}]+\}', '[^/]+', template), split.path)] check(len(matches) == 1, 'Undocumented/ambiguous API route: '+method+' '+path) template, operations = matches[0] check(method.lower() in operations, 'Undocumented method: '+method+' '+template) operation = operations[method.lower()] response = original_call(self, method, path, *args, **kwargs) status = str(response['status']) check(status in operation['responses'], 'Undocumented response: '+method+' '+template+' '+status) declared = operation['responses'][status] for name, header in declared.get('headers', {}).items(): value = response['headers'].get(name) if value is not None: schema = header['schema'] if schema.get('type') == 'integer': value = int(value) validate(value, schema) if declared.get('content'): media = response['headers'].get_content_type() check(media in declared['content'], 'Undocumented response Content-Type: '+media) schema = declared['content'][media]['schema'] if media == 'application/json': validate(json.loads(response['bytes']), schema) else: check(not response['bytes'], 'Body returned for a response with no content') if response['status'] < 400: body = kwargs.get('body') if body is not None: validate(body, operation['requestBody']['content']['application/json']['schema']) query = parse_qs(split.query) path_values = dict(zip(re.findall(r'\{([^}]+)\}', template), [v for t, v in zip(template.split('/'), split.path.split('/')) if t.startswith('{')])) for parameter in operation.get('parameters', []): values = {'path': path_values, 'query': query, 'header': kwargs.get('headers', {})}.get(parameter['in'], {}) name, schema = parameter['name'], parameter['schema'] check(not parameter.get('required') or name in values, 'Required parameter missing: '+name) if name in values: value = values[name] if isinstance(value, list): value = value[0] if schema.get('type') == 'integer': value = int(value) validate(value, schema) OBSERVED[(method, template, status)] += 1 return response def main(): parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--scenario', choices=['smoke', 'attachments'], default='smoke') args, remainder = parser.parse_known_args() sys.argv = [sys.argv[0], *remainder] Client.call = checked_call if args.scenario == 'smoke': import smoke as scenario else: import attachments as scenario scenario.main() for (method, path, status), count in sorted(OBSERVED.items()): print('%s %s -> %s: %d checked' % (method, path, status, count)) routes = {(m, p) for m, p, _ in OBSERVED} print('PASS: actual request/response schemas, formats, headers and statuses match OpenAPI; %d operations, %d exchanges' % (len(routes), sum(OBSERVED.values()))) if __name__ == '__main__': main()