Flask 烧瓶上OAuth的单元测试
我对编码还不熟悉,我觉得自己被卡住了。我决定对我的微型烧瓶应用程序进行单元测试。我使用“谷歌登录”对我的用户进行身份验证,但我无法通过谷歌身份验证并断言页面上的真实内容。我不知道是否需要模拟、使用会话或应用程序上下文 以下是身份验证过程的代码:Flask 烧瓶上OAuth的单元测试,flask,oauth,google-signin,Flask,Oauth,Google Signin,我对编码还不熟悉,我觉得自己被卡住了。我决定对我的微型烧瓶应用程序进行单元测试。我使用“谷歌登录”对我的用户进行身份验证,但我无法通过谷歌身份验证并断言页面上的真实内容。我不知道是否需要模拟、使用会话或应用程序上下文 以下是身份验证过程的代码: def login_required(f): @wraps(f) def decorated_function(*args, **kwargs): user = dict(session).get('profile', N
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
user = dict(session).get('profile', None)
if user is not None:
email = session['profile']['email']
else:
email = None
if user:
return f(*args, **kwargs)
return render_template('login.html', email=email)
return decorated_function
@app.route('/login/')
def login():
google = oauth.create_client('google') # create the google oauth client
redirect_uri = url_for('authorize', _external=True)
return google.authorize_redirect(redirect_uri)
@app.route('/authorize')
def authorize():
google = oauth.create_client('google') # create the google oauth client
token = google.authorize_access_token() # Access token from google (needed to get user info)
resp = google.get('userinfo') # userinfo specificed in the scrope
user_info = resp.json()
# Loading and cleaning all user emails
auth_users = db.session.query(users.email).all()
auth_users = [str(i).strip("(),'") for i in auth_users]
# Checking if user is in the list
if user_info['email'] in auth_users:
flash("Boli ste úspešne prihlásený.", category="success")
else:
flash("Nemáte práva na prístup.", category="primary")
return redirect('/')
session['profile'] = user_info
# make the session pernament so it keeps existing after broweser gets closed
session.permanent = True
return redirect('/')
测试结构如下所示:
class Uvo_page(unittest.TestCase):
def setUp(self):
self.client = app.test_client(self)
self.user_info = {'email': 'xxx'}
self.auth_users = ['xxx', 'yyy']
# UVO page shows results
def test_uvo_page_show_results(self):
response = self.client.get('/show/1',
content_type='html/text',
follow_redirects=True)
test_string = bytes('Sledované stránky', 'utf-8')
self.assertTrue(test_string in response.data)
我认为通过定义“用户信息”,它将用于身份验证评估。取而代之的是,它不会继续进行“/授权”和身份验证,而是停留在登录页面上。我不太明白出了什么问题,只是有两条相同的登录路径。你能在Werkzeug上分享你的错误日志吗?嗯,没有错误。只是测试失败了,因为登录页面总是显示出来。我想要的是通过身份验证来检查页面上是否存在该功能。似乎在安装程序中设置用户信息和身份验证用户不起作用。另一种方法是模拟会话['profile'],并设置电子邮件。不知道怎么做,也不知道它是否有效。(复制是复制粘贴代码的错误。)