# -*- coding:utf-8 -*- """ FAQ分类管理接口测试用例 """ import json import pytest import allure import logging import time import requests from dulizhan.library.BusinessKw.JoyHub.FaqCateManage import FaqCateManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @allure.feature("管理后台 - FAQ分类管理模块") class TestFaqCateManage: faq_cate_id = None token_set = False @classmethod def setup_class(cls): """在整个测试类开始时登录一次,所有测试用例共享token""" logging.info("=============================================") logging.info("=========== 开始登录,获取Token ============") logging.info("=============================================") cls.test_case = FaqCateManage() username = "joytest" password = "Zhou1599" cls.test_case._clear_user_fingerprint(username) url = 'https://joyhub-website-manager-api-test.best-envision.com/admin-api/system/auth/login-dev' payload = {'username': username, 'password': password} headers = {'Content-Type': 'application/json', 'tenant-id': '126'} response = requests.post(url, json=payload, headers=headers, verify=False, timeout=10) login_response = response.json() if login_response and login_response.get('code') == 0: token = login_response.get('data', {}).get('accessToken', '') cls.test_case.set_joyhub_token(token) cls.token_set = True logging.info("登录成功,Token已设置") else: logging.error(f"登录失败: {login_response}") @allure.story("验证登录接口") @allure.title("测试登录接口") def test_joyhub_login_post(self): """测试登录接口""" assert TestFaqCateManage.token_set is True, "登录失败,未获取到token" logging.info("登录验证通过") @allure.story("验证获得FAQ分类列表") @allure.title("测试获得FAQ分类列表接口") def test_joyhub_faq_cate_list_get(self): """测试获得FAQ分类列表接口""" with allure.step("1. 调用列表接口"): resp = self.test_case.kw_joyhub_faq_cate_list_get() allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON) with allure.step("2. 验证响应"): assert resp is not None, "响应为空" assert "code" in resp, "响应中缺少code字段" assert resp["code"] == 0, f"请求失败,code={resp.get('code')}, msg={resp.get('msg')}" assert "data" in resp, "响应中缺少data字段" assert isinstance(resp["data"], list), "data字段不是列表类型" logging.info("获得FAQ分类列表验证通过") @allure.story("验证获得FAQ分类详情") @allure.title("测试获得FAQ分类详情接口") def test_joyhub_faq_cate_get_get(self): """测试获得FAQ分类详情接口""" with allure.step("1. 从列表获取现有FAQ分类ID"): list_resp = self.test_case.kw_joyhub_faq_cate_list_get() assert list_resp.get("code") == 0, f"获取列表失败,code={list_resp.get('code')}, msg={list_resp.get('msg')}" assert list_resp.get("data"), "没有找到可用的FAQ分类数据" faq_cate_id = list_resp["data"][0].get("id") or list_resp["data"][0].get("faqCateId") assert faq_cate_id, "FAQ分类ID为空" with allure.step("2. 调用获取详情接口"): resp = self.test_case.kw_joyhub_faq_cate_get_get(faq_cate_id=faq_cate_id) allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON) with allure.step("3. 验证响应"): assert resp is not None, "响应为空" assert "code" in resp, "响应中缺少code字段" assert resp["code"] == 0, f"请求失败,code={resp.get('code')}, msg={resp.get('msg')}" assert "data" in resp, "响应中缺少data字段" assert "id" in resp["data"], "响应中缺少id字段" assert resp["data"]["id"] == faq_cate_id, "返回的ID与请求的ID不一致" assert "title" in resp["data"], "响应中缺少title字段" assert isinstance(resp["data"]["title"], str), "title字段不是字符串类型" logging.info("获得FAQ分类详情验证通过") @allure.story("验证创建和更新FAQ分类") @allure.title("测试创建和更新FAQ分类接口") def test_joyhub_faq_cate_create_and_update(self): """测试创建和更新FAQ分类接口""" with allure.step("1. 创建FAQ分类"): timestamp = int(time.time()) create_params = { "title": f"测试FAQ分类_{timestamp}", "lang": "zh_CN", "rank_num": 1, "pid": 0, "status": 1 } allure.attach(json.dumps(create_params, ensure_ascii=False), name="创建请求参数", attachment_type=allure.attachment_type.TEXT) create_resp = self.test_case.kw_joyhub_faq_cate_create_post(**create_params) allure.attach(json.dumps(create_resp, ensure_ascii=False, indent=2), name="创建响应数据", attachment_type=allure.attachment_type.JSON) assert create_resp is not None, "响应为空" assert "code" in create_resp, "响应中缺少code字段" assert create_resp["code"] == 0, f"创建失败,code={create_resp.get('code')}, msg={create_resp.get('msg')}" assert "data" in create_resp, "响应中缺少data字段" assert isinstance(create_resp["data"], int), "data字段不是整数类型" faq_cate_id = create_resp["data"] TestFaqCateManage.faq_cate_id = faq_cate_id logging.info(f"创建FAQ分类成功,ID: {faq_cate_id}") with allure.step("2. 调用更新接口"): timestamp = int(time.time()) update_params = { "faq_cate_id": faq_cate_id, "title": f"测试FAQ分类_{timestamp}_updated", "lang": "en", "rank_num": 2, "pid": 0, "status": 2 } allure.attach(json.dumps(update_params, ensure_ascii=False), name="更新请求参数", attachment_type=allure.attachment_type.TEXT) update_resp = self.test_case.kw_joyhub_faq_cate_update_put(**update_params) allure.attach(json.dumps(update_resp, ensure_ascii=False, indent=2), name="更新响应数据", attachment_type=allure.attachment_type.JSON) with allure.step("3. 验证更新响应"): assert update_resp is not None, "响应为空" assert "code" in update_resp, "响应中缺少code字段" assert update_resp["code"] == 0, f"更新失败,code={update_resp.get('code')}, msg={update_resp.get('msg')}" assert "data" in update_resp, "响应中缺少data字段" assert update_resp["data"] is True, "更新FAQ分类失败" logging.info("创建并更新FAQ分类验证通过") @allure.story("验证删除FAQ分类") @allure.title("测试删除FAQ分类接口") def test_joyhub_faq_cate_delete_delete(self): """测试删除FAQ分类接口""" if not TestFaqCateManage.faq_cate_id: pytest.skip("没有可删除的FAQ分类数据") with allure.step("1. 调用删除接口"): resp = self.test_case.kw_joyhub_faq_cate_delete_delete(faq_cate_id=TestFaqCateManage.faq_cate_id) allure.attach(json.dumps(resp, ensure_ascii=False, indent=2), name="响应数据", attachment_type=allure.attachment_type.JSON) with allure.step("2. 验证响应"): assert resp is not None, "响应为空" assert "code" in resp, "响应中缺少code字段" assert resp["code"] == 0, f"删除失败,code={resp.get('code')}, msg={resp.get('msg')}" assert "data" in resp, "响应中缺少data字段" assert resp["data"] is True, "删除FAQ分类失败" logging.info("删除FAQ分类验证通过")