-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads_test.go
144 lines (126 loc) · 9.48 KB
/
threads_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
package main
import (
"errors"
"net/url"
"os"
"strings"
"testing"
"time"
"codeberg.org/FiskFan1999/gemini/gemtest"
bolt "go.etcd.io/bbolt"
)
func TestGetSubforumPrivFromID(t *testing.T) {
Configuration = &ConfigStr{
Forum: []Forum{
Forum{Name: "official", Subforum: []Subforum{
Subforum{Name: "announcements", ID: "ann", ThreadPriviledge: Admin, ReplyPriviledge: Mod},
Subforum{Name: "other", ID: "other", ThreadPriviledge: User, ReplyPriviledge: User},
},
},
},
}
t1, u1, e1 := GetSubforumPrivFromID("ann")
if t1 != Admin || u1 != Mod || e1 != nil {
t.Errorf("Incorrect values recieved for GetSubforumPrivFromID(\"ann\") t=%s u=%s e=%s", t1, u1, e1)
}
t1, u1, e1 = GetSubforumPrivFromID("other")
if t1 != User || u1 != User || e1 != nil {
t.Errorf("Incorrect values recieved for GetSubforumPrivFromID(\"other\") t=%s u=%s e=%s", t1, u1, e1)
}
t1, u1, e1 = GetSubforumPrivFromID("error") // will be not found
if t1 != User || u1 != User || !errors.Is(e1, SubforumNotFound) {
t.Errorf("Incorrect values recieved for GetSubforumPrivFromID(\"error\") t=%s u=%s e=%s", t1, u1, e1)
}
}
func TestCreateThread(t *testing.T) {
var bleveDir = ".testing/TestCreateThread.bleve"
os.RemoveAll(bleveDir)
defer os.RemoveAll(bleveDir)
Configuration = &ConfigStr{
Keywords: bleveDir,
Forum: []Forum{Forum{"first forum", []Subforum{Subforum{"first subforum", "firstsub", 0, 0}}}},
Smtp: ConfigStrSmtp{Enabled: false},
}
databaseFile := ".testing/fullthreadtest.db"
os.Remove(databaseFile)
defer os.Remove(databaseFile)
var err error
db, err = bolt.Open(databaseFile, 0600, &bolt.Options{Timeout: time.Second})
if err != nil {
t.Fatal(err.Error())
}
defer db.Close()
if err := dbCreateBuckets(); err != nil {
t.Fatal(err.Error())
}
initKeyword()
// test server
serv := gemtest.Testd(t, handler, 2)
defer serv.Stop()
urlParse, _ := url.Parse("/new/thread/other/")
serv.Check(gemtest.Input{URL: "/", Cert: 0, Response: []byte("20 text/gemini\r\n# \r\n\r\nCurrently not logged in.\r\n=> /login/ Log in\r\n=> /register Register an account\r\n=> /search/ Search\r\n\r\n## first forum\r\n=> /f/firstsub/ first subforum\r\n\r\n# Source code\r\nlarigot is open-source software. You may download the source code from the following link.\r\n=> https://github.com/ObieSource/larigot\r\n")})
serv.Check(gemtest.Input{URL: "/register/alice/alice%40example.net/?password", Cert: 0, Response: []byte("30 /\r\n")})
serv.Check(gemtest.Input{URL: "/login/alice/?password", Cert: 1, Response: []byte("30 /\r\n")})
serv.Check(gemtest.Input{URL: "/", Cert: 0, Response: []byte("20 text/gemini\r\n# \r\n\r\nCurrently not logged in.\r\n=> /login/ Log in\r\n=> /register Register an account\r\n=> /search/ Search\r\n\r\n## first forum\r\n=> /f/firstsub/ first subforum\r\n\r\n# Source code\r\nlarigot is open-source software. You may download the source code from the following link.\r\n=> https://github.com/ObieSource/larigot\r\n")})
serv.Check(gemtest.Input{URL: "/", Cert: 2, Response: []byte("20 text/gemini\r\n# \r\n\r\nCurrently not logged in.\r\n=> /login/ Log in\r\n=> /register Register an account\r\n=> /search/ Search\r\n\r\n## first forum\r\n=> /f/firstsub/ first subforum\r\n\r\n# Source code\r\nlarigot is open-source software. You may download the source code from the following link.\r\n=> https://github.com/ObieSource/larigot\r\n")})
serv.Check(gemtest.Input{URL: "/", Cert: 1, Response: []byte("20 text/gemini\r\n# \r\n\r\nCurrently logged in as alice.\r\n=> /logout/ Log out\r\n=> /register Register an account\r\n=> /search/ Search\r\n\r\n## first forum\r\n=> /f/firstsub/ first subforum\r\n\r\n# Source code\r\nlarigot is open-source software. You may download the source code from the following link.\r\n=> https://github.com/ObieSource/larigot\r\n")})
serv.Check(gemtest.Input{URL: "/new/thread/firstsub/", Cert: 0, Response: []byte("60 Client certificate required\r\n")})
serv.Check(gemtest.Input{URL: "/new/thread/other/", Cert: 1, Response: PostNudgeHandler(urlParse, nil).Bytes()})
serv.Check(gemtest.Input{URL: "/new/thread/other/", Cert: 1, Response: []byte("59 Subforum not found\r\n")})
serv.Check(gemtest.Input{URL: "/new/thread/firstsub/", Cert: 1, Response: []byte("10 Thread title\r\n")})
serv.Check(gemtest.Input{URL: "/new/thread/firstsub/?title%40here", Cert: 1, Response: []byte("30 /new/thread/firstsub/title%40here/\r\n")})
serv.Check(gemtest.Input{URL: "/new/thread/firstsub/title%40here/", Cert: 1, Response: []byte("10 Thread title\r\n")})
serv.Check(gemtest.Input{URL: "/new/thread/firstsub/title%40here/?first%20thread%20here.%0Agoodbye.", Cert: 1, Response: []byte("30 /f/firstsub/\r\n")})
// posts
// we have to change the date of the thread.
if err := db.Update(func(tx *bolt.Tx) error {
posts := tx.Bucket(DBALLPOSTS)
thispost := posts.Bucket([]byte("0000000000000001"))
thispost.Put([]byte("time"), []byte("2020-01-01T01:00:00.000000-04:00"))
allthreads := tx.Bucket(DBALLTHREADS)
thisthread := allthreads.Bucket([]byte("0000000000000001"))
thisthread.Put([]byte("lastmodified"), []byte("2020-01-01T01:00:00.000000-04:00"))
return nil
}); err != nil {
t.Fatal(err.Error())
}
serv.Check(gemtest.Input{URL: "/f/firstsub/", Cert: 1, Response: []byte("20 text/gemini\r\n# first subforum\r\n=> /new/thread/firstsub Post new thread\r\n\r\n=> /thread/0000000000000001/ title@here (01 Jan 2020)\r\n")})
serv.Check(gemtest.Input{URL: "/thread/0000000000000001/", Cert: 1, Response: []byte("20 text/gemini\r\n# title@here\r\n=> /new/post/0000000000000001/ Write comment\r\n\r\n### alice\r\n=> /report/0000000000000001/ Wed, 01 Jan 2020 05:00:00 UTC (click to report)\r\n> first thread here.\r\n> goodbye.\r\n\r\n=> /new/post/0000000000000001/ Write comment\r\n")})
/*
Test locking of threads
*/
DoCommand("lock 0000000000000001")
serv.Check(gemtest.Input{URL: "/thread/0000000000000001/", Cert: 0, Response: []byte("20 text/gemini\r\n# title@here\r\nThis thread is locked and not accepting new comments.\r\n\r\n### alice\r\n=> /report/0000000000000001/ Wed, 01 Jan 2020 05:00:00 UTC (click to report)\r\n> first thread here.\r\n> goodbye.\r\n\r\nThis thread is locked and not accepting new comments.\r\n")})
serv.Check(gemtest.Input{URL: "/thread/0000000000000001/", Cert: 1, Response: []byte("20 text/gemini\r\n# title@here\r\nThis thread is locked and not accepting new comments.\r\n\r\n### alice\r\n=> /report/0000000000000001/ Wed, 01 Jan 2020 05:00:00 UTC (click to report)\r\n> first thread here.\r\n> goodbye.\r\n\r\nThis thread is locked and not accepting new comments.\r\n")})
serv.Check(gemtest.Input{URL: "/new/post/0000000000000001/?this%20will%20also%20get%20shown.%20Goodbye%21", Cert: 0, Response: []byte("60 Client certificate required\r\n")})
serv.Check(gemtest.Input{URL: "/new/post/0000000000000001/?this%20will%20also%20get%20shown.%20Goodbye%21", Cert: 1, Response: []byte("40 Thread is locked\r\n")})
// unlock
DoCommand("unlock 0000000000000001")
serv.Check(gemtest.Input{URL: "/thread/0000000000000001/", Cert: 1, Response: []byte("20 text/gemini\r\n# title@here\r\n=> /new/post/0000000000000001/ Write comment\r\n\r\n### alice\r\n=> /report/0000000000000001/ Wed, 01 Jan 2020 05:00:00 UTC (click to report)\r\n> first thread here.\r\n> goodbye.\r\n\r\n=> /new/post/0000000000000001/ Write comment\r\n")})
// posts than search
serv.Check(gemtest.Input{URL: "/new/post/0000000000000001/?this%20will%20also%20get%20shown.%20Goodbye%21", Cert: 1, Response: []byte("30 /thread/0000000000000001/\r\n")})
serv.Check(gemtest.Input{URL: "/new/post/0000000000000001/?this%20will%20not%20get%20shown.", Cert: 1, Response: []byte("30 /thread/0000000000000001/\r\n")})
serv.Check(gemtest.Input{URL: "/search/?%40alice", Cert: 0, Response: []byte("20 text/gemini\r\n# Search by user alice\r\n\r\n## Created threads\r\n=> /thread/0000000000000001/ <alice> title@here\r\nID: 0000000000000001\r\n> first thread here.\r\n> goodbye.\r\n## Replies\r\n=> /thread/0000000000000001/ <alice> title@here\r\nID: 0000000000000003 thread: 0000000000000001\r\n> this will not get shown.\r\n=> /thread/0000000000000001/ <alice> title@here\r\nID: 0000000000000002 thread: 0000000000000001\r\n> this will also get shown. Goodbye!\r\n")})
serv.Check(gemtest.Input{URL: "/search/?goodbye", Cert: 0, Response: []byte("20 text/gemini\r\n# Results for keywords goodbye\r\n=> /thread/0000000000000001/ <alice> title@here\r\nID: 0000000000000001 thread: 0000000000000001\r\n> first thread here.\r\n> goodbye.\r\n=> /thread/0000000000000001/ <alice> title@here\r\nID: 0000000000000002 thread: 0000000000000001\r\n> this will also get shown. Goodbye!\r\n")})
serv.Check(gemtest.Input{URL: "/search/?somethingthatwontgetcaught", Cert: 0, Response: []byte("20 text/gemini\r\n# Results for keywords somethingthatwontgetcaught\r\n")})
}
func TestValidateThreadTitle(t *testing.T) {
for c, e := range TestValidateThreadTitleCases {
r := ValidateThreadTitle(c)
if !errors.Is(r, e) {
t.Errorf("For case %q, expected error %s but recieved %s", c, e, r)
}
}
}
var TestValidateThreadTitleCases = map[string]error{
"Thread title here": nil,
"": TitleEmptyNotAllowed,
"bad title here\r\n": TitleIllegalCharacter,
"bad title here\n": TitleIllegalCharacter,
"bad \ttitle here": TitleIllegalCharacter,
"bad title here": nil,
"Good thread here! What do we do now?": nil,
"hëllo": nil,
strings.Repeat("a", TitleMaxLength+1): TitleTooLong,
strings.Repeat("a", TitleMaxLength): nil,
}