1 | # -*- coding: utf-8 -*- |
||
2 | from __future__ import absolute_import, unicode_literals |
||
3 | import os |
||
4 | import six |
||
5 | import unittest |
||
6 | |||
7 | from httmock import urlmatch, HTTMock, response |
||
8 | |||
9 | from wechatpy.enterprise import WeChatClient |
||
10 | from wechatpy.exceptions import WeChatClientException |
||
11 | from wechatpy.utils import json |
||
12 | |||
13 | |||
14 | _TESTS_PATH = os.path.abspath(os.path.dirname(__file__)) |
||
15 | _FIXTURE_PATH = os.path.join(_TESTS_PATH, 'fixtures', 'enterprise') |
||
16 | |||
17 | |||
18 | View Code Duplication | @urlmatch(netloc=r'(.*\.)?qyapi\.weixin\.qq\.com$') |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
19 | def wechat_api_mock(url, request): |
||
20 | path = url.path.replace('/cgi-bin/', '').replace('/', '_') |
||
21 | res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path) |
||
22 | content = { |
||
23 | 'errcode': 99999, |
||
24 | 'errmsg': 'can not find fixture %s' % res_file, |
||
25 | } |
||
26 | headers = { |
||
27 | 'Content-Type': 'application/json' |
||
28 | } |
||
29 | try: |
||
30 | with open(res_file, 'rb') as f: |
||
31 | content = json.loads(f.read().decode('utf-8')) |
||
32 | except (IOError, ValueError) as e: |
||
33 | content['errmsg'] = 'Loads fixture {0} failed, error: {1}'.format( |
||
34 | res_file, |
||
35 | e |
||
36 | ) |
||
37 | return response(200, content, headers, request=request) |
||
38 | |||
39 | |||
40 | class WeChatClientTestCase(unittest.TestCase): |
||
41 | corp_id = '123456' |
||
42 | secret = '123456' |
||
43 | |||
44 | def setUp(self): |
||
45 | self.client = WeChatClient(self.corp_id, self.secret) |
||
46 | |||
47 | def test_fetch_access_token(self): |
||
48 | with HTTMock(wechat_api_mock): |
||
49 | token = self.client.fetch_access_token() |
||
50 | self.assertEqual('1234567890', token['access_token']) |
||
51 | self.assertEqual(7200, token['expires_in']) |
||
52 | self.assertEqual('1234567890', self.client.access_token) |
||
53 | |||
54 | def test_get_wechat_ips(self): |
||
55 | with HTTMock(wechat_api_mock): |
||
56 | res = self.client.misc.get_wechat_ips() |
||
57 | self.assertEqual(['127.0.0.1'], res) |
||
58 | |||
59 | def test_department_create(self): |
||
60 | with HTTMock(wechat_api_mock): |
||
61 | res = self.client.department.create('Test') |
||
62 | self.assertEqual(2, res['id']) |
||
63 | |||
64 | def test_department_update(self): |
||
65 | with HTTMock(wechat_api_mock): |
||
66 | res = self.client.department.update(2, 'Test 1') |
||
67 | self.assertEqual(0, res['errcode']) |
||
68 | |||
69 | def test_department_delete(self): |
||
70 | with HTTMock(wechat_api_mock): |
||
71 | res = self.client.department.delete(2) |
||
72 | self.assertEqual(0, res['errcode']) |
||
73 | |||
74 | def test_department_get(self): |
||
75 | with HTTMock(wechat_api_mock): |
||
76 | res = self.client.department.get() |
||
77 | self.assertEqual(2, len(res)) |
||
78 | |||
79 | def test_department_get_users(self): |
||
80 | with HTTMock(wechat_api_mock): |
||
81 | res = self.client.department.get_users(2) |
||
82 | self.assertEqual(1, len(res)) |
||
83 | |||
84 | def test_department_get_users_detail(self): |
||
85 | with HTTMock(wechat_api_mock): |
||
86 | res = self.client.department.get_users(2, simple=False) |
||
87 | self.assertEqual(1, len(res)) |
||
88 | |||
89 | def test_department_map_users(self): |
||
90 | with HTTMock(wechat_api_mock): |
||
91 | users = self.client.department.get_map_users(2, key='email') |
||
92 | self.assertEqual(users, {'[email protected]': 'zhangthree-userid'}) |
||
93 | |||
94 | users = self.client.department.get_map_users(key='mobile') |
||
95 | self.assertEqual(users, {'15723333333': 'zhangthree-userid'}) |
||
96 | |||
97 | def test_tag_create(self): |
||
98 | with HTTMock(wechat_api_mock): |
||
99 | res = self.client.tag.create('test') |
||
100 | self.assertEqual('1', res['tagid']) |
||
101 | |||
102 | def test_tag_update(self): |
||
103 | with HTTMock(wechat_api_mock): |
||
104 | res = self.client.tag.update(1, 'test') |
||
105 | self.assertEqual(0, res['errcode']) |
||
106 | |||
107 | def test_tag_delete(self): |
||
108 | with HTTMock(wechat_api_mock): |
||
109 | res = self.client.tag.delete(1) |
||
110 | self.assertEqual(0, res['errcode']) |
||
111 | |||
112 | def test_tag_get_users(self): |
||
113 | with HTTMock(wechat_api_mock): |
||
114 | res = self.client.tag.get_users(1) |
||
115 | self.assertEqual(1, len(res['userlist'])) |
||
116 | self.assertEqual(1, len(res['partylist'])) |
||
117 | |||
118 | def test_tag_add_users(self): |
||
119 | with HTTMock(wechat_api_mock): |
||
120 | res = self.client.tag.add_users(1, [1, 2, 3]) |
||
121 | self.assertEqual(0, res['errcode']) |
||
122 | |||
123 | def test_tag_delete_users(self): |
||
124 | with HTTMock(wechat_api_mock): |
||
125 | res = self.client.tag.delete_users(1, [1, 2, 3]) |
||
126 | self.assertEqual(0, res['errcode']) |
||
127 | |||
128 | def test_tag_list(self): |
||
129 | with HTTMock(wechat_api_mock): |
||
130 | res = self.client.tag.list() |
||
131 | self.assertEqual(2, len(res)) |
||
132 | |||
133 | def test_batch_invite_user(self): |
||
134 | with HTTMock(wechat_api_mock): |
||
135 | res = self.client.batch.invite_user( |
||
136 | 'http://example.com', |
||
137 | '123456', |
||
138 | '123456', |
||
139 | '123|456', |
||
140 | [123, 456], |
||
141 | (12, 34), |
||
142 | '' |
||
143 | ) |
||
144 | self.assertEqual(0, res['errcode']) |
||
145 | |||
146 | def test_batch_sync_user(self): |
||
147 | with HTTMock(wechat_api_mock): |
||
148 | res = self.client.batch.sync_user( |
||
149 | 'http://example.com', |
||
150 | '123456', |
||
151 | '123456', |
||
152 | '12345678' |
||
153 | ) |
||
154 | self.assertEqual(0, res['errcode']) |
||
155 | |||
156 | def test_batch_replace_user(self): |
||
157 | with HTTMock(wechat_api_mock): |
||
158 | res = self.client.batch.replace_user( |
||
159 | 'http://example.com', |
||
160 | '123456', |
||
161 | '123456', |
||
162 | '12345678' |
||
163 | ) |
||
164 | self.assertEqual(0, res['errcode']) |
||
165 | |||
166 | def test_batch_replace_party(self): |
||
167 | with HTTMock(wechat_api_mock): |
||
168 | res = self.client.batch.replace_party( |
||
169 | 'http://example.com', |
||
170 | '123456', |
||
171 | '123456', |
||
172 | '12345678' |
||
173 | ) |
||
174 | self.assertEqual(0, res['errcode']) |
||
175 | |||
176 | def test_batch_get_result(self): |
||
177 | with HTTMock(wechat_api_mock): |
||
178 | res = self.client.batch.get_result('123456') |
||
179 | self.assertEqual(0, res['errcode']) |
||
180 | self.assertEqual(1, res['status']) |
||
181 | |||
182 | def test_jsapi_get_ticket(self): |
||
183 | with HTTMock(wechat_api_mock): |
||
184 | result = self.client.jsapi.get_ticket() |
||
185 | self.assertEqual( |
||
186 | 'bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA', # NOQA |
||
187 | result['ticket'] |
||
188 | ) |
||
189 | self.assertEqual(7200, result['expires_in']) |
||
190 | |||
191 | def test_jsapi_get_jsapi_signature(self): |
||
192 | noncestr = 'Wm3WZYTPz0wzccnW' |
||
193 | ticket = 'sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg' # NOQA |
||
194 | timestamp = 1414587457 |
||
195 | url = 'http://mp.weixin.qq.com?params=value' |
||
196 | signature = self.client.jsapi.get_jsapi_signature( |
||
197 | noncestr, |
||
198 | ticket, |
||
199 | timestamp, |
||
200 | url |
||
201 | ) |
||
202 | self.assertEqual( |
||
203 | '0f9de62fce790f9a083d5c99e95740ceb90c27ed', |
||
204 | signature |
||
205 | ) |
||
206 | |||
207 | def test_user_convert_to_openid(self): |
||
208 | with HTTMock(wechat_api_mock): |
||
209 | res = self.client.user.convert_to_openid('zhangsan') |
||
210 | self.assertEqual('oDOGms-6yCnGrRovBj2yHij5JL6E', res['openid']) |
||
211 | self.assertEqual('wxf874e15f78cc84a7', res['appid']) |
||
212 | |||
213 | def test_user_convert_to_user_id(self): |
||
214 | with HTTMock(wechat_api_mock): |
||
215 | user_id = self.client.user.convert_to_user_id( |
||
216 | 'oDOGms-6yCnGrRovBj2yHij5JL6E' |
||
217 | ) |
||
218 | self.assertEqual('zhangsan', user_id) |
||
219 | |||
220 | def test_upload_media(self): |
||
221 | media_file = six.StringIO('nothing') |
||
222 | with HTTMock(wechat_api_mock): |
||
223 | media = self.client.media.upload('image', media_file) |
||
224 | self.assertEqual('image', media['type']) |
||
225 | self.assertEqual('12345678', media['media_id']) |
||
226 | |||
227 | def test_material_get_count(self): |
||
228 | with HTTMock(wechat_api_mock): |
||
229 | res = self.client.material.get_count(1) |
||
230 | self.assertEqual(37, res['total_count']) |
||
231 | self.assertEqual(3, res['video_count']) |
||
232 | self.assertEqual(10, res['voice_count']) |
||
233 | self.assertEqual(12, res['image_count']) |
||
234 | self.assertEqual(3, res['file_count']) |
||
235 | self.assertEqual(6, res['mpnews_count']) |
||
236 | |||
237 | def test_material_batchget_mpnews(self): |
||
238 | with HTTMock(wechat_api_mock): |
||
239 | res = self.client.material.batchget(1, 'mpnews') |
||
240 | self.assertEqual('mpnews', res['type']) |
||
241 | self.assertEqual(20, res['total_count']) |
||
242 | self.assertEqual(3, res['item_count']) |
||
243 | self.assertEqual( |
||
244 | '2-G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu', |
||
245 | res['itemlist'][0]['media_id'] |
||
246 | ) |
||
247 | |||
248 | def test_material_delete(self): |
||
249 | media_id = '2-G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu' |
||
250 | with HTTMock(wechat_api_mock): |
||
251 | res = self.client.material.delete(1, media_id) |
||
252 | self.assertEqual('deleted', res['errmsg']) |
||
253 | |||
254 | def test_material_get_mpnews(self): |
||
255 | media_id = '2-G6nrLmr5EC3MMb_-zK1dDdzmd0p7cNliYu' |
||
256 | with HTTMock(wechat_api_mock): |
||
257 | res = self.client.material.get_articles(1, media_id) |
||
258 | self.assertEqual('mpnews', res['type']) |
||
259 | self.assertEqual( |
||
260 | '2-G6nrLmr5EC3MMb_-zK1dDdzmd0' + |
||
261 | 'p7cNliYu9V5w7o8K0HuucGBZCzw4HmLa5C', |
||
262 | res['mpnews']['articles'][0]['thumb_media_id'] |
||
263 | ) |
||
264 | self.assertEqual( |
||
265 | '2-G6nrLmr5EC3MMb_-zK1dDdzmd0' + |
||
266 | 'p7cNliYu9V5w7oovsUPf3wG4t9N3tE', |
||
267 | res['mpnews']['articles'][1]['thumb_media_id'] |
||
268 | ) |
||
269 | |||
270 | View Code Duplication | def test_reraise_requests_exception(self): |
|
0 ignored issues
–
show
|
|||
271 | @urlmatch(netloc=r'(.*\.)?qyapi\.weixin\.qq\.com$') |
||
272 | def _wechat_api_mock(url, request): |
||
273 | return {'status_code': 404, 'content': '404 not found'} |
||
274 | |||
275 | try: |
||
276 | with HTTMock(_wechat_api_mock): |
||
277 | self.client.material.get_count(1) |
||
278 | except WeChatClientException as e: |
||
279 | self.assertEqual(404, e.response.status_code) |
||
280 | |||
281 | def test_shakearound_get_shake_info(self): |
||
282 | with HTTMock(wechat_api_mock): |
||
283 | res = self.client.shakearound.get_shake_info('123456') |
||
284 | self.assertEqual(14000, res['page_id']) |
||
285 | self.assertEqual('zhangsan', res['userid']) |
||
286 | |||
287 | def test_service_get_provider_token(self): |
||
288 | with HTTMock(wechat_api_mock): |
||
289 | res = self.client.service.get_provider_token('provider_secret') |
||
290 | |||
291 | self.assertEqual(7200, res['expires_in']) |
||
292 | self.assertEqual('enLSZ5xxxxxxJRL', res['provider_access_token']) |
||
293 | |||
294 | def test_service_get_login_info(self): |
||
295 | with HTTMock(wechat_api_mock): |
||
296 | res = self.client.service.get_login_info( |
||
297 | 'enLSZ5xxxxxxJRL', |
||
298 | 'auth_code' |
||
299 | ) |
||
300 | |||
301 | self.assertTrue(res['is_sys']) |
||
302 | self.assertTrue(res['is_inner']) |
||
303 | |||
304 | def test_chat_create(self): |
||
305 | with HTTMock(wechat_api_mock): |
||
306 | res = self.client.chat.create( |
||
307 | '1', 'chat', 'zhangsan', ['zhangsan', 'lisi', 'wangwu'] |
||
308 | ) |
||
309 | |||
310 | self.assertEqual(0, res['errcode']) |
||
311 | |||
312 | def test_chat_get(self): |
||
313 | with HTTMock(wechat_api_mock): |
||
314 | chat = self.client.chat.get('235364212115767297') |
||
315 | |||
316 | self.assertEqual('235364212115767297', chat['chatid']) |
||
317 | self.assertEqual('zhangsan', chat['owner']) |
||
318 | |||
319 | def test_chat_update(self): |
||
320 | with HTTMock(wechat_api_mock): |
||
321 | res = self.client.chat.update( |
||
322 | '235364212115767297', |
||
323 | 'lisi', |
||
324 | '企业应用中心', |
||
325 | 'zhangsan', |
||
326 | ['zhaoli'], |
||
327 | ['zhangsan'] |
||
328 | ) |
||
329 | |||
330 | self.assertEqual(0, res['errcode']) |
||
331 | |||
332 | def test_chat_quit(self): |
||
333 | with HTTMock(wechat_api_mock): |
||
334 | res = self.client.chat.quit('235364212115767297', 'lisi') |
||
335 | |||
336 | self.assertEqual(0, res['errcode']) |
||
337 | |||
338 | def test_chat_clear_notify(self): |
||
339 | with HTTMock(wechat_api_mock): |
||
340 | res = self.client.chat.clear_notify('zhangsan', 'single', 'lisi') |
||
341 | |||
342 | self.assertEqual(0, res['errcode']) |
||
343 | |||
344 | def test_chat_set_mute(self): |
||
345 | mute_list = [ |
||
346 | {'userid': 'zhangsan', 'status': 0}, |
||
347 | {'userid': 'lisi', 'status': 1}, |
||
348 | ] |
||
349 | with HTTMock(wechat_api_mock): |
||
350 | res = self.client.chat.set_mute(mute_list) |
||
351 | |||
352 | self.assertEqual(0, res['errcode']) |
||
353 | self.assertEqual(['zhangsan'], res['invaliduser']) |
||
354 | |||
355 | def test_chat_send_text(self): |
||
356 | with HTTMock(wechat_api_mock): |
||
357 | res = self.client.chat.send_text( |
||
358 | 'zhangsan', 'single', 'lisi', 'hello' |
||
359 | ) |
||
360 | |||
361 | self.assertEqual(0, res['errcode']) |
||
362 | |||
363 | def test_chat_send_image(self): |
||
364 | with HTTMock(wechat_api_mock): |
||
365 | res = self.client.chat.send_image( |
||
366 | 'zhangsan', 'single', 'lisi', 'media_id' |
||
367 | ) |
||
368 | |||
369 | self.assertEqual(0, res['errcode']) |
||
370 | |||
371 | def test_chat_send_file(self): |
||
372 | with HTTMock(wechat_api_mock): |
||
373 | res = self.client.chat.send_file( |
||
374 | 'zhangsan', 'single', 'lisi', 'media_id' |
||
375 | ) |
||
376 | |||
377 | self.assertEqual(0, res['errcode']) |
||
378 | |||
379 | def test_external_contact_get_follow_user_list(self): |
||
380 | with HTTMock(wechat_api_mock): |
||
381 | res = self.client.external_contact.get_follow_user_list() |
||
382 | self.assertEqual(0, res['errcode']) |
||
383 | |||
384 | def test_external_contact_list(self): |
||
385 | with HTTMock(wechat_api_mock): |
||
386 | res = self.client.external_contact.list('userid') |
||
387 | self.assertEqual(0, res['errcode']) |
||
388 | |||
389 | def test_external_contact_get(self): |
||
390 | with HTTMock(wechat_api_mock): |
||
391 | res = self.client.external_contact.get('external_userid') |
||
392 | self.assertEqual(0, res['errcode']) |
||
393 | |||
394 | def test_external_contact_add_contact_way(self): |
||
395 | with HTTMock(wechat_api_mock): |
||
396 | res = self.client.external_contact.add_contact_way( |
||
397 | 1, 1, 1, 'remark', True, 'state', ['UserID1', 'UserID2'], |
||
398 | ['PartyID1', 'PartyID2'] |
||
399 | ) |
||
400 | self.assertEqual(0, res['errcode']) |
||
401 | |||
402 | def test_external_contact_get_contact_way(self): |
||
403 | with HTTMock(wechat_api_mock): |
||
404 | res = self.client.external_contact.get_contact_way( |
||
405 | '42b34949e138eb6e027c123cba77fad7' |
||
406 | ) |
||
407 | self.assertEqual(0, res['errcode']) |
||
408 | |||
409 | def test_external_contact_update_contact_way(self): |
||
410 | with HTTMock(wechat_api_mock): |
||
411 | res = self.client.external_contact.update_contact_way( |
||
412 | '42b34949e138eb6e027c123cba77fad7', '渠道客户', True, |
||
413 | 1, 'teststate', ['UserID1', 'UserID2', 'UserID3'], |
||
414 | ['PartyID1', 'PartyID2'] |
||
415 | ) |
||
416 | self.assertEqual(0, res['errcode']) |
||
417 | |||
418 | def test_external_contact_del_contact_way(self): |
||
419 | with HTTMock(wechat_api_mock): |
||
420 | res = self.client.external_contact.del_contact_way( |
||
421 | '42b34949e138eb6e027c123cba77fad7' |
||
422 | ) |
||
423 | self.assertEqual(0, res['errcode']) |
||
424 | |||
425 | def test_external_contact_add_msg_template(self): |
||
426 | with HTTMock(wechat_api_mock): |
||
427 | res = self.client.external_contact.add_msg_template( |
||
428 | { |
||
429 | "external_userid": [ |
||
430 | "woAJ2GCAAAXtWyujaWJHDDGi0mACas1w", |
||
431 | "wmqfasd1e1927831291723123109r712" |
||
432 | ], |
||
433 | "sender": "zhangsan", |
||
434 | "text": { |
||
435 | "content": "文本消息内容" |
||
436 | }, |
||
437 | "image": { |
||
438 | "media_id": "MEDIA_ID" |
||
439 | } |
||
440 | } |
||
441 | ) |
||
442 | self.assertEqual(0, res['errcode']) |
||
443 | |||
444 | def test_external_contact_get_group_msg_result(self): |
||
445 | with HTTMock(wechat_api_mock): |
||
446 | res = self.client.external_contact.get_group_msg_result( |
||
447 | 'msgGCAAAXtWyujaWJHDDGi0mACas1w' |
||
448 | ) |
||
449 | self.assertEqual(0, res['errcode']) |
||
450 | |||
451 | def test_external_contact_get_user_behavior_data(self): |
||
452 | with HTTMock(wechat_api_mock): |
||
453 | res = self.client.external_contact.get_user_behavior_data( |
||
454 | ["zhangsan", "lisi"], 1536508800, 1536940800 |
||
455 | ) |
||
456 | self.assertEqual(0, res['errcode']) |
||
457 | |||
458 | def test_external_contact_send_welcome_msg(self): |
||
459 | with HTTMock(wechat_api_mock): |
||
460 | res = self.client.external_contact.send_welcome_msg( |
||
461 | { |
||
462 | "welcome_code": "CALLBACK_CODE", |
||
463 | "text": { |
||
464 | "content": "文本消息内容" |
||
465 | }, |
||
466 | "image": { |
||
467 | "media_id": "MEDIA_ID" |
||
468 | }, |
||
469 | "link": { |
||
470 | "title": "消息标题", |
||
471 | "picurl": "https://example.pic.com/path", |
||
472 | "desc": "消息描述", |
||
473 | "url": "https://example.link.com/path" |
||
474 | }, |
||
475 | "miniprogram": { |
||
476 | "title": "消息标题", |
||
477 | "pic_media_id": "MEDIA_ID", |
||
478 | "appid": "wx8bd80126147df384", |
||
479 | "page": "/path/index" |
||
480 | } |
||
481 | } |
||
482 | ) |
||
483 | self.assertEqual(0, res['errcode']) |
||
484 | |||
485 | def test_external_contact_get_unassigned_list(self): |
||
486 | with HTTMock(wechat_api_mock): |
||
487 | res = self.client.external_contact.get_unassigned_list( |
||
488 | 0, 100 |
||
489 | ) |
||
490 | self.assertEqual(0, res['errcode']) |
||
491 | |||
492 | def test_external_contact_transfer(self): |
||
493 | with HTTMock(wechat_api_mock): |
||
494 | res = self.client.external_contact.transfer( |
||
495 | "woAJ2GCAAAXtWyujaWJHDDGi0mACH71w", "zhangsan", "lisi" |
||
496 | ) |
||
497 | self.assertEqual(0, res['errcode']) |
||
498 |