forked from CMU-313/cmu-313-s26-nodebb-spring-26-NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathposts.js
More file actions
1810 lines (1585 loc) · 66.6 KB
/
Copy pathposts.js
File metadata and controls
1810 lines (1585 loc) · 66.6 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
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
const assert = require('assert');
const nconf = require('nconf');
const path = require('path');
const util = require('util');
const sleep = util.promisify(setTimeout);
const db = require('./mocks/databasemock');
const topics = require('../src/topics');
const posts = require('../src/posts');
const categories = require('../src/categories');
const privileges = require('../src/privileges');
const user = require('../src/user');
const groups = require('../src/groups');
const socketPosts = require('../src/socket.io/posts');
const apiPosts = require('../src/api/posts');
const apiTopics = require('../src/api/topics');
const meta = require('../src/meta');
const file = require('../src/file');
const helpers = require('./helpers');
const utils = require('../src/utils');
const request = require('../src/request');
describe('Post\'s', () => {
let voterUid;
let voteeUid;
let globalModUid;
let postData;
let topicData;
let cid;
before(async () => {
voterUid = await user.create({ username: 'upvoter' });
voteeUid = await user.create({ username: 'upvotee' });
globalModUid = await user.create({ username: 'globalmod', password: 'globalmodpwd' });
({ cid } = await categories.create({
name: 'Test Category',
description: 'Test category created by testing script',
}));
({ topicData, postData } = await topics.post({
uid: voteeUid,
cid: cid,
title: 'Test Topic Title',
content: 'The content of test topic',
}));
await groups.join('Global Moderators', globalModUid);
});
it('should update category teaser properly', async () => {
const getCategoriesAsync = async () => (await request.get(`${nconf.get('url')}/api/categories`, { })).body;
const postResult = await topics.post({ uid: globalModUid, cid: cid, title: 'topic title', content: '123456789' });
let data = await getCategoriesAsync();
assert.equal(data.categories[0].teaser.pid, postResult.postData.pid);
assert.equal(data.categories[0].posts[0].content, '123456789');
assert.equal(data.categories[0].posts[0].pid, postResult.postData.pid);
const newUid = await user.create({ username: 'teaserdelete' });
const newPostResult = await topics.post({ uid: newUid, cid: cid, title: 'topic title', content: 'xxxxxxxx' });
data = await getCategoriesAsync();
assert.equal(data.categories[0].teaser.pid, newPostResult.postData.pid);
assert.equal(data.categories[0].posts[0].content, 'xxxxxxxx');
assert.equal(data.categories[0].posts[0].pid, newPostResult.postData.pid);
await user.delete(1, newUid);
data = await getCategoriesAsync();
assert.equal(data.categories[0].teaser.pid, postResult.postData.pid);
assert.equal(data.categories[0].posts[0].content, '123456789');
assert.equal(data.categories[0].posts[0].pid, postResult.postData.pid);
});
it('should change owner of post and topic properly', async () => {
const oldUid = await user.create({ username: 'olduser' });
const newUid = await user.create({ username: 'newuser' });
const postResult = await topics.post({ uid: oldUid, cid: cid, title: 'change owner', content: 'original post' });
const postData = await topics.reply({ uid: oldUid, tid: postResult.topicData.tid, content: 'firstReply' });
const pid1 = postResult.postData.pid;
const pid2 = postData.pid;
assert.deepStrictEqual(await db.sortedSetScores(`tid:${postResult.topicData.tid}:posters`, [oldUid, newUid]), [2, null]);
await socketPosts.changeOwner({ uid: globalModUid }, { pids: [pid1, pid2], toUid: newUid });
assert.deepStrictEqual(await db.sortedSetScores(`tid:${postResult.topicData.tid}:posters`, [oldUid, newUid]), [null, 2]);
assert.deepStrictEqual(await posts.isOwner([pid1, pid2], oldUid), [false, false]);
assert.deepStrictEqual(await posts.isOwner([pid1, pid2], newUid), [true, true]);
assert.strictEqual(await user.getUserField(oldUid, 'postcount'), 0);
assert.strictEqual(await user.getUserField(newUid, 'postcount'), 2);
assert.strictEqual(await user.getUserField(oldUid, 'topiccount'), 0);
assert.strictEqual(await user.getUserField(newUid, 'topiccount'), 1);
assert.strictEqual(await db.sortedSetScore('users:postcount', oldUid), 0);
assert.strictEqual(await db.sortedSetScore('users:postcount', newUid), 2);
assert.strictEqual(await topics.isOwner(postResult.topicData.tid, oldUid), false);
assert.strictEqual(await topics.isOwner(postResult.topicData.tid, newUid), true);
assert.strictEqual(await topics.getTopicField(postResult.topicData.tid, 'postercount'), 1);
});
it('should fail to change owner if new owner does not exist', async () => {
try {
await posts.changeOwner([1], '9999999');
} catch (err) {
assert.strictEqual(err.message, '[[error:no-user]]');
}
});
it('should fail to change owner if user is not authorized', async () => {
try {
await apiPosts.changeOwner({ uid: voterUid }, { pids: [1, 2], uid: voterUid });
} catch (err) {
assert.strictEqual(err.message, '[[error:no-privileges]]');
}
});
it('should return falsy if post does not exist', (done) => {
posts.getPostData(9999, (err, postData) => {
assert.ifError(err);
assert.equal(postData, null);
done();
});
});
describe('voting', () => {
it('should fail to upvote post if group does not have upvote permission', async () => {
await privileges.categories.rescind(['groups:posts:upvote', 'groups:posts:downvote'], cid, 'registered-users');
let err;
try {
await apiPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
} catch (_err) {
err = _err;
}
assert.equal(err.message, '[[error:no-privileges]]');
try {
await apiPosts.downvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
} catch (_err) {
err = _err;
}
assert.equal(err.message, '[[error:no-privileges]]');
await privileges.categories.give(['groups:posts:upvote', 'groups:posts:downvote'], cid, 'registered-users');
});
it('should upvote a post', async () => {
const result = await apiPosts.upvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
assert.equal(result.post.upvotes, 1);
assert.equal(result.post.downvotes, 0);
assert.equal(result.post.votes, 1);
assert.equal(result.user.reputation, 1);
const data = await posts.hasVoted(postData.pid, voterUid);
assert.equal(data.upvoted, true);
assert.equal(data.downvoted, false);
});
it('should add the pid to the :votes sorted set for that user', async () => {
const cid = await posts.getCidByPid(postData.pid);
const { uid, pid } = postData;
const score = await db.sortedSetScore(`cid:${cid}:uid:${uid}:pids:votes`, pid);
assert.strictEqual(score, 1);
});
it('should get voters', (done) => {
socketPosts.getVoters({ uid: globalModUid }, { pid: postData.pid, cid: cid }, (err, data) => {
assert.ifError(err);
assert.equal(data.upvoteCount, 1);
assert.equal(data.downvoteCount, 0);
assert(Array.isArray(data.upvoters));
assert.equal(data.upvoters[0].username, 'upvoter');
done();
});
});
it('should get upvoters', (done) => {
socketPosts.getUpvoters({ uid: globalModUid }, [postData.pid], (err, data) => {
assert.ifError(err);
assert.equal(data.otherCount, 0);
assert.equal(data.usernames, 'upvoter');
done();
});
});
it('should fail to get upvoters if user does not have read privilege', async () => {
await privileges.categories.rescind(['groups:topics:read'], cid, 'guests');
await assert.rejects(socketPosts.getUpvoters({ uid: 0 }, [postData.pid]), {
message: '[[error:no-privileges]]',
});
await privileges.categories.give(['groups:topics:read'], cid, 'guests');
});
it('should unvote a post', async () => {
const result = await apiPosts.unvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 0);
assert.equal(result.post.votes, 0);
assert.equal(result.user.reputation, 0);
const data = await posts.hasVoted(postData.pid, voterUid);
assert.equal(data.upvoted, false);
assert.equal(data.downvoted, false);
});
it('should downvote a post', async () => {
const result = await apiPosts.downvote({ uid: voterUid }, { pid: postData.pid, room_id: 'topic_1' });
assert.equal(result.post.upvotes, 0);
assert.equal(result.post.downvotes, 1);
assert.equal(result.post.votes, -1);
assert.equal(result.user.reputation, -1);
const data = await posts.hasVoted(postData.pid, voterUid);
assert.equal(data.upvoted, false);
assert.equal(data.downvoted, true);
});
it('should add the pid to the :votes sorted set for that user', async () => {
const cid = await posts.getCidByPid(postData.pid);
const { uid, pid } = postData;
const score = await db.sortedSetScore(`cid:${cid}:uid:${uid}:pids:votes`, pid);
assert.strictEqual(score, -1);
});
it('should prevent downvoting more than total daily limit', async () => {
const oldValue = meta.config.downvotesPerDay;
meta.config.downvotesPerDay = 1;
let err;
const p1 = await topics.reply({
uid: voteeUid,
tid: topicData.tid,
content: 'raw content',
});
try {
await apiPosts.downvote({ uid: voterUid }, { pid: p1.pid, room_id: 'topic_1' });
} catch (_err) {
err = _err;
}
assert.equal(err.message, '[[error:too-many-downvotes-today, 1]]');
meta.config.downvotesPerDay = oldValue;
});
it('should prevent downvoting target user more than total daily limit', async () => {
const oldValue = meta.config.downvotesPerUserPerDay;
meta.config.downvotesPerUserPerDay = 1;
let err;
const p1 = await topics.reply({
uid: voteeUid,
tid: topicData.tid,
content: 'raw content',
});
try {
await apiPosts.downvote({ uid: voterUid }, { pid: p1.pid, room_id: 'topic_1' });
} catch (_err) {
err = _err;
}
assert.equal(err.message, '[[error:too-many-downvotes-today-user, 1]]');
meta.config.downvotesPerUserPerDay = oldValue;
});
});
describe('bookmarking', () => {
it('should bookmark a post', async () => {
const data = await apiPosts.bookmark({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
assert.equal(data.isBookmarked, true);
const hasBookmarked = await posts.hasBookmarked(postData.pid, voterUid);
assert.equal(hasBookmarked, true);
});
it('should unbookmark a post', async () => {
const data = await apiPosts.unbookmark({ uid: voterUid }, { pid: postData.pid, room_id: `topic_${postData.tid}` });
assert.equal(data.isBookmarked, false);
const hasBookmarked = await posts.hasBookmarked([postData.pid], voterUid);
assert.equal(hasBookmarked[0], false);
});
});
describe('post tools', () => {
it('should error if data is invalid', (done) => {
socketPosts.loadPostTools({ uid: globalModUid }, null, (err) => {
assert.equal(err.message, '[[error:invalid-data]]');
done();
});
});
it('should load post tools', (done) => {
socketPosts.loadPostTools({ uid: globalModUid }, { pid: postData.pid, cid: cid }, (err, data) => {
assert.ifError(err);
assert(data.posts.display_edit_tools);
assert(data.posts.display_delete_tools);
assert(data.posts.display_moderator_tools);
assert(data.posts.display_move_tools);
done();
});
});
});
describe('delete/restore/purge', () => {
async function createTopicWithReply() {
const topicPostData = await topics.post({
uid: voterUid,
cid: cid,
title: 'topic to delete/restore/purge',
content: 'A post to delete/restore/purge',
});
const replyData = await topics.reply({
uid: voterUid,
tid: topicPostData.topicData.tid,
timestamp: Date.now(),
content: 'A post to delete/restore and purge',
});
return [topicPostData, replyData];
}
let tid;
let mainPid;
let replyPid;
before(async () => {
const [topicPostData, replyData] = await createTopicWithReply();
tid = topicPostData.topicData.tid;
mainPid = topicPostData.postData.pid;
replyPid = replyData.pid;
await privileges.categories.give(['groups:purge'], cid, 'registered-users');
});
it('should error with invalid data', async () => {
try {
await apiPosts.delete({ uid: voterUid }, null);
} catch (err) {
return assert.equal(err.message, '[[error:invalid-data]]');
}
assert(false);
});
it('should delete a post', async () => {
await apiPosts.delete({ uid: voterUid }, { pid: replyPid, tid: tid });
const isDeleted = await posts.getPostField(replyPid, 'deleted');
assert.strictEqual(isDeleted, 1);
});
it('should not see post content if global mod does not have posts:view_deleted privilege', async () => {
const uid = await user.create({ username: 'global mod', password: '123456' });
await groups.join('Global Moderators', uid);
await privileges.categories.rescind(['groups:posts:view_deleted'], cid, 'Global Moderators');
const { jar } = await helpers.loginUser('global mod', '123456');
const { body } = await request.get(`${nconf.get('url')}/api/topic/${tid}`, { jar });
assert.equal(body.posts[1].content, '[[topic:post-is-deleted]]');
await privileges.categories.give(['groups:posts:view_deleted'], cid, 'Global Moderators');
});
it('should restore a post', async () => {
await apiPosts.restore({ uid: voterUid }, { pid: replyPid, tid: tid });
const isDeleted = await posts.getPostField(replyPid, 'deleted');
assert.strictEqual(isDeleted, 0);
});
it('should delete topic if last main post is deleted', async () => {
const data = await topics.post({ uid: voterUid, cid: cid, title: 'test topic', content: 'test topic' });
await apiPosts.delete({ uid: globalModUid }, { pid: data.postData.pid });
const deleted = await topics.getTopicField(data.topicData.tid, 'deleted');
assert.strictEqual(deleted, 1);
});
it('should purge posts and purge topic', async () => {
const [topicPostData, replyData] = await createTopicWithReply();
await apiPosts.purge({ uid: voterUid }, { pid: replyData.pid });
await apiPosts.purge({ uid: voterUid }, { pid: topicPostData.postData.pid });
const pidExists = await posts.exists(replyData.pid);
assert.strictEqual(pidExists, false);
const tidExists = await topics.exists(topicPostData.topicData.tid);
assert.strictEqual(tidExists, false);
});
});
describe('edit', () => {
let pid;
let replyPid;
let tid;
before((done) => {
topics.post({
uid: voterUid,
cid: cid,
title: 'topic to edit',
content: 'A post to edit',
tags: ['nodebb'],
}, (err, data) => {
assert.ifError(err);
pid = data.postData.pid;
tid = data.topicData.tid;
topics.reply({
uid: voterUid,
tid: tid,
timestamp: Date.now(),
content: 'A reply to edit',
}, (err, data) => {
assert.ifError(err);
replyPid = data.pid;
privileges.categories.give(['groups:posts:edit'], cid, 'registered-users', done);
});
});
});
it('should error if user is not logged in', async () => {
try {
await apiPosts.edit({ uid: 0 }, { pid: pid, content: 'gg' });
} catch (err) {
return assert.equal(err.message, '[[error:not-logged-in]]');
}
assert(false);
});
it('should error if data is invalid or missing', async () => {
try {
await apiPosts.edit({ uid: voterUid }, {});
} catch (err) {
return assert.equal(err.message, '[[error:invalid-data]]');
}
assert(false);
});
it('should error if title is too short', async () => {
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: 'a' });
} catch (err) {
return assert.equal(err.message, `[[error:title-too-short, ${meta.config.minimumTitleLength}]]`);
}
assert(false);
});
it('should error if title is too long', async () => {
const longTitle = new Array(meta.config.maximumTitleLength + 2).join('a');
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: longTitle });
} catch (err) {
return assert.equal(err.message, `[[error:title-too-long, ${meta.config.maximumTitleLength}]]`);
}
assert(false);
});
it('should error with too few tags', async () => {
const oldValue = meta.config.minimumTagsPerTopic;
meta.config.minimumTagsPerTopic = 1;
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: [] });
} catch (err) {
assert.equal(err.message, `[[error:not-enough-tags, ${meta.config.minimumTagsPerTopic}]]`);
meta.config.minimumTagsPerTopic = oldValue;
return;
}
assert(false);
});
it('should error with too many tags', async () => {
const tags = [];
for (let i = 0; i < meta.config.maximumTagsPerTopic + 1; i += 1) {
tags.push(`tag${i}`);
}
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: tags });
} catch (err) {
return assert.equal(err.message, `[[error:too-many-tags, ${meta.config.maximumTagsPerTopic}]]`);
}
assert(false);
});
it('should error if content is too short', async () => {
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'e' });
} catch (err) {
return assert.equal(err.message, `[[error:content-too-short, ${meta.config.minimumPostLength}]]`);
}
assert(false);
});
it('should error if content is too long', async () => {
const longContent = new Array(meta.config.maximumPostLength + 2).join('a');
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: longContent });
} catch (err) {
return assert.equal(err.message, `[[error:content-too-long, ${meta.config.maximumPostLength}]]`);
}
assert(false);
});
it('should edit post', async () => {
const data = await apiPosts.edit({ uid: voterUid }, {
pid: pid,
content: 'edited post content',
title: 'edited title',
tags: ['edited'],
});
assert.strictEqual(data.content, 'edited post content');
assert.strictEqual(data.editor, voterUid);
assert.strictEqual(data.topic.title, 'edited title');
assert.strictEqual(data.topic.tags[0].value, 'edited');
const res = await db.getObject(`post:${pid}`);
assert(!res.hasOwnProperty('bookmarks'));
});
it('should disallow post editing for new users if post was made past the threshold for editing', async () => {
meta.config.newbiePostEditDuration = 1;
await sleep(1000);
try {
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content again', title: 'edited title again', tags: ['edited-twice'] });
} catch (err) {
assert.equal(err.message, '[[error:post-edit-duration-expired, 1]]');
meta.config.newbiePostEditDuration = 3600;
return;
}
assert(false);
});
it('should edit a deleted post', async () => {
await apiPosts.delete({ uid: voterUid }, { pid: pid, tid: tid });
const data = await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited deleted content', title: 'edited deleted title', tags: ['deleted'] });
assert.equal(data.content, 'edited deleted content');
assert.equal(data.editor, voterUid);
assert.equal(data.topic.title, 'edited deleted title');
assert.equal(data.topic.tags[0].value, 'deleted');
});
it('should edit a reply post', async () => {
const data = await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'edited reply' });
assert.equal(data.content, 'edited reply');
assert.equal(data.editor, voterUid);
assert.equal(data.topic.isMainPost, false);
assert.equal(data.topic.renamed, false);
});
it('should return diffs', (done) => {
posts.diffs.get(replyPid, 0, (err, data) => {
assert.ifError(err);
assert(Array.isArray(data));
assert(data[0].pid, replyPid);
assert(data[0].patch);
done();
});
});
it('should load diffs and reconstruct post', (done) => {
posts.diffs.load(replyPid, 0, voterUid, (err, data) => {
assert.ifError(err);
assert.equal(data.content, 'A reply to edit');
done();
});
});
it('should not allow guests to view diffs', async () => {
let err = {};
try {
await apiPosts.getDiffs({ uid: 0 }, { pid: 1 });
} catch (_err) {
err = _err;
}
assert.strictEqual(err.message, '[[error:no-privileges]]');
});
it('should allow registered-users group to view diffs', async () => {
const data = await apiPosts.getDiffs({ uid: 1 }, { pid: 1 });
assert.strictEqual('boolean', typeof data.editable);
assert.strictEqual(false, data.editable);
assert.equal(true, Array.isArray(data.timestamps));
assert.strictEqual(1, data.timestamps.length);
assert.equal(true, Array.isArray(data.revisions));
assert.strictEqual(data.timestamps.length, data.revisions.length);
['timestamp', 'username'].every(prop => Object.keys(data.revisions[0]).includes(prop));
});
it('should not delete first diff of a post', async () => {
const timestamps = await posts.diffs.list(replyPid);
await assert.rejects(
posts.diffs.delete(replyPid, timestamps[0], voterUid),
{ message: '[[error:invalid-data]]' }
);
});
it('should delete a post diff', async () => {
await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' });
await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' });
const timestamp = (await posts.diffs.list(replyPid)).pop();
await posts.diffs.delete(replyPid, timestamp, voterUid);
const differentTimestamp = (await posts.diffs.list(replyPid)).pop();
assert.notStrictEqual(timestamp, differentTimestamp);
});
it('should load (oldest) diff and reconstruct post correctly after a diff deletion', async () => {
const data = await posts.diffs.load(replyPid, 0, voterUid);
assert.strictEqual(data.content, 'A reply to edit');
});
});
describe('move', () => {
let replyPid;
let tid;
let moveTid;
before(async () => {
const topic1 = await topics.post({
uid: voterUid,
cid: cid,
title: 'topic 1',
content: 'some content',
});
tid = topic1.topicData.tid;
const topic2 = await topics.post({
uid: voterUid,
cid: cid,
title: 'topic 2',
content: 'some content',
});
moveTid = topic2.topicData.tid;
const reply = await topics.reply({
uid: voterUid,
tid: tid,
timestamp: Date.now(),
content: 'A reply to move',
});
replyPid = reply.pid;
});
it('should error if uid is not logged in', async () => {
try {
await apiPosts.move({ uid: 0 }, {});
} catch (err) {
return assert.equal(err.message, '[[error:not-logged-in]]');
}
assert(false);
});
it('should error if data is invalid', async () => {
try {
await apiPosts.move({ uid: globalModUid }, {});
} catch (err) {
return assert.equal(err.message, '[[error:invalid-data]]');
}
assert(false);
});
it('should error if user does not have move privilege', async () => {
try {
await apiPosts.move({ uid: voterUid }, { pid: replyPid, tid: moveTid });
} catch (err) {
return assert.equal(err.message, '[[error:no-privileges]]');
}
assert(false);
});
it('should move a post', async () => {
await apiPosts.move({ uid: globalModUid }, { pid: replyPid, tid: moveTid });
const tid = await posts.getPostField(replyPid, 'tid');
assert(tid, moveTid);
});
it('should fail to move post if not moderator of target category', async () => {
const cat1 = await categories.create({ name: 'Test Category', description: 'Test category created by testing script' });
const cat2 = await categories.create({ name: 'Test Category', description: 'Test category created by testing script' });
const result = await apiTopics.create({ uid: globalModUid }, { title: 'target topic', content: 'queued topic', cid: cat2.cid });
const modUid = await user.create({ username: 'modofcat1' });
const userPrivilegeList = await privileges.categories.getUserPrivilegeList();
await privileges.categories.give(userPrivilegeList, cat1.cid, modUid);
let err;
try {
await apiPosts.move({ uid: modUid }, { pid: replyPid, tid: result.tid });
} catch (_err) {
err = _err;
}
assert.strictEqual(err.message, '[[error:no-privileges]]');
});
});
describe('getPostSummaryByPids', () => {
it('should return empty array for empty pids', (done) => {
posts.getPostSummaryByPids([], 0, {}, (err, data) => {
assert.ifError(err);
assert.equal(data.length, 0);
done();
});
});
it('should get post summaries', (done) => {
posts.getPostSummaryByPids([postData.pid], 0, {}, (err, data) => {
assert.ifError(err);
assert(data[0].user);
assert(data[0].topic);
assert(data[0].category);
done();
});
});
});
it('should get recent poster uids', (done) => {
topics.reply({
uid: voterUid,
tid: topicData.tid,
timestamp: Date.now(),
content: 'some content',
}, (err) => {
assert.ifError(err);
posts.getRecentPosterUids(0, 1, (err, uids) => {
assert.ifError(err);
assert(Array.isArray(uids));
assert.equal(uids.length, 2);
assert.equal(uids[0], voterUid);
done();
});
});
});
describe('parse', () => {
it('should not crash and return falsy if post data is falsy', (done) => {
posts.parsePost(null, (err, postData) => {
assert.ifError(err);
assert.strictEqual(postData, null);
done();
});
});
it('should store post content in cache', (done) => {
const oldValue = global.env;
global.env = 'production';
const postData = {
pid: 9999,
content: 'some post content',
};
posts.parsePost(postData, (err) => {
assert.ifError(err);
posts.parsePost(postData, (err) => {
assert.ifError(err);
global.env = oldValue;
done();
});
});
});
it('should parse signature and remove links and images', (done) => {
meta.config['signatures:disableLinks'] = 1;
meta.config['signatures:disableImages'] = 1;
const userData = {
signature: '<img src="boop"/><a href="link">test</a> derp',
};
posts.parseSignature(userData, 1, (err, data) => {
assert.ifError(err);
assert.equal(data.userData.signature, 'test derp');
meta.config['signatures:disableLinks'] = 0;
meta.config['signatures:disableImages'] = 0;
done();
});
});
it('should turn relative links in post body to absolute urls', (done) => {
const nconf = require('nconf');
const content = '<a href="/users">test</a> <a href="//youtube.com">youtube</a>';
const parsedContent = posts.relativeToAbsolute(content, posts.urlRegex);
assert.equal(parsedContent, `<a href="${nconf.get('base_url')}/users">test</a> <a href="${nconf.get('url_parsed').protocol}//youtube.com/">youtube</a>`);
done();
});
it('should turn relative links in post body to absolute urls', (done) => {
const nconf = require('nconf');
const content = '<a href="/users">test</a> <a href="//youtube.com">youtube</a> some test <img src="/path/to/img"/>';
let parsedContent = posts.relativeToAbsolute(content, posts.urlRegex);
parsedContent = posts.relativeToAbsolute(parsedContent, posts.imgRegex);
assert.equal(parsedContent, `<a href="${nconf.get('base_url')}/users">test</a> <a href="${nconf.get('url_parsed').protocol}//youtube.com/">youtube</a> some test <img src="${nconf.get('base_url')}/path/to/img"/>`);
done();
});
});
describe('socket methods', () => {
let pid;
before((done) => {
topics.reply({
uid: voterUid,
tid: topicData.tid,
timestamp: Date.now(),
content: 'raw content',
}, (err, postData) => {
assert.ifError(err);
pid = postData.pid;
privileges.categories.rescind(['groups:topics:read'], cid, 'guests', done);
});
});
it('should error with invalid data', async () => {
try {
await apiTopics.reply({ uid: 0 }, null);
assert(false);
} catch (err) {
assert.equal(err.message, '[[error:invalid-data]]');
}
});
it('should error with invalid tid', async () => {
try {
await apiTopics.reply({ uid: 0 }, { tid: 0, content: 'derp' });
assert(false);
} catch (err) {
assert.equal(err.message, '[[error:invalid-data]]');
}
});
it('should fail to get raw post because of privilege', async () => {
const content = await apiPosts.getRaw({ uid: 0 }, { pid });
assert.strictEqual(content, null);
});
it('should fail to get raw post because post is deleted', async () => {
await posts.setPostField(pid, 'deleted', 1);
const content = await apiPosts.getRaw({ uid: voterUid }, { pid });
assert.strictEqual(content, null);
});
it('should allow privileged users to view the deleted post\'s raw content', async () => {
await posts.setPostField(pid, 'deleted', 1);
const content = await apiPosts.getRaw({ uid: globalModUid }, { pid });
assert.strictEqual(content, 'raw content');
});
it('should get raw post content', async () => {
await posts.setPostField(pid, 'deleted', 0);
const postContent = await apiPosts.getRaw({ uid: voterUid }, { pid });
assert.equal(postContent, 'raw content');
});
it('should get post', async () => {
const postData = await apiPosts.get({ uid: voterUid }, { pid });
assert(postData);
});
it('should get post summary', async () => {
const summary = await apiPosts.getSummary({ uid: voterUid }, { pid });
assert(summary);
});
it('should get raw post content', async () => {
const postContent = await socketPosts.getRawPost({ uid: voterUid }, pid);
assert.equal(postContent, 'raw content');
});
it('should get post summary by index', async () => {
const summary = await socketPosts.getPostSummaryByIndex({ uid: voterUid }, {
index: 1,
tid: topicData.tid,
});
assert(summary);
});
it('should get post timestamp by index', async () => {
const timestamp = await socketPosts.getPostTimestampByIndex({ uid: voterUid }, {
index: 1,
tid: topicData.tid,
});
assert(utils.isNumber(timestamp));
});
it('should get post timestamp by index', async () => {
const summary = await socketPosts.getPostSummaryByPid({ uid: voterUid }, {
pid: pid,
});
assert(summary);
});
it('should get post category', async () => {
const postCid = await socketPosts.getCategory({ uid: voterUid }, pid);
assert.equal(cid, postCid);
});
it('should get pid index', async () => {
const index = await socketPosts.getPidIndex({ uid: voterUid }, { pid: pid, tid: topicData.tid, topicPostSort: 'oldest_to_newest' });
assert.equal(index, 4);
});
it('should get pid index', async () => {
const index = await apiPosts.getIndex({ uid: voterUid }, { pid: pid, sort: 'oldest_to_newest' });
assert.strictEqual(index, 4);
});
it('should get pid index in reverse', async () => {
const postData = await topics.reply({
uid: voterUid,
tid: topicData.tid,
content: 'raw content',
});
const index = await apiPosts.getIndex({ uid: voterUid }, { pid: postData.pid, sort: 'newest_to_oldest' });
assert.equal(index, 1);
});
});
describe('filterPidsByCid', () => {
it('should return pids as is if cid is falsy', (done) => {
posts.filterPidsByCid([1, 2, 3], null, (err, pids) => {
assert.ifError(err);
assert.deepEqual([1, 2, 3], pids);
done();
});
});
it('should filter pids by single cid', (done) => {
posts.filterPidsByCid([postData.pid, 100, 101], cid, (err, pids) => {
assert.ifError(err);
assert.deepEqual([postData.pid], pids);
done();
});
});
it('should filter pids by multiple cids', (done) => {
posts.filterPidsByCid([postData.pid, 100, 101], [cid, 2, 3], (err, pids) => {
assert.ifError(err);
assert.deepEqual([postData.pid], pids);
done();
});
});
it('should filter pids by multiple cids', (done) => {
posts.filterPidsByCid([postData.pid, 100, 101], [cid], (err, pids) => {
assert.ifError(err);
assert.deepEqual([postData.pid], pids);
done();
});
});
});
describe('anonymous posting', () => {
let anonUid1;
let anonUid2;
let anonTopic;
let anonReply1;
let anonReply2;
let anonReply3;
let modJar;
before(async () => {
({ jar: modJar } = await helpers.loginUser('globalmod', 'globalmodpwd'));
anonUid1 = await user.create({ username: 'anonposter1' });
anonUid2 = await user.create({ username: 'anonposter2' });
anonTopic = await apiTopics.create(
{ uid: anonUid1 },
{
title: 'anonymous topic',
content: 'anonymous topic content',
cid: cid,
isAnonymous: true,
}
);
anonReply1 = await apiTopics.reply(
{ uid: anonUid1 },
{
tid: anonTopic.tid,
content: 'anonymous reply one',
isAnonymous: true,
}
);
anonReply2 = await apiTopics.reply(
{ uid: anonUid1 },
{
tid: anonTopic.tid,
content: 'anonymous reply two',
isAnonymous: true,
}
);
anonReply3 = await apiTopics.reply(
{ uid: anonUid2 },
{
tid: anonTopic.tid,
content: 'anonymous reply three',
isAnonymous: true,
}
);
});
it('should create anonymous posts and keep identidies hidden', async () => {
const [reply1Data, reply2Data, reply3Data] = await Promise.all([
posts.getPostFields(anonReply1.pid, ['uid', 'tid', 'isAnonymous', 'realUid', 'anonymousAliasId']),
posts.getPostFields(anonReply2.pid, ['uid', 'tid', 'isAnonymous', 'realUid', 'anonymousAliasId']),
posts.getPostFields(anonReply3.pid, ['uid', 'tid', 'isAnonymous', 'realUid', 'anonymousAliasId']),
]);
assert.strictEqual(reply1Data.uid, 0);
assert.strictEqual(reply1Data.isAnonymous, 1);
assert.strictEqual(reply1Data.realUid, anonUid1);
assert(reply1Data.anonymousAliasId > 0);
assert.strictEqual(reply2Data.uid, 0);
assert.strictEqual(reply2Data.isAnonymous, 1);