tutor/tests/test_server.py

55 lines
1.3 KiB
Python
Raw Normal View History

2022-09-27 23:19:28 +00:00
import importlib.metadata
import importlib.resources
2023-02-24 04:18:43 +00:00
import os
from unittest.mock import patch
2022-09-27 23:19:28 +00:00
import tornado.template
import tornado_openapi3.testing
import yaml
import tutor
2023-02-24 04:18:43 +00:00
import tutor.database
2022-09-27 23:19:28 +00:00
import tutor.server
template_path = importlib.resources.files(tutor) / "templates"
2023-02-24 04:18:43 +00:00
template_loader = tornado.template.Loader(str(template_path))
2022-09-27 23:19:28 +00:00
openapi_spec_dict = yaml.safe_load(
template_loader.load("openapi.yaml").generate(
version=importlib.metadata.version("tutor")
)
)
2023-02-25 16:03:22 +00:00
del openapi_spec_dict["servers"]
2022-09-27 23:19:28 +00:00
openapi_spec = None
class ServerTestCase(tornado_openapi3.testing.AsyncOpenAPITestCase):
@property
def spec_dict(self):
global openapi_spec_dict
return openapi_spec_dict
2023-02-24 04:18:43 +00:00
@property
def spec(self):
global openapi_spec
if not openapi_spec:
openapi_spec = super().spec
return openapi_spec
2022-09-27 23:19:28 +00:00
def get_app(self):
2023-02-24 04:18:43 +00:00
return tutor.server.Application(
database=os.environ.get("TUTOR_DATABASE"),
)
2022-09-27 23:19:28 +00:00
class DocumentationTestCase(ServerTestCase):
def test_api_root(self):
2023-02-25 16:03:22 +00:00
self.fetch("/api")
2022-09-27 23:19:28 +00:00
def test_openapi_yaml(self):
self.fetch("/api/openapi.yaml")
2023-02-24 04:18:43 +00:00
class APITestCase(ServerTestCase):
def test_collection_stats(self):
self.fetch("/api/collection")