diff --git a/e2e/README.md b/e2e/README.md index 6239884d..765622b2 100644 --- a/e2e/README.md +++ b/e2e/README.md @@ -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 个读取接口。 diff --git a/e2e/model-keys.example.json b/e2e/model-keys.example.json new file mode 100644 index 00000000..348c46af --- /dev/null +++ b/e2e/model-keys.example.json @@ -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": "" + } + } + ] +} diff --git a/e2e/setup-auth.mjs b/e2e/setup-auth.mjs index 987980f2..94deaf34 100644 --- a/e2e/setup-auth.mjs +++ b/e2e/setup-auth.mjs @@ -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(); diff --git a/e2e/setup-model-keys.mjs b/e2e/setup-model-keys.mjs new file mode 100644 index 00000000..7c8d8987 --- /dev/null +++ b/e2e/setup-model-keys.mjs @@ -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; + } +} diff --git a/e2e/unit/setup-model-keys.test.mjs b/e2e/unit/setup-model-keys.test.mjs new file mode 100644 index 00000000..0c8f35fa --- /dev/null +++ b/e2e/unit/setup-model-keys.test.mjs @@ -0,0 +1,164 @@ +import assert from 'node:assert/strict'; +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { test } from 'node:test'; +import { setupModelKeys } from '../setup-model-keys.mjs'; + +const base = 'http://127.0.0.1:8090'; +const passwordEnv = 'LOOPFORGE_TEST_MODEL_KEYS_FIXTURE_PASSWORD'; +const password = 'fixture-password'; + +function configFile(t, contents) { + const dir = mkdtempSync(join(tmpdir(), 'loopforge-model-keys-')); + t.after(() => rmSync(dir, { recursive: true, force: true })); + const file = join(dir, 'keys.json'); + writeFileSync(file, typeof contents === 'string' ? contents : JSON.stringify(contents), { mode: 0o600 }); + return file; +} + +function setFixturePassword(t) { + const previous = process.env[passwordEnv]; + process.env[passwordEnv] = password; + t.after(() => { + if (previous === undefined) delete process.env[passwordEnv]; + else process.env[passwordEnv] = previous; + }); +} + +test('skips credential setup without a file or any nonblank credential', async t => { + const fetch = t.mock.method(globalThis, 'fetch'); + await setupModelKeys(''); + await setupModelKeys(configFile(t, ' \n')); + await setupModelKeys(configFile(t, { accounts: [] })); + await setupModelKeys(configFile(t, { accounts: [ + { gitToken: ' ', apiKeys: { codex: ' ', 'claude-code': null } }, + { gitToken: null }, + ] })); + assert.equal(fetch.mock.callCount(), 0); +}); + +test('updates each test account with its own model keys, CodeBuddy edition, and Git token', async t => { + setFixturePassword(t); + const file = configFile(t, { accounts: [ + { + login: 'e2e-models', passwordEnv, codeBuddyInternetEnvironment: 'ioa', gitToken: 'fake-github-token', + apiKeys: { + dsh: ' fake-dsh ', codebuddy: 'fake-ioa', cursor: 'fake-cursor', + kimi: 'fake-kimi', zcode: 'fake-zcode', codex: '', 'claude-code': 'fake-claude', + }, + }, + { + login: 'e2e-codebuddy-cn', passwordEnv, codeBuddyInternetEnvironment: 'internal', + apiKeys: { codebuddy: 'fake-cn', codex: 'fake-codex', 'claude-code': null }, + }, + ] }); + const requests = []; + const users = { 'e2e-models': 41, 'e2e-codebuddy-cn': 42 }; + t.mock.method(globalThis, 'fetch', async (url, options) => { + assert.equal(options.redirect, 'error'); + assert.equal(options.headers.Origin, base); + const body = options.body === undefined ? undefined : JSON.parse(options.body); + requests.push([options.method, url, options.headers.Cookie, body]); + if (options.method === 'GET') return Response.json([]); + if (options.method === 'PUT') return Response.json({}); + const id = users[body.login]; + return Response.json({ user: { id, loginName: body.login } }, { + headers: { 'Set-Cookie': `agentflow_user=session-${id}; Path=/; HttpOnly` }, + }); + }); + + await setupModelKeys(file, base); + assert.deepEqual(requests, [ + ['POST', base + '/api/auth/password', undefined, { login: 'e2e-models', password }], + ['POST', base + '/api/auth/password', undefined, { login: 'e2e-codebuddy-cn', password }], + ['PUT', base + '/api/users/41/credentials', 'agentflow_user=session-41', { + codeBuddyApiKey: 'fake-ioa', codeBuddyInternetEnvironment: 'ioa', claudeCodeApiKey: 'fake-claude', + agentCliCredentials: { + dsh: { apiKey: 'fake-dsh' }, cursor: { apiKey: 'fake-cursor' }, + kimi: { apiKey: 'fake-kimi' }, zcode: { apiKey: 'fake-zcode' }, + }, + }], + ['GET', base + '/api/users/41/git-http-credentials', 'agentflow_user=session-41', undefined], + ['PUT', base + '/api/users/41/git-http-credentials', 'agentflow_user=session-41', { + host: 'github.com', name: 'GitHub E2E', platform: 'github', token: 'fake-github-token', + }], + ['PUT', base + '/api/users/42/credentials', 'agentflow_user=session-42', { + codeBuddyApiKey: 'fake-cn', codeBuddyInternetEnvironment: 'internal', codexApiKey: 'fake-codex', + }], + ]); +}); + +test('imports a GitHub-only token and replaces an existing credential by ID', async t => { + setFixturePassword(t); + const login = 'e2e-git'; + const path = base + '/api/users/43/git-http-credentials'; + const account = { login, passwordEnv }; + const otherHost = { id: 'gitlab-id', host: 'gitlab.example.com', name: 'GitLab', platform: 'gitlab' }; + const existingGitHub = { + id: 'github-id', host: 'github.com', name: 'Existing GitHub', platform: 'github', username: 'octo', + }; + let credentials = [otherHost]; + const requests = []; + t.mock.method(globalThis, 'fetch', async (url, options) => { + if (url === base + '/api/auth/password') { + return Response.json({ user: { id: 43, loginName: login } }, { + headers: { 'Set-Cookie': 'agentflow_user=session-43; Path=/' }, + }); + } + assert.equal(url, path); + assert.equal(options.headers.Cookie, 'agentflow_user=session-43'); + requests.push([options.method, options.body === undefined ? undefined : JSON.parse(options.body)]); + return Response.json(credentials); + }); + + await setupModelKeys(configFile(t, { accounts: [{ ...account, gitToken: ' fake-github-first ' }] }), base); + credentials = [otherHost, existingGitHub]; + await setupModelKeys(configFile(t, { accounts: [{ ...account, gitToken: 'fake-github-rotated' }] }), base); + assert.deepEqual(requests, [ + ['GET', undefined], + ['PUT', { name: 'GitHub E2E', platform: 'github', host: 'github.com', token: 'fake-github-first' }], + ['GET', undefined], + ['PUT', { ...existingGitHub, token: 'fake-github-rotated' }], + ]); +}); + +test('rejects unreadable, invalid and duplicate input before making requests', async t => { + setFixturePassword(t); + const fetch = t.mock.method(globalThis, 'fetch'); + const missing = configFile(t, '') + '.missing'; + await assert.rejects(setupModelKeys(missing, base), /cannot read configuration file/); + await assert.rejects(setupModelKeys(configFile(t, '{ "apiKeys": "fake-private"'), base), { + message: 'Test credentials: configuration file contains invalid JSON.', + }); + const account = { login: 'e2e-models', passwordEnv, apiKeys: { dsh: 'fake-key' } }; + const duplicate = configFile(t, { accounts: [account, { ...account, login: 'E2E-models' }] }); + await assert.rejects(setupModelKeys(duplicate, base), /repeats the login/); + const unsupported = configFile(t, { accounts: [{ ...account, apiKeys: { deepseek: 'fake-key' } }] }); + await assert.rejects(setupModelKeys(unsupported, base), /unsupported provider ID/); + const invalidGitToken = configFile(t, { accounts: [{ ...account, gitToken: 123 }] }); + await assert.rejects(setupModelKeys(invalidGitToken, base), /gitToken must be a string or null/); + const valid = configFile(t, { accounts: [account] }); + await assert.rejects(setupModelKeys(valid, 'http://test.example.invalid'), /HTTPS or loopback HTTP/); + assert.equal(fetch.mock.callCount(), 0); +}); + +test('a failed login blocks every save and reports only the HTTP status', async t => { + setFixturePassword(t); + const file = configFile(t, { accounts: [ + { login: 'e2e-models', passwordEnv, apiKeys: { dsh: 'fake-dsh' } }, + { login: 'e2e-codebuddy-cn', passwordEnv, apiKeys: { codebuddy: 'fake-cn' } }, + ] }); + const fetch = t.mock.method(globalThis, 'fetch', async (url, { method, body }) => { + assert.equal(method, 'POST'); + const { login } = JSON.parse(body); + if (login !== 'e2e-models') return new Response('fake-secret-in-error', { status: 401 }); + return Response.json({ user: { id: 41, loginName: login } }, { + headers: { 'Set-Cookie': 'agentflow_user=session-41; Path=/' }, + }); + }); + await assert.rejects(setupModelKeys(file, base), { + message: 'Test credentials: login for e2e-codebuddy-cn failed (HTTP 401).', + }); + assert.equal(fetch.mock.callCount(), 2); +});