-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmisc_test.go
3325 lines (2870 loc) · 112 KB
/
misc_test.go
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
package main
import (
"bytes"
"database/sql"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"runtime/debug"
"strconv"
"testing"
"time"
c "github.com/Azareal/Gosora/common"
"github.com/Azareal/Gosora/common/gauth"
"github.com/Azareal/Gosora/common/phrases"
"github.com/Azareal/Gosora/routes"
"github.com/pkg/errors"
)
func miscinit(t *testing.T) {
if err := gloinit(); err != nil {
t.Fatal(err)
}
}
func recordMustExist(t *testing.T, err error, errmsg string, args ...interface{}) {
if err == ErrNoRows {
debug.PrintStack()
t.Errorf(errmsg, args...)
} else if err != nil {
debug.PrintStack()
t.Fatal(err)
}
}
func recordMustNotExist(t *testing.T, err error, errmsg string, args ...interface{}) {
if err == nil {
debug.PrintStack()
t.Errorf(errmsg, args...)
} else if err != ErrNoRows {
debug.PrintStack()
t.Fatal(err)
}
}
func TestUserStore(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
var err error
uc := c.NewMemoryUserCache(c.Config.UserCacheCapacity)
c.Users, err = c.NewDefaultUserStore(uc)
expectNilErr(t, err)
uc.Flush()
userStoreTest(t, 2)
c.Users, err = c.NewDefaultUserStore(nil)
expectNilErr(t, err)
userStoreTest(t, 5)
}
func userStoreTest(t *testing.T, newUserID int) {
uc := c.Users.GetCache()
// Go doesn't have short-circuiting, so this'll allow us to do one liner tests
cacheLength := func(uc c.UserCache) int {
if uc == nil {
return 0
}
return uc.Length()
}
isCacheLengthZero := func(uc c.UserCache) bool {
return cacheLength(uc) == 0
}
ex, exf := exp(t), expf(t)
exf(isCacheLengthZero(uc), "The initial ucache length should be zero, not %d", cacheLength(uc))
_, err := c.Users.Get(-1)
recordMustNotExist(t, err, "UID #-1 shouldn't exist")
exf(isCacheLengthZero(uc), "We found %d items in the user cache and it's supposed to be empty", cacheLength(uc))
_, err = c.Users.Get(0)
recordMustNotExist(t, err, "UID #0 shouldn't exist")
exf(isCacheLengthZero(uc), "We found %d items in the user cache and it's supposed to be empty", cacheLength(uc))
user, err := c.Users.Get(1)
recordMustExist(t, err, "Couldn't find UID #1")
expectW := func(cond, expec bool, prefix, suffix string) {
midfix := "should not be"
if expec {
midfix = "should be"
}
ex(cond, prefix+" "+midfix+" "+suffix)
}
// TODO: Add email checks too? Do them separately?
expectUser := func(u *c.User, uid int, name string, group int, super, admin, mod, banned bool) {
exf(u.ID == uid, "u.ID should be %d. Got '%d' instead.", uid, u.ID)
exf(u.Name == name, "u.Name should be '%s', not '%s'", name, u.Name)
expectW(u.Group == group, true, u.Name, "in group"+strconv.Itoa(group))
expectW(u.IsSuperAdmin == super, super, u.Name, "a super admin")
expectW(u.IsAdmin == admin, admin, u.Name, "an admin")
expectW(u.IsSuperMod == mod, mod, u.Name, "a super mod")
expectW(u.IsMod == mod, mod, u.Name, "a mod")
expectW(u.IsBanned == banned, banned, u.Name, "banned")
}
expectUser(user, 1, "Admin", 1, true, true, true, false)
user, err = c.Users.GetByName("Admin")
recordMustExist(t, err, "Couldn't find user 'Admin'")
expectUser(user, 1, "Admin", 1, true, true, true, false)
us, err := c.Users.BulkGetByName([]string{"Admin"})
recordMustExist(t, err, "Couldn't find user 'Admin'")
exf(len(us) == 1, "len(us) should be 1, not %d", len(us))
expectUser(us[0], 1, "Admin", 1, true, true, true, false)
_, err = c.Users.Get(newUserID)
recordMustNotExist(t, err, fmt.Sprintf("UID #%d shouldn't exist", newUserID))
// TODO: GetByName tests for newUserID
if uc != nil {
expectIntToBeX(t, uc.Length(), 1, "User cache length should be 1, not %d")
_, err = uc.Get(-1)
recordMustNotExist(t, err, "UID #-1 shouldn't exist, even in the cache")
_, err = uc.Get(0)
recordMustNotExist(t, err, "UID #0 shouldn't exist, even in the cache")
user, err = uc.Get(1)
recordMustExist(t, err, "Couldn't find UID #1 in the cache")
exf(user.ID == 1, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
exf(user.Name == "Admin", "user.Name should be 'Admin', not '%s'", user.Name)
_, err = uc.Get(newUserID)
recordMustNotExist(t, err, "UID #%d shouldn't exist, even in the cache", newUserID)
uc.Flush()
expectIntToBeX(t, uc.Length(), 0, "User cache length should be 0, not %d")
}
// TODO: Lock onto the specific error type. Is this even possible without sacrificing the detailed information in the error message?
bulkGetMapEmpty := func(id int) {
userList, _ := c.Users.BulkGetMap([]int{id})
exf(len(userList) == 0, "The userList length should be 0, not %d", len(userList))
exf(isCacheLengthZero(uc), "User cache length should be 0, not %d", cacheLength(uc))
}
bulkGetMapEmpty(-1)
bulkGetMapEmpty(0)
userList, _ := c.Users.BulkGetMap([]int{1})
exf(len(userList) == 1, "Returned map should have one result (UID #1), not %d", len(userList))
user, ok := userList[1]
if !ok {
t.Error("We couldn't find UID #1 in the returned map")
t.Error("userList", userList)
return
}
exf(user.ID == 1, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
if uc != nil {
expectIntToBeX(t, uc.Length(), 1, "User cache length should be 1, not %d")
user, err = uc.Get(1)
recordMustExist(t, err, "Couldn't find UID #1 in the cache")
exf(user.ID == 1, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
uc.Flush()
}
ex(!c.Users.Exists(-1), "UID #-1 shouldn't exist")
ex(!c.Users.Exists(0), "UID #0 shouldn't exist")
ex(c.Users.Exists(1), "UID #1 should exist")
exf(!c.Users.Exists(newUserID), "UID #%d shouldn't exist", newUserID)
exf(isCacheLengthZero(uc), "User cache length should be 0, not %d", cacheLength(uc))
expectIntToBeX(t, c.Users.Count(), 1, "The number of users should be 1, not %d")
searchUser := func(name, email string, gid, count int) {
f := func(name, email string, gid, count int, m string) {
expectIntToBeX(t, c.Users.CountSearch(name, email, gid), count, "The number of users for "+m+", not %d")
}
f(name, email, 0, count, fmt.Sprintf("name '%s' and email '%s' should be %d", name, email, count))
f(name, "", 0, count, fmt.Sprintf("name '%s' should be %d", name, count))
f("", email, 0, count, fmt.Sprintf("email '%s' should be %d", email, count))
f2 := func(name, email string, gid, offset int, m string, args ...interface{}) {
ulist, err := c.Users.SearchOffset(name, email, gid, offset, 15)
expectNilErr(t, err)
expectIntToBeX(t, len(ulist), count, "The number of users for "+fmt.Sprintf(m, args...)+", not %d")
}
f2(name, email, 0, 0, "name '%s' and email '%s' should be %d", name, email, count)
f2(name, "", 0, 0, "name '%s' should be %d", name, count)
f2("", email, 0, 0, "email '%s' should be %d", email, count)
count = 0
f2(name, email, 0, 10, "name '%s' and email '%s' should be %d", name, email, count)
f2(name, "", 0, 10, "name '%s' should be %d", name, count)
f2("", email, 0, 10, "email '%s' should be %d", email, count)
f2(name, email, 999, 0, "name '%s' and email '%s' should be %d", name, email, 0)
f2(name, "", 999, 0, "name '%s' should be %d", name, 0)
f2("", email, 999, 0, "email '%s' should be %d", email, 0)
f2(name, email, 999, 10, "name '%s' and email '%s' should be %d", name, email, 0)
f2(name, "", 999, 10, "name '%s' should be %d", name, 0)
f2("", email, 999, 10, "email '%s' should be %d", email, 0)
}
searchUser("Sam", "[email protected]", 0, 0)
// TODO: CountSearch gid test
awaitingActivation := 5
// TODO: Write tests for the registration validators
uid, err := c.Users.Create("Sam", "ReallyBadPassword", "[email protected]", awaitingActivation, false)
expectNilErr(t, err)
exf(uid == newUserID, "The UID of the new user should be %d not %d", newUserID, uid)
exf(c.Users.Exists(newUserID), "UID #%d should exist", newUserID)
expectIntToBeX(t, c.Users.Count(), 2, "The number of users should be 2, not %d")
searchUser("Sam", "[email protected]", 0, 1)
// TODO: CountSearch gid test
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectUser(user, newUserID, "Sam", 5, false, false, false, false)
if uc != nil {
expectIntToBeX(t, uc.Length(), 1, "User cache length should be 1, not %d")
user, err = uc.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d in the cache", newUserID)
exf(user.ID == newUserID, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
}
userList, _ = c.Users.BulkGetMap([]int{1, uid})
exf(len(userList) == 2, "Returned map should have 2 results, not %d", len(userList))
// TODO: More tests on userList
{
userList, _ := c.Users.BulkGetByName([]string{"Admin", "Sam"})
exf(len(userList) == 2, "Returned list should have 2 results, not %d", len(userList))
}
if uc != nil {
expectIntToBeX(t, uc.Length(), 2, "User cache length should be 2, not %d")
user, err = uc.Get(1)
recordMustExist(t, err, "Couldn't find UID #%d in the cache", 1)
exf(user.ID == 1, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
user, err = uc.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d in the cache", newUserID)
exf(user.ID == newUserID, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
uc.Flush()
}
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectUser(user, newUserID, "Sam", 5, false, false, false, false)
if uc != nil {
expectIntToBeX(t, uc.Length(), 1, "User cache length should be 1, not %d")
user, err = uc.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d in the cache", newUserID)
exf(user.ID == newUserID, "user.ID does not match the requested UID. Got '%d' instead.", user.ID)
}
expectNilErr(t, user.Activate())
expectIntToBeX(t, user.Group, 5, "Sam should still be in group 5 in this copy")
// ? - What if we change the caching mechanism so it isn't hard purged and reloaded? We'll deal with that when we come to it, but for now, this is a sign of a cache bug
afterUserFlush := func(uid int) {
if uc != nil {
expectIntToBeX(t, uc.Length(), 0, "User cache length should be 0, not %d")
_, err = uc.Get(uid)
recordMustNotExist(t, err, "UID #%d shouldn't be in the cache", uid)
}
}
afterUserFlush(newUserID)
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectUser(user, newUserID, "Sam", c.Config.DefaultGroup, false, false, false, false)
// Permanent ban
duration, _ := time.ParseDuration("0")
// TODO: Attempt a double ban, double activation, and double unban
expectNilErr(t, user.Ban(duration, 1))
exf(user.Group == c.Config.DefaultGroup, "Sam should be in group %d, not %d", c.Config.DefaultGroup, user.Group)
afterUserFlush(newUserID)
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectUser(user, newUserID, "Sam", c.BanGroup, false, false, false, true)
// TODO: Do tests against the scheduled updates table and the task system to make sure the ban exists there and gets revoked when it should
expectNilErr(t, user.Unban())
expectIntToBeX(t, user.Group, c.BanGroup, "Sam should still be in the ban group in this copy")
afterUserFlush(newUserID)
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
expectUser(user, newUserID, "Sam", c.Config.DefaultGroup, false, false, false, false)
reportsForumID := 1 // TODO: Use the constant in common?
generalForumID := 2
dummyResponseRecorder := httptest.NewRecorder()
bytesBuffer := bytes.NewBuffer([]byte(""))
dummyRequest1 := httptest.NewRequest("", "/forum/"+strconv.Itoa(reportsForumID), bytesBuffer)
dummyRequest2 := httptest.NewRequest("", "/forum/"+strconv.Itoa(generalForumID), bytesBuffer)
var user2 *c.User
changeGroupTest := func(oldGroup, newGroup int) {
expectNilErr(t, user.ChangeGroup(newGroup))
// ! I don't think ChangeGroup should be changing the value of user... Investigate this.
ex(oldGroup == user.Group, "Someone's mutated this pointer elsewhere")
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
user2 = c.BlankUser()
*user2 = *user
}
changeGroupTest2 := func(rank string, firstShouldBe, secondShouldBe bool) {
head, e := c.UserCheck(dummyResponseRecorder, dummyRequest1, user)
if e != nil {
t.Fatal(e)
}
head2, e := c.UserCheck(dummyResponseRecorder, dummyRequest2, user2)
if e != nil {
t.Fatal(e)
}
ferr := c.ForumUserCheck(head, dummyResponseRecorder, dummyRequest1, user, reportsForumID)
ex(ferr == nil, "There shouldn't be any errors in forumUserCheck")
ex(user.Perms.ViewTopic == firstShouldBe, rank+" should be able to access the reports forum")
ferr = c.ForumUserCheck(head2, dummyResponseRecorder, dummyRequest2, user2, generalForumID)
ex(ferr == nil, "There shouldn't be any errors in forumUserCheck")
ex(user2.Perms.ViewTopic == secondShouldBe, "Sam should be able to access the general forum")
}
changeGroupTest(c.Config.DefaultGroup, 1)
expectUser(user, newUserID, "Sam", 1, false, true, true, false)
changeGroupTest2("Admins", true, true)
changeGroupTest(1, 2)
expectUser(user, newUserID, "Sam", 2, false, false, true, false)
changeGroupTest2("Mods", true, true)
changeGroupTest(2, 3)
expectUser(user, newUserID, "Sam", 3, false, false, false, false)
changeGroupTest2("Members", false, true)
ex(user.Perms.ViewTopic != user2.Perms.ViewTopic, "user.Perms.ViewTopic and user2.Perms.ViewTopic should never match")
changeGroupTest(3, 4)
expectUser(user, newUserID, "Sam", 4, false, false, false, true)
changeGroupTest2("Members", false, true)
changeGroupTest(4, 5)
expectUser(user, newUserID, "Sam", 5, false, false, false, false)
changeGroupTest2("Members", false, true)
changeGroupTest(5, 6)
expectUser(user, newUserID, "Sam", 6, false, false, false, false)
changeGroupTest2("Members", false, true)
err = user.ChangeGroup(c.Config.DefaultGroup)
expectNilErr(t, err)
ex(user.Group == 6, "Someone's mutated this pointer elsewhere")
exf(user.LastIP == "", "user.LastIP should be blank not %s", user.LastIP)
expectNilErr(t, user.UpdateIP("::1"))
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
exf(user.LastIP == "::1", "user.LastIP should be %s not %s", "::1", user.LastIP)
expectNilErr(t, c.Users.ClearLastIPs())
expectNilErr(t, c.Users.Reload(newUserID))
user, err = c.Users.Get(newUserID)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID)
exf(user.LastIP == "", "user.LastIP should be blank not %s", user.LastIP)
expectNilErr(t, user.Delete())
exf(!c.Users.Exists(newUserID), "UID #%d should no longer exist", newUserID)
afterUserFlush(newUserID)
expectIntToBeX(t, c.Users.Count(), 1, "The number of users should be 1, not %d")
searchUser("Sam", "[email protected]", 0, 0)
// TODO: CountSearch gid test
_, err = c.Users.Get(newUserID)
recordMustNotExist(t, err, "UID #%d shouldn't exist", newUserID)
// And a unicode test, even though I doubt it'll fail
uid, err = c.Users.Create("サム", "😀😀😀", "[email protected]", awaitingActivation, false)
expectNilErr(t, err)
exf(uid == newUserID+1, "The UID of the new user should be %d", newUserID+1)
exf(c.Users.Exists(newUserID+1), "UID #%d should exist", newUserID+1)
user, err = c.Users.Get(newUserID + 1)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID+1)
expectUser(user, newUserID+1, "サム", 5, false, false, false, false)
expectNilErr(t, user.Delete())
exf(!c.Users.Exists(newUserID+1), "UID #%d should no longer exist", newUserID+1)
// MySQL utf8mb4 username test
uid, err = c.Users.Create("😀😀😀", "😀😀😀", "[email protected]", awaitingActivation, false)
expectNilErr(t, err)
exf(uid == newUserID+2, "The UID of the new user should be %d", newUserID+2)
exf(c.Users.Exists(newUserID+2), "UID #%d should exist", newUserID+2)
user, err = c.Users.Get(newUserID + 2)
recordMustExist(t, err, "Couldn't find UID #%d", newUserID+1)
expectUser(user, newUserID+2, "😀😀😀", 5, false, false, false, false)
expectNilErr(t, user.Delete())
exf(!c.Users.Exists(newUserID+2), "UID #%d should no longer exist", newUserID+2)
// TODO: Add unicode login tests somewhere? Probably with the rest of the auth tests
// TODO: Add tests for the Cache* methods
}
// TODO: Add an error message to this?
func expectNilErr(t *testing.T, item error) {
if item != nil {
debug.PrintStack()
t.Fatal(item)
}
}
func expectIntToBeX(t *testing.T, item, expect int, errmsg string) {
if item != expect {
debug.PrintStack()
t.Fatalf(errmsg, item)
}
}
func expect(t *testing.T, item bool, errmsg string) {
if !item {
debug.PrintStack()
t.Fatal(errmsg)
}
}
func expectf(t *testing.T, item bool, errmsg string, args ...interface{}) {
if !item {
debug.PrintStack()
t.Fatalf(errmsg, args...)
}
}
func exp(t *testing.T) func(bool, string) {
return func(val bool, errmsg string) {
if !val {
debug.PrintStack()
t.Fatal(errmsg)
}
}
}
func expf(t *testing.T) func(bool, string, ...interface{}) {
return func(val bool, errmsg string, params ...interface{}) {
if !val {
debug.PrintStack()
t.Fatalf(errmsg, params...)
}
}
}
func TestPermsMiddleware(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
dummyResponseRecorder := httptest.NewRecorder()
bytesBuffer := bytes.NewBuffer([]byte(""))
dummyRequest := httptest.NewRequest("", "/forum/1", bytesBuffer)
user := c.BlankUser()
ex := exp(t)
f := func(ff func(w http.ResponseWriter, r *http.Request, u *c.User) c.RouteError) bool {
ferr := ff(dummyResponseRecorder, dummyRequest, user)
return ferr == nil
}
ex(!f(c.SuperModOnly), "Blank users shouldn't be supermods")
user.IsSuperMod = false
ex(!f(c.SuperModOnly), "Non-supermods shouldn't be allowed through supermod gates")
user.IsSuperMod = true
ex(f(c.SuperModOnly), "Supermods should be allowed through supermod gates")
// TODO: Loop over the Control Panel routes and make sure only supermods can get in
user = c.BlankUser()
ex(!f(c.MemberOnly), "Blank users shouldn't be considered loggedin")
user.Loggedin = false
ex(!f(c.MemberOnly), "Guests shouldn't be able to access member areas")
user.Loggedin = true
ex(f(c.MemberOnly), "Logged in users should be able to access member areas")
// TODO: Loop over the /user/ routes and make sure only members can access the ones other than /user/username
user = c.BlankUser()
ex(!f(c.AdminOnly), "Blank users shouldn't be considered admins")
user.IsAdmin = false
ex(!f(c.AdminOnly), "Non-admins shouldn't be able to access admin areas")
user.IsAdmin = true
ex(f(c.AdminOnly), "Admins should be able to access admin areas")
user = c.BlankUser()
ex(!f(c.SuperAdminOnly), "Blank users shouldn't be considered super admins")
user.IsSuperAdmin = false
ex(!f(c.SuperAdminOnly), "Non-super admins shouldn't be allowed through the super admin gate")
user.IsSuperAdmin = true
ex(f(c.SuperAdminOnly), "Super admins should be allowed through super admin gates")
// TODO: Make sure only super admins can access the backups route
//dummyResponseRecorder = httptest.NewRecorder()
//bytesBuffer = bytes.NewBuffer([]byte(""))
//dummyRequest = httptest.NewRequest("", "/panel/backups/", bytesBuffer)
}
func TestTopicStore(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
var err error
tcache := c.NewMemoryTopicCache(c.Config.TopicCacheCapacity)
c.Topics, err = c.NewDefaultTopicStore(tcache)
expectNilErr(t, err)
c.Config.DisablePostIP = false
topicStoreTest(t, 2, "::1")
c.Config.DisablePostIP = true
topicStoreTest(t, 3, "")
c.Topics, err = c.NewDefaultTopicStore(nil)
expectNilErr(t, err)
c.Config.DisablePostIP = false
topicStoreTest(t, 4, "::1")
c.Config.DisablePostIP = true
topicStoreTest(t, 5, "")
}
func topicStoreTest(t *testing.T, newID int, ip string) {
var topic *c.Topic
var err error
ex, exf := exp(t), expf(t)
_, err = c.Topics.Get(-1)
recordMustNotExist(t, err, "TID #-1 shouldn't exist")
_, err = c.Topics.Get(0)
recordMustNotExist(t, err, "TID #0 shouldn't exist")
topic, err = c.Topics.Get(1)
recordMustExist(t, err, "Couldn't find TID #1")
exf(topic.ID == 1, "topic.ID does not match the requested TID. Got '%d' instead.", topic.ID)
// TODO: Add BulkGetMap() to the TopicStore
ex(!c.Topics.Exists(-1), "TID #-1 shouldn't exist")
ex(!c.Topics.Exists(0), "TID #0 shouldn't exist")
ex(c.Topics.Exists(1), "TID #1 should exist")
count := c.Topics.Count()
exf(count == 1, "Global count for topics should be 1, not %d", count)
//Create(fid int, topicName string, content string, uid int, ip string) (tid int, err error)
tid, err := c.Topics.Create(2, "Test Topic", "Topic Content", 1, ip)
expectNilErr(t, err)
exf(tid == newID, "TID for the new topic should be %d, not %d", newID, tid)
exf(c.Topics.Exists(newID), "TID #%d should exist", newID)
count = c.Topics.Count()
exf(count == 2, "Global count for topics should be 2, not %d", count)
iFrag := func(cond bool) string {
if !cond {
return "n't"
}
return ""
}
testTopic := func(tid int, title, content string, createdBy int, ip string, parentID int, isClosed, sticky bool) {
topic, err = c.Topics.Get(tid)
recordMustExist(t, err, fmt.Sprintf("Couldn't find TID #%d", tid))
exf(topic.ID == tid, "topic.ID does not match the requested TID. Got '%d' instead.", topic.ID)
exf(topic.GetID() == tid, "topic.ID does not match the requested TID. Got '%d' instead.", topic.GetID())
exf(topic.Title == title, "The topic's name should be '%s', not %s", title, topic.Title)
exf(topic.Content == content, "The topic's body should be '%s', not %s", content, topic.Content)
exf(topic.CreatedBy == createdBy, "The topic's creator should be %d, not %d", createdBy, topic.CreatedBy)
exf(topic.IP == ip, "The topic's IP should be '%s', not %s", ip, topic.IP)
exf(topic.ParentID == parentID, "The topic's parent forum should be %d, not %d", parentID, topic.ParentID)
exf(topic.IsClosed == isClosed, "This topic should%s be locked", iFrag(topic.IsClosed))
exf(topic.Sticky == sticky, "This topic should%s be sticky", iFrag(topic.Sticky))
exf(topic.GetTable() == "topics", "The topic's table should be 'topics', not %s", topic.GetTable())
}
tc := c.Topics.GetCache()
shouldNotBeIn := func(tid int) {
if tc != nil {
_, err = tc.Get(tid)
recordMustNotExist(t, err, "Topic cache should be empty")
}
}
if tc != nil {
_, err = tc.Get(newID)
expectNilErr(t, err)
}
testTopic(newID, "Test Topic", "Topic Content", 1, ip, 2, false, false)
expectNilErr(t, topic.Lock())
shouldNotBeIn(newID)
testTopic(newID, "Test Topic", "Topic Content", 1, ip, 2, true, false)
expectNilErr(t, topic.Unlock())
shouldNotBeIn(newID)
testTopic(newID, "Test Topic", "Topic Content", 1, ip, 2, false, false)
expectNilErr(t, topic.Stick())
shouldNotBeIn(newID)
testTopic(newID, "Test Topic", "Topic Content", 1, ip, 2, false, true)
expectNilErr(t, topic.Unstick())
shouldNotBeIn(newID)
testTopic(newID, "Test Topic", "Topic Content", 1, ip, 2, false, false)
expectNilErr(t, topic.MoveTo(1))
shouldNotBeIn(newID)
testTopic(newID, "Test Topic", "Topic Content", 1, ip, 1, false, false)
// TODO: Add more tests for more *Topic methods
expectNilErr(t, c.Topics.ClearIPs())
expectNilErr(t, c.Topics.Reload(topic.ID))
testTopic(newID, "Test Topic", "Topic Content", 1, "", 1, false, false)
// TODO: Add more tests for more *Topic methods
expectNilErr(t, topic.Delete())
shouldNotBeIn(newID)
_, err = c.Topics.Get(newID)
recordMustNotExist(t, err, fmt.Sprintf("TID #%d shouldn't exist", newID))
exf(!c.Topics.Exists(newID), "TID #%d shouldn't exist", newID)
// TODO: Test topic creation and retrieving that created topic plus reload and inspecting the cache
}
func TestForumStore(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
ex, exf := exp(t), expf(t)
// TODO: Test ForumStore.Reload
fcache, ok := c.Forums.(c.ForumCache)
ex(ok, "Unable to cast ForumStore to ForumCache")
ex(c.Forums.Count() == 2, "The forumstore global count should be 2")
ex(fcache.Length() == 2, "The forum cache length should be 2")
_, err := c.Forums.Get(-1)
recordMustNotExist(t, err, "FID #-1 shouldn't exist")
_, err = c.Forums.Get(0)
recordMustNotExist(t, err, "FID #0 shouldn't exist")
testForum := func(f *c.Forum, fid int, name string, active bool, desc string) {
exf(f.ID == fid, "forum.ID should be %d, not %d.", fid, f.ID)
// TODO: Check the preset and forum permissions
exf(f.Name == name, "forum.Name should be %s, not %s", name, f.Name)
str := ""
if !active {
str = "n't"
}
exf(f.Active == active, "The reports forum should%s be active", str)
exf(f.Desc == desc, "forum.Desc should be '%s' not '%s'", desc, f.Desc)
}
forum, err := c.Forums.Get(1)
recordMustExist(t, err, "Couldn't find FID #1")
expectDesc := "All the reports go here"
testForum(forum, 1, "Reports", false, expectDesc)
forum, err = c.Forums.BypassGet(1)
recordMustExist(t, err, "Couldn't find FID #1")
forum, err = c.Forums.Get(2)
recordMustExist(t, err, "Couldn't find FID #2")
forum, err = c.Forums.BypassGet(2)
recordMustExist(t, err, "Couldn't find FID #2")
expectDesc = "A place for general discussions which don't fit elsewhere"
testForum(forum, 2, "General", true, expectDesc)
// Forum reload test, kind of hacky but gets the job done
/*
CacheGet(id int) (*Forum, error)
CacheSet(forum *Forum) error
*/
ex(ok, "ForumCache should be available")
forum.Name = "nanana"
fcache.CacheSet(forum)
forum, err = c.Forums.Get(2)
recordMustExist(t, err, "Couldn't find FID #2")
exf(forum.Name == "nanana", "The faux name should be nanana not %s", forum.Name)
expectNilErr(t, c.Forums.Reload(2))
forum, err = c.Forums.Get(2)
recordMustExist(t, err, "Couldn't find FID #2")
exf(forum.Name == "General", "The proper name should be 2 not %s", forum.Name)
ex(!c.Forums.Exists(-1), "FID #-1 shouldn't exist")
ex(!c.Forums.Exists(0), "FID #0 shouldn't exist")
ex(c.Forums.Exists(1), "FID #1 should exist")
ex(c.Forums.Exists(2), "FID #2 should exist")
ex(!c.Forums.Exists(3), "FID #3 shouldn't exist")
_, err = c.Forums.Create("", "", true, "all")
ex(err != nil, "A forum shouldn't be successfully created, if it has a blank name")
fid, err := c.Forums.Create("Test Forum", "", true, "all")
expectNilErr(t, err)
ex(fid == 3, "The first forum we create should have an ID of 3")
ex(c.Forums.Exists(3), "FID #2 should exist")
ex(c.Forums.Count() == 3, "The forumstore global count should be 3")
ex(fcache.Length() == 3, "The forum cache length should be 3")
forum, err = c.Forums.Get(3)
recordMustExist(t, err, "Couldn't find FID #3")
forum, err = c.Forums.BypassGet(3)
recordMustExist(t, err, "Couldn't find FID #3")
testForum(forum, 3, "Test Forum", true, "")
// TODO: More forum creation tests
expectNilErr(t, c.Forums.Delete(3))
ex(forum.ID == 3, "forum pointer shenanigans")
ex(c.Forums.Count() == 2, "The forumstore global count should be 2")
ex(fcache.Length() == 2, "The forum cache length should be 2")
ex(!c.Forums.Exists(3), "FID #3 shouldn't exist after being deleted")
_, err = c.Forums.Get(3)
recordMustNotExist(t, err, "FID #3 shouldn't exist after being deleted")
_, err = c.Forums.BypassGet(3)
recordMustNotExist(t, err, "FID #3 shouldn't exist after being deleted")
ex(c.Forums.Delete(c.ReportForumID) != nil, "The reports forum shouldn't be deletable")
exf(c.Forums.Exists(c.ReportForumID), "FID #%d should still exist", c.ReportForumID)
_, err = c.Forums.Get(c.ReportForumID)
exf(err == nil, "FID #%d should still exist", c.ReportForumID)
_, err = c.Forums.BypassGet(c.ReportForumID)
exf(err == nil, "FID #%d should still exist", c.ReportForumID)
eforums := map[int]bool{1: true, 2: true}
{
forums, err := c.Forums.GetAll()
expectNilErr(t, err)
found := make(map[int]*c.Forum)
for _, forum := range forums {
_, ok := eforums[forum.ID]
exf(ok, "unknown forum #%d in forums", forum.ID)
found[forum.ID] = forum
}
for fid, _ := range eforums {
_, ok := found[fid]
exf(ok, "unable to find expected forum #%d in forums", fid)
}
}
{
fids, err := c.Forums.GetAllIDs()
expectNilErr(t, err)
found := make(map[int]bool)
for _, fid := range fids {
_, ok := eforums[fid]
exf(ok, "unknown fid #%d in fids", fid)
found[fid] = true
}
for fid, _ := range eforums {
_, ok := found[fid]
exf(ok, "unable to find expected fid #%d in fids", fid)
}
}
vforums := map[int]bool{2: true}
{
forums, err := c.Forums.GetAllVisible()
expectNilErr(t, err)
found := make(map[int]*c.Forum)
for _, forum := range forums {
_, ok := vforums[forum.ID]
exf(ok, "unknown forum #%d in forums", forum.ID)
found[forum.ID] = forum
}
for fid, _ := range vforums {
_, ok := found[fid]
exf(ok, "unable to find expected forum #%d in forums", fid)
}
}
{
fids, err := c.Forums.GetAllVisibleIDs()
expectNilErr(t, err)
found := make(map[int]bool)
for _, fid := range fids {
_, ok := vforums[fid]
exf(ok, "unknown fid #%d in fids", fid)
found[fid] = true
}
for fid, _ := range vforums {
_, ok := found[fid]
exf(ok, "unable to find expected fid #%d in fids", fid)
}
}
forum, err = c.Forums.Get(2)
expectNilErr(t, err)
prevTopicCount := forum.TopicCount
tid, err := c.Topics.Create(forum.ID, "Forum Meta Test", "Forum Meta Test", 1, "")
expectNilErr(t, err)
forum, err = c.Forums.Get(2)
expectNilErr(t, err)
exf(forum.TopicCount == (prevTopicCount+1), "forum.TopicCount should be %d not %d", prevTopicCount+1, forum.TopicCount)
exf(forum.LastTopicID == tid, "forum.LastTopicID should be %d not %d", tid, forum.LastTopicID)
exf(forum.LastPage == 1, "forum.LastPage should be %d not %d", 1, forum.LastPage)
// TODO: Test topic creation and forum topic metadata
// TODO: Test forum update
// TODO: Other forumstore stuff and forumcache?
}
// TODO: Implement this
func TestForumPermsStore(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
ex := exp(t)
f := func(fid, gid int, msg string, inv ...bool) {
fp, err := c.FPStore.Get(fid, gid)
if err == ErrNoRows {
fp = c.BlankForumPerms()
} else {
expectNilErr(t, err)
}
vt := fp.ViewTopic
if len(inv) > 0 && inv[0] == true {
vt = !vt
}
ex(vt, msg)
}
// TODO: Test reporting
initialState := func() {
f(1, 1, "admins should be able to see reports")
f(1, 2, "mods should be able to see reports")
f(1, 3, "members should not be able to see reports", true)
f(1, 4, "banned users should not be able to see reports", true)
f(2, 1, "admins should be able to see general")
f(2, 3, "members should be able to see general")
f(2, 6, "guests should be able to see general")
}
initialState()
expectNilErr(t, c.FPStore.Reload(1))
initialState()
expectNilErr(t, c.FPStore.Reload(2))
initialState()
gid, err := c.Groups.Create("FP Test", "FP Test", false, false, false)
expectNilErr(t, err)
fid, err := c.Forums.Create("FP Test", "FP Test", true, "")
expectNilErr(t, err)
u := c.GuestUser.Copy()
rt := func(gid, fid int, shouldSucceed bool) {
w := httptest.NewRecorder()
bytesBuffer := bytes.NewBuffer([]byte(""))
sfid := strconv.Itoa(fid)
req := httptest.NewRequest("", "/forum/"+sfid, bytesBuffer)
u.Group = gid
h, err := c.UserCheck(w, req, &u)
expectNilErr(t, err)
rerr := routes.ViewForum(w, req, &u, h, sfid)
if shouldSucceed {
ex(rerr == nil, "ViewForum should succeed")
} else {
ex(rerr != nil, "ViewForum should not succeed")
}
}
rt(1, fid, false)
rt(2, fid, false)
rt(3, fid, false)
rt(4, fid, false)
rt(gid, fid, false)
fp, err := c.FPStore.GetCopy(fid, gid)
if err == sql.ErrNoRows {
fp = *c.BlankForumPerms()
} else if err != nil {
expectNilErr(t, err)
}
fmt.Printf("fp: %+v\n", fp)
f(fid, 1, "admins should not be able to see fp test", true)
f(fid, 2, "mods should not be able to see fp test", true)
f(fid, 3, "members should not be able to see fp test", true)
f(fid, 4, "banned users should not be able to see fp test", true)
f(fid, gid, "fp test should not be able to see fp test", true)
fp.ViewTopic = true
forum, err := c.Forums.Get(fid)
expectNilErr(t, err)
expectNilErr(t, forum.SetPerms(&fp, "custom", gid))
rt(1, fid, false)
rt(2, fid, false)
rt(3, fid, false)
rt(4, fid, false)
rt(gid, fid, true)
fp, err = c.FPStore.GetCopy(fid, gid)
if err == sql.ErrNoRows {
fp = *c.BlankForumPerms()
} else if err != nil {
expectNilErr(t, err)
}
f(fid, 1, "admins should not be able to see fp test", true)
f(fid, 2, "mods should not be able to see fp test", true)
f(fid, 3, "members should not be able to see fp test", true)
f(fid, 4, "banned users should not be able to see fp test", true)
f(fid, gid, "fp test should be able to see fp test")
expectNilErr(t, c.Forums.Delete(fid))
rt(1, fid, false)
rt(2, fid, false)
rt(3, fid, false)
rt(4, fid, false)
rt(gid, fid, false)
// TODO: Test changing forum permissions
}
// TODO: Test the group permissions
// TODO: Test group.CanSee for forum presets + group perms
func TestGroupStore(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
ex, exf := exp(t), expf(t)
_, err := c.Groups.Get(-1)
recordMustNotExist(t, err, "GID #-1 shouldn't exist")
// TODO: Refactor the group store to remove GID #0
g, err := c.Groups.Get(0)
recordMustExist(t, err, "Couldn't find GID #0")
exf(g.ID == 0, "g.ID doesn't not match the requested GID. Got '%d' instead.", g.ID)
exf(g.Name == "Unknown", "GID #0 is named '%s' and not 'Unknown'", g.Name)
g, err = c.Groups.Get(1)
recordMustExist(t, err, "Couldn't find GID #1")
exf(g.ID == 1, "g.ID doesn't not match the requested GID. Got '%d' instead.'", g.ID)
ex(len(g.CanSee) > 0, "g.CanSee should not be zero")
ex(!c.Groups.Exists(-1), "GID #-1 shouldn't exist")
// 0 aka Unknown, for system posts and other oddities
ex(c.Groups.Exists(0), "GID #0 should exist")
ex(c.Groups.Exists(1), "GID #1 should exist")
isAdmin, isMod, isBanned := true, true, false
gid, err := c.Groups.Create("Testing", "Test", isAdmin, isMod, isBanned)
expectNilErr(t, err)
ex(c.Groups.Exists(gid), "The group we just made doesn't exist")
ff := func(i bool) string {
if !i {
return "n't"
}
return ""
}
f := func(gid int, isBanned, isMod, isAdmin bool) {
ex(g.ID == gid, "The group ID should match the requested ID")
exf(g.IsAdmin == isAdmin, "This should%s be an admin group", ff(isAdmin))
exf(g.IsMod == isMod, "This should%s be a mod group", ff(isMod))
exf(g.IsBanned == isBanned, "This should%s be a ban group", ff(isBanned))
}
g, err = c.Groups.Get(gid)
expectNilErr(t, err)
f(gid, false, true, true)
ex(len(g.CanSee) == 0, "g.CanSee should be empty")
isAdmin, isMod, isBanned = false, true, true
gid, err = c.Groups.Create("Testing 2", "Test", isAdmin, isMod, isBanned)
expectNilErr(t, err)
ex(c.Groups.Exists(gid), "The group we just made doesn't exist")
g, err = c.Groups.Get(gid)