"""登录界面自定义背景图接口(/api/v1/config/login-background)测试。 接口公开(无凭证可访问): - 运行目录存在 background.jpg / background.png 时返回对应图片二进制; - 两者同时存在时 jpg 优先; - 均不存在时返回 404 FILE_NOT_FOUND 标准错误体。 """ from __future__ import annotations import pytest # 最小合法 PNG 文件头(IHDR 数据块),仅用于让 send_file 返回二进制 _PNG_BYTES = ( b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR" b"\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00" ) _JPEG_BYTES = b"\xff\xd8\xff\xe0\x00\x10JFIF" @pytest.fixture() def background_dir(flask_app, tmp_path): """把背景图查找目录指到临时目录,测试后还原。""" original = flask_app.config.get("DMS_LOGIN_BACKGROUND_DIR") flask_app.config["DMS_LOGIN_BACKGROUND_DIR"] = str(tmp_path) yield tmp_path if original is None: flask_app.config.pop("DMS_LOGIN_BACKGROUND_DIR", None) else: flask_app.config["DMS_LOGIN_BACKGROUND_DIR"] = original def test_login_background_returns_jpg_when_present(client, background_dir): (background_dir / "background.jpg").write_bytes(_JPEG_BYTES) response = client.get("/api/v1/config/login-background") assert response.status_code == 200 assert response.mimetype == "image/jpeg" assert response.data == _JPEG_BYTES assert response.headers["X-Content-Type-Options"] == "nosniff" assert "max-age" in response.headers.get("Cache-Control", "") def test_login_background_returns_png_when_only_png_present(client, background_dir): (background_dir / "background.png").write_bytes(_PNG_BYTES) response = client.get("/api/v1/config/login-background") assert response.status_code == 200 assert response.mimetype == "image/png" assert response.data == _PNG_BYTES def test_login_background_prefers_jpg_over_png(client, background_dir): (background_dir / "background.png").write_bytes(_PNG_BYTES) (background_dir / "background.jpg").write_bytes(_JPEG_BYTES) response = client.get("/api/v1/config/login-background") assert response.status_code == 200 assert response.mimetype == "image/jpeg" assert response.data == _JPEG_BYTES def test_login_background_returns_404_when_absent(client, background_dir): response = client.get("/api/v1/config/login-background") assert response.status_code == 404 body = response.get_json() assert body["code"] == "FILE_NOT_FOUND" assert body["requestId"] # ─── 主题背景图(契约1.31):theme 查询参数优先 background-{theme},回落通用图 ─── def test_login_background_theme_prefers_themed_jpg(client, background_dir): (background_dir / "background.jpg").write_bytes(_JPEG_BYTES) (background_dir / "background-red.png").write_bytes(_PNG_BYTES) response = client.get("/api/v1/config/login-background?theme=red") assert response.status_code == 200 assert response.mimetype == "image/png" assert response.data == _PNG_BYTES def test_login_background_theme_falls_back_to_generic(client, background_dir): (background_dir / "background.jpg").write_bytes(_JPEG_BYTES) response = client.get("/api/v1/config/login-background?theme=green") assert response.status_code == 200 assert response.mimetype == "image/jpeg" assert response.data == _JPEG_BYTES def test_login_background_theme_whitelist_rejects_unknown(client, background_dir): (background_dir / "background.jpg").write_bytes(_JPEG_BYTES) # 白名单外的 theme 按未提供处理,回落通用背景图 response = client.get("/api/v1/config/login-background?theme=..%2Fetc") assert response.status_code == 200 assert response.data == _JPEG_BYTES