- config.json 改为按项目存储(projects.<id>),上传配置全局共用;旧单项目配置自动迁移为 readoor
- 新增项目管理(管理员):Git 仓库/凭据、源码目录、Xcode 工程、拷贝条目、配置替换方式、自定义字段、替换规则
- 通用配置替换:pbxproj 版本号与签名,patch_rules 支持 define / replace_line 及 ${app.X}、${server.X} 模板
- 请求通过 X-Project 头选择项目;打包任务记录 project_id,历史按项目过滤
- 前端顶部切换项目,App/服务器环境表单按项目显示字段
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
331 lines
13 KiB
Python
331 lines
13 KiB
Python
"""多项目配置测试"""
|
|
import json
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from backend.projects import get_project_source
|
|
from backend.services.project_patcher import ProjectPatchError, apply_project_config
|
|
|
|
DY_PROJECT = {
|
|
"id": "dyauction",
|
|
"name": "多云选",
|
|
"git": {"remote_url": "https://git.example.com/dy.git", "username": "bot", "password": "secret"},
|
|
"xcode": {"workspace": "", "project": "DyAuction.xcodeproj"},
|
|
"patcher": "generic",
|
|
"patch_rules": [
|
|
{"type": "define", "file": "DyAuction/Public/DyConstantsMacro.h", "name": "ISDEV", "value": "${server.ISDEV}"},
|
|
{"type": "define", "file": "DyAuction/Public/DyConstantsMacro.h", "name": "APPID", "value": '@"${app.APPID}"'},
|
|
],
|
|
"app_fields": [{"key": "APPID", "label": "APPID", "required": True}],
|
|
"server_fields": [{"key": "ISDEV", "label": "ISDEV", "required": True}],
|
|
}
|
|
|
|
DY = {"X-Project": "dyauction"}
|
|
|
|
|
|
@pytest.fixture
|
|
def dy_project(client, tmp_config):
|
|
resp = client.post("/api/config/projects", json=DY_PROJECT)
|
|
assert resp.status_code == 200, resp.text
|
|
return "dyauction"
|
|
|
|
|
|
def _root(tmp_config: Path) -> dict:
|
|
return json.loads(tmp_config.read_text())
|
|
|
|
|
|
# ---- 迁移与隔离 ----
|
|
|
|
def test_legacy_config_migrates_to_readoor_project(client, tmp_config):
|
|
assert client.get("/api/config/apps").status_code == 200
|
|
|
|
root = _root(tmp_config)
|
|
assert list(root["projects"]) == ["readoor"]
|
|
readoor = root["projects"]["readoor"]
|
|
assert readoor["patcher"] == "readoor"
|
|
assert readoor["git"]["use_env"] is True
|
|
assert readoor["apps"]["1"]["name"] == "测试App"
|
|
assert "apps" not in root
|
|
assert "upload" not in readoor
|
|
|
|
|
|
def test_projects_list_hides_git_password(client, dy_project):
|
|
public = client.get("/api/projects").json()
|
|
assert [p["id"] for p in public] == ["readoor", "dyauction"]
|
|
assert "git" not in public[1]
|
|
assert public[1]["app_fields"][0]["key"] == "APPID"
|
|
|
|
admin = client.get("/api/config/projects").json()
|
|
dy = next(p for p in admin if p["id"] == "dyauction")
|
|
assert dy["git"]["password_set"] is True
|
|
assert "password" not in dy["git"]
|
|
|
|
|
|
def test_project_data_is_isolated(client, dy_project):
|
|
assert client.get("/api/apps", headers=DY).json() == {}
|
|
assert "1" in client.get("/api/apps").json()
|
|
|
|
resp = client.post("/api/config/servers", headers=DY, json={"name": "测试环境", "ISDEV": "1", "api": "x"})
|
|
assert resp.status_code == 200
|
|
servers = client.get("/api/config/servers", headers=DY).json()
|
|
# 通用项目只保留自定义字段
|
|
assert servers["测试环境"] == {"ISDEV": "1", "app_id_prefix": 1}
|
|
# Readoor 项目的同名环境不受影响
|
|
assert client.get("/api/config/servers").json()["测试环境"]["api"] == "https://test.api.com"
|
|
|
|
|
|
def test_unknown_project_rejected(client, tmp_config):
|
|
resp = client.get("/api/apps", headers={"X-Project": "nope"})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_required_custom_fields(client, dy_project):
|
|
resp = client.post("/api/config/servers", headers=DY, json={"name": "正式环境"})
|
|
assert resp.status_code == 400
|
|
client.post("/api/config/servers", headers=DY, json={"name": "正式环境", "ISDEV": "0"})
|
|
|
|
resp = client.post("/api/config/apps", headers=DY, json={"name": "多云选", "server": "正式环境"})
|
|
assert resp.status_code == 400
|
|
resp = client.post("/api/config/apps", headers=DY,
|
|
json={"name": "多云选", "server": "正式环境", "APPID": "1587365345"})
|
|
assert resp.status_code == 200
|
|
assert resp.json()["id"] == "100"
|
|
|
|
|
|
def test_upload_config_is_shared(client, dy_project):
|
|
client.put("/api/config/upload", headers=DY, json={"mode": "webdav"})
|
|
assert client.get("/api/config/upload").json()["mode"] == "webdav"
|
|
|
|
|
|
def test_full_config_put_keeps_current_project(client, dy_project):
|
|
config = client.get("/api/config", headers=DY).json()
|
|
config["branches"] = ["develop"]
|
|
config["_project"] = "readoor"
|
|
assert client.put("/api/config", headers=DY, json=config).status_code == 200
|
|
assert client.get("/api/branches", headers=DY).json() == ["develop"]
|
|
assert client.get("/api/branches").json() == ["main", "dev"]
|
|
|
|
|
|
# ---- 项目管理 ----
|
|
|
|
def test_update_project_keeps_password_when_empty(client, dy_project, tmp_config):
|
|
resp = client.put("/api/config/projects/dyauction",
|
|
json={"name": "多云选拍卖", "git": {"remote_url": "https://git.example.com/dy2.git", "username": "bot"}})
|
|
assert resp.status_code == 200
|
|
dy = _root(tmp_config)["projects"]["dyauction"]
|
|
assert dy["name"] == "多云选拍卖"
|
|
assert dy["git"]["password"] == "secret"
|
|
assert dy["git"]["remote_url"].endswith("dy2.git")
|
|
|
|
|
|
@pytest.mark.parametrize("payload", [
|
|
{"id": "Bad ID"},
|
|
{"id": "readoor"},
|
|
{"id": "x1", "xcode": {"workspace": "X.xcworkspace"}}, # 通用替换需要 project
|
|
{"id": "x2", "xcode": {"project": "X.xcodeproj"}, "app_fields": [{"key": "server"}]},
|
|
{"id": "x3", "xcode": {"project": "X.xcodeproj"}, "patch_rules": [{"type": "define", "file": "../a.h", "name": "A"}]},
|
|
{"id": "x4", "xcode": {"project": "X.xcodeproj"}, "patch_rules": [{"type": "sed"}]},
|
|
])
|
|
def test_create_project_validation(client, tmp_config, payload):
|
|
assert client.post("/api/config/projects", json=payload).status_code == 400
|
|
|
|
|
|
def test_delete_project_keeps_last(client, dy_project):
|
|
assert client.delete("/api/config/projects/dyauction").status_code == 200
|
|
assert client.delete("/api/config/projects/readoor").status_code == 400
|
|
|
|
|
|
def test_project_management_requires_admin(client, tmp_config):
|
|
client.post("/api/users", json={"username": "builder", "password": "builder-password-123", "is_admin": False})
|
|
token = client.post("/api/auth/login",
|
|
json={"username": "builder", "password": "builder-password-123"}).json()["token"]
|
|
headers = {"Authorization": f"Bearer {token}"}
|
|
assert client.get("/api/config/projects", headers=headers).status_code == 403
|
|
assert client.post("/api/config/projects", json=DY_PROJECT, headers=headers).status_code == 403
|
|
assert client.get("/api/projects", headers=headers).status_code == 200
|
|
|
|
|
|
# ---- 任务 ----
|
|
|
|
def _create_dy_app(client):
|
|
client.post("/api/config/servers", headers=DY, json={"name": "正式环境", "ISDEV": "0"})
|
|
client.post("/api/config/schemes", headers=DY, json={"name": "DyAuction", "ossFloder": "dy"})
|
|
resp = client.post("/api/config/apps", headers=DY, json={
|
|
"name": "多云选", "server": "正式环境", "APPID": "1587365345",
|
|
"certificates": {"Ad_Hoc": {"name": "cn.touchv.auction", "cer": "c", "pro": "p"}},
|
|
})
|
|
return resp.json()["id"]
|
|
|
|
|
|
def test_task_belongs_to_project(client, dy_project):
|
|
app_id = _create_dy_app(client)
|
|
with patch("backend.services.build_queue.build_queue.submit", new_callable=AsyncMock):
|
|
resp = client.post("/api/tasks", headers=DY, json={
|
|
"app_id": app_id, "build_type": "Ad_Hoc", "scheme_id": "1", "branch": "main",
|
|
})
|
|
assert resp.status_code == 200, resp.text
|
|
assert resp.json()["project_id"] == "dyauction"
|
|
|
|
assert len(client.get("/api/tasks", headers=DY).json()) == 1
|
|
assert client.get("/api/tasks").json() == []
|
|
|
|
|
|
def test_task_rejects_obfuscation_when_unsupported(client, dy_project):
|
|
app_id = _create_dy_app(client)
|
|
resp = client.post("/api/tasks", headers=DY, json={
|
|
"app_id": app_id, "build_type": "Ad_Hoc", "scheme_id": "1", "branch": "main", "obfuscation": True,
|
|
})
|
|
assert resp.status_code == 400
|
|
|
|
|
|
def test_generic_project_does_not_restrict_schemes(client, dy_project):
|
|
_create_dy_app(client)
|
|
apps = client.get("/api/apps", headers=DY).json()
|
|
assert all(app["allowed_scheme_names"] == [] for app in apps.values())
|
|
|
|
|
|
# ---- 源码目录 ----
|
|
|
|
def test_project_source_uses_own_git_settings():
|
|
source = get_project_source("dyauction", DY_PROJECT)
|
|
assert source.remote_url == "https://git.example.com/dy.git"
|
|
assert source.remote_url_with_credentials == "https://bot:secret@git.example.com/dy.git"
|
|
assert source.shared_dir.parts[-2:] == ("sources", "dyauction")
|
|
assert source.manual_base is None
|
|
|
|
|
|
def test_legacy_project_source_falls_back_to_env():
|
|
with patch("backend.projects.GIT_REMOTE_URL", "https://env.example.com/r.git"), \
|
|
patch("backend.projects.GIT_USERNAME", "env"), patch("backend.projects.GIT_PASSWORD", ""):
|
|
source = get_project_source("readoor", {})
|
|
assert source.remote_url_with_credentials == "https://env@env.example.com/r.git"
|
|
assert source.manual_base is not None
|
|
|
|
|
|
async def test_build_uses_xcodeproj_when_no_workspace(tmp_path):
|
|
from backend.services.build_service import build_project
|
|
from backend.services.log_streamer import LogStreamer
|
|
|
|
proc = AsyncMock(returncode=0)
|
|
proc.wait = AsyncMock(return_value=0)
|
|
|
|
async def _stdout():
|
|
yield b"ok\n"
|
|
|
|
def _make(*args, **kwargs):
|
|
proc.stdout = _stdout()
|
|
return proc
|
|
|
|
with patch("backend.services.build_service.log_streamer", LogStreamer()), \
|
|
patch("asyncio.create_subprocess_exec", side_effect=_make) as mock_exec:
|
|
with pytest.raises(Exception, match="未找到 IPA 文件"):
|
|
await build_project("t1", MagicMock(), {"SCHEME": "DyAuction"}, tmp_path,
|
|
{"workspace": "", "project": "DyAuction.xcodeproj"})
|
|
archive_args = mock_exec.call_args_list[1].args
|
|
assert ("-project", "DyAuction.xcodeproj") == archive_args[2:4]
|
|
|
|
|
|
# ---- 通用配置替换 ----
|
|
|
|
PBXPROJ = """
|
|
A1 /* Release */ = {
|
|
isa = XCBuildConfiguration;
|
|
buildSettings = {
|
|
CODE_SIGN_IDENTITY = "iPhone Developer";
|
|
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
|
|
CURRENT_PROJECT_VERSION = 1.2.25;
|
|
DEVELOPMENT_TEAM = AAA;
|
|
"DEVELOPMENT_TEAM[sdk=iphoneos*]" = BBB;
|
|
MARKETING_VERSION = 1.2.25;
|
|
PRODUCT_BUNDLE_IDENTIFIER = cn.touchv.auction;
|
|
PROVISIONING_PROFILE_SPECIFIER = duoyunxuan;
|
|
"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = TouchLinked_adHoc;
|
|
};
|
|
name = Release;
|
|
};
|
|
A2 /* Release */ = {
|
|
isa = XCBuildConfiguration;
|
|
buildSettings = {
|
|
PRODUCT_BUNDLE_IDENTIFIER = cn.touchv.DyAuctionTests;
|
|
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/DyAuction.app/DyAuction";
|
|
};
|
|
name = Release;
|
|
};
|
|
"""
|
|
|
|
MACRO = """#define APPCHEME @"cntouchauction"
|
|
|
|
#define ISDEV 1
|
|
|
|
#if ISDEV
|
|
//#define APPID @"1587429852"
|
|
#define APPID @"1587026338"
|
|
#else
|
|
#define APPID @"1587365345"
|
|
#endif
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def dy_build_dir(tmp_path):
|
|
(tmp_path / "DyAuction.xcodeproj").mkdir()
|
|
(tmp_path / "DyAuction.xcodeproj" / "project.pbxproj").write_text(PBXPROJ)
|
|
(tmp_path / "DyAuction" / "Public").mkdir(parents=True)
|
|
(tmp_path / "DyAuction" / "Public" / "DyConstantsMacro.h").write_text(MACRO)
|
|
return tmp_path
|
|
|
|
|
|
def _dy_config(**overrides):
|
|
config = {
|
|
"VERSION": "1.2.26", "BUILD_VERSION": "1.2.26.1", "BUILD_TYPE": "Ad_Hoc",
|
|
"BUNDLE_ID": "cn.touchv.auction2", "TEAM_ID": "MG4Z7FU83W", "PROVISIONING_NAME": "Dy AdHoc",
|
|
"CERTIFICATE": "Apple Distribution", "APP_VARS": {"APPID": "1587000000"}, "SERVER_VARS": {"ISDEV": "0"},
|
|
}
|
|
config.update(overrides)
|
|
return config
|
|
|
|
|
|
def test_generic_patcher_replaces_macros_and_settings(dy_build_dir):
|
|
messages = apply_project_config(dy_build_dir, _dy_config(), DY_PROJECT)
|
|
|
|
macro = (dy_build_dir / "DyAuction" / "Public" / "DyConstantsMacro.h").read_text()
|
|
assert "#define ISDEV 0\n" in macro
|
|
assert macro.count('#define APPID @"1587000000"') == 2
|
|
assert '//#define APPID @"1587429852"' in macro
|
|
|
|
pbx = (dy_build_dir / "DyAuction.xcodeproj" / "project.pbxproj").read_text()
|
|
assert "MARKETING_VERSION = 1.2.26;" in pbx
|
|
assert "CURRENT_PROJECT_VERSION = 1.2.26.1;" in pbx
|
|
assert "PRODUCT_BUNDLE_IDENTIFIER = cn.touchv.auction2;" in pbx
|
|
assert "PRODUCT_BUNDLE_IDENTIFIER = cn.touchv.DyAuctionTests;" in pbx
|
|
assert '"DEVELOPMENT_TEAM[sdk=iphoneos*]" = MG4Z7FU83W;' in pbx
|
|
assert '"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = "Dy AdHoc";' in pbx
|
|
assert '"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";' in pbx
|
|
assert (dy_build_dir / "exportOptions.plist").exists()
|
|
assert any("ISDEV 0" in line for line in messages)
|
|
|
|
|
|
def test_generic_patcher_requires_template_values(dy_build_dir):
|
|
with pytest.raises(ProjectPatchError, match="app.APPID"):
|
|
apply_project_config(dy_build_dir, _dy_config(APP_VARS={"APPID": ""}), DY_PROJECT)
|
|
|
|
|
|
def test_generic_patcher_reports_missing_define(dy_build_dir):
|
|
project = {**DY_PROJECT, "patch_rules": [
|
|
{"type": "define", "file": "DyAuction/Public/DyConstantsMacro.h", "name": "NOPE", "value": "1"},
|
|
]}
|
|
with pytest.raises(ProjectPatchError, match="NOPE"):
|
|
apply_project_config(dy_build_dir, _dy_config(), project)
|
|
|
|
|
|
def test_generic_patcher_replace_line_rule(dy_build_dir):
|
|
project = {**DY_PROJECT, "patch_rules": [
|
|
{"type": "replace_line", "file": "DyAuction/Public/DyConstantsMacro.h",
|
|
"keyword": "APPCHEME", "line": '#define APPCHEME @"${BUNDLE_ID}"'},
|
|
]}
|
|
apply_project_config(dy_build_dir, _dy_config(), project)
|
|
macro = (dy_build_dir / "DyAuction" / "Public" / "DyConstantsMacro.h").read_text()
|
|
assert '#define APPCHEME @"cn.touchv.auction2"' in macro
|