-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathgithub-create-branch-post.test.ts
115 lines (98 loc) · 3.07 KB
/
github-create-branch-post.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
103
104
105
106
107
108
109
110
111
112
113
114
115
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Installation } from "models/installation";
import { Subscription } from "models/subscription";
import { GithubCreateBranchPost } from "./github-create-branch-post";
import { getLogger } from "config/logger";
import { mocked } from "jest-mock";
jest.mock("models/subscription");
describe("github-create-branch", () => {
const gitHubInstallationId = 15;
let req, res;
beforeEach(async () => {
await Subscription.create({
gitHubInstallationId,
jiraHost
});
await Installation.create({
jiraHost,
clientKey: "client-key",
encryptedSharedSecret: "shared-secret"
});
req = {
log: getLogger("request"),
params: {},
body: {
owner: "ARC",
repo: "cat-photos",
sourceBranchName: "main",
newBranchName: "chesire"
}
};
res = {
sendStatus: jest.fn(),
status: jest.fn(),
json: jest.fn(),
locals: {
jiraHost,
githubToken: "abc-token",
gitHubAppConfig: {
hostname: "omega"
}
}
};
mocked(Subscription.findForRepoOwner).mockResolvedValue({ gitHubInstallationId, id: 1 } as Subscription);
});
it("Should successfully run through the create branch flow", async () => {
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
// Get reference
githubNock
.get("/repos/ARC/cat-photos/git/refs/heads/main")
.reply(200, { object: { sha: "casd769adf" } });
// Create Branch
githubNock
.post("/repos/ARC/cat-photos/git/refs",{
owner: "ARC",
repo: "cat-photos",
ref: "refs/heads/chesire",
sha: "casd769adf"
})
.reply(200);
await GithubCreateBranchPost(req , res);
expect(res.sendStatus).toHaveBeenCalledWith(200);
});
it.each(["owner", "repo", "sourceBranchName", "newBranchName"])("Should 400 when missing required fields", async (attribute) => {
res.status.mockReturnValue(res);
delete req.body[attribute];
await GithubCreateBranchPost(req , res);
expect(res.status).toHaveBeenCalledWith(400);
});
it("Should return 403 errors with URL to GitHub app settings", async () => {
// To allow res.send().json()
res.status.mockReturnValue(res);
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
githubNock
.post(`/app/installations/${gitHubInstallationId}/access_tokens`)
.reply(200);
const sha = "kenshin";
githubNock.get("/repos/ARC/cat-photos/git/refs/heads/main")
.reply(200, { object: { sha } });
githubNock.post("/repos/ARC/cat-photos/git/refs", {
"owner": "ARC",
"repo": "cat-photos",
"ref": "refs/heads/chesire",
"sha": sha
}).reply(403);
await GithubCreateBranchPost(req , res);
expect(res.status).toHaveBeenCalledWith(403);
expect(res.json).toBeCalledWith({
error: "We couldn’t create this branch, because GitHub for Jira app does not have permission to write to the GitHub repository. If you want to enable this feature, please contact your GitHub admin to grant permission."
});
});
});