-
Notifications
You must be signed in to change notification settings - Fork 88
/
test_flask_github.py
70 lines (53 loc) · 2.1 KB
/
test_flask_github.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import logging
import unittest
import requests
from mock import patch, Mock
from flask import Flask, request, redirect
from flask_github import GitHub
logger = logging.getLogger(__name__)
class GitHubTestCase(unittest.TestCase):
@patch.object(requests.Session, 'post')
@patch.object(GitHub, 'BASE_AUTH_URL')
def test_authorization(self, auth_url, post):
def assert_params(*args, **kwargs):
data = kwargs.pop('data')
assert data['client_id'] == '123'
assert data['client_secret'] == 'SEKRET'
assert data['code'] == 'KODE'
response = Mock()
response.content = b'access_token=asdf&token_type=bearer'
return response
post.side_effect = assert_params
auth_url.__get__ = Mock(return_value='http://localhost/oauth/')
app = Flask(__name__)
app.config['GITHUB_CLIENT_ID'] = '123'
app.config['GITHUB_CLIENT_SECRET'] = 'SEKRET'
github = GitHub(app)
@app.route('/login')
def login():
return github.authorize(redirect_uri="http://localhost/callback")
@app.route('/callback')
@github.authorized_handler
def authorized(token):
access_token.append(token)
return ''
# Mimics GitHub authorization URL
# http://developer.github.com/v3/oauth/#web-application-flow
@app.route('/oauth/authorize')
def handle_auth():
logger.info("in /oauth/authorize")
called_auth.append(1)
assert request.args['client_id'] == '123'
logger.debug("client_id OK")
assert request.args['redirect_uri'] == 'http://localhost/callback'
logger.debug("redirect_uri OK")
return redirect(request.args['redirect_uri'] + '?code=KODE')
access_token = []
called_auth = []
client = app.test_client()
client.get('/login', follow_redirects=True)
assert called_auth
assert access_token == ['asdf'], access_token
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
unittest.main()