-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathadmin.test.js
More file actions
248 lines (230 loc) · 7.26 KB
/
admin.test.js
File metadata and controls
248 lines (230 loc) · 7.26 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import {isAdmin} from '../../src/functions/admin.js'
import {vi, expect, test, beforeEach} from 'vitest'
import {COLORS} from '../../src/functions/colors.js'
import * as github from '@actions/github'
import * as core from '@actions/core'
const debugMock = vi.spyOn(core, 'debug')
const warningMock = vi.spyOn(core, 'warning')
class NotFoundError extends Error {
constructor(message) {
super(message)
this.status = 404
}
}
class WildError extends Error {
constructor(message) {
super(message)
this.status = 500
}
}
var context
var octokit
beforeEach(() => {
vi.clearAllMocks()
process.env.INPUT_ADMINS_PAT = 'faketoken'
process.env.INPUT_ADMINS =
'MoNaLiSa,@lisamona,octoawesome/octo-awEsome-team,bad$user'
context = {
actor: 'monalisa'
}
octokit = {
request: vi.fn().mockReturnValueOnce({
status: 204
}),
rest: {
orgs: {
get: vi.fn().mockReturnValueOnce({
data: {id: '12345'}
})
},
teams: {
getByName: vi.fn().mockReturnValueOnce({
data: {id: '567890'}
})
}
}
}
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
return octokit
})
})
test('runs isAdmin checks and finds a valid admin via handle reference', async () => {
expect(await isAdmin(context)).toStrictEqual(true)
expect(debugMock).toHaveBeenCalledWith(
'monalisa is an admin via handle reference'
)
})
test('runs isAdmin checks and finds a valid handle that is a GitHub EMU', async () => {
process.env.INPUT_ADMINS = 'username_company'
const contextNoAdmin = {
actor: 'username_company'
}
expect(await isAdmin(contextNoAdmin)).toStrictEqual(true)
expect(debugMock).toHaveBeenCalledWith(
'username_company is an admin via handle reference'
)
})
test('runs isAdmin checks and does not find a valid admin due to a bad GitHub handle', async () => {
process.env.INPUT_ADMINS = 'mona%lisa-'
const contextNoAdmin = {
actor: 'mona%lisa-'
}
expect(await isAdmin(contextNoAdmin)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith(
'mona%lisa- is not a valid GitHub username... skipping admin check'
)
})
test('runs isAdmin checks and does not find a valid admin', async () => {
process.env.INPUT_ADMINS = 'monalisa'
const contextNoAdmin = {
actor: 'eviluser'
}
expect(await isAdmin(contextNoAdmin)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith('eviluser is not an admin')
})
test('runs isAdmin checks for an org team and fails due to no admins_pat', async () => {
process.env.INPUT_ADMINS_PAT = 'false'
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome'
expect(await isAdmin(context)).toStrictEqual(false)
expect(warningMock).toHaveBeenCalledWith(
`🚨 no ${COLORS.highlight}admins_pat${COLORS.reset} provided, skipping admin check for org team membership`
)
})
test('runs isAdmin checks for an org team and finds a valid user', async () => {
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome-team'
expect(await isAdmin(context)).toStrictEqual(true)
expect(debugMock).toHaveBeenCalledWith(
'monalisa is in octoawesome/octo-awesome-team'
)
expect(debugMock).toHaveBeenCalledWith(
'monalisa is an admin via org team reference'
)
})
// This only handles the global failure case of any 404 in the admin.js file
test('runs isAdmin checks for an org team and does not find the org', async () => {
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
rest: {
orgs: {
get: vi
.fn()
.mockRejectedValueOnce(
new NotFoundError('Reference does not exist')
)
}
}
}
})
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome-team'
expect(await isAdmin(context)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith(
'monalisa is not a member of the octoawesome/octo-awesome-team team'
)
})
// This only handles the global failure case of any 404 in the admin.js file
test('runs isAdmin checks for an org team and does not find the team', async () => {
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
rest: {
orgs: {
get: vi.fn().mockReturnValueOnce({
data: {id: '12345'}
})
},
teams: {
getByName: vi
.fn()
.mockRejectedValueOnce(
new NotFoundError('Reference does not exist')
)
}
}
}
})
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome-team'
expect(await isAdmin(context)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith(
'monalisa is not a member of the octoawesome/octo-awesome-team team'
)
})
// This test correctly tests if a user is a member of a team or not. If they are in a team a 204 is returned. If they are not a 404 is returned like in this test example
test('runs isAdmin checks for an org team and does not find the user in the team', async () => {
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
request: vi
.fn()
.mockRejectedValueOnce(new NotFoundError('Reference does not exist')),
rest: {
orgs: {
get: vi.fn().mockReturnValueOnce({
data: {id: '12345'}
})
},
teams: {
getByName: vi.fn().mockReturnValueOnce({
data: {id: '567890'}
})
}
}
}
})
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome-team'
expect(await isAdmin(context)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith(
'monalisa is not a member of the octoawesome/octo-awesome-team team'
)
})
test('runs isAdmin checks for an org team and an unexpected status code is received from the request method with octokit', async () => {
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
request: vi.fn().mockReturnValueOnce({
status: 500
}),
rest: {
orgs: {
get: vi.fn().mockReturnValueOnce({
data: {id: '12345'}
})
},
teams: {
getByName: vi.fn().mockReturnValueOnce({
data: {id: '567890'}
})
}
}
}
})
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome-team'
expect(await isAdmin(context)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith('monalisa is not an admin')
expect(warningMock).toHaveBeenCalledWith(
'non 204 response from org team check: 500'
)
})
test('runs isAdmin checks for an org team and an unexpected error is thrown from any API call', async () => {
vi.spyOn(github, 'getOctokit').mockImplementation(() => {
return {
request: vi
.fn()
.mockRejectedValueOnce(new WildError('something went boom')),
rest: {
orgs: {
get: vi.fn().mockReturnValueOnce({
data: {id: '12345'}
})
},
teams: {
getByName: vi.fn().mockReturnValueOnce({
data: {id: '567890'}
})
}
}
}
})
process.env.INPUT_ADMINS = 'octoawesome/octo-awesome-team'
expect(await isAdmin(context)).toStrictEqual(false)
expect(debugMock).toHaveBeenCalledWith('monalisa is not an admin')
expect(warningMock).toHaveBeenCalledWith(
'error checking org team membership: Error: something went boom'
)
})