Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ AGENTFLOW_E2E_CODEBUDDY_API_KEY=ck_... ./local.sh api/40-model.test.mjs # 带

API Token 和 UI 登录账号应属于同一用户。模型 Key 在平台个人设置中配置,网络环境必须匹配 Key 所属版本。测试不会将模型 Key 写进测试源码。不要公开 `.auth/`、原始失败 trace 或本地凭据文件。

### 测试部署时批量导入模型 Key 和 Git Token

把配置模板复制到 Git 已忽略的 `.local/e2e-model-keys.json`,或在 CI 中将同格式的私密 JSON 挂载为文件。每个账号按需填写 `apiKeys` 和 GitHub 个人访问令牌 `gitToken`,密码从 `passwordEnv` 指定的 CI 密钥环境变量读取。测试账号需要提前创建并设置密码。平台健康检查通过后,在仓库根目录执行下面的命令(Node.js 20+)。[配置模板](model-keys.example.json)

```bash
export AGENTFLOW_E2E_BASE=http://127.0.0.1:8090
export AGENTFLOW_E2E_MODEL_KEYS_FILE="$PWD/.local/e2e-model-keys.json"
node e2e/setup-model-keys.mjs
```

如果流水线已调用 `node e2e/setup-auth.mjs`,给该步骤传入相同的环境变量即可,它会先导入凭证再创建 LoopForge 测试 API Token。设置 `LOOPFORGE_TEST_USER` 以选择 Token 对应的测试账号。远程测试平台使用 HTTPS。

- 未指定文件路径、文件为空或所有凭证值为空时跳过导入。空白字符串和 `null` 都按空值处理,对应的已有凭证会保留。显式指定的文件无法读取、配置错误、登录或保存失败时退出非零。
- `gitToken` 保存到该测试账号的 `github.com` HTTPS Git 凭证。再次导入会更新同一条凭证。单独导入 Git Token 时可省略 `apiKeys`。测试部署需支持按主机保存 Git 凭证。
- `dsh` 对应 DeepSeek Harness,`kimi` 对应 Kimi Code。CodeBuddy 每个账号只能保存一把 Key,iOA 和国内版分别使用 `ioa` 和 `internal`。国际版显式填 `""`,省略环境字段则沿用账号已有配置。
- 导入只保存凭证。跑真实模型用例时仍需设置 `AGENTFLOW_E2E_MODEL=1`,并按用例选择执行器和模型 ID。测试部署需支持配置中使用的 CLI。

## 覆盖范围

- `api/00-smoke`:健康、匿名和伪造身份拒绝、Token 权限边界及约 40 个读取接口。
Expand Down
27 changes: 27 additions & 0 deletions e2e/model-keys.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"accounts": [
{
"login": "e2e-models",
"passwordEnv": "LOOPFORGE_TEST_PASSWORD",
"codeBuddyInternetEnvironment": "ioa",
"gitToken": "",
"apiKeys": {
"dsh": "",
"codebuddy": "",
"cursor": "",
"kimi": "",
"zcode": "",
"codex": "",
"claude-code": ""
}
},
{
"login": "e2e-codebuddy-cn",
"passwordEnv": "LOOPFORGE_TEST_CN_PASSWORD",
"codeBuddyInternetEnvironment": "internal",
"apiKeys": {
"codebuddy": ""
}
}
]
}
4 changes: 4 additions & 0 deletions e2e/setup-auth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
if (process.env.AGENTFLOW_E2E_MODEL_KEYS_FILE?.trim()) {
const { setupModelKeys } = await import('./setup-model-keys.mjs');
await setupModelKeys();
}
const root=path.dirname(fileURLToPath(import.meta.url));
const base=(process.env.AGENTFLOW_E2E_BASE||'http://127.0.0.1:8090').replace(/\/$/,'');
const password=process.env.LOOPFORGE_TEST_PASSWORD||fs.readFileSync(process.env.LOOPFORGE_TEST_PASSWORD_FILE||path.join(root,'../.local/admin-password'),'utf8').trim();
Expand Down
198 changes: 198 additions & 0 deletions e2e/setup-model-keys.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Optional post-deploy setup for dedicated E2E accounts.
import { readFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';

const keyFields = new Map([
['codebuddy', 'codeBuddyApiKey'],
['codex', 'codexApiKey'],
['claude-code', 'claudeCodeApiKey'],
]);
const agentCLIs = new Set(['agy', 'cursor', 'dsh', 'kimi', 'opencode', 'pi', 'zcode']);
const codeBuddyEnvironments = new Set(['', 'internal', 'ioa']);
const githubHost = 'github.com';

function isObject(value) {
return value !== null && typeof value === 'object' && !Array.isArray(value);
}

function readAccounts(file) {
let raw;
try {
raw = readFileSync(file, 'utf8').trim();
} catch (error) {
throw new Error(`Test credentials: cannot read configuration file (${error.code || 'file error'}).`);
}
if (!raw) return [];

let config;
try {
config = JSON.parse(raw);
} catch {
throw new Error('Test credentials: configuration file contains invalid JSON.');
}
if (!Array.isArray(config?.accounts)) {
throw new Error('Test credentials: configuration must contain an accounts array.');
}

const accounts = [];
const seen = new Set();
for (const [index, account] of config.accounts.entries()) {
const label = `Test credentials: account #${index + 1}`;
if (!isObject(account) || (account.apiKeys !== undefined && !isObject(account.apiKeys))) {
throw new Error(`${label} must be an object with an optional apiKeys object.`);
}
if (account.gitToken != null && typeof account.gitToken !== 'string') {
throw new Error(`${label} gitToken must be a string or null.`);
}
const gitToken = account.gitToken?.trim() ?? '';
const update = {};
let keyCount = 0;
for (const [provider, value] of Object.entries(account.apiKeys ?? {})) {
if (value === null) continue;
if (typeof value !== 'string') {
throw new Error(`${label} apiKeys values must be strings or null.`);
}
const apiKey = value.trim();
if (!apiKey) continue;
const field = keyFields.get(provider);
if (field) {
update[field] = apiKey;
} else if (agentCLIs.has(provider)) {
update.agentCliCredentials ??= {};
update.agentCliCredentials[provider] = { apiKey };
} else {
throw new Error(`${label} has an unsupported provider ID. Use a LoopForge CLI ID.`);
}
keyCount++;
}
if (keyCount === 0 && !gitToken) continue;

const { login, passwordEnv, codeBuddyInternetEnvironment } = account;
if (typeof login !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9_.-]{1,63}$/.test(login)) {
throw new Error(`${label} must specify a valid local login.`);
}
const identity = login.toLowerCase();
if (seen.has(identity)) throw new Error(`${label} repeats the login ${login}.`);
seen.add(identity);
if (typeof passwordEnv !== 'string' || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(passwordEnv)) {
throw new Error(`${label} must specify a password environment variable name in passwordEnv.`);
}
const password = process.env[passwordEnv];
if (!password) {
throw new Error(`${label} (${login}) needs password environment variable ${passwordEnv}.`);
}
if (update.codeBuddyApiKey && codeBuddyInternetEnvironment !== undefined) {
if (!codeBuddyEnvironments.has(codeBuddyInternetEnvironment)) {
throw new Error(`${label} codeBuddyInternetEnvironment must be "", "internal", or "ioa".`);
}
update.codeBuddyInternetEnvironment = codeBuddyInternetEnvironment;
}
accounts.push({ login, password, update, keyCount, gitToken });
}
return accounts;
}

