# -*- coding:utf-8 -*- """ 二维码管理接口测试用例 """ import json import pytest import allure import logging import time import requests from dulizhan.library.BusinessKw.JoyHub.DownloadQrcodeManage import DownloadQrcodeManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @allure.feature("管理后台 - 二维码管理模块") class TestDownloadQrcodeManage: qrcode_id = None token_set = False @classmethod def setup_class(cls): """在整个测试类开始时登录一次,所有测试用例共享token""" logging.info("=============================================") logging.info("=========== 开始登录,获取Token ============") logging.info("=============================================") cls.test_case = DownloadQrcodeManage() 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 TestDownloadQrcodeManage.token_set is True, "登录失败,未获取到token" logging.info("登录验证通过") @allure.story("验证获得二维码分页列表") @allure.title("测试获得二维码分页列表接口") def test_joyhub_download_qrcode_page_get(self): """测试获得二维码分页列表接口""" with allure.step("1. 调用分页接口"): resp = self.test_case.kw_joyhub_download_qrcode_page_get(page_no=1, page_size=10) 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 "pageResult" in resp["data"], "响应中缺少pageResult字段" assert "list" in resp["data"]["pageResult"], "响应中缺少list字段" assert isinstance(resp["data"]["pageResult"]["list"], list), "list字段不是列表类型" assert "total" in resp["data"]["pageResult"], "响应中缺少total字段" assert isinstance(resp["data"]["pageResult"]["total"], int), "total字段不是整数类型" logging.info("获得二维码分页列表验证通过") @allure.story("验证获得二维码详情") @allure.title("测试获得二维码详情接口") def test_joyhub_download_qrcode_get_get(self): """测试获得二维码详情接口""" with allure.step("1. 从分页获取现有二维码ID"): page_resp = self.test_case.kw_joyhub_download_qrcode_page_get(page_no=1, page_size=10) assert page_resp.get("code") == 0, f"获取分页失败,code={page_resp.get('code')}, msg={page_resp.get('msg')}" assert page_resp.get("data", {}).get("pageResult", {}).get("list"), "没有找到可用的二维码数据" qrcode_id = page_resp["data"]["pageResult"]["list"][0].get("id") assert qrcode_id, "二维码ID为空" with allure.step("2. 调用获取详情接口"): resp = self.test_case.kw_joyhub_download_qrcode_get_get(qrcode_id=qrcode_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"] == qrcode_id, "返回的ID与请求的ID不一致" assert "title" in resp["data"], "响应中缺少title字段" assert isinstance(resp["data"]["title"], str), "title字段不是字符串类型" logging.info("获得二维码详情验证通过") @allure.story("验证创建和更新二维码") @allure.title("测试创建和更新二维码接口") def test_joyhub_download_qrcode_create_and_update(self): """测试创建和更新二维码接口""" created_title = None with allure.step("1. 创建二维码"): timestamp = int(time.time()) created_title = f"测试二维码_{timestamp}" create_params = { "title": created_title, "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_download_qrcode_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字段不是整数类型" qrcode_id = create_resp["data"] TestDownloadQrcodeManage.qrcode_id = qrcode_id logging.info(f"创建二维码成功,ID: {qrcode_id}") with allure.step("2. 调用更新接口"): timestamp = int(time.time()) update_params = { "qrcode_id": qrcode_id, "title": f"测试二维码_{timestamp}_updated", "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_download_qrcode_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, "更新二维码失败" logging.info("创建并更新二维码验证通过") @classmethod def teardown_class(cls): """在整个测试类结束时清理测试数据""" logging.info("=============================================") logging.info("=========== 清理测试数据 ============") logging.info("=============================================") # 从数据库表jh_download_qrcode中删除测试数据 if cls.qrcode_id: cls.test_case.clean_test_data_from_db(f"测试二维码_{cls.qrcode_id}") else: # 如果没有获取到qrcode_id,尝试删除所有测试数据 cls.test_case.clean_test_data_from_db("测试二维码_")