# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test61
# FileName:      test_pytest_decorator.py
# Author:       lao_zhao
# Datetime:     2024/7/12 9:40
# Description:
# 
# ---------------------------------------------------------------------------
import random
import string
import allure
import pytest
import requests


case_datas = [
    [{"code": "需要更新"}, {"isDelete":"0"}, "正向用例"],
    [{"code": ""}, {"message":"必填：code维度编码必填！"}, "反向用例1"],
    [{"": "abctest"}, {"message":"Required String parameter 'code' is not present"}, "反向用例2"]
]


class TestBPM:
    host = "http://36.139.193.99:8088"

    @allure.epic("bpm-场景测试")
    @allure.feature("认证接口")
    @allure.story("登录系统")
    @allure.title("bpm-场景测试-登录-正向")
    @pytest.mark.dependency()
    def test_login(self, set_depends_value):
        # 获取登录的url
        login_url = self.host + "/auth"
        # 配置登录数据
        login_data = {"username": "admin",
                      "password": "FMgcBcmzaLp5J8AFyR8XF0MhHRpBhHNFQyDn7HI2nBO0B9RgCEnhVjvXjLMNjwoRKp01dwHueQGZbZcsEdAQQwY1QXvJrr8Z4Jv2Wn5UCC4IKuH9cIeevsc2zSEMmKULN9FJV4e9DBbnTMoxHqyawMlxVN/pidjeA0kkXjOsBE8="}
        res = requests.request(method="post", url=login_url, json=login_data)
        try:
            assert "超级管理员" in res.text
        except AssertionError:
            raise AssertionError("断言失败")
        else:
            token = res.json().get("token")
            set_depends_value("TOKEN", token)

    @allure.epic("bpm-场景测试")
    @allure.feature("维度管理")
    @allure.story("添加维度")
    @allure.title("bpm-场景测试-添加维度-正向")
    @pytest.mark.dependency(depends=["TestBPM::test_login"])
    def test_add_dem(self, get_depends_value, set_depends_value):
        add_dem_url = self.host + "/api/demension/v1/dem/addDem"
        dem_code = "".join(random.sample(string.ascii_letters, 12))
        add_dem_data = {
                "code": dem_code,
                "description": "",
                "isDefault": 0,
                "name": "测试"+"".join(random.sample(string.ascii_letters, 8))
            }

        token = get_depends_value("TOKEN")
        res = requests.post(url=add_dem_url, json=add_dem_data, headers={"Authorization": "Bearer "+token})
        try:
            assert "成功" in res.text
        except AssertionError:
            raise AssertionError("断言失败")
        else:
            set_depends_value("demCode", dem_code)

    @allure.epic("bpm-场景测试")
    @allure.feature("维度管理")
    @allure.story("根据维度编码获取维度信息")
    # @allure.title("bpm-场景测试-添加维度-正向")
    @pytest.mark.parametrize("case_data, expect_data, title", case_datas)
    @pytest.mark.dependency(depends=["TestBPM::test_add_dem"])
    def test_get_dem_msg(self, get_depends_value, set_depends_value, case_data, expect_data, title):
        allure.dynamic.title(title)

        get_dem_msg_url = self.host + "/api/demension/v1/dem/getDem"
        # get_dem_msg_data = {"code": get_depends_value("demCode")}
        token = get_depends_value("TOKEN")

        if "code" in case_data.keys():
            if case_data["code"] == "需要更新":
                case_data["code"] = get_depends_value("demCode")

        res = requests.get(url=get_dem_msg_url, params=case_data, headers={"Authorization": "Bearer "+token})


        try:
            for key in expect_data:
                assert expect_data[key] == res.json().get(key)
        except AssertionError:
            raise AssertionError("断言失败")
        else:
            # 判断服务器返回的数据中是否有id
            if "id" in res.json():
                dem_id = res.json()["id"]
                set_depends_value("demId", dem_id)
            print("断言成功")

    # @pytest.mark.dependency(depends=["TestBPM::test_get_dem_msg"])
    # def test_org(self, get_depends_value):
    #     print(get_depends_value("TOKEN"))
    #     print(get_depends_value("demId"))
    #     print(get_depends_value("demCode"))