# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test_58
# FileName:      test_dependecy_template.py
# Author:       xxxxxxx
# Datetime:     2024/1/4 14:12
# Description:
# 
# ---------------------------------------------------------------------------
import base64

import allure
import pytest
import requests

data_dict = {}   # 数据的依赖


@allure.epic("BPM系统依赖")
class TestDependency:
    # 标记用例需要被依赖
    @allure.feature("认证接口")
    @allure.story("登录系统")
    @allure.title("正向用例")
    @allure.severity("高")
    @pytest.mark.dependency()
    def test_login(self):
        login_url = "http://120.46.172.186:8080/auth"
        login_data = {"username": "admin", "password": base64.b64encode("123456".encode()).decode()}
        res = requests.request(method="post", url=login_url, json=login_data)
        # 断言，断言成功提取token
        try:
            assert "超级管理员" in res.text
        except AssertionError:
            raise AssertionError("断言失败")
        else:
            token = res.json().get("token")
            # 把token存放到字典中
            data_dict["token"] = token

    # 指定需要依赖的用例
    @allure.feature("维度管理")
    @allure.story("添加维度")
    @allure.title("正向用例")
    @allure.severity("高")
    @pytest.mark.dependency(depends=["TestDependency::test_login"])
    def test_add_demo(self):
        add_dem_url = "http://120.46.172.186:8080/api/demension/v1/dem/addDem"
        add_dem_data = {
            "code": "dem_code_test_528",
            "description": "测试维度528",
            "isDefault": 0,
            "name": "测试维度518"
        }
        header = {"Authorization": "Bearer " + data_dict["token"]}
        print(header)
        res = requests.request(method="post", url=add_dem_url, json=add_dem_data, headers=header)
        # 断言
        try:
            assert "成功" in res.text
        except AssertionError:
            raise AssertionError("断言失败")
        else:
            data_dict["dem_code"] = "dem_code_test_58"

    @allure.feature("测试模块")
    @allure.story("测试参数化")
    @allure.title("正向用例")
    @allure.severity("高")
    @pytest.mark.dependency(depends=["TestDependency::test_login", "TestDependency::test_add_demo"])
    @pytest.mark.parametrize("a, b, c", [[1, 2, 3], [4, 5, 6]])
    def test3(self, a, b, c):
        print("用例函数3", a, b, c)
        print("token:", data_dict["token"], "维度code:", data_dict["dem_code"])
