forked from carimura/github-pages-deploy-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.test.ts
330 lines (296 loc) · 9.59 KB
/
util.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {ActionInterface, TestFlag} from '../src/constants'
import {
isNullOrUndefined,
generateTokenType,
generateRepositoryPath,
generateFolderPath,
suppressSensitiveInformation,
checkParameters,
stripProtocolFromUrl
} from '../src/util'
describe('util', () => {
describe('isNullOrUndefined', () => {
it('should return true if the value is null', async () => {
const value = null
expect(isNullOrUndefined(value)).toBeTruthy()
})
it('should return true if the value is undefined', async () => {
const value = undefined
expect(isNullOrUndefined(value)).toBeTruthy()
})
it('should return false if the value is defined', async () => {
const value = 'montezuma'
expect(isNullOrUndefined(value)).toBeFalsy()
})
it('should return false if the value is empty string', async () => {
const value = ''
expect(isNullOrUndefined(value)).toBeTruthy()
})
})
describe('generateTokenType', () => {
it('should return ssh if ssh is provided', async () => {
const action = {
branch: '123',
workspace: 'src/',
folder: 'build',
token: null,
sshKey: 'real_token',
silent: false,
isTest: TestFlag.NONE
}
expect(generateTokenType(action)).toEqual('SSH Deploy Key')
})
it('should return deploy token if token is provided', async () => {
const action = {
branch: '123',
workspace: 'src/',
folder: 'build',
token: '123',
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
expect(generateTokenType(action)).toEqual('Deploy Token')
})
it('should return ... if no token is provided', async () => {
const action = {
branch: '123',
workspace: 'src/',
folder: 'build',
token: null,
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
expect(generateTokenType(action)).toEqual('…')
})
})
describe('generateRepositoryPath', () => {
it('should return ssh if ssh is provided', async () => {
const action = {
repositoryName: 'JamesIves/github-pages-deploy-action',
branch: '123',
workspace: 'src/',
folder: 'build',
hostname: 'github.com',
token: null,
sshKey: 'real_token',
silent: false,
isTest: TestFlag.NONE
}
expect(generateRepositoryPath(action)).toEqual(
'git@github.com:JamesIves/github-pages-deploy-action'
)
})
it('should return https with x-access-token if deploy token is provided', async () => {
const action = {
repositoryName: 'JamesIves/github-pages-deploy-action',
branch: '123',
workspace: 'src/',
folder: 'build',
hostname: 'enterprise.github.com',
token: '123',
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
expect(generateRepositoryPath(action)).toEqual(
'https://x-access-token:123@enterprise.github.com/JamesIves/github-pages-deploy-action.git'
)
})
describe('suppressSensitiveInformation', () => {
it('should replace any sensitive information with ***', async () => {
const action = {
repositoryName: 'JamesIves/github-pages-deploy-action',
repositoryPath:
'https://x-access-token:supersecret999%%%@github.com/anothersecret123333',
branch: '123',
workspace: 'src/',
folder: 'build',
token: 'anothersecret123333',
silent: false,
isTest: TestFlag.NONE
}
const string = `This is an error message! It contains ${action.token} and ${action.repositoryPath} and ${action.token} again!`
expect(suppressSensitiveInformation(string, action)).toBe(
'This is an error message! It contains *** and *** and *** again!'
)
})
it('should not suppress information when in debug mode', async () => {
const action = {
repositoryName: 'JamesIves/github-pages-deploy-action',
repositoryPath:
'https://x-access-token:supersecret999%%%@github.com/anothersecret123333',
branch: '123',
workspace: 'src/',
folder: 'build',
token: 'anothersecret123333',
silent: false,
isTest: TestFlag.NONE
}
process.env['RUNNER_DEBUG'] = '1'
const string = `This is an error message! It contains ${action.token} and ${action.repositoryPath}`
expect(suppressSensitiveInformation(string, action)).toBe(
'This is an error message! It contains anothersecret123333 and https://x-access-token:supersecret999%%%@github.com/anothersecret123333'
)
})
})
})
describe('generateFolderPath', () => {
it('should return absolute path if folder name is provided', () => {
const action = {
branch: '123',
workspace: 'src/',
folder: 'build',
token: null,
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
expect(generateFolderPath(action)).toEqual('src/build')
})
it('should return original path if folder name begins with /', () => {
const action = {
branch: '123',
workspace: 'src/',
folder: '/home/user/repo/build',
token: null,
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
expect(generateFolderPath(action)).toEqual('/home/user/repo/build')
})
it('should process as relative path if folder name begins with ./', () => {
const action = {
branch: '123',
workspace: 'src/',
folder: './build',
token: null,
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
expect(generateFolderPath(action)).toEqual('src/build')
})
it('should return absolute path if folder name begins with ~', () => {
const action = {
branch: '123',
workspace: 'src/',
folder: '~/repo/build',
token: null,
sshKey: null,
silent: false,
isTest: TestFlag.NONE
}
process.env.HOME = '/home/user'
expect(generateFolderPath(action)).toEqual('/home/user/repo/build')
})
})
describe('hasRequiredParameters', () => {
it('should fail if there is no provided GitHub Token, Access Token or SSH bool', () => {
const action = {
silent: false,
repositoryPath: undefined,
branch: 'branch',
folder: 'build',
workspace: 'src/',
isTest: TestFlag.NONE
}
try {
checkParameters(action)
} catch (e) {
expect(e.message).toMatch(
'No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.'
)
}
})
it('should fail if token is defined but it is an empty string', () => {
const action = {
silent: false,
repositoryPath: undefined,
token: '',
branch: 'branch',
folder: 'build',
workspace: 'src/',
isTest: TestFlag.NONE
}
try {
checkParameters(action)
} catch (e) {
expect(e.message).toMatch(
'No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.'
)
}
})
it('should fail if there is no branch', () => {
const action = {
silent: false,
repositoryPath: undefined,
token: '123',
branch: '',
folder: 'build',
workspace: 'src/',
isTest: TestFlag.NONE
}
try {
checkParameters(action)
} catch (e) {
expect(e.message).toMatch('Branch is required.')
}
})
it('should fail if there is no folder', () => {
const action = {
silent: false,
repositoryPath: undefined,
token: '123',
branch: 'branch',
folder: '',
workspace: 'src/',
isTest: TestFlag.NONE
}
try {
checkParameters(action)
} catch (e) {
expect(e.message).toMatch(
'You must provide the action with a folder to deploy.'
)
}
})
it('should fail if the folder does not exist in the tree', () => {
const action: ActionInterface = {
silent: false,
repositoryPath: undefined,
token: '123',
branch: 'branch',
folder: 'notARealFolder',
workspace: '.',
isTest: TestFlag.NONE
}
try {
action.folderPath = generateFolderPath(action)
checkParameters(action)
} catch (e) {
expect(e.message).toMatch(
`The directory you're trying to deploy named notARealFolder doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗`
)
}
})
})
describe('stripProtocolFromUrl', () => {
it('removes https', () => {
expect(stripProtocolFromUrl('https://github.com')).toBe('github.com')
})
it('removes http', () => {
expect(stripProtocolFromUrl('http://github.com')).toBe('github.com')
})
it('removes https|http and www.', () => {
expect(stripProtocolFromUrl('http://www.github.com')).toBe('github.com')
})
it('works with a url that is not github.com', () => {
expect(stripProtocolFromUrl('http://github.enterprise.jamesiv.es')).toBe(
'github.enterprise.jamesiv.es'
)
})
})
})