我正在尝试使用unittest库实施单元测试,但是遇到了一些问题。
我的测试骨架如下:
class TestSetup(unittest.TestCase):
############################
#### setup and teardown ####
############################
def setUp(self):
app_settings = os.getenv('APP_SETTINGS', 'project.app.config.DockerConfig')
app.config.from_object(app_settings)
app.config['DEBUG'] = True
app.config['TESTING'] = True
self.app = app
self.client = app.test_client()
self.db = db
self.assertEqual(app.config['ALLOW_TESTING'], True)
print(self.login().data)
def tearDown(self):
pass
def login(self):
return self.client.post(
'/login',
data=json.dumps({
"username":self.app.config['TEST_USERNAME'],
"password":self.app.config['TEST_PWD']
}),
headers={
"Content-Type": "application/json"
}
)
我在使用登录函数时遇到问题,因为它返回了500条代码和以下错误消息:
Flask object has no attribute jwt_manager
登录端点如下:
@main_blueprint.route('/login', methods=['POST'])
@auto.doc()
@validate_json('username', 'password')
def login():
username = request.json.get('username')
password = hashlib.sha224(request.json.get('password').encode('utf-8')).hexdigest()
user = UserModel.query.filter(UserModel.username == username).first()
if user == None:
app.logger.info('Invalid login from ' + str(request.remote_addr))
return jsonify({"msg": "Bad username or password"}), 401
if user.password != password:
return jsonify({"msg": "Bad username or password"}), 401
userToken = UserToken(user)
access_token = create_access_token(identity=userToken)
refresh_token = create_refresh_token(identity=userToken)
ret = {
'access_token': access_token,
'refresh_token': refresh_token
}
return jsonify(ret), 200
主要问题是,当我在服务器启动期间看到烧瓶扩展时,仅显示的扩展名是 SQLAlchemy ,因此我不知道如何解决此问题。
寻求帮助。