This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
397 lines (356 loc) · 12.3 KB
/
test.js
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/* eslint-disable no-unused-vars */
/* eslint-disable no-undef */
process.env.NODE_ENV = 'test'
const chai = require('chai')
const chaiHttp = require('chai-http')
const rimraf = require('rimraf')
const cheerio = require('cheerio')
const assert = require('assert')
const crypto = require('crypto')
const should = chai.should()
let server
const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tcwuNrqppTCncMqihx6118y+9+8OQCRTh
ltn5FVFmGLTbNp3iw2pNR9jA3phXoc4=
=r3dV
-----END PGP PUBLIC KEY BLOCK-----`
const signedMessage = {
valid: `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
I am requesting the removal of my public key from dat-keyserver. token=be354767a89dd09b4d1e739c0b9e5a4d0fd43526ce9105d63683503db47f4df9
-----BEGIN PGP SIGNATURE-----
iQEzBAEBCAAdFiEEzPgVre6I8V30V/z2RiN4G7RVLCUFAl1uxrsACgkQRiN4G7RV
LCXyvwgAn4C5YhMqBuACKlcxSVpQWz2PSsWjS0x/kGOt8jaNDBko9bUuuT4S7VsA
6LCiRU2xCAs+zv1/hnXEjiEi+wJpliwNK8b7s+ku+yzJPgHwiqhT58hMYIdVwpv+
Dz3nIlywCdtObUJuLCAE1/vW8Jnfvp0CpJXhRH8tdKPGGiNqpSsiZ+3w0kYcc9AS
8a15rHOVFk6eESjGXWofWuQRaTwob4poQVCZPbY2p/FK2iBLQYszlJn0aKftgGqg
CMIF79wNEa+uADm/WMcuIilW1jL/YuDbgpVwjnetrzNU71nR921tS8RA8zrToBQY
E9iOXSqfiFEfL40A+z3Ul+tffusNrw==
=gaQ6
-----END PGP SIGNATURE-----`,
invalid: `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
I am requesting the removal of my public key from dat-keyserver. token=1234123412341234123412341234123412341234123412341234123412341234
-----BEGIN PGP SIGNATURE-----
iQEzBAEBCAAdFiEEzPgVre6I8V30V/z2RiN4G7RVLCUFAl1uxrsACgkQRiN4G7RV
LCXyvwgAn4C5YhMqBuACKlcxSVpQWz2PSsWjS0x/kGOt8jaNDBko9bUuuT4S7VsA
6LCiRU2xCAs+zv1/hnXEjiEi+wJpliwNK8b7s+ku+yzJPgHwiqhT58hMYIdVwpv+
Dz3nIlywCdtObUJuLCAE1/vW8Jnfvp0CpJXhRH8tdKPGGiNqpSsiZ+3w0kYcc9AS
8a15rHOVFk6eESjGXWofWuQRaTwob4poQVCZPbY2p/FK2iBLQYszlJn0aKftgGqg
CMIF79wNEa+uADm/WMcuIilW1jL/YuDbgpVwjnetrzNU71nR921tS8RA8zrToBQY
E9iOXSqfiFEfL40A+z3Ul+tffusNrw==
=gaQ6
-----END PGP SIGNATURE-----`,
validButWrongToken: `-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
I am requesting the removal of my public key from dat-keyserver. token=1234123412341234123412341234123412341234123412341234123412341234
-----BEGIN PGP SIGNATURE-----
iQEzBAEBCAAdFiEEzPgVre6I8V30V/z2RiN4G7RVLCUFAl1uy+kACgkQRiN4G7RV
LCXQKQf/f2yUkW1W9u7tAnxiFW5bDN/DzunTb51fN2Jnpnjj5IrZI5pEtq51yaq5
TPU5h757bD3mnVGuAwSDI2iTgGx/H2uBqDC+wSanf5mwaxwaxVwrOqHJPT0io5om
sFx0MzmodzdgHk5ORVMe9mw7SE5YT6v5m3P3JIXHrnneXq7vAFSOgA6KDxuHJMxc
WwXifsFBE5iUMXQv4fJoR/cs+Hl0RD3KWqstU0SI4bd5VfaZDeLKTwr/yEy4s3sD
QpexJLBmz0nuB3VzThSShRjJHKa+g6dl6o4X1cDKgQMUPzgmBPZMxPxO9MKXarep
/n+7YaqW7lg1f15h9mPokFLasWMktg==
=o0VQ
-----END PGP SIGNATURE-----`
}
const token = crypto
.createHash('sha256')
.update(pubKey)
.digest('hex')
chai.use(chaiHttp)
describe('dat-keyserver', () => {
before(done => {
// delete test database and wait for new database to initialise before tests
rimraf.sync('testdb')
server = require('./index')
setTimeout(() => {
done()
}, 3000)
})
describe('GET /', () => {
it('should render the index page', done => {
chai
.request(server)
.get('/')
.end((err, res) => {
res.should.have.status(200)
done()
})
})
it('should render the FAQ page', done => {
chai
.request(server)
.get('/faq')
.end((err, res) => {
res.should.have.status(200)
done()
})
})
it('should render the key page', done => {
chai
.request(server)
.get('/key')
.end((err, res) => {
res.should.have.status(200)
done()
})
})
})
describe('POST /publish', () => {
it('should publish a new public key', done => {
chai
.request(server)
.post('/publish')
.set('content-type', 'application/x-www-form-urlencoded')
.send({ key: pubKey })
.end((err, res) => {
res.should.have.status(200)
res.text.should.be.eql(
'<pre>Success! Published key ccf815adee88f15df457fcf64623781bb4552c25</pre>'
)
done()
})
})
it('should fail to publish and invalid key', done => {
chai
.request(server)
.post('/publish')
.set('content-type', 'application/x-www-form-urlencoded')
.send({ key: 'malformed key' })
.end((err, res) => {
res.should.have.status(500)
done()
})
})
it('should return 400 if no key is provided', done => {
chai
.request(server)
.post('/publish')
.set('content-type', 'application/x-www-form-urlencoded')
.end((err, res) => {
res.should.have.status(400)
done()
})
})
})
describe('GET /fetch', () => {
it('should fetch a public key by key ID', done => {
chai
.request(server)
.get('/fetch?fingerprint=4623781bb4552c25')
.end((err, res) => {
res.text.should.be.eql(`<pre>\n${pubKey}\n</pre>`)
res.should.have.status(200)
done()
})
})
it('should fetch a public key by fingerprint', done => {
chai
.request(server)
.get('/fetch?fingerprint=ccf815adee88f15df457fcf64623781bb4552c25')
.end((err, res) => {
res.text.should.be.eql(`<pre>\n${pubKey}\n</pre>`)
res.should.have.status(200)
done()
})
})
it('should return 404 if no key is found', done => {
chai
.request(server)
.get('/fetch?fingerprint=1234567890abcdef')
.end((err, res) => {
res.should.have.status(404)
done()
})
})
it('should return 400 if no fingerprint is provided', done => {
chai
.request(server)
.get('/fetch')
.end((err, res) => {
res.should.have.status(400)
done()
})
})
})
describe('GET /search', () => {
it('should return a correct search result', done => {
chai
.request(server)
.get('/search?query=tdjsnelling')
.end((err, res) => {
res.should.have.status(200)
const $ = cheerio.load(res.text)
assert($('.searchResults li').length, 1)
assert.strictEqual(
$($('.searchResults li p').get(0)).text(),
'pub 2048/B4552C25 (cr. 2016-09-21, exp. n/a) CCF815ADEE88F15DF457FCF64623781BB4552C25'
)
done()
})
})
it('should return a "no results" message if there are no results', done => {
chai
.request(server)
.get('/search?query=this+will+return+no+results')
.end((err, res) => {
res.should.have.status(200)
const $ = cheerio.load(res.text)
assert.strictEqual($('.searchResults').length, 0)
assert(
res.text.includes(
'No results for query “this will return no results”'
)
)
done()
})
})
it('should return 400 if no query is provided', done => {
chai
.request(server)
.get('/search')
.end((err, res) => {
res.should.have.status(400)
done()
})
})
})
describe('POST /remove/request', () => {
it('should provide a correct message to be signed', done => {
chai
.request(server)
.post('/remove/request')
.set('content-type', 'application/x-www-form-urlencoded')
.send({ fingerprint: 'ccf815adee88f15df457fcf64623781bb4552c25' })
.end((err, res) => {
res.should.have.status(200)
const $ = cheerio.load(res.text)
assert.strictEqual(
$('code').text(),
`I am requesting the removal of my public key from dat-keyserver. token=${token}`
)
done()
})
})
it('should return 404 if no key is found', done => {
chai
.request(server)
.post('/remove/request')
.set('content-type', 'application/x-www-form-urlencoded')
.send({ fingerprint: '1234567890abcdef' })
.end((err, res) => {
res.should.have.status(404)
done()
})
})
it('should return 400 if no fingerprint is provided', done => {
chai
.request(server)
.post('/remove/request')
.set('content-type', 'application/x-www-form-urlencoded')
.end((err, res) => {
res.should.have.status(400)
done()
})
})
})
describe('POST /remove/verify', () => {
it('should return 500 if signed message is malformed', done => {
chai
.request(server)
.post('/remove/verify')
.set('content-type', 'application/x-www-form-urlencoded')
.send({
fingerprint: 'ccf815adee88f15df457fcf64623781bb4552c25',
message: 'malformed message'
})
.end((err, res) => {
res.should.have.status(500)
done()
})
})
it('should return 401 if signed message is invalid', done => {
chai
.request(server)
.post('/remove/verify')
.set('content-type', 'application/x-www-form-urlencoded')
.send({
fingerprint: 'ccf815adee88f15df457fcf64623781bb4552c25',
message: signedMessage.invalid
})
.end((err, res) => {
res.should.have.status(401)
done()
})
})
it('should returm 401 if signed message is valid but token incorrect', done => {
chai
.request(server)
.post('/remove/verify')
.set('content-type', 'application/x-www-form-urlencoded')
.send({
fingerprint: 'ccf815adee88f15df457fcf64623781bb4552c25',
message: signedMessage.validButWrongToken
})
.end((err, res) => {
res.should.have.status(401)
done()
})
})
it('should remove a key if provided with valid message and token', done => {
chai
.request(server)
.post('/remove/verify')
.set('content-type', 'application/x-www-form-urlencoded')
.send({
fingerprint: 'ccf815adee88f15df457fcf64623781bb4552c25',
message: signedMessage.valid
})
.end((err, res) => {
res.should.have.status(200)
done()
})
})
})
after(() => {
// delete the test database and exit cleanly after tests are complete
setTimeout(() => {
rimraf.sync('testdb')
process.exit(0)
}, 1000)
})
})