tests: adds test for auth ghost user; refactors accessing default/ghost user

This commit is contained in:
ievgenii vdovenko 2025-07-26 15:08:32 +02:00
parent 544e757a34
commit 1db5e92ca3
2 changed files with 46 additions and 13 deletions

View file

@ -1261,15 +1261,6 @@ class User(Base, BaseModel):
raise Exception("FATAL: Missing administrative account!")
return user
@classmethod
def get_ghost_user(cls):
stmt = cls.select().where(User.username == User.GHOST_USER)
user = cls.scalars(stmt).first()
if user is None:
raise Exception("FATAL: Missing ghost user!")
return user
@classmethod
def get_all_super_admins(cls, only_active=False):
"""
@ -1291,13 +1282,21 @@ class User(Base, BaseModel):
qry = qry.filter(User.active == true())
return [x.user_id for x in qry]
@classmethod
def get_ghost_user(cls, cache=False, refresh=False):
return cls._get_system_user(username=User.GHOST_USER, cache=cache, refresh=refresh)
@classmethod
def get_default_user(cls, cache=False, refresh=False):
user = User.get_by_username(User.DEFAULT_USER, cache=cache)
return cls._get_system_user(username=User.DEFAULT_USER, cache=cache, refresh=refresh)
@classmethod
def _get_system_user(cls, username, cache=False, refresh=False):
user = User.get_by_username(username, cache=cache)
if user is None:
raise Exception("FATAL: Missing default account!")
raise Exception("FATAL: Missing %s account!" % username)
if refresh:
# The default user might be based on outdated state which
# The 'system' user might be based on outdated state which
# has been loaded from the cache.
# A call to refresh() ensures that the
# latest state from the database is used.