flask_登录-当前_用户
我正在使用我的flask应用程序中的flask\u login扩展来登录用户。您必须知道,此扩展有一个变量,用于存储当前用户。这段代码运行得很好,除了在测试时 当我测试代码时(使用unittest),我注册一个“测试用户”并登录。但是current_user变量不保留登录的用户 这是我的应用程序代码;添加到类别中的部分(当前用户在用户登录时设置): 以下是我测试应用程序的代码:flask_登录-当前_用户,flask,flask-login,Flask,Flask Login,我正在使用我的flask应用程序中的flask\u login扩展来登录用户。您必须知道,此扩展有一个变量,用于存储当前用户。这段代码运行得很好,除了在测试时 当我测试代码时(使用unittest),我注册一个“测试用户”并登录。但是current_user变量不保留登录的用户 这是我的应用程序代码;添加到类别中的部分(当前用户在用户登录时设置): 以下是我测试应用程序的代码: import unittest import os import json import app from app i
import unittest
import os
import json
import app
from app import create_app, db
class CategoryTestCase(unittest.TestCase):
"""This class represents the Category test case"""
def setUp(self):
"""setup test variables"""
self.app = create_app(config_name="testing")
self.client = self.app.test_client
self.category_data = {'name' : 'Yummy'}
# binds the app with the current context
with self.app.app_context():
#create all tables
db.session.close()
db.drop_all()
db.create_all()
def register_user(self, first_name='Tester', last_name='Api', username='apitester', email='tester@api.com', password='abc'):
"""This helper method helps register a test user"""
user_data = {
'first_name' : first_name,
'last_name' : last_name,
'username' : username,
'email' : email,
'password' : password
}
return self.client().post('/api/v1.0/register', data=json.dumps(user_data), content_type='application/json')
def login_user(self, email='tester@api.com', password='abc'):
"""this helper method helps log in a test user"""
user_data = {
'email' : email,
'password' : password
}
return self.client().post('/api/v1.0/login', data=json.dumps(user_data), content_type='application/json')
def test_category_creation(self):
"""Test that the Api can create a category"""
self.register_user()
login_result = self.login_user()
token = json.loads(login_result.data)
token = token['access_token']
# Create a category by going to that link
response = self.client().post('/api/v1.0/category', headers=dict(Authorization="Bearer " + token), data=json.dumps(self.category_data), content_type='application/json')
self.assertEquals(response.status_code, 201)
您需要使用与登录相同的上下文。因此,您需要在代码中添加以下内容:
with self.client() as c:
然后使用c
发出get、post或任何其他您想要的请求。下面是一个完整的示例:
import unittest
from app import create_app
class CategoryTestCase(unittest.TestCase):
"""This class represents the Category test case"""
def setUp(self):
"""setup test variables"""
self.app = create_app(config_name="testing")
self.client = self.app.test_client
self.category_data = {'name' : 'Yummy'}
def test_category_creation(self):
"""Test that the user can create a category"""
with self.client() as c:
# use the c to make get, post or any other requests
login_response = c.post("""login the user""")
# Create a category by going to that link using the same context i.e c
response = c.post('/api/v1.0/category', self.category_data)
可能重复我没有使用会话来标识我的用户,我使用的是flask_登录扩展提供的curre_用户变量。
import unittest
from app import create_app
class CategoryTestCase(unittest.TestCase):
"""This class represents the Category test case"""
def setUp(self):
"""setup test variables"""
self.app = create_app(config_name="testing")
self.client = self.app.test_client
self.category_data = {'name' : 'Yummy'}
def test_category_creation(self):
"""Test that the user can create a category"""
with self.client() as c:
# use the c to make get, post or any other requests
login_response = c.post("""login the user""")
# Create a category by going to that link using the same context i.e c
response = c.post('/api/v1.0/category', self.category_data)