-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.test.js
754 lines (588 loc) · 22.8 KB
/
app.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
const request = require('supertest')
const dayjs = require('dayjs')
const path = require('path')
const admin = { username: 'admin', password: 'test', grant_type: 'password', client_id: 'self' }
let token
let app
let places = []
let event_id
beforeAll(async () => {
switch (process.env.DB) {
case 'mariadb':
process.env.config_path = path.resolve(__dirname, './seeds/config.mariadb.json')
break
case 'postgresql':
process.env.config_path = path.resolve(__dirname, './seeds/config.postgres.json')
break
case 'sqlite':
default:
process.env.config_path = path.resolve(__dirname, './seeds/config.sqlite.json')
}
try {
app = await require('../server/routes.js').main()
const { sequelize } = require('../server/api/models/index')
const { col } = require('../server/helpers')
// sequelize.sync({ force: true })
// await sequelize.query('PRAGMA foreign_keys = OFF')
await sequelize.query(`DELETE FROM ${col('user_followers')}`)
await sequelize.query(`DELETE FROM ${col('events')} where ${col('parentId')} IS NOT NULL`)
await sequelize.query('DELETE FROM ap_users')
await sequelize.query('DELETE FROM events')
await sequelize.query('DELETE FROM event_tags')
await sequelize.query('DELETE FROM resources')
await sequelize.query('DELETE FROM instances')
await sequelize.query('DELETE FROM settings')
await sequelize.query('DELETE FROM announcements')
await sequelize.query('DELETE FROM oauth_tokens')
await sequelize.query('DELETE FROM users')
await sequelize.query('DELETE FROM tags')
await sequelize.query('DELETE FROM places')
await sequelize.query('DELETE FROM filters')
await sequelize.query('DELETE FROM collections')
await sequelize.query('DELETE FROM notifications')
await sequelize.query('DELETE FROM tasks')
// await sequelize.query('PRAGMA foreign_keys = ON')
} catch (e) {
// console.error(e)
}
})
afterAll(async () => {
await require('../server/initialize.server.js').shutdown(false)
})
describe('Basic', () => {
test('shoud return an empty list', async () => {
const response = await request(app).get('/api/events')
.expect(200)
expect(response.body.length).toBe(0)
})
})
describe('Authentication / Authorization', () => {
test('should not return an user when not authenticated', () => {
return request(app).get('/api/user')
.expect(403)
})
test('should not authenticate with wrong user/password', () => {
return request(app).post('/oauth/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send({ email: 'admin', password: 'wrong'})
.expect(401)
})
test('should register an admin as first user', async () => {
const response = await request(app)
.post('/api/user/register')
.send({ email: 'admin', password: 'test' })
.expect(200)
expect(response.body.id).toBeDefined()
})
test('should authenticate with correct user/password', async () => {
const response = await request(app)
.post('/oauth/login')
.set('Content-Type', 'application/x-www-form-urlencoded')
.send(admin)
.expect(200)
expect(response.body.refresh_token).toBeDefined()
expect(response.body.access_token).toBeDefined()
expect(response.body.token_type).toBe('Bearer')
token = response.body
})
test('should get user when authenticated', async () => {
const response = await request(app).get('/api/user')
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.email).toBe(admin.username)
expect(response.body.is_admin).toBe(true)
})
})
describe('Settings', () => {
test('should not change settings when not allowed', () => {
return request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: false })
.expect(403)
})
test('should change settings when allowed', () => {
return request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: true })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
})
test('should retrieve stored array settings', async () => {
await request(app).post('/api/settings')
.auth(token.access_token, { type: 'bearer' })
.send({ key: 'test', value: [1, 2, 'test'] })
.expect(200)
const response = await request(app)
.get('/api/settings')
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.test.length).toBe(3)
expect(response.body.test).toStrictEqual([1, 2, 'test'])
})
test('should retrieve stored object settings', async () => {
await request(app).post('/api/settings')
.auth(token.access_token, { type: 'bearer' })
.send({ key: 'test', value: { name: 'test object' } })
.expect(200)
const response = await request(app)
.get('/api/settings')
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.test.name).toBe('test object')
})
test('should retrieve stored string settings', async () => {
await request(app).post('/api/settings')
.auth(token.access_token, { type: 'bearer' })
.send({ key: 'test', value: 'test string' })
.expect(200)
const response = await request(app)
.get('/api/settings')
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.test).toBe('test string')
})
})
describe('Events', () => {
test('should not allow event creation without required fields', async () => {
const required_fields = {
'title': {},
'start_datetime': { title: 'test title' },
'place_id or place_name and place_address are': { title: 'test title', start_datetime: dayjs().unix() + 1000, place_name: 'test place name' },
}
const promises = Object.keys(required_fields).map(async field => {
const response = await request(app).post('/api/event').send(required_fields[field])
.expect(400)
expect(response.text).toBe(`${field} required`)
})
await Promise.all(promises)
})
test('should create anon event only when allowed', async () => {
await request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: false })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
await request(app).post('/api/event')
.expect(403)
let response = await request(app).post('/api/event')
.send({ title: 'test title 2', place_name: 'place name', place_address: 'address', tags: ['test'], start_datetime: dayjs().unix() + 1000 })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.place.id).toBeDefined()
places.push(response.body.place.id)
await request(app).post('/api/settings')
.send({ key: 'allow_anon_event', value: true })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
response = await request(app).post('/api/event')
.send({ title: 'test title 3', place_name: 'place name 2', place_address: 'address 2', tags: ['test'], start_datetime: dayjs().unix() + 1000 })
.expect(200)
expect(response.body.place.id).toBeDefined()
places.push(response.body.place.id)
})
test('should not confirm anon events', async () => {
const response = await request(app).post('/api/event')
.send({ title: 'test title 6', place_id: places[0], start_datetime: dayjs().unix() + 1000 })
.expect(200)
expect(response.body.is_visible).toBe(false)
event_id = response.body.id
})
test('should not get unconfirmed events', async () => {
let response = await request(app).get(`/api/event/detail/${event_id}`)
.expect(404)
response = await request(app).get(`/api/event/detail/${event_id}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
})
test('should confirm event if allowed', async () => {
let response = await request(app).put(`/api/event/confirm/${event_id}`)
.send()
.expect(403)
response = await request(app).put(`/api/event/confirm/${event_id}`)
.auth(token.access_token, { type: 'bearer' })
.send()
.expect(200)
})
test('should not allow start_datetime greater than end_datetime', async () => {
const event = {
title: ' test title 5',
place_id: places[0],
start_datetime: dayjs().unix() + 1000,
end_datetime: dayjs().unix(),
}
const response = await request(app).post('/api/event')
.send(event)
.expect(400)
expect(response.text).toBe('start datetime is greater than end datetime')
})
test('should not allow start_datetime greater than 3000', async () => {
const event = {
title: ' test title 5',
start_datetime: dayjs().set('year', 4000).unix(),
place_id: places[0],
}
const response = await request(app).post('/api/event')
.send(event)
.expect(400)
expect(response.text).toBe('are you sure?')
})
test('should validate start_datime', async () => {
const event = {
title: ' test title 5',
start_datetime: "antani",
place_id: places[0],
}
const response = await request(app).post('/api/event')
.send(event)
.expect(400)
})
test('should validate end_datime', async () => {
const event = {
title: ' test title 5',
start_datetime: dayjs().unix() + 1000,
end_datetime: "Antani",
place_id: places[0],
}
const response = await request(app).post('/api/event')
.send(event)
.expect(400)
})
test('should not allow create anonymous event in the past', async() => {
const time_in_past = dayjs().unix() - 1;
await request(app).post('/api/event')
.send({
title: 'anonymous event in the past',
place_id: places[0],
start_datetime: time_in_past
})
.expect(400)
.then(response => {
expect(response.text)
.toEqual('Anonymous user cannot create past events')
})
})
test('should allow create event in the past when authenticated', async() => {
const time_in_past = dayjs().unix() - 1
await request(app).post('/api/event')
.send({
title: 'authenticated event in the past',
place_id: places[0],
start_datetime: time_in_past
})
.auth(token.access_token, { type: 'bearer' })
.expect(200)
})
test('should trim tags and title', async () => {
const event = {
title: ' test title 4 ',
place_id: places[0],
start_datetime: dayjs().unix() + 1000,
tags: [' test tag ']
}
const response = await request(app).post('/api/event')
.send(event)
.expect(200)
.expect('Content-Type', /json/)
expect(response.body.title).toBe('test title 4')
expect(response.body.tags[0]).toBe('test tag')
})
test('should sanitize htlm in description', async () => {
const event = {
title: 'test title',
place_id: places[0],
start_datetime: dayjs().unix() + 1000,
tags: ['test tags'],
description: `<p wrong-attr="" onclick="alert('test');">inside paragraph</p><a href="https://test.com/?query=true&fbclid=facebook_id">link with fb reference</a>`
}
const response = await request(app).post('/api/event')
.send(event)
.expect(200)
.expect('Content-Type', /json/)
event_id = response.body.id
expect(response.body.description).toBe(`<p>inside paragraph</p><a href="https://test.com/?query=true">link with fb reference</a>`)
})
test('should not update event with invalid start_datetime', async () => {
const event = {
id: event_id,
place_id: places[0],
start_datetime: "antani"
}
const response = await request(app).put('/api/event')
.auth(token.access_token, { type: 'bearer' })
.send(event)
.expect(400)
expect(response.text).toBe('Wrong format for start datetime')
})
test('should not update event with invalid end_datetime', async () => {
const event = {
id: event_id,
place_id: places[0],
end_datetime: 2
}
const response = await request(app).put('/api/event')
.auth(token.access_token, { type: 'bearer' })
.send(event)
.expect(400)
expect(response.text).toBe('start datetime is greater than end datetime')
})
})
let event = {}
describe('Tags', () => {
test('should create event with tags', async () => {
event = await request(app).post('/api/event')
.send({ title: 'test tags', place_id: places[1], start_datetime: dayjs().unix() + 1000, tags: ['tag1', 'Tag2', 'tAg3'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(event.body.tags.length).toBe(3)
expect(event.body.tags).toStrictEqual(['tag1', 'Tag2', 'tAg3'])
})
test('should dedup tags', async () => {
event = await request(app).post('/api/event')
.send({ title: 'test tags', place_id: places[0], start_datetime: dayjs().unix() + 1000, tags: ['ciao', ' Ciao '] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(event.body.tags.length).toBe(1)
expect(event.body.tags).toStrictEqual(['ciao'])
})
test('should not allow non-array tags field', async () => {
const response = await request(app).post('/api/event')
.send({ title: 'test non-array tags', place_id: places[1], start_datetime: dayjs().unix() + 1000, tags: 'Tag1' })
.auth(token.access_token, { type: 'bearer' })
.expect(400)
expect(response.text).toBe('tags field must be an array')
})
test('should create event trimming tags / ignore sensitiviness', async () => {
const ret = await request(app).post('/api/event')
.send({ title: 'test trimming tags', place_id: places[1], start_datetime: dayjs().unix() + 1000, tags: ['Tag1', 'taG2 '] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(ret.body.tags.length).toBe(2)
// expect(ret.body.tags).toStrictEqual(['Tag1', 'taG2'])
expect(ret.body.tags[0]).toBe('tag1')
expect(ret.body.tags[1]).toBe('Tag2')
})
test('should modify event tags', async () => {
const ret = await request(app).put('/api/event')
.send({ id: event.body.id, tags: ['tag1', 'tag3', 'tag4'], place_id: places[0] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(ret.body.tags).toStrictEqual(['tag1', 'tAg3', 'tag4'])
})
test('shoud support utf-8 chars in tag', async () => {
let ret = await request(app).post('/api/event')
.send({ title: 'test trimming tags',
place_id: places[1],
start_datetime: dayjs().unix() + 1000,
tags: ['/\'"%^&*~`!@#$*()_+=-\\}{', 'üniversite etkinliği', 'ÜNIVERSITE ETKINLIĞI', 'antani', '$$antan$i'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(ret.body.tags).toEqual(expect.arrayContaining(['/\'"%^&*~`!@#$*()_+=-\\}{', 'üniversite etkinliği', 'antani', '$$antan$i']))
ret = await request(app).post('/api/event')
.send({ title: 'test trimming tags',
place_id: places[0],
start_datetime: dayjs().unix() + 1000,
tags: ['/\'"%^&*~`!@#$*()_+=-\\}{', 'üniversite etkinliği', 'ÜNIVERSITE ETKINLIĞI', 'antani', '$$antan$i'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(ret.body.tags).toStrictEqual(expect.arrayContaining(['/\'"%^&*~`!@#$*()_+=-\\}{', 'üniversite etkinliği', 'antani', '$$antan$i']))
})
test('should return events searching for tags', async () => {
const response = await request(app).get('/api/events?tags=tAg3')
.expect(200)
expect(response.body.length).toBe(2)
// expect(response.body[0].title).toBe('test tags')
expect(response.body[0].tags.length).toBe(3)
})
test('should return limited events', async () => {
let response = await request(app).get('/api/events?max=1')
.expect(200)
expect(response.body.length).toBe(1)
response = await request(app).get('/api/events?max=2')
.expect(200)
expect(response.body.length).toBe(2)
})
})
describe('Place', () => {
test('should get events by place', async () => {
const response = await request(app).get('/api/place/place name 2')
.expect(200)
expect(response.body.place.name).toBe('place name 2')
expect(response.body.events.length).toBe(4)
expect(response.body.events[0].place.name).toBe('place name 2')
})
test('admin should get all places', async () => {
await request(app).get('/api/places')
.expect(403)
const response = await request(app).get('/api/places')
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.length).toBe(2)
})
test('should search for a place', async () => {
const response = await request(app).get('/api/place?search=place')
.expect(200)
expect(response.body.length).toBe(2)
})
test('should trim place\'s name and address', async () => {
const ret = await request(app).post('/api/event')
.send({ title: 'test trimming', place_name: ' test place with white Space ',
place_address: ' address with Space ', start_datetime: dayjs().unix() + 1000 })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(ret.body.place.name).toBe('test place with white Space')
expect(ret.body.place.address).toBe('address with Space')
})
})
let collections = []
let filters = []
describe('Collection', () => {
test('should not create a new collection if not allowed', () => {
return request(app).post('/api/collections')
.send({ name: 'test collection' })
.expect(403)
})
test('should create a new collection', async () => {
const response = await request(app).post('/api/collections')
.send({ name: 'test collection' })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.id).toBeDefined()
collections.push(response.body)
})
test('should do not have any event when no filters', async () => {
const response = await request(app).get('/api/collections/test collection')
.expect(200)
expect(response.body.length).toBe(0)
})
test('should add a new filter', async () => {
await request(app)
.post('/api/filter')
.send({ collectionId: collections[0].id, tags: ['test'] })
.expect(403)
const response = await request(app).post('/api/filter')
.send({ collectionId: collections[0].id, tags: ['test'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.id).toBeDefined()
filters.push(response.body.id)
})
test('shoud get collection\'s filters using withFilters parameter', async () => {
let response = await request(app)
.get('/api/collections?withFilters=true')
.expect(200)
expect(response.body.length).toBe(1)
expect(response.body[0].name).toBe('test collection')
expect(response.body[0].filters.length).toBe(1)
expect(response.body[0].filters[0].tags.length).toBe(1)
expect(response.body[0].filters[0].tags[0]).toBe('test')
response = await request(app)
.get('/api/collections')
.expect(200)
expect(response.body[0].filters).toBeUndefined()
response = await request(app)
.get('/api/collections?withFilters=false')
.expect(200)
expect(response.body[0].filters).toBeUndefined()
})
test('should get collection events', async () => {
const response = await request(app)
.get(`/api/collections/test collection`)
.expect(200)
expect(response.body.length).toBe(1)
})
test('should remove filter', async () => {
await request(app)
.delete(`/api/filter/${filters[0]}`)
.expect(403)
await request(app)
.delete(`/api/filter/${filters[0]}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
const response = await request(app)
.get(`/api/filter/${filters[0]}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.length).toBe(0)
})
test('shoud filter for tags', async () => {
await request(app)
.post('/api/filter')
.send({ collectionId: collections[0].id, tags: ['test'] })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
const response = await request(app)
.get(`/api/collections/test collection`)
.expect(200)
expect(response.body.length).toBe(1)
})
test('should create a second collection', async () => {
let response = await request(app).post('/api/collections')
.send({ name: 'second collection' })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
expect(response.body.id).toBeDefined()
collections.push(response.body)
response = await request(app)
.get('/api/collections')
.expect(200)
expect(response.body?.length).toBe(2)
expect(response.body[0].id).toBe(collections[0].id)
expect(response.body[1].id).toBe(collections[1].id)
})
test('should change collections order', async () =>{
let response = await request(app).put(`/api/collection/moveup/${collections[1].sortIndex}`)
.auth(token.access_token, { type: 'bearer' })
.expect(200)
response = await request(app)
.get('/api/collections')
.expect(200)
expect(response.body?.length).toBe(2)
expect(response.body[0].id).toBe(collections[1].id)
expect(response.body[1].id).toBe(collections[0].id)
})
})
describe('Export', () => {
test('should export an rss feed', async () => {
await request(app).get('/feed/rss')
.expect('Content-Type', /application\/rss\+xml/)
.expect(200)
await request(app).get('/api/export/rss')
.expect('Content-Type', /application\/rss\+xml/)
.expect(200)
})
test('should export a json feed', async () => {
await request(app).get('/feed/json')
.expect('Content-Type', /application\/json/)
.expect(200)
await request(app).get('/api/export/json')
.expect('Content-Type', /application\/json/)
.expect(200)
})
test('should export an ics feed', async () => {
await request(app).get('/feed/ics')
.expect('Content-Type', /text\/calendar/)
.expect(200)
await request(app).get('/api/export/ics')
.expect('Content-Type', /text\/calendar/)
.expect(200)
})
})
describe('Geocoding', () => {
test('should not be enabled by default', async () => {
await request(app)
.post('/api/settings')
.send({ key: 'allow_geolocation', value: false })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
const response = await request(app).get('/api/placeOSM/Nominatim/test')
.expect(403)
expect(response.body).toBeDefined()
})
test('should geocode when enabled', async () => {
await request(app)
.post('/api/settings')
.send({ key: 'allow_geolocation', value: true })
.auth(token.access_token, { type: 'bearer' })
.expect(200)
const response = await request(app).get('/api/placeOSM/Nominatim/test')
.expect(200)
expect(response.body).toBeDefined()
})
})