# -*- coding: utf-8 -*-
# 作者 周琦 2026/04/30
"""
JoyHub 角色管理测试用例
"""
import allure
import logging
import json
import requests

from dulizhan.library.BusinessKw.JoyHub.RoleManage import RoleManage

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')


@allure.feature('管理后台 - 角色模块')
class TestRoleManage(object):
    test_case = RoleManage()
    created_role_id = None
    token_set = False

    @classmethod
    def setup_class(cls):
        """在整个测试类开始时登录一次，所有测试用例共享token"""
        logging.info("=============================================")
        logging.info("=========== 开始登录，获取Token ============")
        logging.info("=============================================")
        
        username = "joytest"
        password = "Zhou1599"

        # 直接调用登录接口（不使用加密头）
        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'}

        try:
            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', '')
                if token:
                    cls.test_case.set_joyhub_token(token)
                    cls.token_set = True
                    logging.info("登录成功，获取到Token: {}...".format(token[:20]))
                else:
                    logging.warning("登录成功但未获取到Token")
            else:
                logging.error("登录失败: {}".format(login_response))
        except Exception as e:
            logging.error("登录异常: {}".format(str(e)))

        logging.info("=============================================")
        logging.info("=========== 登录完成 ============")
        logging.info("=============================================")

    def setup_method(self):
        """每个测试用例执行前的准备工作"""
        if not self.token_set:
            logging.warning("Token未设置，可能影响后续接口调用")
        
        logging.info("-----------------------------Test Start-------------------------------")

    def teardown_method(self):
        logging.info("-----------------------------Test End-------------------------------")

    @allure.story("验证获得角色分页列表")
    def test_joyhub_role_page_get(self):
        """测试角色分页列表查询"""
        with allure.step("准备查询参数"):
            page_no = 1
            page_size = 10
            params = {"pageNo": page_no, "pageSize": page_size}
            allure.attach(json.dumps(params, ensure_ascii=False, indent=2), "请求参数", attachment_type=allure.attachment_type.JSON)
            logging.info("查询角色分页列表，pageNo: {}, pageSize: {}".format(page_no, page_size))

        with allure.step("调用查询接口"):
            response_data = self.test_case.kw_joyhub_role_page_get(
                note="获得角色分页列表",
                pageNo=page_no,
                pageSize=page_size
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert response_data['data'] is not None, "响应数据中的data字段不能为空"
            logging.info("断言: data字段不为空 ✓")

            data = response_data.get('data', {})
            assert isinstance(data, dict), "data字段应为字典类型"
            logging.info("断言: data字段为字典类型 ✓")
            
            if 'records' in data:
                assert isinstance(data['records'], list), "records字段应为列表类型"
                logging.info("断言: records字段为列表类型 ✓，记录数: {}".format(len(data['records'])))

        logging.info("✓ 角色分页列表查询成功")

    @allure.story("验证新增角色")
    def test_joyhub_role_create_post(self):
        """测试新增角色"""
        import random
        
        with allure.step("准备新增角色参数"):
            role_name = "测试角色{}".format(random.randint(1000, 9999))
            role_code = "TEST_ROLE_{}".format(random.randint(1000, 9999))
            payload = {
                "name": role_name,
                "code": role_code,
                "sort": 100,
                "status": 1,
                "remark": "测试角色备注"
            }
            allure.attach(json.dumps(payload, ensure_ascii=False, indent=2), "请求参数", attachment_type=allure.attachment_type.JSON)
            logging.info("准备新增角色: {}".format(role_name))

        with allure.step("调用新增角色接口"):
            response_data = self.test_case.kw_joyhub_role_create_post(
                note="新增角色",
                **payload
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert response_data['data'] is not None, "响应数据中的data字段不能为空"
            logging.info("断言: data字段不为空 ✓")
            
            assert isinstance(response_data['data'], int), "data字段应为整数类型"
            logging.info("断言: data字段为整数类型 ✓")

        logging.info("✓ 新增角色成功，角色ID: {}".format(response_data['data']))
        self.created_role_id = response_data['data']

    @allure.story("验证获得角色详情")
    def test_joyhub_role_get_get(self):
        """测试角色详情查询"""
        import random
        
        with allure.step("创建测试角色"):
            role_name = "测试角色详情{}".format(random.randint(1000, 9999))
            role_code = "TEST_ROLE_DETAIL_{}".format(random.randint(1000, 9999))
            create_resp = self.test_case.kw_joyhub_role_create_post(
                note="创建测试角色",
                name=role_name,
                code=role_code,
                sort=200,
                status=1
            )
            role_id = create_resp.get('data')
            logging.info("创建测试角色成功，角色ID: {}".format(role_id))

        with allure.step("调用查询角色详情接口"):
            params = {"id": role_id}
            allure.attach(json.dumps(params, ensure_ascii=False, indent=2), "请求参数", attachment_type=allure.attachment_type.JSON)
            response_data = self.test_case.kw_joyhub_role_get_get(
                note="获得角色信息",
                id=role_id
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert response_data['data'] is not None, "响应数据中的data字段不能为空"
            logging.info("断言: data字段不为空 ✓")

            data = response_data.get('data', {})
            assert 'id' in data, "角色详情中缺少id字段"
            logging.info("断言: 角色详情包含id字段 ✓")
            
            assert data['id'] == role_id, "返回的角色ID与请求不一致"
            logging.info("断言: 返回的角色ID与请求一致 ✓")
            
            assert 'name' in data, "角色详情中缺少name字段"
            logging.info("断言: 角色详情包含name字段 ✓")
            
            assert data['name'] == role_name, "返回的角色名称与创建时不一致"
            logging.info("断言: 返回的角色名称与创建时一致 ✓")

        logging.info("✓ 角色详情查询成功")

    @allure.story("验证修改角色")
    def test_joyhub_role_update_put(self):
        """测试修改角色"""
        import random
        
        with allure.step("创建测试角色"):
            role_name = "测试角色修改{}".format(random.randint(1000, 9999))
            role_code = "TEST_ROLE_UPDATE_{}".format(random.randint(1000, 9999))
            create_resp = self.test_case.kw_joyhub_role_create_post(
                note="创建测试角色",
                name=role_name,
                code=role_code,
                sort=300,
                status=1
            )
            role_id = create_resp.get('data')
            logging.info("创建测试角色成功，角色ID: {}".format(role_id))

        with allure.step("准备修改参数"):
            new_name = "修改后的角色名称{}".format(random.randint(1000, 9999))
            payload = {
                "id": role_id,
                "name": new_name,
                "code": role_code,
                "sort": 301,
                "status": 1,
                "remark": "修改后的备注"
            }
            allure.attach(json.dumps(payload, ensure_ascii=False, indent=2), "请求参数", attachment_type=allure.attachment_type.JSON)
            logging.info("准备修改角色，新名称: {}".format(new_name))

        with allure.step("调用修改角色接口"):
            response_data = self.test_case.kw_joyhub_role_update_put(
                note="修改角色",
                **payload
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert response_data['data'] is True, "角色修改失败"
            logging.info("断言: 角色修改成功 ✓")

        logging.info("✓ 修改角色成功")

    @allure.story("验证删除角色")
    def test_joyhub_role_delete_post(self):
        """测试删除角色"""
        import random
        
        with allure.step("创建测试角色"):
            role_name = "测试角色删除{}".format(random.randint(1000, 9999))
            role_code = "TEST_ROLE_DELETE_{}".format(random.randint(1000, 9999))
            create_resp = self.test_case.kw_joyhub_role_create_post(
                note="创建测试角色",
                name=role_name,
                code=role_code,
                sort=400,
                status=1
            )
            role_id = create_resp.get('data')
            logging.info("创建测试角色成功，角色ID: {}".format(role_id))

        with allure.step("准备删除参数"):
            payload = {"id": role_id}
            allure.attach(json.dumps(payload, ensure_ascii=False, indent=2), "请求参数", attachment_type=allure.attachment_type.JSON)
            logging.info("准备删除角色，角色ID: {}".format(role_id))

        with allure.step("调用删除接口"):
            response_data = self.test_case.kw_joyhub_role_delete_post(
                note="删除角色",
                id=role_id
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert response_data['data'] is True, "角色删除失败"
            logging.info("断言: 角色删除成功 ✓")

        logging.info("✓ 删除角色成功")

    @allure.story("验证批量删除角色")
    def test_joyhub_role_delete_list_post(self):
        """测试批量删除角色"""
        import random
        
        with allure.step("创建测试角色"):
            role_ids = []
            for i in range(2):
                role_name = "测试批量删除角色{}".format(random.randint(1000, 9999))
                role_code = "TEST_ROLE_BATCH_{}_{}".format(random.randint(1000, 9999), i)
                create_resp = self.test_case.kw_joyhub_role_create_post(
                    note="创建测试角色{}".format(i),
                    name=role_name,
                    code=role_code,
                    sort=500 + i,
                    status=1
                )
                role_ids.append(create_resp.get('data'))
            logging.info("创建测试角色成功，角色ID列表: {}".format(role_ids))

        with allure.step("准备批量删除参数"):
            payload = {"ids": role_ids}
            allure.attach(json.dumps(payload, ensure_ascii=False, indent=2), "请求参数", attachment_type=allure.attachment_type.JSON)
            logging.info("准备批量删除角色")

        with allure.step("调用批量删除接口"):
            response_data = self.test_case.kw_joyhub_role_delete_list_post(
                note="批量删除角色",
                ids=role_ids
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert response_data['data'] is True, "批量删除失败"
            logging.info("断言: 批量删除成功 ✓")

        logging.info("✓ 批量删除角色成功")

    @allure.story("验证获取角色精简信息列表")
    def test_joyhub_role_simple_list_get(self):
        """测试获取角色精简信息列表"""
        with allure.step("调用查询接口"):
            response_data = self.test_case.kw_joyhub_role_simple_list_get(
                note="获取角色精简信息列表"
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)
            logging.info("获取角色精简信息列表")

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert isinstance(response_data['data'], list), "data字段应为列表类型"
            logging.info("断言: data字段为列表类型 ✓，记录数: {}".format(len(response_data['data'])))

        logging.info("✓ 角色精简信息列表查询成功")

    @allure.story("验证获取所有角色精简信息列表")
    def test_joyhub_role_list_all_simple_get(self):
        """测试获取所有角色精简信息列表"""
        with allure.step("调用查询接口"):
            response_data = self.test_case.kw_joyhub_role_list_all_simple_get(
                note="获取所有角色精简信息列表"
            )
            allure.attach(json.dumps(response_data, ensure_ascii=False, indent=2), "响应数据", attachment_type=allure.attachment_type.JSON)
            logging.info("获取所有角色精简信息列表")

        with allure.step("验证响应数据"):
            assert response_data is not None, "响应数据不能为空"
            logging.info("断言: 响应数据不为空 ✓")
            
            assert 'code' in response_data, "响应数据中缺少code字段"
            logging.info("断言: 响应数据包含code字段 ✓")
            
            assert response_data['code'] == 0, "接口调用失败，code: {}, msg: {}".format(
                response_data.get('code'), response_data.get('msg', '未知错误'))
            logging.info("断言: 接口调用成功，code=0 ✓")
            
            assert 'data' in response_data, "响应数据中缺少data字段"
            logging.info("断言: 响应数据包含data字段 ✓")
            
            assert isinstance(response_data['data'], list), "data字段应为列表类型"
            logging.info("断言: data字段为列表类型 ✓，记录数: {}".format(len(response_data['data'])))

        logging.info("✓ 所有角色精简信息列表查询成功")
