Files
iOSBuildServer/tests/test_distribution.py
T
shenleiandClaude Opus 5.5 6890f13aa9 fix: 钉钉桌面端通知二维码被裁剪
桌面端钉钉会把 Markdown 图片裁成横向区域,正方形二维码上下被裁掉。
二维码居中放到 3:2 白底画布上;网页中的二维码图片改用 object-fit 避免拉伸。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 10:44:35 +09:00

228 lines
8.4 KiB
Python

"""服务端分发产物生成测试。"""
import plistlib
from unittest.mock import patch
from backend.services.distribution import (
_artifact_stem,
_upload_webdav,
delete_published_artifacts,
_write_distribution_files,
_write_download_page,
_write_manifest,
publish_ipa,
get_published_download_url,
)
def test_distribution_artifact_name_contains_sanitized_branch():
assert _artifact_stem({"APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "feature/pay-v2", "BUILD_TYPE": "Ad_Hoc"}) == "100_2_0_0_0_feature_pay-v2_adhoc"
assert _artifact_stem({"APPID": "100", "VERSION": "2.0.0.0"}) == "100_2_0_0_0"
def test_distribution_files_use_current_service_config(tmp_path):
ipa = tmp_path / "source.ipa"
ipa.write_bytes(b"ipa")
config = {
"APPID": "100", "VERSION": "2.0.0", "APPID_NAME": "Test App",
"BUNDLE_ID": "com.example.test",
}
ipa_file, manifest, html = _write_distribution_files(config, ipa, tmp_path / "distribution")
_write_manifest(config, manifest, "https://files.example.com/app.ipa")
_write_download_page(config, html, "https://files.example.com/app.plist")
with manifest.open("rb") as f:
plist = plistlib.load(f)
assert ipa_file.read_bytes() == b"ipa"
assert plist["items"][0]["metadata"]["bundle-identifier"] == "com.example.test"
assert "itms-services://" in html.read_text(encoding="utf-8")
def test_app_store_distribution_uploads_only_ipa(tmp_path):
ipa = tmp_path / "source.ipa"
ipa.write_bytes(b"app-store-ipa")
config = {
"APPID": "100",
"VERSION": "2.0.0",
"BUILD_TYPE": "App_Store",
"SOURCE_BRANCH": "main",
"OSS_FLODER": "readoor",
"_upload_config": {"mode": "oss", "oss": {}},
}
with patch(
"backend.services.distribution._upload_oss",
return_value={".ipa": "https://files.example.com/readoor/iOS/100_2_0_0_main_appstore.ipa"},
) as upload:
download_url, qr_path = publish_ipa(config, ipa, tmp_path / "build")
uploaded_files = upload.call_args.args[1]
assert len(uploaded_files) == 1
assert uploaded_files[0][0].suffix == ".ipa"
assert download_url.endswith("100_2_0_0_main_appstore.ipa")
assert qr_path == ""
def test_adhoc_distribution_uploads_qrcode(tmp_path):
ipa = tmp_path / "source.ipa"
ipa.write_bytes(b"ad-hoc-ipa")
config = {
"APPID": "100",
"VERSION": "2.0.0",
"BUILD_TYPE": "Ad_Hoc",
"SOURCE_BRANCH": "dev",
"OSS_FLODER": "readoor",
"_upload_config": {"mode": "oss", "oss": {}},
}
def upload_files(_upload_config, files):
local_path, remote_path = files[0]
return {local_path.suffix: f"https://files.example.com/{remote_path}"}
with patch("backend.services.distribution._upload_oss", side_effect=upload_files) as upload:
download_url, qr_url = publish_ipa(config, ipa, tmp_path / "build")
assert download_url.endswith("100_2_0_0_dev_adhoc.html")
assert qr_url.endswith("100_2_0_0_dev_adhoc.png")
assert [call.args[1][0][0].suffix for call in upload.call_args_list] == [
".ipa", ".plist", ".html", ".png",
]
# 桌面端钉钉会把图片裁成横向比例,二维码需放在 3:2 的横向画布上
from PIL import Image
with Image.open(tmp_path / "build" / "distribution" / "100_2_0_0_dev_adhoc.png") as image:
assert image.width == round(image.height * 1.5)
def test_delete_adhoc_artifacts_deletes_all_remote_files():
config = {"APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "main", "BUILD_TYPE": "Ad_Hoc", "OSS_FLODER": "readoor"}
with patch("backend.services.distribution._delete_oss") as delete:
delete_published_artifacts(config, {"mode": "oss", "oss": {}})
assert delete.call_args.args[1] == [
"readoor/iOS/100_2_0_0_0_main_adhoc.ipa",
"readoor/iOS/100_2_0_0_0_main_adhoc.plist",
"readoor/iOS/100_2_0_0_0_main_adhoc.html",
"readoor/iOS/100_2_0_0_0_main_adhoc.png",
]
def test_delete_uses_saved_url_for_legacy_artifact_name():
# 旧任务已有 SOURCE_BRANCH 快照,但上传时仍采用未带分支的旧命名。
# 不能再根据当前命名规则推算,否则 OSS 会对不存在的键返回成功。
config = {
"APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "main",
"BUILD_TYPE": "Ad_Hoc", "OSS_FLODER": "readoor",
}
with patch("backend.services.distribution._delete_oss") as delete:
delete_published_artifacts(
config,
{"mode": "oss", "oss": {"base_url": "https://files.example.com"}},
"https://files.example.com/readoor/iOS/100_2_0_0_0.html",
)
assert delete.call_args.args[1] == [
"readoor/iOS/100_2_0_0_0.ipa",
"readoor/iOS/100_2_0_0_0.plist",
"readoor/iOS/100_2_0_0_0.html",
"readoor/iOS/100_2_0_0_0.png",
]
def test_webdav_upload_uses_configured_directory_without_oss_path(tmp_path, monkeypatch):
ipa = tmp_path / "build.ipa"
ipa.write_bytes(b"ipa")
calls = {"mkcol": [], "put": []}
class FakeResponse:
status_code = 201
class FakeClient:
def __init__(self, **_kwargs): pass
def __enter__(self): return self
def __exit__(self, *_args): pass
def request(self, method, url):
calls["mkcol"].append((method, url))
return FakeResponse()
def put(self, url, content):
calls["put"].append((url, content))
return FakeResponse()
monkeypatch.setattr("backend.services.distribution.httpx.Client", FakeClient)
result = _upload_webdav(
{
"webdav": {
"server_url": "http://dav.example.com",
"username": "u",
"password": "p",
"base_path": "/iOS/",
"public_url": "https://download.example.com",
},
},
[(ipa, "ios-builds/iOS/legacy-path.ipa")],
)
assert calls["mkcol"] == [("MKCOL", "http://dav.example.com/iOS/")]
assert calls["put"] == [("http://dav.example.com/iOS/build.ipa", b"ipa")]
assert result[".ipa"] == "https://download.example.com/iOS/build.ipa"
def test_delete_webdav_artifacts_uses_path_from_published_url():
config = {
"APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "main",
"BUILD_TYPE": "Ad_Hoc", "OSS_FLODER": "legacy-oss-folder",
}
upload = {
"mode": "webdav",
"webdav": {
"base_path": "/Android",
"public_url": "https://download.example.com",
},
}
with patch("backend.services.distribution._delete_webdav") as delete:
delete_published_artifacts(
config, upload,
"https://download.example.com/Android/build.html",
)
assert delete.call_args.args[1] == [
"Android/build.ipa",
"Android/build.plist",
"Android/build.html",
"Android/build.png",
]
def test_delete_webdav_dsym_uses_published_url():
config = {"APPID": "100", "VERSION": "2.0.0.0", "SOURCE_BRANCH": "main"}
upload = {
"mode": "webdav",
"webdav": {
"base_path": "/iOS",
"public_url": "https://download.example.com",
},
}
with patch("backend.services.distribution._delete_webdav") as delete:
from backend.services.distribution import delete_published_dsym
delete_published_dsym(config, upload, "https://download.example.com/iOS/build.dSYM.zip")
assert delete.call_args.args[1] == ["iOS/build.dSYM.zip"]
def test_oss_download_url_is_short_lived_and_uses_actual_object_key(monkeypatch):
class FakeBucket:
def __init__(self, *_args, **_kwargs): pass
def sign_url(self, method, key, expires, **kwargs):
assert (method, key, expires, kwargs) == ("GET", "readoor/iOS/100_2_0_0_0.ipa", 600, {"slash_safe": True})
return "https://signed.example.com/app.ipa?signature=1"
class FakeOss:
Auth = staticmethod(lambda *_args: object())
Bucket = FakeBucket
monkeypatch.setitem(__import__("sys").modules, "oss2", FakeOss)
url = get_published_download_url(
{"BUILD_TYPE": "App_Store"},
{"mode": "oss", "oss": {"access_key_id": "id", "access_key_secret": "secret", "endpoint": "oss.example.com", "bucket_name": "bucket", "base_url": "https://files.example.com"}},
"https://files.example.com/readoor/iOS/100_2_0_0_0.ipa",
)
assert "signature=1" in url