Issues (41)

tests/test_external_contact.py (1 issue)

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
from wechatpy.enterprise import WeChatClient
8
from wechatpy.utils import json
9
10
_TESTS_PATH = os.path.abspath(os.path.dirname(__file__))
11
_FIXTURE_PATH = os.path.join(_TESTS_PATH, 'fixtures', 'enterprise')
12
13
14 View Code Duplication
@urlmatch(netloc=r'(.*\.)?qyapi\.weixin\.qq\.com$')
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
15
def wechat_api_mock(url, request):
16
    path = url.path.replace('/cgi-bin/', '').replace('/', '_')
17
    res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path)
18
    content = {
19
        'errcode': 99999,
20
        'errmsg': 'can not find fixture %s' % res_file,
21
    }
22
    headers = {
23
        'Content-Type': 'application/json'
24
    }
25
    try:
26
        with open(res_file, 'rb') as f:
27
            content = json.loads(f.read().decode('utf-8'))
28
    except (IOError, ValueError) as e:
29
        content['errmsg'] = 'Loads fixture {0} failed, error: {1}'.format(
30
            res_file,
31
            e
32
        )
33
    return response(200, content, headers, request=request)
34
35
36
class WeChatClientTestCase(unittest.TestCase):
37
    app_id = '123456'
38
    secret = '123456'
39
40
    def setUp(self):
41
        self.client = WeChatClient(self.app_id, self.secret)
42
43
    def test_ec_addcorptag(self):
44
        tags = [{
45
            "name": "大鸟"
46
        },
47
            {
48
                "name": "小菜"
49
            }]
50
        with HTTMock(wechat_api_mock):
51
            res = self.client.external_contact.add_corp_tag(None, "开发1组", 1, tags=tags)
52
        self.assertEqual(0, res['errcode'])
53
54
    def test_ec_edit_corp_tag(self):
55
        with HTTMock(wechat_api_mock):
56
            res = self.client.external_contact.edit_corp_tag('etm7wjCgAA-DYuu_JX8DrN0EUfa1ycDw', '开发2组', 1)
57
        self.assertEqual(0, res['errcode'])
58
59
    def test_ec_del_corp_tag(self):
60
        with HTTMock(wechat_api_mock):
61
            res = self.client.external_contact.del_corp_tag(tag_id=['etm7wjCgAAADvErs_p_VhdNdN6-i2zAg'])
62
        self.assertEqual(0, res['errcode'])
63
64
    def test_ec_mark_tag(self):
65
        with HTTMock(wechat_api_mock):
66
            res = self.client.external_contact.mark_tag('zm', 'wmm7wjCgAAkLAv_eiVt53eBokOC3_Tww',
67
                                                        add_tag=['etm7wjCgAAD5hhvyfhPUpBbCs0CYuQMg'])
68
        self.assertEqual(0, res['errcode'])
69