-
Hi, i still working in test in my flask app, and i have question about how to fake out the authentication process to access protected routes with @auth_required ? i have read this old issues and example file of test but i stuck in this error. def set_current_user(app, user_with_role):
"""Set up so that when request is received, the token will cause 'user' to be made the current_user"""
def token_cb(request):
if request.headers.get("Cookie") == "session":
return user_with_role
return app.security.login_manager.anonymous_user()
> app.security.login_manager.request_loader(token_cb)
E AttributeError: 'Flask' object has no attribute 'security' What wrong with this process ? are the function are changes due the update the version of FS ? this is my source code conftest.py @pytest.fixture
def app():
app = create_app("testing")
return app utils.py ...
def create_fake_user(email=EMAIL, password=PASSWORD, userid=1, roles=None):
user = User()
user.email = email
user.id = userid
user.password = password
user.active = True
if roles:
user.roles = []
if isinstance(roles, str):
roles = [roles]
for role_name in roles:
role = Role()
role.name = role_name
user.roles.append(role)
return user
def set_current_user(app, user_with_role):
def token_cb(request):
if request.headers.get("Cookie") == "session":
return user_with_role
return app.security.login_manager.anonymous_user()
app.security.login_manager.request_loader(token_cb)
... test_front_home.py def test_ads_page(app):
user = create_fake_user(roles=["Editor"])
set_current_user(app, user)
rv = app.client.get(url_for("front.home_ads"))
assert rv.status_code == HTTPStatus.OK
assert b"Basic Information" in rv.data
assert b"Amenities Information" in rv.data dependency :
Thank you in advance |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's not clear from what you provided how flask_security is getting initialized? presumably it is part of create_app() but I would need to see that. |
Beta Was this translation helpful? Give feedback.
It's not clear from what you provided how flask_security is getting initialized? presumably it is part of create_app() but I would need to see that.