-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoiodi_test.go
395 lines (323 loc) · 10.8 KB
/
goiodi_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
package main
import (
"crypto/tls"
"fmt"
"gopkg.in/mgo.v2"
"io"
// "io/ioutil"
"net/http"
"net/http/cookiejar"
"os/exec"
"strings"
"testing"
)
var (
client *http.Client
reader io.Reader
userPwd string
userPwdSalt string
addUserUrl string
loginUserUrl string
checkUserLoggedUrl string
addWordUrl string
getWordsUrl string
getWordsInclUrl string
getWordInfoUrl string
cookieJar, _ = cookiejar.New(nil)
)
// Testing parameters
const (
SERVER_URL = "https://localhost:8083"
USER_USERNAME = "test_username"
USER_PWD = "test_password"
USER_EMAIL = "[email protected]"
INVALID_USER_USERNAME = "test_invalid_username"
INVALID_USER_PWD = "test_invalid_password"
)
// Testing function called once at the beginning
func init() {
// Initialize DB (prepare it for tests)
err := emptyDB()
if err != nil {
panic(err)
}
cmd := exec.Command("/bin/sh", DB_INIT_SCRIPT)
err = cmd.Start()
if err != nil {
panic(err)
}
Log.Notice("Waiting for command to finish...")
err = cmd.Wait()
if err != nil {
panic(err)
}
// Get the API URL
addUserUrl = SERVER_URL + "/users/add"
loginUserUrl = SERVER_URL + "/user/login"
checkUserLoggedUrl = SERVER_URL + "/user/login/check"
addWordUrl = SERVER_URL + "/words/add"
getWordsUrl = SERVER_URL + "/words"
getWordsInclUrl = SERVER_URL + "/words/incl"
getWordInfoUrl = SERVER_URL + "/word"
// Do not check TLS certificate and set default cookie Jar
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{
Transport: tr,
Jar: cookieJar,
}
}
// emptyDB empties the MongoDB dictionary database
func emptyDB() error {
// Get DB session
dbs, err := mgo.Dial(HOST)
if err != nil {
return err
}
defer dbs.Close()
// Switch the session to a monotonic behavior.
dbs.SetMode(mgo.Monotonic, true)
dbs.DB(GOIODI_DB).C(WORD_COLLECTION).RemoveAll(nil)
return err
}
// addWord is an API to add a new word to the dictionary
func TestAddWord(t *testing.T) {
fmt.Println("-- addWord --")
// Valid case 1
wordJSON := `{
"word": "test",
"definition": "Definition of test"
}`
reader = strings.NewReader(wordJSON) // Convert string to reader
request, err := client.Post(addWordUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 1: Missing word
invalidWordJSON := `{
"definition": "Definition of test"
}`
reader = strings.NewReader(invalidWordJSON) // Convert string to reader
request, err = client.Post(addWordUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusInternalServerError {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 2: Missing definition
invalidWordJSON = `{
"word": "test"
}`
reader = strings.NewReader(invalidWordJSON) // Convert string to reader
request, err = client.Post(addWordUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusInternalServerError {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// getWords is an API to get all the dictionary words
func TestGetWords(t *testing.T) {
fmt.Println("-- getWords --")
// Valid case 1
request, err := client.Get(getWordsUrl) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
defer request.Body.Close()
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// getWordsIncl is an API to get all the dictionary words that include a
// user specified string
func TestGetWordsIncl(t *testing.T) {
fmt.Println("-- getWordsIncl --")
// Valid case 1
filterJSON := `{
"filter_str": "est"
}`
reader = strings.NewReader(filterJSON) //Convert string to reader
request, err := client.Post(getWordsInclUrl, CONTENT_TYPE, reader) //Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Valid case 2: Invalid word filter
invalidFilterJSON := `{
"filter_str": "fff"
}`
reader = strings.NewReader(invalidFilterJSON) //Convert string to reader
request, err = client.Post(getWordsInclUrl, CONTENT_TYPE, reader) //Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusOK {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// getWordInfo is an API to get a user specified word information (creation date,
// definition, comments, rating)
func TestGetWordInfo(t *testing.T) {
fmt.Println("-- getWordInfo --")
// Valid case 1
searchedWord := "test"
t.Log(getWordInfoUrl)
request, err := client.Get(getWordInfoUrl + "/" + searchedWord) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
defer request.Body.Close()
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 1: Invalid searched word
invalidSearchedWord := "test_invalid"
t.Log(getWordInfoUrl)
request, err = client.Get(getWordInfoUrl + "/" + invalidSearchedWord) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
defer request.Body.Close()
if request.StatusCode != http.StatusNotFound {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// getWordComments is an API to get a user specified word comments (made by app users)
func TestGetWordComments(t *testing.T) {
fmt.Println("-- getWordComments --")
// Valid case 1
searchedWord := "test"
t.Log(getWordInfoUrl)
request, err := client.Get(getWordInfoUrl + "/" + searchedWord + "/comments") // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
defer request.Body.Close()
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 1: Invalid searched word
invalidSearchedWord := "test_invalid"
t.Log(getWordInfoUrl)
request, err = client.Get(getWordInfoUrl + "/" + invalidSearchedWord + "/comments") // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
defer request.Body.Close()
if request.StatusCode != http.StatusNotFound {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// addUser is an API to add a new user
func TestAddUser(t *testing.T) {
fmt.Println("-- addUser --")
// Valid case 1
userJSON := `{
"email": "` + USER_EMAIL + `",
"password": "` + USER_PWD + `",
"username": "` + USER_USERNAME + `"
}`
reader = strings.NewReader(userJSON) // Convert string to reader
request, err := client.Post(addUserUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 1: Missing user email
invalidUserJSON := `{
"password": "` + USER_PWD + `",
"username": "` + USER_USERNAME + "_invalid_1" + `"
}`
reader = strings.NewReader(invalidUserJSON) // Convert string to reader
request, err = client.Post(addUserUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusInternalServerError {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 2: Missing user password
invalidUserJSON = `{
"email": "` + USER_EMAIL + "_invalid_2" + `",
"username": "` + USER_USERNAME + "_invalid_2" + `"
}`
reader = strings.NewReader(invalidUserJSON) // Convert string to reader
request, err = client.Post(addUserUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusInternalServerError {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Invalid case 3: Missing user username
invalidUserJSON = `{
"email": "` + USER_EMAIL + "_invalid_3" + `",
"password": "` + USER_PWD + `"
}`
reader = strings.NewReader(invalidUserJSON) // Convert string to reader
request, err = client.Post(addUserUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusInternalServerError {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// loginUser is an API to add a new user
func TestLoginUser(t *testing.T) {
fmt.Println("-- loginUser --")
// Valid case 1
userJSON := `{
"password": "` + USER_PWD + `",
"username": "` + USER_USERNAME + `"
}`
reader = strings.NewReader(userJSON) // Convert string to reader
request, err := client.Post(loginUserUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
// Display cookie
// body, _ := ioutil.ReadAll(request.Body)
// request.Body.Close()
// fmt.Println(string(body))
// Invalid case 1
invalidUserJSON := `{
"password": "` + INVALID_USER_PWD + `",
"username": "` + INVALID_USER_USERNAME + `"
}`
reader = strings.NewReader(invalidUserJSON) // Convert string to reader
request, err = client.Post(loginUserUrl, CONTENT_TYPE, reader) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
if request.StatusCode != http.StatusInternalServerError {
t.Errorf("Internal server error expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}
// checkUserLogged is an API to check if a user is logged
func TestCheckUserLogged(t *testing.T) {
fmt.Println("-- checkUserLogged --")
// Valid case 1
request, err := client.Get(checkUserLoggedUrl) // Create request with JSON body
if err != nil {
t.Error(err.Error())
}
defer request.Body.Close()
if request.StatusCode != http.StatusOK {
t.Errorf("Success expected: %d", request.StatusCode) // HTTP request failed: Test failed
}
}