-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
223 lines (188 loc) · 5.42 KB
/
main_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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/alex1ai/ugr-master-cc/authentication"
. "github.com/alex1ai/ugr-master-cc/data"
"github.com/mongodb/mongo-go-driver/bson"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
var db *DB
func populateDB(db *DB, instances int) {
collection := db.Client.Database(Database).Collection(ContentCollection)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
for i := 0; i < instances; i++ {
content := createDummyContent(i)
_, err := collection.InsertOne(ctx, content)
if err != nil {
panic(err)
}
}
}
func setupDB(t *testing.T) {
if db == nil {
Database = "testing"
data := DB{}
portI, err := strconv.Atoi(MongoPort)
err = data.Connect(MongoIp, portI)
if err != nil {
t.Error(err)
}
data.Reset()
populateDB(&data, 10)
if err != nil {
t.Fatal(err)
}
db = &data
}
}
func TestRootHandler(t *testing.T) {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(StatusHandler)
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
}
func TestGetHandler(t *testing.T) {
tt := []struct {
lang string
id string
shouldPass bool
}{
{"de", "1", true},
{"en", "2", true},
{"es", "-1", false},
{"", "", true},
{"de", "", true},
{"", "1", true},
{"asdf", "1", false},
}
setupDB(t)
for _, tc := range tt {
req, err := http.NewRequest("GET", "/content", nil)
if err != nil {
t.Error(err)
}
q := req.URL.Query()
q.Add("lang", tc.lang)
q.Add("id", tc.id)
req.URL.RawQuery = q.Encode()
rr := httptest.NewRecorder()
// Need to create a router that we can pass the request through so that the vars will be added to the context
router := Router(db)
router.ServeHTTP(rr, req)
if rr.Code == http.StatusOK && !tc.shouldPass {
t.Errorf("handler should have failed on lang %s and id %s: got %v want %v",
tc.lang, tc.id, rr.Code, http.StatusOK)
}
}
}
func TestDeleteHandler(t *testing.T) {
setupDB(t)
content := createDummyContent(1)
// First add something that we will delete next
conn := db.Client.Database(Database).Collection(ContentCollection)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
res, err := conn.InsertOne(ctx, content)
if res.InsertedID == nil || err != nil {
t.Error("could not put content in DB")
}
cancel()
var other Content
err = conn.FindOne(context.Background(), bson.M{"lang": content.Language, "id": content.Id}).Decode(&other)
if err != nil {
t.Error("Did not find instance")
}
for i := 0; i < 2; i++ {
path := fmt.Sprintf("/content/%s/%d", content.Language, content.Id)
req, err := http.NewRequest("DELETE", path, nil)
if err != nil {
t.Error(err)
}
rr := httptest.NewRecorder()
// Need to create a router that we can pass the request through so that the vars will be added to the context
router := Router(db)
router.ServeHTTP(rr, req)
if rr.Code != http.StatusNoContent {
t.Errorf("handler should have returned NoContent")
print(rr.Code)
}
}
}
func TestPostHandler(t *testing.T) {
setupDB(t)
content := createDummyContent(123)
js, err := json.Marshal(content)
if err != nil {
t.Error(err)
}
// First request
req, err := http.NewRequest("POST", "/content", strings.NewReader(string(js)))
if err != nil {
t.Error(err)
}
rr := httptest.NewRecorder()
router := Router(db)
router.ServeHTTP(rr, req)
code := rr.Code
if !(code == http.StatusNoContent || code == http.StatusCreated) {
t.Error("handler did not pass on first POST")
}
// Now the instance is created => must return NoContent
router.ServeHTTP(rr, req)
if code = rr.Code; code != http.StatusNoContent {
t.Errorf("handler did not pass on second POST of same instance, returned %d ", code)
}
}
func TestLoginHandler(t *testing.T) {
setupDB(t)
db.Reset()
_, err := authentication.AddUserIfNotThere("test1234", "test1234", db)
if err != nil {
t.Error(err)
}
users := []struct {
U authentication.User
Pass bool
}{
{authentication.User{Name: "abc", Password: "test1234"}, false},
{authentication.User{Name: "test1234", Password: "test1234"}, true},
{authentication.User{Name: "test1234", Password: "test124"}, false},
}
for _, user := range users {
js, err := json.Marshal(user.U)
// First request
req, err := http.NewRequest("POST", "/login", strings.NewReader(string(js)))
if err != nil {
t.Error(err)
}
rr := httptest.NewRecorder()
router := Router(db)
router.ServeHTTP(rr, req)
tokenString := rr.Body.String()
if len(tokenString) == 0 && user.Pass {
t.Errorf("Valid user %s reveiced illegal tokenString, found %s", user.U, tokenString)
} else if len(tokenString) > 0 && !user.Pass && rr.Code != http.StatusUnauthorized {
t.Errorf("Unvalid user %s received legal tokenString, found %s with status %d", user.U, tokenString, rr.Code)
}
}
}