95 lines
5.1 KiB
Python
95 lines
5.1 KiB
Python
"""本机 Docker 行业演示 HTTP 验收,不改变默认官网。"""
|
|
import os
|
|
import sys
|
|
import time
|
|
import uuid
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
|
|
if os.environ.get("DJANGO_SETTINGS_MODULE") != "wagtailcms.settings.docker_local":
|
|
raise SystemExit("仅允许在 docker_local 隔离环境执行。")
|
|
|
|
import django
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
|
|
django.setup()
|
|
|
|
from wagtail.models import Page, Site
|
|
from apps.core.industry_templates import INDUSTRIES, seed_industry_templates
|
|
from apps.core.models import NavigationMenu, SiteSettings
|
|
from apps.forms.models import Lead
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise AssertionError(message)
|
|
|
|
|
|
def main():
|
|
session = requests.Session()
|
|
backend = "http://localhost:8000/api/v1/custom/core/demos/"
|
|
frontend = "http://frontend:3000"
|
|
site = Site.objects.get(is_default_site=True)
|
|
original_root = site.root_page_id
|
|
settings = list(SiteSettings.objects.values())
|
|
menus = list(NavigationMenu.objects.values())
|
|
page_state = list(Page.objects.order_by("pk").values_list("pk", "title", "live", "latest_revision_id"))
|
|
require(seed_industry_templates() == [], "重复初始化仍新增页面")
|
|
require(page_state == list(Page.objects.order_by("pk").values_list("pk", "title", "live", "latest_revision_id")), "初始化改变页面或修订")
|
|
site.refresh_from_db()
|
|
require(site.root_page_id == original_root, "默认官网根页面发生变化")
|
|
require(list(SiteSettings.objects.values()) == settings, "默认企业配置发生变化")
|
|
require(list(NavigationMenu.objects.values()) == menus, "原有导航被修改")
|
|
print("PASS: 初始化幂等、页面修订及默认官网配置保留")
|
|
listing = session.get(backend, timeout=30)
|
|
listing.raise_for_status()
|
|
require(len(listing.json()["items"]) == len(INDUSTRIES), "演示模板未全部初始化")
|
|
response = session.get(f"{frontend}/demo", timeout=30)
|
|
require(response.status_code == 200, "演示入口不可用")
|
|
for industry, spec in INDUSTRIES.items():
|
|
base = f"/demo/{industry}"
|
|
home = session.get(f"{backend}{industry}/", timeout=30)
|
|
home.raise_for_status()
|
|
data = home.json()
|
|
require(data["company"] == spec["company"], "行业企业资料串用")
|
|
paths = ["", "products", "solutions", "cases", "blog", "about", "contact", "privacy-policy",
|
|
*[f"products/{product[0]}" for product in spec["products"]],
|
|
f"solutions/{spec['solution'][0]}", "cases/example-project", "blog/project-checklist"]
|
|
for path in paths:
|
|
time.sleep(3)
|
|
response = session.get(f"{frontend}{base}/{path}", timeout=30)
|
|
require(response.status_code == 200, f"页面失败:{industry}/{path} HTTP {response.status_code}")
|
|
soup = BeautifulSoup(response.text, "html.parser")
|
|
require(spec["company"] in soup.header.get_text(), f"页头企业串用:{industry}/{path}")
|
|
nav = soup.find("nav", {"aria-label": "行业导航"})
|
|
require(nav is not None and all(a["href"].rstrip("/") == base or a["href"].startswith(base + "/") for a in nav.find_all("a")), "行业导航越界")
|
|
require("虚构" in response.text, "缺少演示标注")
|
|
require(soup.find("meta", {"name": "robots"}) is not None, "缺少演示搜索限制")
|
|
contact = session.get(f"{backend}{industry}/", params={"path": "contact"}, timeout=30).json()
|
|
form = next(block["value"]["form"] for block in contact["blocks"] if block["type"] == "form")
|
|
require(len(form["fields"]) == 5, "表单字段未持久化")
|
|
marker = f"industry-smoke-{uuid.uuid4().hex}"
|
|
payload = {
|
|
"form_id": form["id"], "data": {"name": "虚构测试", "company": spec["company"], "phone": "010-12345678", "email": "demo@example.test", "message": marker},
|
|
"source_url": f"http://localhost:13000{base}/contact", "website": "",
|
|
}
|
|
for attempt in range(3):
|
|
response = session.post("http://localhost:8000/api/v1/custom/leads/", json=payload, timeout=30)
|
|
if response.status_code != 429 or attempt == 2:
|
|
break
|
|
wait = min(120, max(1, int(response.headers.get("Retry-After", "60"))))
|
|
print(f"表单限流:遵守 Retry-After 等待 {wait} 秒")
|
|
time.sleep(wait)
|
|
require(response.status_code == 201, f"行业表单提交失败:{response.status_code}")
|
|
require(Lead.objects.filter(form_id=form["id"], data__message=marker).exists(), "行业线索未入库")
|
|
require(not Lead.objects.get(data__message=marker).form.notification_email, "演示表单配置了外部邮箱")
|
|
print(f"PASS: {spec['label']} 13 页面、独立导航、演示标注及表单入库")
|
|
for path in ["/demo/unknown", "/demo/software/missing", "/demo/manufacturing/products/content-platform"]:
|
|
require(session.get(frontend + path, timeout=30).status_code == 404, f"未知或跨行业路径未拒绝:{path}")
|
|
print("ALL INDUSTRY HTTP SMOKE CHECKS PASSED")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|