This repository has been archived by the owner on Feb 15, 2020. It is now read-only.
forked from BooBSD/flask-odesk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
75 lines (59 loc) · 2.45 KB
/
tests.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
71
72
73
74
75
"""
Flask-oDesk
-----------
Adds oDesk API support to Flask.
flask-odesk version 0.4.1
(C) 2011 oDesk
"""
from flask import Flask, url_for
from flaskext.odesk import odesk
from mock import patch
import unittest
import odesk as python_odesk
import oauth2 as oauth
class ODeskTestCase(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.config['SECRET_KEY'] = 'blahblah'
app.config['ODESK_KEY'] = 'some_key'
app.config['ODESK_SECRET'] = 'some_secret'
app.debug = True
app.register_module(odesk, url_prefix='/odesk')
ctx = app.test_request_context()
ctx.push()
self.app = app
self.tc = self.app.test_client()
def test_url_for(self):
assert url_for('odesk.login') == '/odesk/login'
assert url_for('odesk.complete') == '/odesk/complete'
assert url_for('odesk.logout') == '/odesk/logout'
def test_login_required(self):
def patched_oauth_client_request(*args, **kwargs):
return {'status': '200'},\
'oauth_callback_confirmed=1&oauth_token=token&oauth_token_secret=secret'
def patched_get_authorize_url(*args, **kwargs):
return url_for('odesk.complete', next='/admin',\
oauth_verifier='verifier')
def patched_get_request_token(*args, **kwargs):
return 'request_token', 'request_token_secret'
def patched_get_get_access_token(*args, **kwargs):
return 'access_token', 'access_token_secret'
@self.app.route('/admin')
@patch('odesk.oauth.OAuth.get_authorize_url', patched_get_authorize_url)
@patch('odesk.oauth.OAuth.get_request_token', patched_get_request_token)
@patch('odesk.oauth.OAuth.get_access_token', patched_get_get_access_token)
@odesk.login_required
def admin():
self.odesk_is_authorized = odesk.is_authorized()
self.odesk_access_token = odesk.get_access_token()
odesk.logout()
self.odesk_is_not_authorized = odesk.is_authorized()
return "Welcome, oDesk user!"
oauth.Client.request = patched_oauth_client_request
response = self.tc.get('/admin', follow_redirects=True)
assert "Welcome" in response.data, response.data
assert self.odesk_is_authorized == True
assert self.odesk_access_token == ('token', 'secret')
assert self.odesk_is_not_authorized == False
if __name__ == '__main__':
unittest.main()