""" 用户管理测试用例 """ import pytest from api.user_api import UserAPI from config.settings import settings @pytest.mark.user @pytest.mark.regression class TestUser: """用户管理测试类""" @pytest.mark.asyncio async def test_create_user_success(self, authenticated_client, test_user_data, cleanup_user): """测试创建用户成功""" user_api = UserAPI(authenticated_client) response = await user_api.create_user(test_user_data) print(f"Response status: {response.status_code}") print(f"Response text: {response.text}") assert response.status_code == 201 data = response.json() assert "id" in data assert data["username"] == test_user_data["username"] assert data["email"] == test_user_data["email"] assert "password" not in data or data["password"] != test_user_data["password"] cleanup_user.append(data["id"]) @pytest.mark.asyncio async def test_create_user_duplicate_username(self, authenticated_client, test_user_data, cleanup_user): """测试创建重复用户名""" user_api = UserAPI(authenticated_client) await user_api.create_user(test_user_data) response = await user_api.create_user(test_user_data) assert response.status_code in [400, 409] @pytest.mark.asyncio async def test_get_user_by_id_success(self, authenticated_client, test_user_data, cleanup_user): """测试根据ID获取用户成功""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] response = await user_api.get_user_by_id(user_id) assert response.status_code == 200 data = response.json() assert data["id"] == user_id assert data["username"] == test_user_data["username"] cleanup_user.append(user_id) @pytest.mark.asyncio async def test_get_user_by_id_not_found(self, authenticated_client): """测试获取不存在的用户""" user_api = UserAPI(authenticated_client) response = await user_api.get_user_by_id(999999) assert response.status_code == 404 @pytest.mark.asyncio async def test_get_all_users_success(self, authenticated_client): """测试获取所有用户成功""" user_api = UserAPI(authenticated_client) response = await user_api.get_all_users() assert response.status_code == 200 data = response.json() assert isinstance(data, list) @pytest.mark.asyncio async def test_update_user_success(self, authenticated_client, test_user_data, cleanup_user): """测试更新用户成功""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] update_data = {"email": "updated@example.com"} response = await user_api.update_user(user_id, update_data) assert response.status_code == 200 data = response.json() assert data["email"] == "updated@example.com" cleanup_user.append(user_id) @pytest.mark.asyncio async def test_delete_user_success(self, authenticated_client, test_user_data, cleanup_user): """测试删除用户成功""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] response = await user_api.delete_user(user_id) assert response.status_code == 204 @pytest.mark.asyncio async def test_logical_delete_user_success(self, authenticated_client, test_user_data, cleanup_user): """测试逻辑删除用户成功""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] response = await user_api.logical_delete_user(user_id) assert response.status_code == 200 get_response = await user_api.get_user_by_id(user_id) assert get_response.status_code == 404 get_deleted_response = await user_api.get_all_users(include_deleted=True) deleted_users = get_deleted_response.json() assert any(u["id"] == user_id for u in deleted_users) cleanup_user.append(user_id) @pytest.mark.asyncio async def test_restore_user_success(self, authenticated_client, test_user_data, cleanup_user): """测试恢复用户成功""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] await user_api.logical_delete_user(user_id) response = await user_api.restore_user(user_id) assert response.status_code == 200 get_response = await user_api.get_user_by_id(user_id) assert get_response.status_code == 200 cleanup_user.append(user_id) @pytest.mark.asyncio async def test_check_username_exists_true(self, authenticated_client, test_user_data, cleanup_user): """测试检查用户名存在-返回true""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] response = await user_api.check_username_exists(test_user_data["username"]) assert response.status_code == 200 assert response.json() is True cleanup_user.append(user_id) @pytest.mark.asyncio async def test_check_username_exists_false(self, authenticated_client): """测试检查用户名存在-返回false""" user_api = UserAPI(authenticated_client) response = await user_api.check_username_exists("nonexistent_user") assert response.status_code == 200 assert response.json() is False @pytest.mark.asyncio async def test_check_email_exists_true(self, authenticated_client, test_user_data, cleanup_user): """测试检查邮箱存在-返回true""" user_api = UserAPI(authenticated_client) create_response = await user_api.create_user(test_user_data) user_id = create_response.json()["id"] response = await user_api.check_email_exists(test_user_data["email"]) assert response.status_code == 200 assert response.json() is True cleanup_user.append(user_id) @pytest.mark.asyncio async def test_check_email_exists_false(self, authenticated_client): """测试检查邮箱存在-返回false""" user_api = UserAPI(authenticated_client) response = await user_api.check_email_exists("nonexistent@example.com") assert response.status_code == 200 assert response.json() is False