1
- import { beforeAll , beforeEach , describe , expect , vi , it , type Mock } from 'vitest' ;
1
+ import { afterEach , beforeAll , beforeEach , describe , expect , vi , it , type Mock } from 'vitest' ;
2
2
import { createCommanderCommand } from './commander.js' ;
3
3
import { copyFile , mkdir , readFile , rm } from 'node:fs/promises' ;
4
4
import type { Command , CommanderError , OutputConfiguration } from '@commander-js/extra-typings' ;
@@ -106,6 +106,10 @@ describe('--version', () => {
106
106
} ) ;
107
107
108
108
describe ( 'whoami' , ( ) => {
109
+ afterEach ( ( ) => {
110
+ vi . unstubAllEnvs ( ) ;
111
+ } ) ;
112
+
109
113
it ( 'should error if --config file does not exist' , async ( ) => {
110
114
const { program } = mockedProgram ( ) ;
111
115
@@ -136,11 +140,13 @@ describe('whoami', () => {
136
140
) . rejects . toThrowError ( 'Invalid access token.' ) ;
137
141
} ) ;
138
142
139
- it ( '--auth-token should override config file' , async ( ) => {
143
+ it ( '--auth-token should override config file and environment variables ' , async ( ) => {
140
144
const { program } = mockedProgram ( ) ;
141
145
142
146
const myCliAuthToken = 'my-cli-auth-token' ;
143
147
148
+ vi . stubEnv ( 'MERMAID_CHART_AUTH_TOKEN' , 'my-env-auth-token' ) ;
149
+
144
150
await program . parseAsync (
145
151
[ '--config' , CONFIG_AUTHED , '--auth-token' , myCliAuthToken , 'whoami' ] ,
146
152
{ from : 'user' } ,
@@ -157,6 +163,21 @@ describe('whoami', () => {
157
163
158
164
expect ( consoleLogSpy ) . toBeCalledWith ( mockedMCUser . emailAddress ) ;
159
165
} ) ;
166
+
167
+ it ( 'should support loading auth from environment variables' , async ( ) => {
168
+ const { program } = mockedProgram ( ) ;
169
+
170
+ const authToken = 'my-api-key-from-env-var' ;
171
+ vi . stubEnv ( 'MERMAID_CHART_AUTH_TOKEN' , authToken ) ;
172
+ vi . stubEnv ( 'MERMAID_CHART_BASE_URL' , 'https://test.mermaidchart.invalid' ) ;
173
+
174
+ const consoleLogSpy = vi . spyOn ( global . console , 'log' ) ;
175
+ await program . parseAsync ( [ '--config' , CONFIG_AUTHED , 'whoami' ] , { from : 'user' } ) ;
176
+
177
+ // environment variables should override config file
178
+ expect ( vi . mocked ( MermaidChart . prototype . setAccessToken ) ) . toHaveBeenCalledWith ( authToken ) ;
179
+ expect ( consoleLogSpy ) . toBeCalledWith ( mockedMCUser . emailAddress ) ;
180
+ } ) ;
160
181
} ) ;
161
182
162
183
describe ( 'login' , ( ) => {
0 commit comments