-
Notifications
You must be signed in to change notification settings - Fork 78
/
approve.test.ts
102 lines (79 loc) 路 2.77 KB
/
approve.test.ts
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import * as core from "@actions/core";
import { Context } from "@actions/github/lib/context";
import nock from "nock";
import { approve } from "./approve";
beforeEach(() => {
jest.restoreAllMocks();
jest.spyOn(core, "setFailed").mockImplementation(jest.fn());
jest.spyOn(core, "info").mockImplementation(jest.fn());
process.env["GITHUB_REPOSITORY"] = "hmarr/test";
});
test("when a review is successfully created", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(200, { id: 1 });
await approve("gh-tok", ghContext());
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining("Approved pull request #101")
);
});
test("when a review is successfully created using pull-request-number", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(200, { id: 1 });
await approve("gh-tok", new Context(), 101);
expect(core.info).toHaveBeenCalledWith(
expect.stringContaining("Approved pull request #101")
);
});
test("without a pull request", async () => {
await approve("gh-tok", new Context());
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("Make sure you're triggering this")
);
});
test("when the token is invalid", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(401, { message: "Bad credentials" });
await approve("gh-tok", ghContext());
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("`github-token` input parameter")
);
});
test("when the token doesn't have write permissions", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(403, { message: "Resource not accessible by integration" });
await approve("gh-tok", ghContext());
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("pull_request_target")
);
});
test("when a user tries to approve their own pull request", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(422, { message: "Unprocessable Entity" });
await approve("gh-tok", ghContext());
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("same user account")
);
});
test("when the token doesn't have access to the repository", async () => {
nock("https://api.github.com")
.post("/repos/hmarr/test/pulls/101/reviews")
.reply(404, { message: "Not Found" });
await approve("gh-tok", ghContext());
expect(core.setFailed).toHaveBeenCalledWith(
expect.stringContaining("doesn't have access")
);
});
function ghContext(): Context {
const ctx = new Context();
ctx.payload = {
pull_request: {
number: 101,
},
};
return ctx;
}