-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy path_gitlab_api.test.ts
141 lines (119 loc) · 4.39 KB
/
_gitlab_api.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import nock, { NockDefinition } from "nock"
import { default as GitLabAPI, getGitLabAPICredentialsFromEnv } from "../GitLabAPI"
import { resolve } from "path"
import { readFileSync } from "fs"
const nockBack = nock.back
nockBack.fixtures = __dirname + "/fixtures"
// We're testing https://gitlab.com/456/merge_requests/27117
// This has been chosen because it is already merged and publicly available, it's unlikely to change
/** Returns a fixture. */
const loadFixture = (path: string): any =>
JSON.parse(readFileSync(resolve(nockBack.fixtures, `${path}.json`), {}).toString())[0]
describe("GitLab API", () => {
let api: GitLabAPI
beforeAll(() => {
nock.recorder.rec()
nockBack.setMode("record")
})
afterAll(() => {
nock.restore()
})
beforeEach(() => {
api = new GitLabAPI(
{ pullRequestID: "27117", repoSlug: "456" },
getGitLabAPICredentialsFromEnv({
DANGER_GITLAB_HOST: "gitlab.com",
DANGER_GITLAB_API_TOKEN: "FAKE_DANGER_GITLAB_API_TOKEN",
})
)
})
it("configures host from CI_API_V4_URL", () => {
api = new GitLabAPI(
{ pullRequestID: "27117", repoSlug: "456" },
getGitLabAPICredentialsFromEnv({
CI_API_V4_URL: "https://testciapiv4url.com/api/v4",
DANGER_GITLAB_API_TOKEN: "FAKE_DANGER_GITLAB_API_TOKEN",
})
)
expect(api.projectURL).toBe("https://testciapiv4url.com/456")
})
it("projectURL is defined", () => {
expect(api.projectURL).toBe("https://gitlab.com/456")
})
it("mergeRequestURL is defined", () => {
expect(api.mergeRequestURL).toBe("https://gitlab.com/456/merge_requests/27117")
})
const sanitizeUserResponse = (nocks: NockDefinition[]): NockDefinition[] => {
return nocks.map((nock: NockDefinition) => {
let { response, ...restNock } = nock
// @ts-ignore
const { identities } = response
response = {
// @ts-ignore
...response,
username: "username",
name: "First Last",
organization: "My Organization",
email: "[email protected]",
avatar_url: "https://www.",
web_url: "https://www.",
identities: identities.map(({ extern_uid, ...rest }: any) => ({ ...rest, extern_uid: "xxxx" })),
}
return { ...restNock, response }
})
}
it("getUser returns the current user profile id", async () => {
// To re-record this you need to provide a valid DANGER_GITLAB_API_TOKEN
const { nockDone } = await nockBack("getUser.json", { afterRecord: sanitizeUserResponse })
const result = await api.getUser()
nockDone()
const { response } = loadFixture("getUser")
expect(result).toEqual(response)
})
it("getMergeRequestInfo", async () => {
const { nockDone } = await nockBack("getMergeRequestInfo.json")
const result = await api.getMergeRequestInfo()
nockDone()
const { response } = loadFixture("getMergeRequestInfo")
expect(result).toEqual(response)
})
it("getMergeRequestChanges", async () => {
const { nockDone } = await nockBack("getMergeRequestChanges.json")
const result = await api.getMergeRequestChanges()
nockDone()
expect(result).toEqual(
expect.arrayContaining([
expect.objectContaining({
old_path: expect.any(String),
new_path: expect.any(String),
a_mode: expect.any(String),
b_mode: expect.any(String),
diff: expect.any(String),
new_file: expect.any(Boolean),
deleted_file: expect.any(Boolean),
}),
])
)
})
it("getMergeRequestCommits", async () => {
const { nockDone } = await nockBack("getMergeRequestCommits.json")
const result = await api.getMergeRequestCommits()
nockDone()
const { response } = loadFixture("getMergeRequestCommits")
expect(result).toEqual(response)
})
it("getMergeRequestNotes", async () => {
const { nockDone } = await nockBack("getMergeRequestNotes.json")
const result = await api.getMergeRequestNotes()
nockDone()
const { response } = loadFixture("getMergeRequestNotes")
expect(result).toEqual(response)
})
it("getMergeRequestInlineNotes", async () => {
const { nockDone } = await nockBack("getMergeRequestInlineNotes.json")
const result = await api.getMergeRequestInlineNotes()
nockDone()
// TODO: There are no inline notes on this MR, we should look for a public one that has inline notes to improve this test
expect(result).toEqual([])
})
})