| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- """登录界面自定义背景图接口(/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"]
|