Skip to content

Commit

Permalink
Add WeChat OAuth2 APIs test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Feb 10, 2015
1 parent 8ecc293 commit 271c152
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tests/fixtures/sns_oauth2_access_token.json
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"
}
4 changes: 4 additions & 0 deletions tests/fixtures/sns_oauth2_auth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"errcode": 0,
"errmsg": "ok"
}
7 changes: 7 additions & 0 deletions tests/fixtures/sns_oauth2_refresh_token.json
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"
}
14 changes: 14 additions & 0 deletions tests/fixtures/sns_userinfo.json
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"
}
75 changes: 75 additions & 0 deletions tests/test_oauth.py
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)

0 comments on commit 271c152

Please sign in to comment.