-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaddSecurity.test.js
53 lines (47 loc) · 1.34 KB
/
addSecurity.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { beforeEach, jest } from "@jest/globals";
import debugLib from "debug";
const debug = debugLib("pin-github-action-test");
jest.unstable_mockModule("fs/promises", () => ({
stat: jest.fn(),
writeFile: jest.fn(),
}));
beforeEach(() => {
jest.clearAllMocks();
});
const fs = await import("fs/promises");
const { default: run } = await import("./addSecurity");
const addSecurity = async (filename, allowed) => {
return run.apply(null, [filename, allowed, debug]);
};
test("writes config if file does not exist", async () => {
fs.stat.mockImplementation(() => {
return Promise.reject({ code: "ENOENT" });
});
fs.writeFile.mockImplementation(() => {
return Promise.resolve();
});
const actual = await addSecurity(".github/workflows/security.yaml", []);
expect(actual).toBe(true);
});
test("writes allowlisted config correctly", async () => {
fs.stat.mockImplementation(() => {
return Promise.reject({ code: "ENOENT" });
});
fs.writeFile.mockImplementation(() => {
return Promise.resolve();
});
await addSecurity(".github/workflows/security.yaml", [
"Demo1/*",
"Demo2/*",
"Demo3/*",
]);
expect(fs.writeFile).toHaveBeenCalledWith(
".github/workflows/security.yaml",
expect.stringContaining(`
with:
allowlist: |
Demo1/
Demo2/
Demo3/`),
);
});