forked from offu/WeRoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_replies.py
338 lines (296 loc) · 9.21 KB
/
test_replies.py
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding:utf-8 -*-
import time
import pytest
from werobot.parser import parse_user_msg
from werobot.replies import WeChatReply, TextReply, ImageReply, MusicReply
from werobot.replies import VoiceReply, VideoReply
from werobot.replies import Article, ArticlesReply
from werobot.replies import TransferCustomerServiceReply, SuccessReply
from werobot.replies import process_function_reply
from werobot.utils import to_binary, to_text
def test_wechat_reply():
message = parse_user_msg("""
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[this is a url]]></PicUrl>
<MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>
""")
s = to_binary("喵fdsjaklfsk")
reply = WeChatReply(message=message, s=s)
assert reply._args['source'] == 'toUser'
assert reply._args['target'] == 'fromUser'
assert reply._args['s'] == to_text(s)
assert isinstance(reply._args['time'], int)
def test_text_reply():
t = int(time.time())
reply = TextReply(
target='fromUser', source='toUser',
content="aa", time=t
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[fromUser]]></ToUserName>
<FromUserName><![CDATA[toUser]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[aa]]></Content>
</xml>""".format(time=t).strip()
def test_image_reply():
t = int(time.time())
reply = ImageReply(
target='fromUser',
source='toUser',
media_id="fdasfdasfasd", time=t
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[fromUser]]></ToUserName>
<FromUserName><![CDATA[toUser]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[fdasfdasfasd]]></MediaId>
</Image>
</xml>""".format(time=t).strip()
def test_voice_reply():
t = int(time.time())
reply = VoiceReply(
target='tgu',
source='su',
media_id="fdasfdasfasd", time=t
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[tgu]]></ToUserName>
<FromUserName><![CDATA[su]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
<Voice>
<MediaId><![CDATA[fdasfdasfasd]]></MediaId>
</Voice>
</xml>""".format(time=t).strip()
def test_video_reply():
t = int(time.time())
reply = VideoReply(
target='tgu',
source='su',
media_id="fdasfdasfasd", time=t,
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[tgu]]></ToUserName>
<FromUserName><![CDATA[su]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[fdasfdasfasd]]></MediaId>
<Title><![CDATA[]]></Title>
<Description><![CDATA[]]></Description>
</Video>
</xml>""".format(time=t).strip()
reply_2 = VideoReply(
target='tgu',
source='su',
media_id="fdasfdasfasd", time=t,
title='meow'
)
assert reply_2.render().strip() == """
<xml>
<ToUserName><![CDATA[tgu]]></ToUserName>
<FromUserName><![CDATA[su]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[fdasfdasfasd]]></MediaId>
<Title><![CDATA[meow]]></Title>
<Description><![CDATA[]]></Description>
</Video>
</xml>""".format(time=t).strip()
reply_3 = VideoReply(
target='tgu',
source='su',
media_id="fdasfdasfasd", time=t,
title='meow',
description='www'
)
assert reply_3.render().strip() == """
<xml>
<ToUserName><![CDATA[tgu]]></ToUserName>
<FromUserName><![CDATA[su]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<Video>
<MediaId><![CDATA[fdasfdasfasd]]></MediaId>
<Title><![CDATA[meow]]></Title>
<Description><![CDATA[www]]></Description>
</Video>
</xml>""".format(time=t).strip()
def test_video_reply_process_args():
reply = VideoReply(
target='tgu',
source='su',
media_id="fdasfdasfasd",
)
assert reply._args['title'] == ''
assert reply._args['description'] == ''
def test_music_reply():
t = int(time.time())
reply = MusicReply(
target='tg',
source='ss',
time=t,
title='tt',
description='ds',
url='u1',
hq_url='u2',
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[tg]]></ToUserName>
<FromUserName><![CDATA[ss]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[tt]]></Title>
<Description><![CDATA[ds]]></Description>
<MusicUrl><![CDATA[u1]]></MusicUrl>
<HQMusicUrl><![CDATA[u2]]></HQMusicUrl>
</Music>
</xml>""".format(time=t).strip()
def test_music_reply_process_args():
reply = MusicReply(
target='tg',
source='ss',
title='tt',
description='ds',
url='u1',
)
assert reply._args['hq_url'] == 'u1'
reply_2 = MusicReply(
target='tg',
source='ss',
title='tt',
description='ds',
url='u1',
hq_url='u2'
)
assert reply_2._args['hq_url'] == 'u2'
def test_article():
article = Article(
title="tt",
description=to_binary("附近的萨卡里发生"),
img="http",
url="uuu"
)
assert article.render().strip() == to_text("""
<item>
<Title><![CDATA[tt]]></Title>
<Description><![CDATA[附近的萨卡里发生]]></Description>
<PicUrl><![CDATA[http]]></PicUrl>
<Url><![CDATA[uuu]]></Url>
</item>
""").strip()
def test_articles_reply():
article = Article(
title="tt",
description="附近的萨卡里发生",
img="http",
url="uuu"
)
t = int(time.time())
reply = ArticlesReply(
target='tg',
source='ss',
time=t
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[tg]]></ToUserName>
<FromUserName><![CDATA[ss]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>0</ArticleCount>
<Articles></Articles>
</xml>""".format(time=t).strip()
reply._args['content'] = 'wwww'
assert '<Content><![CDATA[wwww]]></Content>' in reply.render()
reply.add_article(article)
assert '<ArticleCount>1</ArticleCount>' in reply.render()
assert article.render() in reply.render()
for _ in range(9):
reply.add_article(article)
assert '<ArticleCount>10</ArticleCount>' in reply.render()
with pytest.raises(AttributeError):
reply.add_article(article)
def test_transfer_customer_service_reply():
t = int(time.time())
reply = TransferCustomerServiceReply(
source='aaa',
target='bbb',
time=t,
account="test1@test"
)
assert reply.render().strip() == """
<xml>
<ToUserName><![CDATA[bbb]]></ToUserName>
<FromUserName><![CDATA[aaa]]></FromUserName>
<CreateTime>{time}</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
<TransInfo>
<KfAccount><![CDATA[test1@test]]></KfAccount>
</TransInfo>
</xml>
""".format(time=t).strip()
def test_success_reply():
assert SuccessReply().render() == "success"
def test_process_unknown_function_reply():
reply = SuccessReply()
assert process_function_reply(reply) == reply
def test_process_text_function_reply():
reply = process_function_reply("test")
assert isinstance(reply, TextReply)
assert reply.content == "test"
def test_process_music_function_reply():
reply = process_function_reply([
"title",
"desc",
"url"
])
assert isinstance(reply, MusicReply)
assert reply.title == "title"
assert reply.description == "desc"
assert reply.url == reply.hq_url == "url"
reply = process_function_reply(
[
"title",
"desc",
"url",
"hq"
])
assert isinstance(reply, MusicReply)
assert reply.title == "title"
assert reply.description == "desc"
assert reply.url == "url"
assert reply.hq_url == "hq"
def test_process_articles_function_reply():
reply = process_function_reply([
["tt1", 'ds1', 'img', 'url'],
["tt2", 'ds2', 'im2g', 'u2rl']
])
assert isinstance(reply, ArticlesReply)
assert len(reply._articles) == 2
article_1, article_2 = reply._articles
assert isinstance(article_1, Article), isinstance(article_2, Article)
assert article_1.title == "tt1", article_2.title == "tt2"
assert article_1.description == "ds1", article_2.description == "ds2"
assert article_1.img == "img", article_2.img == "im2g"
assert article_1.url == "url", article_2.url == "u2rl"
process_function_reply([[1, 2, 3, 4]] * 10)
with pytest.raises(AttributeError):
process_function_reply([[1, 2, 3, 4]] * 11)