-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommon.go
68 lines (59 loc) · 1.91 KB
/
common.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
//go:build e2e
package main
import (
"net/http"
"testing"
"github.com/gavv/httpexpect/v2"
)
var serverAddr = "https://toktik.xctra.cn"
// TODO: use safer way to store test user credentials
var testUserA = "douyinTestUserA"
var testUserB = "douyinTestUserB"
func newExpect(t *testing.T) *httpexpect.Expect {
return httpexpect.WithConfig(httpexpect.Config{
Client: http.DefaultClient,
BaseURL: serverAddr,
Reporter: httpexpect.NewAssertReporter(t),
Printers: []httpexpect.Printer{
httpexpect.NewDebugPrinter(t, true),
},
})
}
func getTestUserToken(user string, e *httpexpect.Expect) (int, string) {
registerResp := e.POST("/douyin/user/register/").
WithQuery("username", user).WithQuery("password", user).
WithFormField("username", user).WithFormField("password", user).
Expect().
Status(http.StatusOK).
JSON().Object()
userId := 0
token := registerResp.Value("token").String().Raw()
if len(token) == 0 {
loginResp := e.POST("/douyin/user/login/").
WithQuery("username", user).WithQuery("password", user).
WithFormField("username", user).WithFormField("password", user).
Expect().
Status(http.StatusOK).
JSON().Object()
loginToken := loginResp.Value("token").String()
loginToken.Length().Gt(0)
token = loginToken.Raw()
userId = int(loginResp.Value("user_id").Number().Raw())
} else {
userId = int(registerResp.Value("user_id").Number().Raw())
}
return userId, token
}
func ValidateUser(user *httpexpect.Object) {
user.ContainsKey("id")
user.Value("name").String().NotEmpty()
user.ContainsKey("follow_count")
user.ContainsKey("follower_count")
user.ContainsKey("is_follow")
user.Value("avatar").String().NotEmpty()
user.Value("background_image").String().NotEmpty()
user.Value("signature").String().NotEmpty()
user.ContainsKey("total_favorited") // TODO: determine if this field should be string or int
user.ContainsKey("work_count")
user.ContainsKey("favorite_count")
}