1+ import { readFileSync } from "fs" ;
2+ import { join } from "path" ;
13import { describe , expect , test } from "vite-plus/test" ;
2- import { isDashScopeE2EReady , parseStdoutJson , runCli } from "./helpers.ts" ;
4+ import { isDashScopeE2EReady , makeE2eOutputDir , parseStdoutJson , runCli } from "./helpers.ts" ;
35
46/**
57 * Auth 相关 E2E:只验证 CLI 进程能正常解析参数并退出。
@@ -18,6 +20,7 @@ describe("e2e: auth", () => {
1820 expect ( exitCode , stderr ) . toBe ( 0 ) ;
1921 expect ( stderr ) . toMatch ( / l o g i n | a p i - k e y / i) ;
2022 expect ( stderr ) . toMatch ( / - - c o n s o l e - s i t e / ) ;
23+ expect ( stderr ) . toMatch ( / - - o p e n - a p i / ) ;
2124 } ) ;
2225
2326 test ( "auth logout --help 正常退出" , async ( ) => {
@@ -35,7 +38,58 @@ describe("e2e: auth", () => {
3538 test ( "auth login 缺少 --api-key 时报用法错误并退出 (2)" , async ( ) => {
3639 const { stderr, exitCode } = await runCli ( [ "auth" , "login" , "--quiet" ] ) ;
3740 expect ( exitCode , stderr ) . toBe ( 2 ) ;
38- expect ( stderr ) . toMatch ( / - - a p i - k e y | U s a g e : / i) ;
41+ expect ( stderr ) . toMatch ( / C h o o s e e x a c t l y o n e l o g i n m o d e / ) ;
42+ } ) ;
43+
44+ test ( "auth login 一次只能选择一种登录模式" , async ( ) => {
45+ const { stderr, exitCode } = await runCli ( [
46+ "auth" ,
47+ "login" ,
48+ "--console" ,
49+ "--api-key" ,
50+ "sk-e2e-placeholder" ,
51+ ] ) ;
52+ expect ( exitCode ) . toBe ( 2 ) ;
53+ expect ( stderr ) . toMatch ( / C h o o s e e x a c t l y o n e l o g i n m o d e / ) ;
54+ } ) ;
55+
56+ test ( "auth login 模式专属参数不能脱离对应模式" , async ( ) => {
57+ const openApiFlagOnly = await runCli ( [ "auth" , "login" , "--access-key-id" , "LTAI-e2e" ] ) ;
58+ expect ( openApiFlagOnly . exitCode ) . toBe ( 2 ) ;
59+ expect ( openApiFlagOnly . stderr ) . toMatch ( / U s e - - o p e n - a p i w i t h - - a c c e s s - k e y - i d / ) ;
60+
61+ const baseUrlWithoutApiKey = await runCli ( [
62+ "auth" ,
63+ "login" ,
64+ "--console" ,
65+ "--base-url" ,
66+ "https://dashscope.aliyuncs.com" ,
67+ ] ) ;
68+ expect ( baseUrlWithoutApiKey . exitCode ) . toBe ( 2 ) ;
69+ expect ( baseUrlWithoutApiKey . stderr ) . toMatch ( / U s e - - b a s e - u r l o n l y w i t h - - a p i - k e y / ) ;
70+
71+ const consoleSiteWithoutConsole = await runCli ( [
72+ "auth" ,
73+ "login" ,
74+ "--api-key" ,
75+ "sk-e2e-placeholder" ,
76+ "--console-site" ,
77+ "international" ,
78+ ] ) ;
79+ expect ( consoleSiteWithoutConsole . exitCode ) . toBe ( 2 ) ;
80+ expect ( consoleSiteWithoutConsole . stderr ) . toMatch ( / U s e - - c o n s o l e - s i t e o n l y w i t h - - c o n s o l e / ) ;
81+ } ) ;
82+
83+ test ( "auth login --open-api 要求 AK/SK 成对输入" , async ( ) => {
84+ const { stderr, exitCode } = await runCli ( [
85+ "auth" ,
86+ "login" ,
87+ "--open-api" ,
88+ "--access-key-id" ,
89+ "LTAI-e2e" ,
90+ ] ) ;
91+ expect ( exitCode ) . toBe ( 2 ) ;
92+ expect ( stderr ) . toMatch ( / P r o v i d e - - a c c e s s - k e y - i d a n d - - a c c e s s - k e y - s e c r e t w i t h - - o p e n - a p i / ) ;
3993 } ) ;
4094
4195 test ( "auth login --dry-run --api-key 不发起校验与落盘" , async ( ) => {
@@ -71,7 +125,7 @@ describe("e2e: auth", () => {
71125 expect ( exitCode ) . toBe ( 2 ) ;
72126 const err = JSON . parse ( stderr . trim ( ) ) as { error ?: { code ?: number ; message ?: string } } ;
73127 expect ( err . error ?. code ) . toBe ( 2 ) ;
74- expect ( err . error ?. message ) . toMatch ( / - - a p i - k e y | c o n s o l e / i ) ;
128+ expect ( err . error ?. message ) . toMatch ( / C h o o s e e x a c t l y o n e l o g i n m o d e / ) ;
75129 } ) ;
76130
77131 test ( "auth logout --dry-run 不写入配置" , async ( ) => {
@@ -138,4 +192,77 @@ describe("e2e: auth", () => {
138192 expect ( exitCode ) . not . toBe ( 0 ) ;
139193 expect ( stderr ) . toMatch ( / U n k n o w n f l a g .* - - b a s e - u r l / ) ;
140194 } ) ;
195+
196+ test ( "auth status 展示 env OpenAPI AK/SK 且不接受 OpenAPI flag 覆盖" , async ( ) => {
197+ const { stdout, stderr, exitCode } = await runCli ( [ "auth" , "status" , "--output" , "json" ] , {
198+ ALIBABA_CLOUD_ACCESS_KEY_ID : "LTAI-e2e-placeholder" ,
199+ ALIBABA_CLOUD_ACCESS_KEY_SECRET : "secret-e2e-placeholder" ,
200+ } ) ;
201+ expect ( exitCode , stderr ) . toBe ( 0 ) ;
202+ const data = parseStdoutJson < {
203+ authenticated ?: boolean ;
204+ openapi ?: { source ?: string ; access_key_id ?: string ; access_key_secret ?: string } ;
205+ } > ( stdout ) ;
206+ expect ( data . authenticated ) . toBe ( true ) ;
207+ expect ( data . openapi ?. source ) . toBe ( "env" ) ;
208+ expect ( data . openapi ?. access_key_id ) . not . toBe ( "LTAI-e2e-placeholder" ) ;
209+ expect ( data . openapi ?. access_key_secret ) . not . toBe ( "secret-e2e-placeholder" ) ;
210+
211+ const denied = await runCli ( [ "auth" , "status" , "--access-key-id" , "ak" ] ) ;
212+ expect ( denied . exitCode ) . not . toBe ( 0 ) ;
213+ expect ( denied . stderr ) . toMatch ( / U n k n o w n f l a g .* - - a c c e s s - k e y - i d / ) ;
214+ } ) ;
215+
216+ test ( "auth login --open-api 持久化 OpenAPI AK/SK 并支持单独 logout" , async ( ) => {
217+ const configDir = makeE2eOutputDir ( "auth-openapi-login" ) ;
218+ const env = {
219+ BAILIAN_CONFIG_DIR : configDir ,
220+ ALIBABA_CLOUD_ACCESS_KEY_ID : "" ,
221+ ALIBABA_CLOUD_ACCESS_KEY_SECRET : "" ,
222+ } ;
223+
224+ const login = await runCli (
225+ [
226+ "auth" ,
227+ "login" ,
228+ "--open-api" ,
229+ "--access-key-id" ,
230+ "LTAI-e2e-login-placeholder" ,
231+ "--access-key-secret" ,
232+ "secret-e2e-login-placeholder" ,
233+ ] ,
234+ env ,
235+ ) ;
236+ expect ( login . exitCode , login . stderr ) . toBe ( 0 ) ;
237+ expect ( login . stderr ) . toMatch ( / O p e n A P I c r e d e n t i a l s s a v e d / ) ;
238+
239+ const config = JSON . parse ( readFileSync ( join ( configDir , "config.json" ) , "utf8" ) ) as Record <
240+ string ,
241+ unknown
242+ > ;
243+ expect ( config . access_key_id ) . toBe ( "LTAI-e2e-login-placeholder" ) ;
244+ expect ( config . access_key_secret ) . toBe ( "secret-e2e-login-placeholder" ) ;
245+ expect ( config . openapi_access_key_id ) . toBeUndefined ( ) ;
246+ expect ( config . openapi_access_key_secret ) . toBeUndefined ( ) ;
247+
248+ const status = await runCli ( [ "auth" , "status" , "--output" , "json" ] , env ) ;
249+ expect ( status . exitCode , status . stderr ) . toBe ( 0 ) ;
250+ const data = parseStdoutJson < {
251+ authenticated ?: boolean ;
252+ openapi ?: { source ?: string ; access_key_id ?: string ; access_key_secret ?: string } ;
253+ } > ( status . stdout ) ;
254+ expect ( data . authenticated ) . toBe ( true ) ;
255+ expect ( data . openapi ?. source ) . toBe ( "config" ) ;
256+ expect ( data . openapi ?. access_key_id ) . not . toBe ( "LTAI-e2e-login-placeholder" ) ;
257+ expect ( data . openapi ?. access_key_secret ) . not . toBe ( "secret-e2e-login-placeholder" ) ;
258+
259+ const logout = await runCli ( [ "auth" , "logout" , "--open-api" ] , env ) ;
260+ expect ( logout . exitCode , logout . stderr ) . toBe ( 0 ) ;
261+ expect ( logout . stderr ) . toMatch ( / C l e a r e d a c c e s s _ k e y _ i d / ) ;
262+
263+ const after = await runCli ( [ "auth" , "status" , "--output" , "json" ] , env ) ;
264+ expect ( after . exitCode , after . stderr ) . toBe ( 0 ) ;
265+ const afterData = parseStdoutJson < { authenticated ?: boolean ; openapi ?: unknown } > ( after . stdout ) ;
266+ expect ( afterData . openapi ) . toBeUndefined ( ) ;
267+ } ) ;
141268} ) ;
0 commit comments