Added utility function current_user() that acts like flask_login.current_user

This actually returns an AnonymousUser object, instead of None, when the
user is not logged in.

For compatibility with existing code, this function doesn't set
g.current_user to that AnonymousUser instance. We may decide to do this
later.
This commit is contained in:
2017-08-23 12:16:11 +02:00
parent efc1890871
commit cf51d1a280
2 changed files with 49 additions and 2 deletions

View File

@@ -700,3 +700,35 @@ class UserCreationTest(AbstractPillarTest):
auth_token='admin-token', expected_status=422)
self.put(f'/api/users/{user_id}', json=without_email, etag=etag,
auth_token='admin-token', expected_status=422)
class CurrentUserTest(AbstractPillarTest):
def test_current_user_logged_in(self):
self.enter_app_context()
from flask import g
from pillar.auth import UserClass
from pillar.api.utils.authentication import current_user
with self.app.test_request_context():
g.current_user = UserClass.construct('the token', ctd.EXAMPLE_USER)
user = current_user()
self.assertIs(g.current_user, user)
def test_current_user_anonymous(self):
self.enter_app_context()
from flask import g
from pillar.auth import AnonymousUser
from pillar.api.utils.authentication import current_user
with self.app.test_request_context():
g.current_user = None
user = current_user()
self.assertIsInstance(user, AnonymousUser)
self.assertIsNone(user.user_id)
self.assertTrue(user.is_anonymous)
self.assertFalse(user.is_authenticated)