-
Notifications
You must be signed in to change notification settings - Fork 164
/
models_test.go
173 lines (142 loc) · 4.06 KB
/
models_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
package workwx
import (
"encoding/json"
"net/url"
"testing"
c "github.com/smartystreets/goconvey/convey"
)
func TestReqAccessToken(t *testing.T) {
c.Convey("构造一个 reqAccessToken", t, func() {
a := reqAccessToken{
CorpID: "foo",
CorpSecret: "bar",
}
c.Convey("序列化结果应该符合预期", func() {
v := a.intoURLValues()
expected := url.Values{
"corpid": []string{"foo"},
"corpsecret": []string{"bar"},
}
c.So(v, c.ShouldResemble, expected)
})
})
}
func TestRespCommon(t *testing.T) {
payloadOk := []byte(`{"errcode":0,"errmsg":"ok"}`)
payloadErr := []byte(`{"errcode":40014,"errmsg":"invalid access_token"}`)
c.Convey("构造一个成功响应的 respCommon", t, func() {
var a respCommon
err := json.Unmarshal(payloadOk, &a)
c.Convey("应该能成功反序列化", func() {
c.So(err, c.ShouldBeNil)
c.Convey("反序列化之后字段应该正确对应", func() {
c.So(a.ErrCode, c.ShouldEqual, 0)
c.So(a.ErrMsg, c.ShouldEqual, "ok")
})
c.Convey("IsOk 应该为真", func() {
c.So(a.IsOK(), c.ShouldBeTrue)
})
})
})
c.Convey("构造一个失败响应的 respCommon", t, func() {
var a respCommon
err := json.Unmarshal(payloadErr, &a)
c.Convey("应该能成功反序列化", func() {
c.So(err, c.ShouldBeNil)
c.Convey("反序列化之后字段应该正确对应", func() {
c.So(a.ErrCode, c.ShouldEqual, 40014)
c.So(a.ErrMsg, c.ShouldEqual, "invalid access_token")
})
c.Convey("IsOk 应该为假", func() {
c.So(a.IsOK(), c.ShouldBeFalse)
})
})
})
}
func TestReqMessage(t *testing.T) {
c.Convey("构造 reqMessage", t, func() {
content := map[string]interface{}{"content": "test"}
a := reqMessage{
AgentID: 233,
MsgType: "text",
Content: content,
}
c.Reset(func() {
a.ToUser = nil
a.ToParty = nil
a.ToTag = nil
a.ChatID = ""
a.Content = content
a.IsSafe = false
})
c.Convey("故意放一个不能 marshal 的 Content", func() {
a.Content = map[string]interface{}{
"heh": make(chan struct{}),
}
c.Convey("执行序列化", func() {
result, err := a.intoBody()
c.Convey("序列化应该失败", func() {
c.So(err, c.ShouldNotBeNil)
c.So(result, c.ShouldBeNil)
})
})
})
c.Convey("发给用户列表 & 设置为保密信息", func() {
a.ToUser = []string{"foo", "bar", "baz"}
a.IsSafe = true
c.Convey("执行序列化", func() {
result, err := a.intoBody()
c.Convey("序列化应该成功", func() {
c.So(err, c.ShouldBeNil)
c.Convey("序列化结果应该符合预期", func() {
expectedPayload := []byte(`{
"touser": "foo|bar|baz",
"toparty": "",
"totag": "",
"msgtype": "text",
"agentid": 233,
"text": {"content": "test"},
"safe": 1
}`)
var expected map[string]interface{}
err := json.Unmarshal(expectedPayload, &expected)
c.So(err, c.ShouldBeNil)
var actual map[string]interface{}
err = json.Unmarshal(result, &actual)
c.So(err, c.ShouldBeNil)
// we're comparing JSON *outputs*
// so assertions.ShouldEqualJSON is not suitable
c.So(actual, c.ShouldResemble, expected)
})
})
})
})
c.Convey("发给 chatid", func() {
a.ChatID = "quux"
c.Convey("执行序列化", func() {
result, err := a.intoBody()
c.Convey("序列化应该成功", func() {
c.So(err, c.ShouldBeNil)
c.Convey("序列化结果应该符合预期", func() {
expectedPayload := []byte(`{
"chatid": "quux",
"msgtype": "text",
"agentid": 233,
"text": {"content": "test"},
"safe": 0
}`)
var expected map[string]interface{}
err := json.Unmarshal(expectedPayload, &expected)
c.So(err, c.ShouldBeNil)
var actual map[string]interface{}
err = json.Unmarshal(result, &actual)
c.So(err, c.ShouldBeNil)
// we're comparing JSON *outputs*
// so assertions.ShouldEqualJSON is not suitable
c.So(actual, c.ShouldResemble, expected)
})
})
})
})
})
}