function testBase(raw) {
let base;
try {
base = new URL(raw);
} catch {
throw new Error('Test credentials: set AGENTFLOW_E2E_BASE to the test deployment URL.');
}
const loopback = ['localhost', '127.0.0.1', '[::1]'].includes(base.hostname);
if (base.username || base.password || base.search || base.hash ||
!(base.protocol === 'https:' || (base.protocol === 'http:' && loopback))) {
throw new Error('Test credentials: the test URL must use HTTPS or loopback HTTP, with no credentials, query or fragment.');
}
return base;
}

async function request(base, path, method, data, action, cookie) {
const headers = { Origin: base.origin, 'Content-Type': 'application/json' };
if (cookie) headers.Cookie = cookie;
let response;
try {
response = await fetch(base.origin + base.pathname.replace(/\/+$/, '') + path, {
method, headers, body: JSON.stringify(data),
redirect: 'error',
signal: AbortSignal.timeout(15_000),
});
} catch {
throw new Error(`Test credentials: ${action} failed (network error, timeout or redirect).`);
}
if (!response.ok) {
await response.body?.cancel().catch(() => {});
throw new Error(`Test credentials: ${action} failed (HTTP ${response.status}).`);
}
return response;
}

async function saveGitHubToken(base, path, cookie, login, token) {
const response = await request(base, path, 'GET', undefined, `read Git credentials for ${login}`, cookie);
const credentials = await response.json().catch(() => null);
if (!Array.isArray(credentials)) {
throw new Error(`Test credentials: unexpected Git credentials response for ${login}.`);
}

// An existing personal credential must be updated by ID. The list carries
// only metadata, so we can preserve its settings while replacing the token.
const existing = credentials.find(credential => credential?.host === githubHost && !credential.managed);
const update = existing
? { id: existing.id, name: existing.name, platform: existing.platform, username: existing.username }
: { name: 'GitHub E2E', platform: 'github' };
const saved = await request(
base, path, 'PUT', { ...update, host: githubHost, token }, `save GitHub token for ${login}`, cookie,
);
await saved.body?.cancel().catch(() => {});
console.log(`Test credentials: saved GitHub token for ${login}.`);
}

export async function setupModelKeys(
file = process.env.AGENTFLOW_E2E_MODEL_KEYS_FILE,
rawBase = process.env.AGENTFLOW_E2E_BASE,
) {
if (!file?.trim()) return;
const accounts = readAccounts(file.trim());
if (accounts.length === 0) {
console.log('Test credentials: nothing configured, skipped.');
return;
}
const base = testBase(rawBase);

// Validate every account before writing through the personal settings APIs.
const sessions = [];
for (const account of accounts) {
const { login, password } = account;
const response = await request(base, '/api/auth/password', 'POST', { login, password }, `login for ${login}`);
const cookie = response.headers.getSetCookie()
.find(value => value.startsWith('agentflow_user='))?.split(';', 1)[0];
const user = (await response.json().catch(() => null))?.user;
if (!cookie || cookie === 'agentflow_user=' || !Number.isSafeInteger(user?.id) || user.id <= 0 ||
typeof user.loginName !== 'string' || user.loginName.toLowerCase() !== login.toLowerCase()) {
throw new Error(`Test credentials: login for ${login} did not return a matching user session.`);
}
sessions.push({ account, id: user.id, cookie });
}

for (const { account, id, cookie } of sessions) {
const { login, update, keyCount, gitToken } = account;
const userPath = `/api/users/${id}`;
if (keyCount > 0) {
const response = await request(base, userPath + '/credentials', 'PUT', update, `save model keys for ${login}`, cookie);
await response.body?.cancel().catch(() => {});
console.log(`Test credentials: saved ${keyCount} model key(s) for ${login}.`);
}
if (gitToken) {
await saveGitHubToken(base, userPath + '/git-http-credentials', cookie, login, gitToken);
}
}
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
try {
await setupModelKeys();
} catch (error) {
console.error(error.message);
process.exitCode = 1;
}
}
Loading
Loading