|
| 1 | +import { TaskcatArtifactManager } from "../src/post-entrypoint"; |
| 2 | +import { readFileSync, writeFileSync } from "fs"; |
| 3 | +import { sync } from "glob"; |
| 4 | + |
| 5 | +jest.mock("fs"); |
| 6 | +jest.mock("glob"); |
| 7 | + |
| 8 | +describe("the maskAccountId() function", () => { |
| 9 | + const taskcatArtifactManager: TaskcatArtifactManager = new TaskcatArtifactManager(); |
| 10 | + |
| 11 | + const mockedReadFileSync = (readFileSync as unknown) as jest.MockedFunction< |
| 12 | + typeof readFileSync |
| 13 | + >; |
| 14 | + const mockedWriteFileSync = (writeFileSync as unknown) as jest.MockedFunction< |
| 15 | + typeof writeFileSync |
| 16 | + >; |
| 17 | + const mockedGlobSync = (sync as unknown) as jest.MockedFunction<typeof sync>; |
| 18 | + |
| 19 | + it("should not modify the `tasckat_output/` files if the AWS account ID has not been printed in the logs", () => { |
| 20 | + expect.assertions(1); |
| 21 | + jest.clearAllMocks(); |
| 22 | + |
| 23 | + const filePath = "taskcat_outputs/"; |
| 24 | + const fileContents = "abcd1234"; |
| 25 | + const awsAccountId = "1234567890"; |
| 26 | + |
| 27 | + mockedGlobSync.mockReturnValue(["taskcat_outputs/test.txt"]); |
| 28 | + mockedReadFileSync.mockReturnValue(fileContents); |
| 29 | + |
| 30 | + taskcatArtifactManager.maskAccountId(awsAccountId, filePath); |
| 31 | + |
| 32 | + expect(mockedWriteFileSync).not.toHaveBeenCalled(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should throw an exception if the AWS account ID has been passed as an empty string", () => { |
| 36 | + expect.assertions(2); |
| 37 | + jest.clearAllMocks(); |
| 38 | + |
| 39 | + const filePath = "taskcat_outputs/test.txt"; |
| 40 | + const awsAccountId = ""; |
| 41 | + |
| 42 | + expect(() => { |
| 43 | + taskcatArtifactManager.maskAccountId(awsAccountId, filePath); |
| 44 | + }).toThrow(Error); |
| 45 | + |
| 46 | + expect(mockedWriteFileSync).not.toHaveBeenCalled(); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should mask the AWS account ID from the `taskcat_outputs/` files if any references are found in the logs", () => { |
| 50 | + expect.assertions(1); |
| 51 | + jest.clearAllMocks(); |
| 52 | + |
| 53 | + const filePath = "taskcat_outputs/test.txt"; |
| 54 | + const fileContents = "abcd1234 1234567890"; |
| 55 | + const awsAccountId = "1234567890"; |
| 56 | + |
| 57 | + mockedGlobSync.mockReturnValue(["taskcat_outputs/test.txt"]); |
| 58 | + mockedReadFileSync.mockImplementation(path => { |
| 59 | + switch (path) { |
| 60 | + case filePath: |
| 61 | + return fileContents; |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + taskcatArtifactManager.maskAccountId(awsAccountId, "taskcat_outputs/"); |
| 66 | + |
| 67 | + expect(mockedWriteFileSync).toHaveBeenCalledWith( |
| 68 | + filePath, |
| 69 | + "abcd1234 ***", |
| 70 | + "utf-8" |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
0 commit comments