From d1b28bd6000f90ca4631f63c722761ae9775288e Mon Sep 17 00:00:00 2001 From: Jeny Sadadia Date: Tue, 22 Oct 2024 16:09:30 +0530 Subject: [PATCH] tests: fix unit and e2e tests after the upgrade Fix root and token endpoint unit tests. Fix node creation test. Signed-off-by: Jeny Sadadia --- tests/e2e_tests/conftest.py | 7 ++++--- tests/e2e_tests/test_pipeline.py | 1 + tests/unit_tests/conftest.py | 9 +++++++++ tests/unit_tests/test_node_handler.py | 2 +- tests/unit_tests/test_root_handler.py | 2 +- tests/unit_tests/test_token_handler.py | 5 ++++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/e2e_tests/conftest.py b/tests/e2e_tests/conftest.py index b86a818a..217d075a 100644 --- a/tests/e2e_tests/conftest.py +++ b/tests/e2e_tests/conftest.py @@ -19,8 +19,8 @@ db_client = AsyncIOMotorClient(DB_URL) db = db_client[DB_NAME] -node_model_fields = set(Node.__fields__.keys()) -regression_model_fields = set(Regression.__fields__.keys()) +node_model_fields = set(Node.model_fields.keys()) +regression_model_fields = set(Regression.model_fields.keys()) paginated_response_keys = { 'items', 'total', @@ -42,7 +42,8 @@ async def db_create(collection, obj): """Database create method""" delattr(obj, 'id') col = db[collection] - res = await col.insert_one(obj.dict(by_alias=True)) + # res = await col.insert_one(obj.dict(by_alias=True)) + res = await col.insert_one(obj.model_dump(by_alias=True)) obj.id = res.inserted_id return obj diff --git a/tests/e2e_tests/test_pipeline.py b/tests/e2e_tests/test_pipeline.py index 736669a5..e78c0bde 100644 --- a/tests/e2e_tests/test_pipeline.py +++ b/tests/e2e_tests/test_pipeline.py @@ -42,6 +42,7 @@ async def test_node_pipeline(test_async_client): # Create a node node = { + "kind": "checkout", "name": "checkout", "path": ["checkout"], "data": { diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py index 4447b1b9..d0f7f6ab 100644 --- a/tests/unit_tests/conftest.py +++ b/tests/unit_tests/conftest.py @@ -277,6 +277,15 @@ async def mock_beanie_get_user_by_id(mocker): return async_mock +@pytest.fixture +async def mock_beanie_user_update(mocker): + """Mocks async call to external method to update user""" + async_mock = AsyncMock() + mocker.patch('fastapi_users_db_beanie.BeanieUserDatabase.update', + side_effect=async_mock) + return async_mock + + @pytest.fixture def mock_auth_current_user(mocker): """ diff --git a/tests/unit_tests/test_node_handler.py b/tests/unit_tests/test_node_handler.py index a4e50708..51fa97c7 100644 --- a/tests/unit_tests/test_node_handler.py +++ b/tests/unit_tests/test_node_handler.py @@ -33,7 +33,7 @@ def test_create_node_endpoint(mock_db_create, mock_publish_cloudevent, "describe": "v5.16-rc4-31-g2a987e65025e", } - revision_obj = Revision.parse_obj(revision_data) + revision_obj = Revision.model_validate(revision_data) node_obj = Node( id="61bda8f2eb1a63d2b7152418", kind="checkout", diff --git a/tests/unit_tests/test_root_handler.py b/tests/unit_tests/test_root_handler.py index 295ebefc..9d0efa5b 100644 --- a/tests/unit_tests/test_root_handler.py +++ b/tests/unit_tests/test_root_handler.py @@ -15,6 +15,6 @@ def test_root_endpoint(test_client): HTTP Response Code 200 OK JSON with 'message' key """ - response = test_client.get("/latest") + response = test_client.get("/") assert response.status_code == 200 assert response.json() == {"message": "KernelCI API"} diff --git a/tests/unit_tests/test_token_handler.py b/tests/unit_tests/test_token_handler.py index dc5d07a3..d5753faa 100644 --- a/tests/unit_tests/test_token_handler.py +++ b/tests/unit_tests/test_token_handler.py @@ -15,7 +15,8 @@ @pytest.mark.asyncio -async def test_token_endpoint(test_async_client, mock_user_find): +async def test_token_endpoint(test_async_client, mock_user_find, + mock_beanie_user_update): """ Test Case : Test KernelCI API /user/login endpoint Expected Result : @@ -34,6 +35,8 @@ async def test_token_endpoint(test_async_client, mock_user_find): is_verified=True ) mock_user_find.return_value = user + mock_beanie_user_update.return_value = user + response = await test_async_client.post( "user/login", headers={