diff --git a/WebHostLib/api/generate.py b/WebHostLib/api/generate.py index 0f090039..528a14df 100644 --- a/WebHostLib/api/generate.py +++ b/WebHostLib/api/generate.py @@ -27,7 +27,10 @@ def generate_api(): race = bool(0 if request.form["race"] in {"false"} else int(request.form["race"])) meta_options_source = request.form - json_data = request.get_json() + # json_data is optional, we can have it silently fall to None as it used to do. + # See https://flask.palletsprojects.com/en/2.2.x/api/#flask.Request.get_json -> Changelog -> 2.1 + json_data = request.get_json(silent=True) + if json_data: meta_options_source = json_data if 'weights' in json_data: diff --git a/test/webhost/TestAPIGenerate.py b/test/webhost/TestAPIGenerate.py new file mode 100644 index 00000000..e50bf96e --- /dev/null +++ b/test/webhost/TestAPIGenerate.py @@ -0,0 +1,40 @@ +import unittest +import json + + +class TestDocs(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + from WebHost import get_app, raw_app + raw_app.config["PONY"] = { + "provider": "sqlite", + "filename": ":memory:", + "create_db": True, + } + app = get_app() + app.config.update({ + "TESTING": True, + }) + cls.client = app.test_client() + + def testCorrectErrorEmptyRequest(self): + response = self.client.post("/api/generate") + self.assertIn("No options found. Expected file attachment or json weights.", response.text) + + def testGenerationQueued(self): + options = { + "Tester1": + { + "game": "Archipelago", + "name": "Tester", + "Archipelago": {} + } + } + response = self.client.post( + "/api/generate", + data=json.dumps({"weights": options}), + content_type='application/json' + ) + json_data = response.get_json() + self.assertTrue(json_data["text"].startswith("Generation of seed ")) + self.assertTrue(json_data["text"].endswith(" started successfully."))