1+ import { readFileSync } from "fs" ;
2+ import { join } from "path" ;
13import { 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" ;
310import { 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 ( / l o g i n | a p i - k e y / i) ;
1421 expect ( stderr ) . toMatch ( / - - c o n s o l e - s i t e / ) ;
22+ expect ( stderr ) . toMatch ( / - - o p e n - a p i / ) ;
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 ( / 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 / ) ;
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 ( / 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 / ) ;
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 ( / U s e - - b a s e - u r l o n l y w i t h - - a p i - k e y / ) ;
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 ( / 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 / ) ;
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 ( / 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 / ) ;
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 ( / - - a p i - k e y | U s a g e : / i ) ;
96+ 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 / ) ;
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 ( / - - a p i - k e y | c o n s o l e / i ) ;
137+ 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 / ) ;
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 ( / U n k n o w n f l a g .* - - b a s e - u r l / ) ;
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 ( / U n k n o w n f l a g .* - - a c c e s s - k e y - i d / ) ;
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 ( / O p e n A P I c r e d e n t i a l s s a v e d / ) ;
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 ( / C l e a r e d a c c e s s _ k e y _ i d / ) ;
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} ) ;
0 commit comments