1 | # -*- coding: utf-8 -*- |
||
2 | from __future__ import absolute_import, unicode_literals |
||
3 | import os |
||
4 | import unittest |
||
5 | |||
6 | from httmock import urlmatch, HTTMock, response |
||
7 | |||
8 | from wechatpy.component import WeChatComponent |
||
9 | from wechatpy.utils import json |
||
10 | |||
11 | |||
12 | _TESTS_PATH = os.path.abspath(os.path.dirname(__file__)) |
||
13 | _FIXTURE_PATH = os.path.join(_TESTS_PATH, 'fixtures', 'component') |
||
14 | |||
15 | |||
16 | View Code Duplication | @urlmatch(netloc=r'(.*\.)?api\.weixin\.qq\.com$') |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
17 | def wechat_api_mock(url, request): |
||
18 | path = url.path.replace('/cgi-bin/component/', '').replace('/', '_') |
||
19 | res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path) |
||
20 | content = { |
||
21 | 'errcode': 99999, |
||
22 | 'errmsg': 'can not find fixture %s' % res_file, |
||
23 | } |
||
24 | headers = { |
||
25 | 'Content-Type': 'application/json' |
||
26 | } |
||
27 | try: |
||
28 | with open(res_file, 'rb') as f: |
||
29 | content = json.loads(f.read().decode('utf-8')) |
||
30 | except (IOError, ValueError): |
||
31 | pass |
||
32 | return response(200, content, headers, request=request) |
||
33 | |||
34 | |||
35 | class WeChatComponentTestCase(unittest.TestCase): |
||
36 | app_id = '123456' |
||
37 | app_secret = '123456' |
||
38 | token = 'sdfusfsssdc' |
||
39 | encoding_aes_key = 'yguy3495y79o34vod7843933902h9gb2834hgpB90rg' |
||
40 | |||
41 | def setUp(self): |
||
42 | self.client = WeChatComponent( |
||
43 | self.app_id, self.app_secret, self.token, self.encoding_aes_key) |
||
44 | |||
45 | def test_fetch_access_token(self): |
||
46 | with HTTMock(wechat_api_mock): |
||
47 | token = self.client.fetch_access_token() |
||
48 | self.assertEqual('1234567890', token['component_access_token']) |
||
49 | self.assertEqual(7200, token['expires_in']) |
||
50 | self.assertEqual('1234567890', self.client.access_token) |
||
51 | |||
52 | def test_create_preauthcode(self): |
||
53 | with HTTMock(wechat_api_mock): |
||
54 | result = self.client.create_preauthcode() |
||
55 | self.assertEqual('1234567890', result['pre_auth_code']) |
||
56 | self.assertEqual(600, result['expires_in']) |
||
57 | |||
58 | def test_query_auth(self): |
||
59 | authorization_code = '1234567890' |
||
60 | with HTTMock(wechat_api_mock): |
||
61 | result = self.client.query_auth(authorization_code) |
||
62 | self.assertEqual( |
||
63 | 'wxf8b4f85f3a794e77', |
||
64 | result['authorization_info']['authorizer_appid'] |
||
65 | ) |
||
66 | |||
67 | def test_refresh_authorizer_token(self): |
||
68 | appid = 'appid' |
||
69 | refresh_token = 'refresh_token' |
||
70 | with HTTMock(wechat_api_mock): |
||
71 | result = self.client.refresh_authorizer_token(appid, refresh_token) |
||
72 | self.assertEqual('1234567890', result['authorizer_access_token']) |
||
73 | self.assertEqual('123456789', result['authorizer_refresh_token']) |
||
74 | self.assertEqual(7200, result['expires_in']) |
||
75 | |||
76 | def test_get_authorizer_info(self): |
||
77 | authorizer_appid = 'wxf8b4f85f3a794e77' |
||
78 | with HTTMock(wechat_api_mock): |
||
79 | result = self.client.get_authorizer_info(authorizer_appid) |
||
80 | self.assertEqual('paytest01', result['authorizer_info']['alias']) |
||
81 | |||
82 | def test_get_authorizer_option(self): |
||
83 | with HTTMock(wechat_api_mock): |
||
84 | appid = 'wxf8b4f85f3a794e77' |
||
85 | result = self.client.get_authorizer_option(appid, 'voice_recognize') |
||
86 | self.assertEqual('voice_recognize', result['option_name']) |
||
87 | self.assertEqual('1', result['option_value']) |
||
88 | |||
89 | def test_set_authorizer_option(self): |
||
90 | with HTTMock(wechat_api_mock): |
||
91 | appid = 'wxf8b4f85f3a794e77' |
||
92 | result = self.client.set_authorizer_option( |
||
93 | appid, 'voice_recognize', '0' |
||
94 | ) |
||
95 | self.assertEqual(0, result['errcode']) |
||
96 |