Skip to content

Commit 94adb91

Browse files
committed
refactor(e2e): enhance auth login tests and remove unused dependencies
- Removed the `bailian-cli-commands` dependency from `pnpm-lock.yaml` and `package.json` as it was no longer needed. - Expanded the E2E tests in `auth.e2e.test.ts` to cover additional scenarios for the `auth login` command, ensuring proper error handling for conflicting flags and missing parameters. - Improved test assertions for better clarity and coverage of authentication flows.
1 parent a0b4666 commit 94adb91

3 files changed

Lines changed: 145 additions & 7 deletions

File tree

packages/commands/tests/e2e/auth.e2e.test.ts

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import { readFileSync } from "fs";
2+
import { join } from "path";
13
import { describe, expect, test } from "vite-plus/test";
2-
import { isDashScopeE2EReady, parseStdoutJson, runCommandE2e } from "./helpers.ts";
4+
import {
5+
isDashScopeE2EReady,
6+
makeE2eOutputDir,
7+
parseStdoutJson,
8+
runCommandE2e,
9+
} from "./helpers.ts";
310
import { AUTH_ROUTES } from "./topic-routes.ts";
411

512
/**
@@ -12,6 +19,63 @@ describe("e2e: auth", () => {
1219
expect(exitCode, stderr).toBe(0);
1320
expect(stderr).toMatch(/login|api-key/i);
1421
expect(stderr).toMatch(/--console-site/);
22+
expect(stderr).toMatch(/--open-api/);
23+
});
24+
25+
test("auth login 一次只能选择一种登录模式", async () => {
26+
const { stderr, exitCode } = await runCommandE2e(AUTH_ROUTES, [
27+
"auth",
28+
"login",
29+
"--console",
30+
"--api-key",
31+
"sk-e2e-placeholder",
32+
]);
33+
expect(exitCode).toBe(2);
34+
expect(stderr).toMatch(/Choose exactly one login mode/);
35+
});
36+
37+
test("auth login 模式专属参数不能脱离对应模式", async () => {
38+
const openApiFlagOnly = await runCommandE2e(AUTH_ROUTES, [
39+
"auth",
40+
"login",
41+
"--access-key-id",
42+
"LTAI-e2e",
43+
]);
44+
expect(openApiFlagOnly.exitCode).toBe(2);
45+
expect(openApiFlagOnly.stderr).toMatch(/Use --open-api with --access-key-id/);
46+
47+
const baseUrlWithoutApiKey = await runCommandE2e(AUTH_ROUTES, [
48+
"auth",
49+
"login",
50+
"--console",
51+
"--base-url",
52+
"https://dashscope.aliyuncs.com",
53+
]);
54+
expect(baseUrlWithoutApiKey.exitCode).toBe(2);
55+
expect(baseUrlWithoutApiKey.stderr).toMatch(/Use --base-url only with --api-key/);
56+
57+
const consoleSiteWithoutConsole = await runCommandE2e(AUTH_ROUTES, [
58+
"auth",
59+
"login",
60+
"--api-key",
61+
"sk-e2e-placeholder",
62+
"--console-site",
63+
"international",
64+
]);
65+
expect(consoleSiteWithoutConsole.exitCode).toBe(2);
66+
expect(consoleSiteWithoutConsole.stderr).toMatch(/Use --console-site only with --console/);
67+
});
68+
69+
test("auth login --open-api 要求 AK/SK 成对输入", async () => {
70+
const { stderr, exitCode } = await runCommandE2e(AUTH_ROUTES, [
71+
"auth",
72+
"login",
73+
"--open-api",
74+
"--access-key-id",
75+
"LTAI-e2e",
76+
]);
77+
expect(exitCode).toBe(2);
78+
expect(stderr).toMatch(/Provide --access-key-id and --access-key-secret with --open-api/);
1579
});
1680

1781
test("auth logout --help 正常退出", async () => {
@@ -29,7 +93,7 @@ describe("e2e: auth", () => {
2993
test("auth login 缺少 --api-key 时报用法错误并退出 (2)", async () => {
3094
const { stderr, exitCode } = await runCommandE2e(AUTH_ROUTES, ["auth", "login", "--quiet"]);
3195
expect(exitCode, stderr).toBe(2);
32-
expect(stderr).toMatch(/--api-key|Usage:/i);
96+
expect(stderr).toMatch(/Choose exactly one login mode/);
3397
});
3498

3599
test("auth login --dry-run --api-key 不发起校验与落盘", async () => {
@@ -70,7 +134,7 @@ describe("e2e: auth", () => {
70134
expect(exitCode).toBe(2);
71135
const err = JSON.parse(stderr.trim()) as { error?: { code?: number; message?: string } };
72136
expect(err.error?.code).toBe(2);
73-
expect(err.error?.message).toMatch(/--api-key|console/i);
137+
expect(err.error?.message).toMatch(/Choose exactly one login mode/);
74138
});
75139

76140
test("auth logout --dry-run 不写入配置", async () => {
@@ -162,4 +226,82 @@ describe("e2e: auth", () => {
162226
expect(exitCode).not.toBe(0);
163227
expect(stderr).toMatch(/Unknown flag.*--base-url/);
164228
});
229+
230+
test("auth status 展示 env OpenAPI AK/SK 且不接受 OpenAPI flag 覆盖", async () => {
231+
const { stdout, stderr, exitCode } = await runCommandE2e(
232+
AUTH_ROUTES,
233+
["auth", "status", "--output", "json"],
234+
{
235+
ALIBABA_CLOUD_ACCESS_KEY_ID: "LTAI-e2e-placeholder",
236+
ALIBABA_CLOUD_ACCESS_KEY_SECRET: "secret-e2e-placeholder",
237+
},
238+
);
239+
expect(exitCode, stderr).toBe(0);
240+
const data = parseStdoutJson<{
241+
authenticated?: boolean;
242+
openapi?: { source?: string; access_key_id?: string; access_key_secret?: string };
243+
}>(stdout);
244+
expect(data.authenticated).toBe(true);
245+
expect(data.openapi?.source).toBe("env");
246+
expect(data.openapi?.access_key_id).not.toBe("LTAI-e2e-placeholder");
247+
expect(data.openapi?.access_key_secret).not.toBe("secret-e2e-placeholder");
248+
249+
const denied = await runCommandE2e(AUTH_ROUTES, ["auth", "status", "--access-key-id", "ak"]);
250+
expect(denied.exitCode).not.toBe(0);
251+
expect(denied.stderr).toMatch(/Unknown flag.*--access-key-id/);
252+
});
253+
254+
test("auth login --open-api 持久化 OpenAPI AK/SK 并支持单独 logout", async () => {
255+
const configDir = makeE2eOutputDir("auth-openapi-login");
256+
const env = {
257+
BAILIAN_CONFIG_DIR: configDir,
258+
ALIBABA_CLOUD_ACCESS_KEY_ID: "",
259+
ALIBABA_CLOUD_ACCESS_KEY_SECRET: "",
260+
};
261+
262+
const login = await runCommandE2e(
263+
AUTH_ROUTES,
264+
[
265+
"auth",
266+
"login",
267+
"--open-api",
268+
"--access-key-id",
269+
"LTAI-e2e-login-placeholder",
270+
"--access-key-secret",
271+
"secret-e2e-login-placeholder",
272+
],
273+
env,
274+
);
275+
expect(login.exitCode, login.stderr).toBe(0);
276+
expect(login.stderr).toMatch(/OpenAPI credentials saved/);
277+
278+
const config = JSON.parse(readFileSync(join(configDir, "config.json"), "utf8")) as Record<
279+
string,
280+
unknown
281+
>;
282+
expect(config.access_key_id).toBe("LTAI-e2e-login-placeholder");
283+
expect(config.access_key_secret).toBe("secret-e2e-login-placeholder");
284+
expect(config.openapi_access_key_id).toBeUndefined();
285+
expect(config.openapi_access_key_secret).toBeUndefined();
286+
287+
const status = await runCommandE2e(AUTH_ROUTES, ["auth", "status", "--output", "json"], env);
288+
expect(status.exitCode, status.stderr).toBe(0);
289+
const data = parseStdoutJson<{
290+
authenticated?: boolean;
291+
openapi?: { source?: string; access_key_id?: string; access_key_secret?: string };
292+
}>(status.stdout);
293+
expect(data.authenticated).toBe(true);
294+
expect(data.openapi?.source).toBe("config");
295+
expect(data.openapi?.access_key_id).not.toBe("LTAI-e2e-login-placeholder");
296+
expect(data.openapi?.access_key_secret).not.toBe("secret-e2e-login-placeholder");
297+
298+
const logout = await runCommandE2e(AUTH_ROUTES, ["auth", "logout", "--open-api"], env);
299+
expect(logout.exitCode, logout.stderr).toBe(0);
300+
expect(logout.stderr).toMatch(/Cleared access_key_id/);
301+
302+
const after = await runCommandE2e(AUTH_ROUTES, ["auth", "status", "--output", "json"], env);
303+
expect(after.exitCode, after.stderr).toBe(0);
304+
const afterData = parseStdoutJson<{ authenticated?: boolean; openapi?: unknown }>(after.stdout);
305+
expect(afterData.openapi).toBeUndefined();
306+
});
165307
});

packages/runtime/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
"@types/node": "catalog:",
5151
"@typescript/native-preview": "7.0.0-dev.20260328.1",
5252
"ajv": "catalog:",
53-
"bailian-cli-commands": "workspace:*",
5453
"e2e": "workspace:*",
5554
"typescript": "^6.0.2",
5655
"vite-plus": "0.1.22",

pnpm-lock.yaml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)