From d78013964af8044e4d7b761e0bf1e5113a4bfdd1 Mon Sep 17 00:00:00 2001 From: euvre <93761161+euvre@users.noreply.github.com> Date: Mon, 27 Apr 2026 12:01:28 +0000 Subject: [PATCH] tests: add missing HTTP API tests for dataset management endpoints removed in #14222 (#14390) ### What problem does this PR solve? ### Summary PR #14222 consolidated KB (web) API endpoints into RESTful Dataset (HTTP) API endpoints and deleted the web API test suite under `test_web_api/test_kb_app/` and `test_web_api/test_document_app/`. While most test coverage was migrated to the HTTP API test suite, some tests were not ported over. This PR adds back the missing coverage. ### Route migration reference | Old Web API | New HTTP API | Missing tests | |---|---|---| | `POST /v1/kb/update_metadata_setting` | `PUT /api/v1/datasets//metadata/config` | auth & error paths | | `GET /api/v1/datasets//auto_metadata` | `GET /api/v1/datasets//metadata/config` | auth & CRUD | | `PUT /api/v1/datasets//auto_metadata` | `PUT /api/v1/datasets//metadata/config` | auth & CRUD | | `GET /v1/kb//basic_info` | `GET /api/v1/datasets//ingestions/summary` | covered | | `POST /v1/kb/list_pipeline_logs` | `GET /api/v1/datasets//ingestions` | edge cases missing | ### Changes #### `test_file_management_within_dataset/test_metadata_config.py` (new, 10 tests) Covers `GET/PUT /datasets//metadata/config` (migrated from `test_kb_tags_meta.py`'s `test_update_metadata_setting` and `test_document_metadata.py`'s negative tests): - Authorization for dataset metadata config GET/PUT - Authorization for document metadata config PUT - Success, invalid dataset, missing payload, not found scenarios #### `test_dataset_management/test_ingestion_logs.py` (extended, +2 tests) Covers `GET /datasets//ingestions` edge cases (migrated from `test_kb_pipeline_tasks.py`): - Missing dataset ID - Abnormal date filter ### Type of change - [x] Other: Test coverage improvement --------- Signed-off-by: noob --- .../test_ingestion_logs.py | 18 +++ .../test_metadata_config.py | 140 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py diff --git a/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py b/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py index f74f7855b..8dcb58c31 100644 --- a/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py +++ b/test/testcases/test_http_api/test_dataset_management/test_ingestion_logs.py @@ -51,3 +51,21 @@ class TestGetIngestionLog: def test_get_ingestion_log_invalid_dataset(self, HttpApiAuth): res = get_ingestion_log(HttpApiAuth, "invalid_id", "some_log_id") assert res["code"] != 0, res + + +@pytest.mark.usefixtures("clear_datasets") +class TestListIngestionLogsEdgeCases: + @pytest.mark.p3 + def test_list_ingestion_logs_abnormal_date_filter(self, HttpApiAuth, add_dataset_func): + """Test list ingestion logs when create_date_from > create_date_to.""" + dataset_id = add_dataset_func + res = list_ingestion_logs( + HttpApiAuth, + dataset_id, + params={ + "desc": "false", + "create_date_from": "2025-02-01", + "create_date_to": "2025-01-01", + }, + ) + assert res["code"] != 0, res diff --git a/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py b/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py new file mode 100644 index 000000000..549340a47 --- /dev/null +++ b/test/testcases/test_http_api/test_file_management_within_dataset/test_metadata_config.py @@ -0,0 +1,140 @@ +# +# Copyright 2025 The InfiniFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +import pytest +import requests +from configs import HOST_ADDRESS, VERSION, INVALID_API_TOKEN +from libs.auth import RAGFlowHttpApiAuth +from common import HEADERS + +DATASETS_API_URL = f"/api/{VERSION}/datasets" + + +def get_dataset_metadata_config(auth, dataset_id, *, headers=HEADERS): + url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/metadata/config" + res = requests.get(url=url, headers=headers, auth=auth) + return res.json() + + +def update_dataset_metadata_config(auth, dataset_id, payload=None, *, headers=HEADERS): + url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/metadata/config" + res = requests.put(url=url, headers=headers, auth=auth, json=payload) + return res.json() + + +def update_document_metadata_config(auth, dataset_id, document_id, payload=None, *, headers=HEADERS): + url = f"{HOST_ADDRESS}{DATASETS_API_URL}/{dataset_id}/documents/{document_id}/metadata/config" + res = requests.put(url=url, headers=headers, auth=auth, json=payload) + return res.json() + + +@pytest.mark.p1 +class TestDatasetMetadataConfigAuthorization: + @pytest.mark.parametrize( + "invalid_auth, expected_code, expected_message", + [ + (None, 401, ""), + ( + RAGFlowHttpApiAuth(INVALID_API_TOKEN), + 401, + "", + ), + ], + ) + def test_get_metadata_config_auth_invalid(self, invalid_auth, expected_code, expected_message): + res = get_dataset_metadata_config(invalid_auth, "dataset_id") + assert res["code"] == expected_code, res + assert res["message"] == expected_message, res + + @pytest.mark.parametrize( + "invalid_auth, expected_code, expected_message", + [ + (None, 401, ""), + ( + RAGFlowHttpApiAuth(INVALID_API_TOKEN), + 401, + "", + ), + ], + ) + def test_update_metadata_config_auth_invalid(self, invalid_auth, expected_code, expected_message): + res = update_dataset_metadata_config(invalid_auth, "dataset_id", {}) + assert res["code"] == expected_code, res + assert res["message"] == expected_message, res + + +@pytest.mark.usefixtures("clear_datasets") +class TestDatasetMetadataConfig: + @pytest.mark.p2 + def test_get_metadata_config_success(self, HttpApiAuth, add_dataset_func): + dataset_id = add_dataset_func + res = get_dataset_metadata_config(HttpApiAuth, dataset_id) + assert res["code"] == 0, res + + @pytest.mark.p2 + def test_get_metadata_config_invalid_dataset(self, HttpApiAuth): + res = get_dataset_metadata_config(HttpApiAuth, "invalid_dataset_id") + assert res["code"] != 0, res + + @pytest.mark.p2 + def test_update_metadata_config_missing_payload(self, HttpApiAuth, add_dataset_func): + dataset_id = add_dataset_func + res = update_dataset_metadata_config(HttpApiAuth, dataset_id) + assert res["code"] != 0, res + + @pytest.mark.p2 + def test_update_metadata_config_invalid_dataset(self, HttpApiAuth): + res = update_dataset_metadata_config(HttpApiAuth, "invalid_dataset_id", {"fields": []}) + assert res["code"] != 0, res + + +@pytest.mark.p1 +class TestDocumentMetadataConfigAuthorization: + @pytest.mark.parametrize( + "invalid_auth, expected_code, expected_message", + [ + (None, 401, ""), + ( + RAGFlowHttpApiAuth(INVALID_API_TOKEN), + 401, + "", + ), + ], + ) + def test_update_document_metadata_config_auth_invalid(self, invalid_auth, expected_code, expected_message): + res = update_document_metadata_config(invalid_auth, "dataset_id", "document_id", {}) + assert res["code"] == expected_code, res + assert res["message"] == expected_message, res + + +@pytest.mark.usefixtures("clear_datasets") +class TestDocumentMetadataConfig: + @pytest.mark.p2 + def test_update_document_metadata_config_not_found(self, HttpApiAuth, add_dataset_func): + dataset_id = add_dataset_func + res = update_document_metadata_config(HttpApiAuth, dataset_id, "nonexistent_doc_id", {}) + assert res["code"] != 0, res + + @pytest.mark.p2 + def test_update_document_metadata_config_invalid_dataset(self, HttpApiAuth, add_document_func): + _, doc_id = add_document_func + res = update_document_metadata_config(HttpApiAuth, "invalid_dataset_id", doc_id, {}) + assert res["code"] != 0, res + + @pytest.mark.p2 + def test_update_document_metadata_config_invalid_document(self, HttpApiAuth, add_dataset_func): + dataset_id = add_dataset_func + res = update_document_metadata_config(HttpApiAuth, dataset_id, "invalid_doc_id", {}) + assert res["code"] != 0, res