# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test60
# FileName:      test_dependency.py
# Author:       lao_zhao
# Datetime:     2024/5/20 17:47
# Description:
# 
# ---------------------------------------------------------------------------
# 标记此用例会被依赖
import random
import string

import allure
import pytest, requests, base64
from faker import Faker

bpm_session = requests.sessions.Session()


@allure.epic("BPM-场景测试")
@allure.feature("认证接口")
@allure.story("登录系统")
@allure.title("登录--前置")
@pytest.mark.dependency()
def test_login():
    login_url = "http://120.46.172.186:8080/auth"
    login_data = {"username": "admin", "password": base64.b64encode("123456".encode()).decode()}
    res = bpm_session.request(method="post", url=login_url, json=login_data)
    try:
        assert "loginStatus" in res.text
    except AssertionError as e:
        print(e)
        raise e
    else:
        bpm_session.headers["Authorization"] = "Bearer " + res.json().get("token")
        print("断言成功")


# 标记当前用例会依赖前面的哪一条用例, 标记此用例会被依赖
@allure.epic("BPM-场景测试")
@allure.feature("维度管理")
@allure.story("添加维度")
@allure.title("添加维度-依赖登录")
@pytest.mark.dependency(depends=["test_login"])
def test_add_dem():
    add_dem_url = "http://120.46.172.186:8080/api/demension/v1/dem/addDem"
    data = Faker(locale="zh_cn")
    add_dem_data = {
        "code": "".join(random.sample(string.ascii_letters + string.digits, 12)),
        "description": data.address(),
        "isDefault": 0,
        "name": data.name()
    }

    res = bpm_session.request(method="post", url=add_dem_url, json=add_dem_data)
    try:
        assert "成功" in res.text
    except Exception as e:
        print(e)
        raise e
    else:
        print("断言成功")


@allure.epic("BPM-场景测试")
@allure.feature("test")
@allure.story("test")
@allure.title("test用例函数-依赖登录成功和添加维度成功")
@pytest.mark.dependency(depends=["test_add_dem", "test_login"])
def test1():
    print("用例函数1")
