68 lines
1.7 KiB
Python
68 lines
1.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
共享 fixtures:base_url、admin_token、miniapp_token
|
||
"""
|
||
import pytest
|
||
import requests
|
||
|
||
from config import (
|
||
API_BASE,
|
||
ADMIN_USERNAME,
|
||
ADMIN_PASSWORD,
|
||
MINIAPP_DEV_USER_ID,
|
||
get_env_banner,
|
||
)
|
||
|
||
|
||
def pytest_report_header(config):
|
||
"""pytest 报告头部显示测试环境,避免误测"""
|
||
return get_env_banner().strip().split("\n")
|
||
|
||
|
||
@pytest.fixture(scope="session")
|
||
def base_url():
|
||
"""API 基础地址"""
|
||
return API_BASE
|
||
|
||
|
||
@pytest.fixture(scope="session")
|
||
def admin_token(base_url):
|
||
"""
|
||
管理端 JWT。通过 POST /api/admin 登录获取。
|
||
失败时返回空字符串,用例可 skip。
|
||
"""
|
||
try:
|
||
r = requests.post(
|
||
f"{base_url}/api/admin",
|
||
json={"username": ADMIN_USERNAME, "password": ADMIN_PASSWORD},
|
||
timeout=10,
|
||
)
|
||
data = r.json()
|
||
if data.get("success") and data.get("token"):
|
||
return data["token"]
|
||
except Exception:
|
||
pass
|
||
return ""
|
||
|
||
|
||
@pytest.fixture(scope="session")
|
||
def miniapp_token(base_url):
|
||
"""
|
||
小程序 token。通过 POST /api/miniprogram/dev/login-as 获取(仅 APP_ENV=development)。
|
||
若 MINIAPP_DEV_USER_ID 未配置或接口不可用,返回空字符串。
|
||
"""
|
||
if not MINIAPP_DEV_USER_ID:
|
||
return ""
|
||
try:
|
||
r = requests.post(
|
||
f"{base_url}/api/miniprogram/dev/login-as",
|
||
json={"userId": MINIAPP_DEV_USER_ID},
|
||
timeout=10,
|
||
)
|
||
data = r.json()
|
||
if data.get("success") and data.get("data", {}).get("token"):
|
||
return data["data"]["token"]
|
||
except Exception:
|
||
pass
|
||
return ""
|