Issues (41)

tests/test_oauth.py (2 issues)

1
# -*- coding: utf-8 -*-
2
from __future__ import absolute_import, print_function, unicode_literals
3
4
import os
5
import unittest
6
7
from httmock import HTTMock, response, urlmatch
8
9
from wechatpy import ComponentOAuth, WeChatOAuth
10
from wechatpy.exceptions import WeChatClientException
11
from wechatpy.utils import json
12
13
_TESTS_PATH = os.path.abspath(os.path.dirname(__file__))
14
_FIXTURE_PATH = os.path.join(_TESTS_PATH, 'fixtures')
15
16
17
@urlmatch(netloc=r'(.*\.)?api\.weixin\.qq\.com$')
18
def wechat_api_mock(url, request):
19
    path = url.path[1:].replace('/', '_')
20
    res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path)
21
    content = {
22
        'errcode': 99999,
23
        'errmsg': 'can not find fixture: %s' % res_file
24
    }
25
    headers = {
26
        'Content-Type': 'application/json'
27
    }
28
    try:
29
        with open(res_file) as f:
30
            content = json.loads(f.read())
31
    except (IOError, ValueError):
32
        content['errmsg'] = 'Fixture %s json decode error' % res_file
33
    return response(200, content, headers, request=request)
34
35
36
class WeChatOAuthTestCase(unittest.TestCase):
37
    app_id = '123456'
38
    secret = '123456'
39
    redirect_uri = 'http://localhost'
40
41
    def setUp(self):
42
        self.oauth = WeChatOAuth(
43
            self.app_id,
44
            self.secret,
45
            self.redirect_uri
46
        )
47
48
    def test_get_authorize_url(self):
49
        authorize_url = self.oauth.authorize_url
50
        self.assertEqual(
51
            'https://open.weixin.qq.com/connect/oauth2/authorize?appid=123456&redirect_uri=http%3A%2F%2Flocalhost'
52
            '&response_type=code&scope=snsapi_base#wechat_redirect',
53
            authorize_url
54
        )
55
56
    def test_get_qrconnect_url(self):
57
        url = self.oauth.qrconnect_url
58
        self.assertEqual(
59
            'https://open.weixin.qq.com/connect/qrconnect?appid=123456&redirect_uri=http%3A%2F%2Flocalhost'
60
            '&response_type=code&scope=snsapi_login#wechat_redirect',
61
            url
62
        )
63
64
    def test_fetch_access_token(self):
65
        with HTTMock(wechat_api_mock):
66
            res = self.oauth.fetch_access_token('123456')
67
            self.assertEqual('ACCESS_TOKEN', res['access_token'])
68
69
    def test_refresh_access_token(self):
70
        with HTTMock(wechat_api_mock):
71
            res = self.oauth.refresh_access_token('123456')
72
            self.assertEqual('ACCESS_TOKEN', res['access_token'])
73
74
    def test_get_user_info(self):
75
        with HTTMock(wechat_api_mock):
76
            self.oauth.fetch_access_token('123456')
77
            res = self.oauth.get_user_info()
78
            self.assertEqual('OPENID', res['openid'])
79
80
    def test_check_access_token(self):
81
        with HTTMock(wechat_api_mock):
82
            self.oauth.fetch_access_token('123456')
83
            res = self.oauth.check_access_token()
84
            self.assertEqual(True, res)
85
86 View Code Duplication
    def test_reraise_requests_exception(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
87
        @urlmatch(netloc=r'(.*\.)?api\.weixin\.qq\.com$')
88
        def _wechat_api_mock(url, request):
89
            return {'status_code': 404, 'content': '404 not found'}
90
91
        try:
92
            with HTTMock(_wechat_api_mock):
93
                self.oauth.fetch_access_token('123456')
94
        except WeChatClientException as e:
95
            self.assertEqual(404, e.response.status_code)
96
97
98
class ComponentOAuthTestCase(unittest.TestCase):
99
    app_id = '123456'
100
    component_appid = '456789'
101
    component_access_token = '654321'
102
    redirect_uri = 'http://localhost'
103
104
    def setUp(self):
105
        self.oauth = ComponentOAuth(
106
            self.app_id,
107
            self.component_appid,
108
            self.component_access_token,
109
            self.redirect_uri
110
        )
111
112
    def test_get_authorize_url(self):
113
        authorize_url = self.oauth.authorize_url
114
        self.assertEqual(
115
            'https://open.weixin.qq.com/connect/oauth2/authorize?appid=123456&redirect_uri=http%3A%2F%2Flocalhost'
116
            '&response_type=code&scope=snsapi_base&component_appid=456789#wechat_redirect',
117
            authorize_url
118
        )
119
120
    def test_fetch_access_token(self):
121
        with HTTMock(wechat_api_mock):
122
            res = self.oauth.fetch_access_token('123456')
123
            self.assertEqual('ACCESS_TOKEN', res['access_token'])
124
125
    def test_refresh_access_token(self):
126
        with HTTMock(wechat_api_mock):
127
            res = self.oauth.refresh_access_token('123456')
128
            self.assertEqual('ACCESS_TOKEN', res['access_token'])
129
130
    def test_get_user_info(self):
131
        with HTTMock(wechat_api_mock):
132
            self.oauth.fetch_access_token('123456')
133
            res = self.oauth.get_user_info()
134
            self.assertEqual('OPENID', res['openid'])
135
136 View Code Duplication
    def test_reraise_requests_exception(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
137
        @urlmatch(netloc=r'(.*\.)?api\.weixin\.qq\.com$')
138
        def _wechat_api_mock(url, request):
139
            return {'status_code': 404, 'content': '404 not found'}
140
141
        try:
142
            with HTTMock(_wechat_api_mock):
143
                self.oauth.fetch_access_token('123456')
144
        except WeChatClientException as e:
145
            self.assertEqual(404, e.response.status_code)
146