From db353278ce1afdeaeb81107a0c3913eb0c2f1a1c Mon Sep 17 00:00:00 2001 From: benoit74 Date: Mon, 6 Nov 2023 08:48:19 +0100 Subject: [PATCH 1/3] Check schedule flags validity also when creating a schedule from scratch --- .../backend/src/routes/schedules/schedule.py | 4 ++ .../routes/schedules/test_freecodecamp.py | 64 +++++++++++++++++-- .../routes/schedules/test_schedule.py | 37 +++++++++++ 3 files changed, 100 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"] + ) diff --git a/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py b/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py index 70249d32..9144abee 100644 --- a/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py +++ b/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py @@ -303,6 +303,43 @@ def test_create_schedule_missing_config_keys(self, client, access_token, key): ) assert response.status_code == 400 + def test_create_schedule_flags_ko(self, client, access_token): + schedule = { + "name": "ifixit flags ko", + "category": "ifixit", + "enabled": False, + "tags": [], + "language": { + "code": "en", + "name_en": "English", + "name_native": "English", + }, + "config": { + "task_name": "ifixit", + "warehouse_path": "/ifixit", + "flags": {}, + "image": {"name": "openzim/ifixit", "tag": "latest"}, + "monitor": False, + "platform": "ifixit", + "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) + assert response.status_code == 400 + assert "error_description" in response_data + assert "language" in response_data["error_description"] + assert ( + "Missing data for required field." + in response_data["error_description"]["language"] + ) + def test_image_names(self, client, schedule, access_token): url = "/schedules/{}/image-names".format(schedule["name"]) response = client.get( From 6faccfce03241513cf2adbf2b47d43f26d1aa24d Mon Sep 17 00:00:00 2001 From: benoit74 Date: Mon, 6 Nov 2023 09:07:52 +0100 Subject: [PATCH 2/3] Ensure schedule creation tests are failing for the good reason --- .../routes/schedules/test_schedule.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py b/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py index 9144abee..337407dd 100644 --- a/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py +++ b/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py @@ -265,7 +265,10 @@ def test_create_schedule_missing_keys(self, client, access_token, key): "flags": {}, "image": {"name": "openzim/phet", "tag": "latest"}, "monitor": False, + "platform": None, + "resources": {"cpu": 3, "memory": 1024, "disk": 0}, }, + "periodicity": "quarterly", } del schedule[key] @@ -273,7 +276,15 @@ def test_create_schedule_missing_keys(self, client, access_token, key): response = client.post( url, json=schedule, headers={"Authorization": access_token} ) + response_data = response.get_json() + print(response_data) assert response.status_code == 400 + assert "error_description" in response_data + assert key in response_data["error_description"] + assert ( + "Missing data for required field." + in response_data["error_description"][key] + ) @pytest.mark.parametrize("key", ["warehouse_path", "flags", "image"]) def test_create_schedule_missing_config_keys(self, client, access_token, key): @@ -293,7 +304,10 @@ def test_create_schedule_missing_config_keys(self, client, access_token, key): "flags": {}, "image": {"name": "openzim/phet", "tag": "latest"}, "monitor": False, + "platform": None, + "resources": {"cpu": 3, "memory": 1024, "disk": 0}, }, + "periodicity": "quarterly", } del schedule["config"][key] @@ -301,7 +315,16 @@ def test_create_schedule_missing_config_keys(self, client, access_token, key): response = client.post( url, json=schedule, headers={"Authorization": access_token} ) + response_data = response.get_json() + print(response_data) assert response.status_code == 400 + assert "error_description" in response_data + assert "config" in response_data["error_description"] + assert key in response_data["error_description"]["config"] + assert ( + "Missing data for required field." + in response_data["error_description"]["config"][key] + ) def test_create_schedule_flags_ko(self, client, access_token): schedule = { From 2e690411476b731bb551264c0b92a508d1f055f9 Mon Sep 17 00:00:00 2001 From: benoit74 Date: Mon, 6 Nov 2023 11:53:32 +0100 Subject: [PATCH 3/3] Remove debug print --- .../tests/integration/routes/schedules/test_freecodecamp.py | 2 -- .../src/tests/integration/routes/schedules/test_schedule.py | 3 --- 2 files changed, 5 deletions(-) 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 7f558cc2..0533dc93 100644 --- a/dispatcher/backend/src/tests/integration/routes/schedules/test_freecodecamp.py +++ b/dispatcher/backend/src/tests/integration/routes/schedules/test_freecodecamp.py @@ -31,7 +31,6 @@ def test_create_freecodecamp_schedule_ok( 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 == 201 @@ -94,7 +93,6 @@ def test_create_freecodecamp_schedule_ko( 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 diff --git a/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py b/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py index 337407dd..c5c03315 100644 --- a/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py +++ b/dispatcher/backend/src/tests/integration/routes/schedules/test_schedule.py @@ -277,7 +277,6 @@ def test_create_schedule_missing_keys(self, client, access_token, key): url, json=schedule, headers={"Authorization": access_token} ) response_data = response.get_json() - print(response_data) assert response.status_code == 400 assert "error_description" in response_data assert key in response_data["error_description"] @@ -316,7 +315,6 @@ def test_create_schedule_missing_config_keys(self, client, access_token, key): url, json=schedule, headers={"Authorization": access_token} ) response_data = response.get_json() - print(response_data) assert response.status_code == 400 assert "error_description" in response_data assert "config" in response_data["error_description"] @@ -354,7 +352,6 @@ def test_create_schedule_flags_ko(self, client, access_token): url, json=schedule, headers={"Authorization": access_token} ) response_data = response.get_json() - print(response_data) assert response.status_code == 400 assert "error_description" in response_data assert "language" in response_data["error_description"]