|
1 | | -import { describe, it, expect } from 'bun:test'; |
| 1 | +import { describe, expect, it, spyOn } from 'bun:test'; |
2 | 2 | import { default as setCommand } from '../../../src/commands/config/set'; |
| 3 | +import * as configLoader from '../../../src/config/loader'; |
3 | 4 |
|
4 | 5 | describe('config set command', () => { |
5 | 6 | it('has correct name', () => { |
@@ -129,4 +130,140 @@ describe('config set command', () => { |
129 | 130 | }), |
130 | 131 | ).resolves.toBeUndefined(); |
131 | 132 | }); |
| 133 | + |
| 134 | + it('masks api_key in dry-run output', async () => { |
| 135 | + const apiKey = 'sk-test-secret-123456'; |
| 136 | + const writeConfigFile = spyOn(configLoader, 'writeConfigFile').mockResolvedValue(); |
| 137 | + let output = ''; |
| 138 | + const originalLog = console.log; |
| 139 | + console.log = (message: string) => { output += message; }; |
| 140 | + |
| 141 | + try { |
| 142 | + await setCommand.execute({ |
| 143 | + region: 'global', |
| 144 | + baseUrl: 'https://api.mmx.io', |
| 145 | + output: 'json', |
| 146 | + timeout: 10, |
| 147 | + verbose: false, |
| 148 | + quiet: false, |
| 149 | + noColor: true, |
| 150 | + yes: false, |
| 151 | + dryRun: true, |
| 152 | + nonInteractive: true, |
| 153 | + async: false, |
| 154 | + }, { |
| 155 | + key: 'api_key', |
| 156 | + value: apiKey, |
| 157 | + quiet: false, |
| 158 | + verbose: false, |
| 159 | + noColor: true, |
| 160 | + yes: false, |
| 161 | + dryRun: true, |
| 162 | + help: false, |
| 163 | + nonInteractive: true, |
| 164 | + async: false, |
| 165 | + }); |
| 166 | + } finally { |
| 167 | + console.log = originalLog; |
| 168 | + writeConfigFile.mockRestore(); |
| 169 | + } |
| 170 | + |
| 171 | + expect(output).not.toContain(apiKey); |
| 172 | + expect(JSON.parse(output).would_set.api_key).toBe('sk-t...3456'); |
| 173 | + expect(writeConfigFile).not.toHaveBeenCalled(); |
| 174 | + }); |
| 175 | + |
| 176 | + it('removes OAuth and masks output when setting api_key', async () => { |
| 177 | + const apiKey = 'sk-test-secret-123456'; |
| 178 | + const readConfigFile = spyOn(configLoader, 'readConfigFile').mockReturnValue({ |
| 179 | + region: 'cn', |
| 180 | + oauth: { |
| 181 | + access_token: 'old-access-token', |
| 182 | + refresh_token: 'old-refresh-token', |
| 183 | + expires_at: '2099-01-01T00:00:00.000Z', |
| 184 | + }, |
| 185 | + }); |
| 186 | + let writtenConfig: Record<string, unknown> | undefined; |
| 187 | + const writeConfigFile = spyOn(configLoader, 'writeConfigFile').mockImplementation(async (data) => { |
| 188 | + writtenConfig = { ...data }; |
| 189 | + }); |
| 190 | + let output = ''; |
| 191 | + const originalLog = console.log; |
| 192 | + console.log = (message: string) => { output += message; }; |
| 193 | + |
| 194 | + try { |
| 195 | + await setCommand.execute({ |
| 196 | + region: 'cn', |
| 197 | + baseUrl: 'https://api.mmx.io', |
| 198 | + output: 'json', |
| 199 | + timeout: 10, |
| 200 | + verbose: false, |
| 201 | + quiet: false, |
| 202 | + noColor: true, |
| 203 | + yes: false, |
| 204 | + dryRun: false, |
| 205 | + nonInteractive: true, |
| 206 | + async: false, |
| 207 | + }, { |
| 208 | + key: 'api_key', |
| 209 | + value: ` ${apiKey} `, |
| 210 | + quiet: false, |
| 211 | + verbose: false, |
| 212 | + noColor: true, |
| 213 | + yes: false, |
| 214 | + dryRun: false, |
| 215 | + help: false, |
| 216 | + nonInteractive: true, |
| 217 | + async: false, |
| 218 | + }); |
| 219 | + } finally { |
| 220 | + console.log = originalLog; |
| 221 | + readConfigFile.mockRestore(); |
| 222 | + writeConfigFile.mockRestore(); |
| 223 | + } |
| 224 | + |
| 225 | + expect(writtenConfig).toEqual({ api_key: apiKey }); |
| 226 | + expect(output).not.toContain(apiKey); |
| 227 | + expect(JSON.parse(output).api_key).toBe('sk-t...3456'); |
| 228 | + }); |
| 229 | + |
| 230 | + it('rejects empty api_key values without changing existing credentials', async () => { |
| 231 | + const readConfigFile = spyOn(configLoader, 'readConfigFile'); |
| 232 | + const writeConfigFile = spyOn(configLoader, 'writeConfigFile').mockResolvedValue(); |
| 233 | + |
| 234 | + try { |
| 235 | + for (const value of ['', ' ']) { |
| 236 | + await expect(setCommand.execute({ |
| 237 | + region: 'cn', |
| 238 | + baseUrl: 'https://api.mmx.io', |
| 239 | + output: 'json', |
| 240 | + timeout: 10, |
| 241 | + verbose: false, |
| 242 | + quiet: false, |
| 243 | + noColor: true, |
| 244 | + yes: false, |
| 245 | + dryRun: false, |
| 246 | + nonInteractive: true, |
| 247 | + async: false, |
| 248 | + }, { |
| 249 | + key: 'api_key', |
| 250 | + value, |
| 251 | + quiet: false, |
| 252 | + verbose: false, |
| 253 | + noColor: true, |
| 254 | + yes: false, |
| 255 | + dryRun: false, |
| 256 | + help: false, |
| 257 | + nonInteractive: true, |
| 258 | + async: false, |
| 259 | + })).rejects.toThrow('must not be empty'); |
| 260 | + } |
| 261 | + } finally { |
| 262 | + readConfigFile.mockRestore(); |
| 263 | + writeConfigFile.mockRestore(); |
| 264 | + } |
| 265 | + |
| 266 | + expect(readConfigFile).not.toHaveBeenCalled(); |
| 267 | + expect(writeConfigFile).not.toHaveBeenCalled(); |
| 268 | + }); |
132 | 269 | }); |
0 commit comments