-
-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"access_token":"ACCESS_TOKEN", | ||
"expires_in":7200, | ||
"refresh_token":"REFRESH_TOKEN", | ||
"openid":"OPENID", | ||
"scope":"SCOPE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"errcode": 0, | ||
"errmsg": "ok" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"access_token":"ACCESS_TOKEN", | ||
"expires_in":7200, | ||
"refresh_token":"REFRESH_TOKEN", | ||
"openid":"OPENID", | ||
"scope":"SCOPE" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"openid": "OPENID", | ||
"nickname": "NICKNAME", | ||
"sex": "1", | ||
"province": "PROVINCE", | ||
"city": "CITY", | ||
"country": "COUNTRY", | ||
"headimgurl": "http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46", | ||
"privilege": [ | ||
"PRIVILEGE1", | ||
"PRIVILEGE2" | ||
], | ||
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import absolute_import, unicode_literals, print_function | ||
import os | ||
import unittest | ||
|
||
from httmock import urlmatch, HTTMock, response | ||
|
||
from wechatpy import WeChatOAuth | ||
from wechatpy._compat import json | ||
|
||
|
||
_TESTS_PATH = os.path.abspath(os.path.dirname(__file__)) | ||
_FIXTURE_PATH = os.path.join(_TESTS_PATH, 'fixtures') | ||
|
||
|
||
@urlmatch(netloc=r'(.*\.)?api\.weixin\.qq\.com$') | ||
def wechat_api_mock(url, request): | ||
path = url.path[1:].replace('/', '_') | ||
res_file = os.path.join(_FIXTURE_PATH, '%s.json' % path) | ||
content = { | ||
'errcode': 99999, | ||
'errmsg': 'can not find fixture: %s' % res_file | ||
} | ||
headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
try: | ||
with open(res_file) as f: | ||
content = json.loads(f.read()) | ||
except (IOError, ValueError): | ||
content['errmsg'] = 'Fixture %s json decode error' % res_file | ||
return response(200, content, headers, request=request) | ||
|
||
|
||
class WeChatOAuthTestCase(unittest.TestCase): | ||
|
||
app_id = '123456' | ||
secret = '123456' | ||
redirect_uri = 'http://localhost' | ||
|
||
def setUp(self): | ||
self.oauth = WeChatOAuth( | ||
self.app_id, | ||
self.secret, | ||
self.redirect_uri | ||
) | ||
|
||
def test_get_authorize_url(self): | ||
authorize_url = self.oauth.authorize_url | ||
self.assertEqual( | ||
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=123456&redirect_uri=http%3A//localhost&response_type=code&scope=snsapi_base#wechat_redirect', # NOQA | ||
authorize_url | ||
) | ||
|
||
def test_fetch_access_token(self): | ||
with HTTMock(wechat_api_mock): | ||
res = self.oauth.fetch_access_token('123456') | ||
self.assertEqual('ACCESS_TOKEN', res['access_token']) | ||
|
||
def test_refresh_access_token(self): | ||
with HTTMock(wechat_api_mock): | ||
res = self.oauth.refresh_access_token('123456') | ||
self.assertEqual('ACCESS_TOKEN', res['access_token']) | ||
|
||
def test_get_user_info(self): | ||
with HTTMock(wechat_api_mock): | ||
self.oauth.fetch_access_token('123456') | ||
res = self.oauth.get_user_info() | ||
self.assertEqual('OPENID', res['openid']) | ||
|
||
def test_check_access_token(self): | ||
with HTTMock(wechat_api_mock): | ||
self.oauth.fetch_access_token('123456') | ||
res = self.oauth.check_access_token() | ||
self.assertEqual(True, res) |