forked from Ptt-official-app/Ptt-backend
/
route_users_test.go
138 lines (106 loc) · 3.76 KB
/
route_users_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
package main
import (
"github.com/PichuChen/go-bbs"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
type MockUserRecord struct {
userId string
}
func NewMockUserRecord(userId string) *MockUserRecord { return &MockUserRecord{userId: userId} }
func (u *MockUserRecord) UserId() string { return u.userId }
// HashedPassword return user hashed password, it only for debug,
// If you want to check is user password correct, please use
// VerifyPassword insteaded.
func (u *MockUserRecord) HashedPassword() string { return "" }
// VerifyPassword will check user's password is OK. it will return null
// when OK and error when there are something wrong
func (u *MockUserRecord) VerifyPassword(password string) error { return nil }
// Nickname return a string for user's nickname, this string may change
// depend on user's mood, return empty string if this bbs system do not support
func (u *MockUserRecord) Nickname() string { return "" }
// RealName return a string for user's real name, this string may not be changed
// return empty string if this bbs system do not support
func (u *MockUserRecord) RealName() string { return "" }
// NumLoginDays return how many days this have been login since account created.
func (u *MockUserRecord) NumLoginDays() int { return 0 }
// NumPosts return how many posts this user has posted.
func (u *MockUserRecord) NumPosts() int { return 0 }
// Money return the money this user have.
func (u *MockUserRecord) Money() int { return 0 }
// LastLogin return last login time of user
func (u *MockUserRecord) LastLogin() time.Time { return time.Now() }
// LastHost return last login host of user, it is IPv4 address usually, but it
// could be domain name or IPv6 address.
func (u *MockUserRecord) LastHost() string { return "" }
func TestGetUserInformation(t *testing.T) {
expected := NewMockUserRecord("SYSOP")
userRecs = []bbs.UserRecord{
expected,
}
req, err := http.NewRequest("GET", "/v1/users/SYSOP/information", nil)
if err != nil {
t.Fatal(err)
}
token := newAccessTokenWithUsername(expected.UserId())
t.Logf("testing token: %v", token)
req.Header.Add("Authorization", "bearer "+token)
rr := httptest.NewRecorder()
r := http.NewServeMux()
r.HandleFunc("/v1/users/", routeUsers)
r.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
responsedMap := map[string]interface{}{}
json.Unmarshal(rr.Body.Bytes(), &responsedMap)
t.Logf("got response %v", rr.Body.String())
responsedData := responsedMap["data"].(map[string]interface{})
if responsedData["user_id"] != expected.UserId() {
t.Errorf("handler returned unexpected body, user_id not match: got %v want userId %v",
rr.Body.String(), expected.UserId())
}
}
func TestParseUserPath(t *testing.T) {
type TestCase struct {
input string
expectdUserId string
expectdItem string
}
cases := []TestCase{
{
input: "/v1/users/Pichu/information",
expectdUserId: "Pichu",
expectdItem: "information",
},
{
input: "/v1/users/Pichu/",
expectdUserId: "Pichu",
expectdItem: "",
},
{
input: "/v1/users/Pichu",
expectdUserId: "Pichu",
expectdItem: "",
},
}
for index, c := range cases {
input := c.input
expectdUserId := c.expectdUserId
expectdItem := c.expectdItem
actualUserId, actualItem, err := parseUserPath(input)
if err != nil {
t.Errorf("error on index %d, got: %v", index, err)
}
if actualUserId != expectdUserId {
t.Errorf("userId not match on index %d, expected: %v, got: %v", index, expectdUserId, actualUserId)
}
if actualItem != expectdItem {
t.Errorf("item not match on index %d, expected: %v, got: %v", index, expectdItem, actualItem)
}
}
}