# -*- coding: utf-8 -*-
"""
@Time : 2025/8/4 15:27
@Auth : 九月的海
@File : test_login.py
@IDE : PyCharm
@Motto : Catch as catch can....
"""
import os
import allure
import pytest
import requests
from api_auto_framework.utils.handle_path import report_path


@allure.feature('创量账户登录')
@allure.story('管理员登录功能')
class TestAdminLogin:

    @allure.title('管理员邮箱正确登录')
    @allure.severity(allure.severity_level.CRITICAL)
    @allure.description('验证管理员使用正确邮箱和密码登录成功')
    def test_admin_login_success(self):
        # 准备测试数据
        url = "https://180.213.188.227/User/AdminUser/login"
        payload = {
            "email": "974509022@qq.com",
            "password": "0e3bc01f8f0409f4541015737925ed8e",
            "product_version": 0
        }
        headers = {'Content-Type': 'application/json'}

        # 添加请求详细信息到报告
        with allure.step("准备请求数据"):
            allure.attach(f"URL: {url}", "请求URL")
            allure.attach(str(payload), "请求体", allure.attachment_type.JSON)

        # 发送请求
        with allure.step("发送登录请求"):
            response = requests.post(url, json=payload, headers=headers, verify=False)

        # 添加响应信息到报告
        with allure.step("验证响应结果"):
            allure.attach(f"状态码: {response.status_code}", "响应状态")
            allure.attach(response.text, "响应内容", allure.attachment_type.JSON)

        # 断言验证
        assert response.status_code == 200
        assert response.json().get("code") == 0  # 假设0表示成功
        # assert "session" in response.json().get("data", {})

    @allure.title('测试管理员错误密码登录')
    @allure.severity(allure.severity_level.NORMAL)
    def test_admin_login_wrong_password(self):
        # 测试数据准备
        url = "https://180.213.188.227/User/AdminUser/login"
        payload = {
            "email": "974509022@qq.com",
            "password": "wrong_password",
            "product_version": 0
        }
        headers = {'Content-Type': 'application/json'}

        # 执行请求
        response = requests.post(url, json=payload, headers=headers, verify=False)

        # 添加步骤信息
        allure.attach(str(response.status_code), name="状态码")
        allure.attach(response.text, name="响应内容")

        # 验证预期结果
        assert response.status_code == 200
        assert response.json().get("code") != 0  # 非0表示失败


if __name__ == "__main__":
    # 运行测试并生成Allure报告
    report_dir = report_path

    pytest.main([
        '-v',  # 显示详细日志
        '-s',  # 打印输出信息
        '--alluredir', report_dir
    ])
    # 自动打开报告
    os.system(f'allure open {report_dir}')
