From f5dc5f655b01717ab8d19d1af5714bd2c7807089 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Mon, 6 Nov 2023 08:48:19 +0100 Subject: [PATCH] Check schedule flags validity also when creating a schedule from scratch --- .../backend/src/routes/schedules/schedule.py | 4 ++ .../routes/schedules/test_freecodecamp.py | 64 +++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/dispatcher/backend/src/routes/schedules/schedule.py b/dispatcher/backend/src/routes/schedules/schedule.py index 1502a89d..02900714 100644 --- a/dispatcher/backend/src/routes/schedules/schedule.py +++ b/dispatcher/backend/src/routes/schedules/schedule.py @@ -125,6 +125,10 @@ def post(self, session: so.Session, token: AccessToken.Payload): try: document = ScheduleSchema().load(request.get_json()) + flags_schema = ScheduleConfigSchema.get_offliner_schema( + document["config"]["task_name"] + ) + flags_schema().load(document["config"]["flags"]) except ValidationError as e: raise InvalidRequestJSON(e.messages) diff --git a/dispatcher/backend/src/tests/integration/routes/schedules/test_freecodecamp.py b/dispatcher/backend/src/tests/integration/routes/schedules/test_freecodecamp.py index 11ceebfa..7f558cc2 100644 --- a/dispatcher/backend/src/tests/integration/routes/schedules/test_freecodecamp.py +++ b/dispatcher/backend/src/tests/integration/routes/schedules/test_freecodecamp.py @@ -1,9 +1,9 @@ class TestFreeCodeCamp: - def test_create_freecodecamp_schedule( + def test_create_freecodecamp_schedule_ok( self, client, access_token, garbage_collector ): schedule = { - "name": "fcc_javascript_test", + "name": "fcc_javascript_test_ok", "category": "freecodecamp", "enabled": False, "tags": [], @@ -14,7 +14,13 @@ def test_create_freecodecamp_schedule( "image": {"name": "openzim/freecodecamp", "tag": "1.0.0"}, "monitor": False, "platform": None, - "flags": {}, + "flags": { + "course": ("somecourse"), + "language": "eng", + "name": "acourse", + "title": "a title", + "description": "a description", + }, "resources": {"cpu": 3, "memory": 1024, "disk": 0}, }, "periodicity": "quarterly", @@ -24,9 +30,11 @@ def test_create_freecodecamp_schedule( response = client.post( url, json=schedule, headers={"Authorization": access_token} ) - assert response.status_code == 201 response_data = response.get_json() - garbage_collector.add_schedule_id(response_data["_id"]) + print(response_data) + if "_id" in response_data: + garbage_collector.add_schedule_id(response_data["_id"]) + assert response.status_code == 201 patch_data = { "enabled": True, @@ -50,3 +58,49 @@ def test_create_freecodecamp_schedule( url, json=patch_data, headers={"Authorization": access_token} ) assert response.status_code == 204 + + def test_create_freecodecamp_schedule_ko( + self, client, access_token, garbage_collector + ): + schedule = { + "name": "fcc_javascript_test_ko", + "category": "freecodecamp", + "enabled": False, + "tags": [], + "language": {"code": "fr", "name_en": "French", "name_native": "Français"}, + "config": { + "task_name": "freecodecamp", + "warehouse_path": "/freecodecamp", + "image": {"name": "openzim/freecodecamp", "tag": "1.0.0"}, + "monitor": False, + "platform": None, + "flags": { + "course": ("somecourse"), + "language": "eng", + "name": "acourse", + "title": "a title", + "description": ( + "a description which is way way way way way way way way way " + "way way way way way way way way way too long" + ), + }, + "resources": {"cpu": 3, "memory": 1024, "disk": 0}, + }, + "periodicity": "quarterly", + } + + url = "/schedules/" + response = client.post( + url, json=schedule, headers={"Authorization": access_token} + ) + response_data = response.get_json() + print(response_data) + if "_id" in response_data: + garbage_collector.add_schedule_id(response_data["_id"]) + assert response.status_code == 400 + assert "error_description" in response_data + assert "description" in response_data["error_description"] + assert ( + "Longer than maximum length 80." + in response_data["error_description"]["description"] + )