Skip to content

Commit 7128969

Browse files
committed
add test case for password credential
1 parent 7f057a8 commit 7128969

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

tests/oauth2_server.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
db = SQLAlchemy()
1010

1111

12+
def enable_log(name='flask_oauthlib'):
13+
import logging
14+
logger = logging.getLogger(name)
15+
logger.addHandler(logging.StreamHandler())
16+
logger.setLevel(logging.DEBUG)
17+
18+
1219
class User(db.Model):
1320
id = db.Column(db.Integer, primary_key=True)
1421
username = db.Column(db.Unicode(40), unique=True, index=True,
@@ -102,17 +109,26 @@ def prepare_app(app):
102109
db.app = app
103110
db.create_all()
104111

105-
client = Client(
112+
client1 = Client(
106113
name=u'dev', client_id=u'dev', client_secret=u'dev',
107114
_redirect_uris=u'http://localhost:8000/authorized'
108115
)
116+
117+
client2 = Client(
118+
name=u'confidential', client_id=u'confidential',
119+
client_secret=u'confidential', client_type=u'confidential',
120+
_redirect_uris=u'http://localhost:8000/authorized'
121+
)
122+
109123
user = User(username=u'admin')
124+
110125
try:
126+
db.session.add(client1)
127+
db.session.add(client2)
111128
db.session.add(user)
112-
db.session.add(client)
113129
db.session.commit()
114130
except:
115-
pass
131+
db.session.rollback()
116132
return app
117133

118134

@@ -161,6 +177,12 @@ def set_token(token, request, *args, **kwargs):
161177
db.session.add(tok)
162178
db.session.commit()
163179

180+
@oauth.usergetter
181+
def get_user(username, password, *args, **kwargs):
182+
# This is optional, if you don't need password credential
183+
# there is no need to implement this method
184+
return User.query.get(1)
185+
164186
@app.before_request
165187
def load_current_user():
166188
user = User.query.get(1)

tests/test_oauth2.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import os
44
import tempfile
5+
import unittest
56
from urlparse import urlparse
67
from flask import Flask
7-
from .oauth2_server import create_server
8+
from .oauth2_server import create_server, db
89
from .oauth2_client import create_client
910

1011

11-
class BaseSuite(object):
12+
class BaseSuite(unittest.TestCase):
1213
def setUp(self):
1314
app = Flask(__name__)
1415
app.debug = True
@@ -28,6 +29,9 @@ def setUp(self):
2829
return app
2930

3031
def tearDown(self):
32+
db.session.remove()
33+
db.drop_all()
34+
3135
os.close(self.db_fd)
3236
os.unlink(self.db_file)
3337

@@ -37,8 +41,7 @@ def tearDown(self):
3741
'&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fauthorized&scope=email'
3842
)
3943

40-
41-
class TestAuth(BaseSuite):
44+
class TestWebAuth(BaseSuite):
4245
def test_login(self):
4346
rv = self.client.get('/login')
4447
assert 'response_type=code' in rv.location
@@ -72,10 +75,23 @@ def test_get_access_token(self):
7275
assert 'access_token' in rv.data
7376

7477
def test_full_flow(self):
75-
self.test_get_access_token()
78+
rv = self.client.post(authorize_url, data={'confirm': 'yes'})
79+
rv = self.client.get(clean_url(rv.location))
80+
assert 'access_token' in rv.data
81+
7682
rv = self.client.get('/')
7783
assert 'username' in rv.data
7884

85+
class TestPasswordAuth(BaseSuite):
86+
def test_get_access_token(self):
87+
auth_code = 'confidential:confidential'.encode('base64').strip()
88+
url = ('/oauth/access_token?grant_type=password'
89+
'&scope=email+address&username=admin&password=admin')
90+
rv = self.client.get(url, headers={
91+
'HTTP_AUTHORIZATION': 'Basic %s' % auth_code,
92+
}, data={'confirm': 'yes'})
93+
assert 'access_token' in rv.data
94+
7995

8096
def clean_url(location):
8197
ret = urlparse(location)

0 commit comments

Comments
 (0)