|
1 | 1 | import path from 'node:path'; |
2 | | -import { after, before, beforeEach, describe, it } from 'node:test'; |
| 2 | +import { after, before, beforeEach, describe, it, mock } from 'node:test'; |
3 | 3 | import fs from 'fs/promises'; |
4 | 4 | import { ProfileController } from './profile_controller.js'; |
5 | 5 | import assert from 'node:assert'; |
6 | 6 | import { loadSharedConfigFiles } from '@smithy/shared-ini-file-loader'; |
7 | 7 | import { EOL } from 'node:os'; |
8 | | -import { chmodSync } from 'node:fs'; |
9 | 8 | import { AmplifyUserError } from '@aws-amplify/platform-core'; |
10 | 9 |
|
11 | 10 | const testAccessKeyId = 'testAccessKeyId'; |
@@ -220,64 +219,6 @@ void describe('profile controller', () => { |
220 | 219 | ); |
221 | 220 | }); |
222 | 221 |
|
223 | | - void it('throws error if config file already exists and is missing read permissions', async () => { |
224 | | - if (process.platform.startsWith('win')) { |
225 | | - // Windows does not have the same behavior when files are missing permissions |
226 | | - return; |
227 | | - } |
228 | | - |
229 | | - const expectedErr = new AmplifyUserError('PermissionsError', { |
230 | | - message: `You do not have the permissions to read this file: ${configFilePath}.`, |
231 | | - resolution: `Ensure that you have the right permissions to read from ${configFilePath}.`, |
232 | | - }); |
233 | | - chmodSync(configFilePath, 0o000); |
234 | | - await assert.rejects( |
235 | | - async () => |
236 | | - await profileController.createOrAppendAWSFiles({ |
237 | | - profile: testProfile2, |
238 | | - region: testRegion2, |
239 | | - accessKeyId: testAccessKeyId2, |
240 | | - secretAccessKey: testSecretAccessKey2, |
241 | | - }), |
242 | | - (error: AmplifyUserError) => { |
243 | | - assert.strictEqual(error.name, expectedErr.name); |
244 | | - assert.strictEqual(error.message, expectedErr.message); |
245 | | - assert.strictEqual(error.resolution, expectedErr.resolution); |
246 | | - return true; |
247 | | - } |
248 | | - ); |
249 | | - }); |
250 | | - |
251 | | - void it('throws error if config file already exists and is missing write permissions', async () => { |
252 | | - if (process.platform.startsWith('win')) { |
253 | | - // Windows does not have the same behavior when files are missing permissions |
254 | | - return; |
255 | | - } |
256 | | - |
257 | | - const expectedErr = new AmplifyUserError('PermissionsError', { |
258 | | - message: `You do not have the permissions to write to this file: ${configFilePath}`, |
259 | | - resolution: `Ensure that you have the right permissions to write to ${configFilePath}.`, |
260 | | - }); |
261 | | - |
262 | | - chmodSync(configFilePath, 0o444); |
263 | | - |
264 | | - await assert.rejects( |
265 | | - async () => |
266 | | - await profileController.createOrAppendAWSFiles({ |
267 | | - profile: testProfile2, |
268 | | - region: testRegion2, |
269 | | - accessKeyId: testAccessKeyId2, |
270 | | - secretAccessKey: testSecretAccessKey2, |
271 | | - }), |
272 | | - (error: AmplifyUserError) => { |
273 | | - assert.strictEqual(error.name, expectedErr.name); |
274 | | - assert.strictEqual(error.message, expectedErr.message); |
275 | | - assert.strictEqual(error.resolution, expectedErr.resolution); |
276 | | - return true; |
277 | | - } |
278 | | - ); |
279 | | - }); |
280 | | - |
281 | 222 | void it('creates directory if does not exist', async () => { |
282 | 223 | // delete directory |
283 | 224 | await fs.rm(testDir, { recursive: true, force: true }); |
@@ -354,6 +295,98 @@ void describe('profile controller', () => { |
354 | 295 | assert.equal(await profileController.profileExists(testProfile), true); |
355 | 296 | }); |
356 | 297 | }); |
| 298 | + |
| 299 | + void describe('Missing permissions on config file', () => { |
| 300 | + let testDir: string; |
| 301 | + let configFilePath: string; |
| 302 | + let credFilePath: string; |
| 303 | + |
| 304 | + const fsMock = { |
| 305 | + readFile: mock.fn< |
| 306 | + (path: string, encoding: BufferEncoding) => Promise<void | string> |
| 307 | + >(() => Promise.resolve()), |
| 308 | + appendFile: mock.fn<() => Promise<void>>(() => Promise.resolve()), |
| 309 | + }; |
| 310 | + |
| 311 | + const profileController = new ProfileController( |
| 312 | + fsMock as unknown as typeof fs |
| 313 | + ); |
| 314 | + |
| 315 | + before(async () => { |
| 316 | + testDir = await fs.mkdtemp('amplify_cmd_test'); |
| 317 | + configFilePath = path.join(process.cwd(), testDir, 'config'); |
| 318 | + credFilePath = path.join(process.cwd(), testDir, 'credentials'); |
| 319 | + |
| 320 | + process.env.AWS_CONFIG_FILE = configFilePath; |
| 321 | + process.env.AWS_SHARED_CREDENTIALS_FILE = credFilePath; |
| 322 | + |
| 323 | + fsMock.readFile.mock.resetCalls(); |
| 324 | + fsMock.appendFile.mock.resetCalls(); |
| 325 | + }); |
| 326 | + |
| 327 | + after(async () => { |
| 328 | + await fs.rm(testDir, { recursive: true, force: true }); |
| 329 | + delete process.env.AWS_CONFIG_FILE; |
| 330 | + delete process.env.AWS_SHARED_CREDENTIALS_FILE; |
| 331 | + }); |
| 332 | + |
| 333 | + void it('throws error if config file already exists and is missing read permissions', async () => { |
| 334 | + const expectedErr = new AmplifyUserError('PermissionsError', { |
| 335 | + message: `You do not have the permissions to read this file: ${configFilePath}.`, |
| 336 | + resolution: `Ensure that you have the right permissions to read from ${configFilePath}.`, |
| 337 | + }); |
| 338 | + |
| 339 | + fsMock.readFile.mock.mockImplementationOnce(() => |
| 340 | + Promise.reject(new Error('EACCES: Permission denied')) |
| 341 | + ); |
| 342 | + |
| 343 | + await assert.rejects( |
| 344 | + async () => |
| 345 | + profileController.createOrAppendAWSFiles({ |
| 346 | + profile: testProfile2, |
| 347 | + region: testRegion2, |
| 348 | + accessKeyId: testAccessKeyId2, |
| 349 | + secretAccessKey: testSecretAccessKey2, |
| 350 | + }), |
| 351 | + (error: AmplifyUserError) => { |
| 352 | + assert.strictEqual(error.name, expectedErr.name); |
| 353 | + assert.strictEqual(error.message, expectedErr.message); |
| 354 | + assert.strictEqual(error.resolution, expectedErr.resolution); |
| 355 | + return true; |
| 356 | + } |
| 357 | + ); |
| 358 | + }); |
| 359 | + |
| 360 | + void it('throws error if config file already exists and is missing write permissions', async () => { |
| 361 | + const expectedErr = new AmplifyUserError('PermissionsError', { |
| 362 | + message: `You do not have the permissions to write to this file: ${configFilePath}`, |
| 363 | + resolution: `Ensure that you have the right permissions to write to ${configFilePath}.`, |
| 364 | + }); |
| 365 | + |
| 366 | + fsMock.readFile.mock.mockImplementationOnce( |
| 367 | + (path: string, encoding: BufferEncoding) => fs.readFile(path, encoding) |
| 368 | + ); |
| 369 | + fsMock.appendFile.mock.mockImplementationOnce(() => |
| 370 | + Promise.reject(new Error('EACCES: Permission denied')) |
| 371 | + ); |
| 372 | + |
| 373 | + await assert.rejects( |
| 374 | + async () => |
| 375 | + profileController.createOrAppendAWSFiles({ |
| 376 | + profile: testProfile2, |
| 377 | + region: testRegion2, |
| 378 | + accessKeyId: testAccessKeyId2, |
| 379 | + secretAccessKey: testSecretAccessKey2, |
| 380 | + }), |
| 381 | + (error: AmplifyUserError) => { |
| 382 | + assert.strictEqual(error.name, expectedErr.name); |
| 383 | + assert.strictEqual(error.message, expectedErr.message); |
| 384 | + assert.strictEqual(error.resolution, expectedErr.resolution); |
| 385 | + return true; |
| 386 | + } |
| 387 | + ); |
| 388 | + }); |
| 389 | + }); |
357 | 390 | }); |
358 | 391 |
|
359 | 392 | const assertFilePermissionsAreOwnerReadWriteOnly = async (filePath: string) => { |
|
0 commit comments