-
Notifications
You must be signed in to change notification settings - Fork 0
/
minitwit_test.go
207 lines (164 loc) · 5.84 KB
/
minitwit_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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"github.com/ContainerMaintainers/MiniTwit-Golang/database"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
var router *gin.Engine
// ------ Initialization ------ //
func init() {
router = SetupRouter()
database.ConnectToTestDatabase()
database.MigrateEntities()
}
// ------ Helper Functions ------ //
func login(username string, password string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
body := map[string]interface{}{
"username": username,
"password": password,
}
json_body, err := json.Marshal(body)
if err != nil {
log.Fatalln(err)
}
req, _ := http.NewRequest("POST", "/login", bytes.NewReader(json_body))
router.ServeHTTP(w, req)
return w
}
func register_helper(username string, password string, password2 string, email string) *httptest.ResponseRecorder {
if password2 == "" {
password2 = password
}
if email == "" {
email = fmt.Sprintf("%s@example.com", username)
}
w := httptest.NewRecorder()
body := map[string]interface{}{
"username": username,
"password": password,
"password2": password2,
"email": email,
}
json_body, err := json.Marshal(body)
if err != nil {
log.Fatalln(err)
}
req, _ := http.NewRequest("POST", "/register", bytes.NewReader(json_body))
router.ServeHTTP(w, req)
return w
}
func register_and_login(username string, password string) *httptest.ResponseRecorder {
register_helper(username, password, "", "")
return login(username, password)
}
func logout() *httptest.ResponseRecorder {
w := httptest.NewRecorder()
req, _ := http.NewRequest("PUT", "/logout", nil)
router.ServeHTTP(w, req)
return w
}
func add_message(text string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
body := map[string]interface{}{
"text": text,
}
json_body, err := json.Marshal(body)
if err != nil {
log.Fatalln(err)
}
req, _ := http.NewRequest("POST", "/add_message", bytes.NewReader(json_body))
router.ServeHTTP(w, req)
return w
}
// ------ Tests ------ //
func TestRegister(t *testing.T) {
w1 := register_helper("user1", "default", "", "")
assert.Contains(t, w1.Body.String(), "You were successfully registered and can login now")
w2 := register_helper("user1", "default", "", "")
assert.Contains(t, w2.Body.String(), "The username is already taken")
w3 := register_helper("", "default", "", "")
assert.Contains(t, w3.Body.String(), "You have to enter a username")
w4 := register_helper("meh", "", "", "")
assert.Contains(t, w4.Body.String(), "You have to enter a password")
w5 := register_helper("meh", "x", "y", "")
assert.Contains(t, w5.Body.String(), "The two passwords do not match")
w6 := register_helper("meh", "default", "", "broken")
assert.Contains(t, w6.Body.String(), "You have to enter a valid email address")
}
func TestLoginLogout(t *testing.T) {
w1 := register_and_login("user1", "default")
assert.Contains(t, w1.Body.String(), "You were logged in")
w2 := logout()
assert.Contains(t, w2.Body.String(), "You were logged out")
w3 := login("user1", "wrongpassword")
assert.Contains(t, w3.Body.String(), "Invalid password")
w4 := login("user2", "wrongpassword")
assert.Contains(t, w4.Body.String(), "Invalid username")
}
func TestMessageRecording(t *testing.T) {
w1 := register_and_login("foo", "default")
assert.Contains(t, w1.Body.String(), "You were logged in")
w2 := add_message("test message 1")
assert.Contains(t, w2.Body.String(), "Your message was recorded")
w3 := add_message("<test message 2>")
assert.Contains(t, w3.Body.String(), "Your message was recorded")
w4 := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w4, req)
log.Println(w4.Body.String())
assert.Contains(t, w4.Body.String(), "test message 1")
assert.Contains(t, w4.Body.String(), "\\u003ctest message 2\\u003e") // OR: "<test message 2>"
}
func TestTimeline(t *testing.T) {
register_and_login("foo", "default")
add_message("the message by foo")
logout()
register_and_login("bar", "default")
add_message("the message by bar")
w1 := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/public", nil)
router.ServeHTTP(w1, req)
assert.Contains(t, w1.Body.String(), "the message by foo")
assert.Contains(t, w1.Body.String(), "the message by bar")
w2 := httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/", nil)
router.ServeHTTP(w2, req)
assert.NotContains(t, w2.Body.String(), "the message by foo")
assert.Contains(t, w2.Body.String(), "the message by bar")
w3 := httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/foo/follow", nil)
router.ServeHTTP(w3, req)
assert.Contains(t, w3.Body.String(), "You are now following \"foo\"") // OR: "You are now following "foo""
w4 := httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/", nil)
router.ServeHTTP(w4, req)
assert.Contains(t, w4.Body.String(), "the message by foo")
assert.Contains(t, w4.Body.String(), "the message by bar")
w5 := httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/bar", nil)
router.ServeHTTP(w5, req)
assert.NotContains(t, w5.Body.String(), "the message by foo")
assert.Contains(t, w5.Body.String(), "the message by bar")
w6 := httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/foo", nil)
router.ServeHTTP(w6, req)
assert.Contains(t, w6.Body.String(), "the message by foo")
assert.NotContains(t, w6.Body.String(), "the message by bar")
w7 := httptest.NewRecorder()
req, _ = http.NewRequest("DELETE", "/foo/unfollow", nil)
router.ServeHTTP(w7, req)
assert.Contains(t, w7.Body.String(), "You are no longer following \"foo\"") // OR: "You are no longer following "foo""
w8 := httptest.NewRecorder()
req, _ = http.NewRequest("GET", "/", nil)
router.ServeHTTP(w8, req)
assert.NotContains(t, w8.Body.String(), "the message by foo")
assert.Contains(t, w8.Body.String(), "the message by bar")
}