This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
318 lines (256 loc) · 7.52 KB
/
server_test.go
File metadata and controls
318 lines (256 loc) · 7.52 KB
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
package main
import (
"bytes"
"fmt"
"strconv"
"testing"
"encoding/json"
"net/http"
"github.com/pganguli/hnews/pkg/jwt"
)
type GQL_Query struct {
OperationName string
Query string
}
type GQL_Response struct {
Data map[string]interface{}
Errors []map[string]interface{}
}
type httpHeaders map[string]string
var GQL_Url = "http://localhost:8080/graphql"
func GqlQuery(gql_query GQL_Query, headers httpHeaders) (response GQL_Response) {
json_str, err := json.Marshal(gql_query)
if err != nil {
panic(err)
}
req, err := http.NewRequest(http.MethodPost, GQL_Url, bytes.NewReader(json_str))
if err != nil {
panic(err)
}
req.Header.Set("Content-Type", "application/json")
for key, value := range headers {
req.Header.Set(key, value)
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
resp_body := new(bytes.Buffer)
if _, err := resp_body.ReadFrom(resp.Body); err != nil {
panic(err)
}
if err := json.Unmarshal(resp_body.Bytes(), &response); err != nil {
panic(err)
}
return response
}
func CreateUser(username string, password string) (response GQL_Response) {
operationName := "create_user"
query := fmt.Sprintf("mutation %v{createUser(input:{username:\"%v\",password:\"%v\"})}",
operationName, username, password)
data := GQL_Query{
OperationName: operationName,
Query: query,
}
return GqlQuery(data, nil)
}
func CreateLink(title string, address string, bearerToken httpHeaders) (response GQL_Response) {
operationName := "create_link"
query := fmt.Sprintf("mutation %v{createLink(input:{title:\"%v\",address:\"%v\"}){id title address user{id username}}}",
operationName, title, address)
data := GQL_Query{
OperationName: operationName,
Query: query,
}
return GqlQuery(data, bearerToken)
}
func ListLinks() (response GQL_Response) {
operationName := "list_links"
query := fmt.Sprintf("query %v{links{id title address user{id username}}}",
operationName)
data := GQL_Query{
OperationName: operationName,
Query: query,
}
return GqlQuery(data, nil)
}
func Login(username string, password string) (response GQL_Response) {
operationName := "login"
query := fmt.Sprintf("mutation %v{login(input:{username:\"%v\",password:\"%v\"})}",
operationName, username, password)
data := GQL_Query{
OperationName: operationName,
Query: query,
}
return GqlQuery(data, nil)
}
func RefreshToken(token string) (response GQL_Response) {
operationName := "refresh_token"
query := fmt.Sprintf("mutation %v{refreshToken(input:{token:\"%v\"})}",
operationName, token)
data := GQL_Query{
OperationName: operationName,
Query: query,
}
return GqlQuery(data, nil)
}
func TestCreateUser(t *testing.T) {
response := CreateUser("user0", "pa$$word")
if response.Errors != nil {
t.Errorf("%+v", response.Errors)
}
authToken, ok := response.Data["createUser"].(string)
if !ok {
t.Errorf("Invalid data:\n%+v", response.Data)
}
userName, err := jwt.ParseToken(authToken)
if err != nil {
t.Error(err)
}
if userName != "user0" {
t.Errorf("Claimed user name is incorrect: %q", userName)
}
duplicate_user_text := "ERROR: duplicate key value violates unique constraint \"users_username_key\" (SQLSTATE 23505)"
response = CreateUser("user0", "pa$$word")
if response.Errors == nil {
t.Errorf("Did not get expected error: %q", duplicate_user_text)
} else {
message := response.Errors[0]["message"].(string)
if message != duplicate_user_text {
t.Errorf("Expected error: %q\nGot error: %q", duplicate_user_text, message)
}
}
}
func TestCreateUserLink(t *testing.T) {
response := CreateUser("user1", "pa$$word")
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
authToken := response.Data["createUser"].(string)
access_denied_text := "access denied"
// No bearer token
response = CreateLink("title1", "ww1.example.net", httpHeaders{})
if response.Errors == nil {
t.Fatalf("Did not get expected error: %q", access_denied_text)
} else {
message := response.Errors[0]["message"].(string)
if message != access_denied_text {
t.Errorf("Expected error: %q\nGot error: %q", access_denied_text, message)
}
}
// Invalid bearer token
response = CreateLink("title1", "ww1.example.net", httpHeaders{"Authorization": "bearer " + authToken})
if response.Errors == nil {
t.Fatalf("Did not get expected error: %q", access_denied_text)
} else {
message := response.Errors[0]["message"].(string)
if message != access_denied_text {
t.Errorf("Expected error: %q\nGot error: %q", access_denied_text, message)
}
}
// Valid bearer token
response = CreateLink("title1", "ww1.example.net", httpHeaders{"Authorization": "Bearer " + authToken})
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
data, ok := response.Data["createLink"].(map[string]interface{})
if !ok {
t.Errorf("Invalid data:\n%+v", data)
}
address, ok := data["address"].(string)
if !ok || address != "ww1.example.net" {
t.Errorf("Invalid address: %q", address)
}
id_s, ok := data["id"].(string)
if _, err := strconv.Atoi(id_s); !ok || err != nil {
t.Errorf("Invalid id: %q", id_s)
}
title, ok := data["title"].(string)
if !ok || title != "title1" {
t.Errorf("Invalid title: %q", title)
}
user := data["user"].(map[string]interface{})
user_id_s, ok := user["id"].(string)
if _, err := strconv.Atoi(user_id_s); !ok || err != nil {
t.Errorf("Invalid user_id: %q", user_id_s)
}
user_username, ok := user["username"].(string)
if !ok || user_username != "user1" {
t.Errorf("Invalid user_username: %q", user_username)
}
}
func TestListLinks(t *testing.T) {
response := CreateUser("user2", "pa$$word")
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
authToken := response.Data["createUser"].(string)
bearerToken := httpHeaders{"Authorization": "Bearer " + authToken}
for i := 1; i <= 5; i++ {
response = CreateLink(
"title"+strconv.Itoa(i),
"ww"+strconv.Itoa(i)+".example.net",
bearerToken)
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
}
response = ListLinks()
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
links, ok := response.Data["links"].([]interface{})
if !ok {
t.Errorf("Invalid links:\n%+v", response.Data)
}
count := 0
for _, link := range links {
user, ok := link.(map[string]interface{})["user"]
if !ok {
t.Errorf("Invalid user:\n%+v", link)
}
username, ok := user.(map[string]interface{})["username"]
if !ok {
t.Errorf("Invalid username:\n%+v", user)
}
if username == "user2" {
count++
}
}
if count != 5 {
t.Errorf("Expected 5 links; got: %d", count)
}
}
func TestLogin(t *testing.T) {
response := CreateUser("user3", "pa$$word")
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
authToken := response.Data["createUser"].(string)
response = Login("user3", "pa$$word")
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
} else {
loginToken := response.Data["login"].(string)
if loginToken != authToken {
t.Errorf("Expected token: %q\nGot token: %q", authToken, loginToken)
}
}
}
func TestRefreshToken(t *testing.T) {
response := CreateUser("user4", "pa$$word")
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
}
authToken := response.Data["createUser"].(string)
response = RefreshToken(authToken)
if response.Errors != nil {
t.Fatalf("%+v", response.Errors)
} else {
refreshedToken := response.Data["refreshToken"].(string)
if refreshedToken != authToken {
t.Errorf("Expected token: %q\nGot token: %q", authToken, refreshedToken)
}
}
}