-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
331 lines (276 loc) · 9.7 KB
/
test.js
File metadata and controls
331 lines (276 loc) · 9.7 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
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
331
'use strict'
const { runTests, test, testAsync } = require('mvt')
const http = require('http')
const path = require('path')
const semver = require('semver')
const qs = require('tiny-params')
const {
getConfig,
getReleaseList,
latestByChannel,
requestHandler,
simpleGet,
on,
off
} = require('./index')
const repo = 'doesdev/oneclick-release-test'
const fullUrl = 'https://github.com/doesdev/oneclick-release-test'
let secrets
try {
secrets = require('./secrets.json')
} catch (ex) {
const err = 'Tests require secrets.json file with private repo and token'
console.error(err)
process.exit(1)
}
const publicConfig = { repo, token: secrets.token }
const fullUrlConfig = (c) => getConfig(Object.assign({}, c, { repo: fullUrl }))
runTests('Starting oneclick-update tests', async () => {
for (const type of ['public', 'private']) {
const isPublic = type === 'public'
const config = await getConfig(isPublic ? publicConfig : secrets)
const metaChannel = isPublic ? 'vendora' : null
const preChannel = isPublic ? 'prerelease' : null
const useLinux = isPublic
const usePlatformFilters = !isPublic
const platformFilters = {
winsub: (assets, action) => {
return assets.find((a) => a.name.indexOf('win-sub') !== -1)
}
}
if (usePlatformFilters) config.platformFilters = platformFilters
let latest, notLatest, randomAsset
test(`[${type}] getReleaseList gets list of recent releases`,
Array.isArray(await getReleaseList(config))
)
test(`[${type}] getReleaseList strips github url from repo`,
Array.isArray(await getReleaseList(await fullUrlConfig(config)))
)
await testAsync(`[${type}] latestByChannel`, async () => {
const result = await latestByChannel(config)
test(`[${type}] channels are of expected type`, typeof result, 'object')
if (metaChannel) {
test(`[${type}] channel parsed from build metadata exists`,
result[metaChannel].channel,
metaChannel
)
}
if (preChannel) {
test(`[${type}] prerelease channel exists`,
result[preChannel].channel,
preChannel
)
}
latest = result[''].tag_name
notLatest = semver.inc(latest, 'patch')
randomAsset = result[''].assets[0]
return true
})
const getServerResponse = async ({
action,
channel,
platform,
version,
redirect,
filename,
release,
filetype,
noUnref
}) => {
const server = http.createServer(await requestHandler(config))
await new Promise((resolve, reject) => server.listen(resolve))
const port = server.address().port
const path = [
action,
channel,
platform,
version,
release ? 'RELEASES' : null
].filter((v) => v).join('/')
const qEls = [
filename ? `filename=${filename}` : null,
filetype ? `filetype=${filetype}` : null
].filter((v) => v)
const q = qEls.length ? `?${qEls.join('&')}` : ''
const url = `http://localhost:${port}/${path}${q}`
const result = await simpleGet(url, { redirect })
result.server = server
result.serverPort = port
if (!noUnref) server.unref()
return result
}
const testPlatformDownload = async (
platform,
ext,
expectNoContent,
forceExt,
channel
) => {
const host = isPublic ? 'github.com' : 'amazonaws.com'
const action = 'download'
const redirect = false
const filetype = forceExt ? ext : null
const args = { action, platform, redirect, filetype, channel }
const result = await getServerResponse(args)
const location = result.headers.location
if (expectNoContent) {
return test(`[${type}] download expecting no content for ${platform}`,
result.statusCode,
204
)
}
test(`[${type}] download for ${platform} redirects with 302`,
result.statusCode,
302
)
test(`[${type}] download for ${platform} redirects to ${host}`,
(new URL(location)).hostname.slice(-host.length),
host
)
let resultFile
if (isPublic) {
resultFile = location.slice(location.lastIndexOf('/') + 1)
} else {
const meta = qs(location)['response-content-disposition']
resultFile = meta.split('filename=')[1]
}
test(`[${type}] download for ${platform} is expected filetype`,
path.extname(resultFile).slice(1),
ext
)
return true
}
let onResult
on('download', (r) => { onResult = r })
await testAsync(`[${type}] requestHandler download/win32`, () => {
return testPlatformDownload('win32', 'exe')
})
test(`[${type}] on('download') fires`, onResult.platform, 'win32')
off('download')
await testAsync(`[${type}] requestHandler download/darwin`, () => {
return testPlatformDownload('darwin', 'dmg')
})
if (usePlatformFilters) {
await testAsync(`[${type}] platformFilters download/winsub`, () => {
return testPlatformDownload('winsub', 'exe')
})
}
if (useLinux) {
await testAsync(`[${type}] requestHandler download/linux`, () => {
return testPlatformDownload('linux', 'deb')
})
await testAsync(`[${type}] requestHandler download/linux as rpm`, () => {
return testPlatformDownload('linux', 'rpm', null, true)
})
}
if (metaChannel) {
await testAsync(`[${type}] requestHandler download/${metaChannel}/darwin`, () => {
return testPlatformDownload('darwin', 'dmg', null, null, metaChannel)
})
}
await testAsync(`[${type}] download fails with no content`, () => {
return testPlatformDownload('notaplatform', null, true)
})
await testAsync(`[${type}] download specific file works`, async () => {
const filename = randomAsset.name
const args = { action: 'download', filename, redirect: false }
const result = await getServerResponse(args)
const location = result.headers.location
let resultFile
if (isPublic) {
resultFile = location.slice(location.lastIndexOf('/') + 1)
} else {
const meta = qs(location)['response-content-disposition']
resultFile = meta.split('filename=')[1]
}
return test(`[${type}] redirect url points to filename`,
filename,
resultFile
)
})
const testPlatformUpdate = async (
platform,
expectNoContent,
version,
urlIncludes
) => {
const { serverUrl } = config
const host = isPublic ? 'github.com' : (new URL(serverUrl)).hostname
const args = { action: 'update', platform, version }
const result = await getServerResponse(args)
const { data } = result
if (expectNoContent) {
return test(`[${type}] update expecting no content for ${platform}`,
result.statusCode,
204
)
}
test(`[${type}] update for ${platform} contains name`,
typeof data.name,
'string'
)
if (urlIncludes) {
test(`[${type}] update for ${platform} url includes expected string`,
data.url.indexOf(urlIncludes) !== -1,
undefined,
{ url: data.url, shouldInclude: urlIncludes }
)
}
test(`[${type}] update for ${platform} contains expected url`,
(new URL(data.url)).hostname.slice(-host.length),
host
)
return true
}
await testAsync(`[${type}] requestHandler update/win32`, () => {
const shouldInclude = isPublic ? '.exe' : '/win32'
return testPlatformUpdate('win32', null, null, shouldInclude)
})
await testAsync(`[${type}] requestHandler update/darwin`, () => {
const shouldInclude = isPublic ? '.zip' : '/darwin'
return testPlatformUpdate('darwin', null, null, shouldInclude)
})
if (useLinux) {
await testAsync(`[${type}] requestHandler update/darwin`, () => {
const shouldInclude = isPublic ? '.deb' : '/linux'
return testPlatformUpdate('linux', null, null, shouldInclude)
})
}
await testAsync(`[${type}] update returns no content for bad platform`, () => {
return testPlatformUpdate('notaplatform', true)
})
await testAsync(`[${type}] update returns no content for latest version`, () => {
return testPlatformUpdate('win32', true, latest)
})
await testAsync(`[${type}] update works for non-latest version`, () => {
return testPlatformUpdate('win32', false, notLatest)
})
await testAsync(`[${type}] RELEASES gets expected data`, async () => {
const { serverUrl } = config
const expectHost = isPublic ? 'github.com' : (new URL(serverUrl)).hostname
const args = {
action: 'update',
platform: 'win32',
version: notLatest,
redirect: true,
release: true,
noUnref: true
}
const { data, server, serverPort } = await getServerResponse(args)
const firstLine = data.split('\n')[0]
const split = firstLine.split(' ')
const [hash, url, size] = split
const host = (new URL(url)).hostname.slice(-expectHost.length)
test(`[${type}] RELEASES has expected sections`, split.length, 3)
test(`[${type}] RELEASES hash is expected length`, hash.length, 40)
test(`[${type}] RELEASES url is as expected`, host, expectHost)
test(`[${type}] RELEASES size is a number`, !Number.isNaN(+size))
const privUrl = url.replace(serverUrl, `http://localhost:${serverPort}`)
const nupkgUrl = isPublic ? url : privUrl
const { statusCode: code } = await simpleGet(nupkgUrl, { redirect: false })
server.unref()
test(`[${type}] RELEASES url responds with redirect`, code, 302)
return true
})
}
})