# -*- coding:utf-8 -*- """ 国家信息管理接口测试用例 """ import pytest import json import time import logging import allure from dulizhan.library.BusinessKw.JoyHub.AddressCountryManage import AddressCountryManage logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') @allure.feature("管理后台 - 国家信息管理模块") class TestAddressCountryManage: country_id = None token_set = False @classmethod def setup_class(cls): """在整个测试类开始时登录一次,所有测试用例共享token""" logging.info("=============================================") logging.info("=========== 开始登录,获取Token ============") logging.info("=============================================") cls.test_case = AddressCountryManage() username = "joytest" password = "Zhou1599" # 登录逻辑 cls.test_case._clear_user_fingerprint(username) import requests 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}") pytest.skip("登录失败,跳过所有测试") @allure.story("验证登录") @allure.title("测试登录接口") def test_joyhub_login_post(self): """测试登录接口""" assert TestAddressCountryManage.token_set is True, "登录失败" logging.info("登录验证通过") @allure.story("验证获得国家信息分页") @allure.title("测试获得国家信息分页接口") def test_joyhub_address_country_page_get(self): """测试获得国家信息分页接口""" with allure.step("1. 调用接口"): resp = self.test_case.kw_joyhub_address_country_page_get(page_num=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')}" assert "data" in resp, "响应中缺少data字段" assert isinstance(resp["data"], dict), "data字段不是字典类型" assert "list" in resp["data"], "响应中缺少list字段" assert isinstance(resp["data"]["list"], list), "list字段不是列表类型" assert "total" in resp["data"], "响应中缺少total字段" assert isinstance(resp["data"]["total"], int), "total字段不是整数类型" logging.info("获得国家信息分页列表验证通过") @allure.story("验证创建和更新国家信息") @allure.title("测试创建和更新国家信息接口") def test_joyhub_address_country_create_and_update(self): """测试创建和更新国家信息接口""" with allure.step("1. 准备创建请求参数"): timestamp = int(time.time()) params = { "country_code": f"TC{timestamp}", "country_name": f"测试国家_{timestamp}", "country_name_en": f"Test Country {timestamp}", "phone_code": f"+{timestamp % 1000}", "status": 1 } allure.attach(json.dumps(params, ensure_ascii=False), name="创建请求参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用创建接口"): resp = self.test_case.kw_joyhub_address_country_create_post(**params) 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')}" assert "data" in resp, "响应中缺少data字段" assert isinstance(resp["data"], int), "data字段不是整数类型" country_id = resp["data"] TestAddressCountryManage.country_id = country_id logging.info(f"创建国家信息成功,国家信息ID: {country_id}") with allure.step("4. 调用更新接口"): update_timestamp = int(time.time()) update_params = { "country_id": country_id, "country_code": f"TC{update_timestamp}", "country_name": f"已更新国家_{update_timestamp}", "country_name_en": f"Updated Country {update_timestamp}", "phone_code": f"+{update_timestamp % 1000}", "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_address_country_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("5. 验证更新响应"): assert update_resp is not None, "响应为空" assert "code" in update_resp, "响应中缺少code字段" assert update_resp["code"] == 0, f"请求失败,code={update_resp.get('code')}" assert "data" in update_resp, "响应中缺少data字段" assert update_resp["data"] is True, "更新国家信息失败" logging.info("更新国家信息验证通过") @allure.story("验证获得国家信息详情") @allure.title("测试获得国家信息详情接口") def test_joyhub_address_country_get_get(self): """测试获得国家信息详情接口""" with allure.step("1. 先创建一个国家信息"): timestamp = int(time.time()) create_resp = self.test_case.kw_joyhub_address_country_create_post( country_code=f"TC{timestamp}", country_name=f"详情测试国家_{timestamp}", country_name_en=f"Detail Test Country {timestamp}", phone_code=f"+{timestamp % 1000}", status=2 ) country_id = create_resp.get("data") if create_resp and create_resp.get("code") == 0 else None if not country_id: pytest.skip("创建测试国家信息失败,跳过详情测试") allure.attach(json.dumps({"id": country_id}, ensure_ascii=False), name="国家信息ID", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用获得详情接口"): resp = self.test_case.kw_joyhub_address_country_get_get(country_id=country_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')}" assert "data" in resp, "响应中缺少data字段" assert isinstance(resp["data"], dict), "data字段不是字典类型" assert "id" in resp["data"], "响应中缺少id字段" assert resp["data"]["id"] == country_id, "返回的ID与请求的不一致" logging.info("获得国家信息详情验证通过") @allure.story("验证删除国家信息") @allure.title("测试删除国家信息接口") def test_joyhub_address_country_delete_delete(self): """测试删除国家信息接口""" with allure.step("1. 先创建一个测试国家信息"): timestamp = int(time.time()) create_resp = self.test_case.kw_joyhub_address_country_create_post( country_code=f"TC{timestamp}", country_name=f"待删除国家_{timestamp}", country_name_en=f"Delete Test Country {timestamp}", phone_code=f"+{timestamp % 1000}", status=2 ) country_id = create_resp.get("data") if create_resp and create_resp.get("code") == 0 else None if not country_id: pytest.skip("创建测试国家信息失败,跳过删除测试") allure.attach(json.dumps({"id": country_id}, ensure_ascii=False), name="国家信息ID", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用删除接口"): resp = self.test_case.kw_joyhub_address_country_delete_delete(country_id=country_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')}" assert "data" in resp, "响应中缺少data字段" assert resp["data"] is True, "删除国家信息失败" logging.info("删除国家信息验证通过") @allure.story("验证批量删除国家信息") @allure.title("测试批量删除国家信息接口") def test_joyhub_address_country_delete_list_delete(self): """测试批量删除国家信息接口""" with allure.step("1. 先创建两个测试国家信息"): timestamp = int(time.time()) resp1 = self.test_case.kw_joyhub_address_country_create_post( country_code=f"TC{timestamp}", country_name=f"批量删除国家1_{timestamp}", country_name_en=f"Batch Delete Country 1 {timestamp}", phone_code=f"+{timestamp % 1000}", status=2 ) resp2 = self.test_case.kw_joyhub_address_country_create_post( country_code=f"TC{timestamp+1}", country_name=f"批量删除国家2_{timestamp}", country_name_en=f"Batch Delete Country 2 {timestamp}", phone_code=f"+{(timestamp+1) % 1000}", status=2 ) country_id1 = resp1.get("data") if resp1 and resp1.get("code") == 0 else None country_id2 = resp2.get("data") if resp2 and resp2.get("code") == 0 else None if not country_id1 or not country_id2: pytest.skip("创建测试国家信息失败,跳过批量删除测试") country_ids = [country_id1, country_id2] allure.attach(json.dumps({"ids": country_ids}, ensure_ascii=False), name="待删除国家信息IDs", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用批量删除接口"): resp = self.test_case.kw_joyhub_address_country_delete_list_delete(country_ids=country_ids) 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')}" assert "data" in resp, "响应中缺少data字段" assert resp["data"] is True, "批量删除国家信息失败" logging.info("批量删除国家信息验证通过") @allure.story("验证批量更新国家信息状态") @allure.title("测试批量更新国家信息状态接口") def test_joyhub_address_country_update_status_list_put(self): """测试批量更新国家信息状态接口""" with allure.step("1. 先创建两个测试国家信息"): timestamp = int(time.time()) resp1 = self.test_case.kw_joyhub_address_country_create_post( country_code=f"TC{timestamp}", country_name=f"状态更新国家1_{timestamp}", country_name_en=f"Status Update Country 1 {timestamp}", phone_code=f"+{timestamp % 1000}", status=1 ) resp2 = self.test_case.kw_joyhub_address_country_create_post( country_code=f"TC{timestamp+1}", country_name=f"状态更新国家2_{timestamp}", country_name_en=f"Status Update Country 2 {timestamp}", phone_code=f"+{(timestamp+1) % 1000}", status=1 ) country_id1 = resp1.get("data") if resp1 and resp1.get("code") == 0 else None country_id2 = resp2.get("data") if resp2 and resp2.get("code") == 0 else None if not country_id1 or not country_id2: pytest.skip("创建测试国家信息失败,跳过批量更新状态测试") country_ids = [country_id1, country_id2] allure.attach(json.dumps({"ids": country_ids, "status": 2}, ensure_ascii=False), name="批量更新状态参数", attachment_type=allure.attachment_type.TEXT) with allure.step("2. 调用批量更新状态接口"): resp = self.test_case.kw_joyhub_address_country_update_status_list_put(country_ids=country_ids, status=2) 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')}" assert "data" in resp, "响应中缺少data字段" assert resp["data"] is True, "批量更新国家信息状态失败" logging.info("批量更新国家信息状态验证通过")