import importlib.metadata import importlib.resources import os from unittest.mock import patch import tornado.template import tornado_openapi3.testing import yaml import tutor.database import tutor.server template_path = importlib.resources.path("tutor", "templates") template_loader = tornado.template.Loader(str(template_path)) openapi_spec_dict = yaml.safe_load( template_loader.load("openapi.yaml").generate( version=importlib.metadata.version("tutor") ) ) del openapi_spec_dict["servers"] openapi_spec = None class ServerTestCase(tornado_openapi3.testing.AsyncOpenAPITestCase): @property def spec_dict(self): global openapi_spec_dict return openapi_spec_dict @property def spec(self): global openapi_spec if not openapi_spec: openapi_spec = super().spec return openapi_spec def get_app(self): return tutor.server.Application( database=os.environ.get("TUTOR_DATABASE"), ) class DocumentationTestCase(ServerTestCase): def test_api_root(self): self.fetch("/api") def test_openapi_yaml(self): self.fetch("/api/openapi.yaml") class APITestCase(ServerTestCase): def test_collection_stats(self): self.fetch("/api/collection")