Commit ccb732f2 authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Fixed broken tests.

parent 10a9f641
Pipeline #51640 failed with stages
in 19 minutes and 19 seconds
......@@ -125,26 +125,29 @@ class User(Base): # type: ignore
def from_user_id(user_id) -> 'User':
return infrastructure.repository_db.query(User).get(user_id)
def get_auth_token(self):
def get_auth_token(self, create: bool = True):
repo_db = infrastructure.repository_db
session = repo_db.query(Session).filter_by(user_id=self.user_id).first()
if not session:
# No session, user probably not logged in at NOMAD-coe repository GUI
repo_db.begin()
try:
# TODO this has to change, e.g. trade for JWTs
token = ''.join(random.choices(User._token_chars, k=64))
session = Session(token=token, user=self)
repo_db.add(session)
repo_db.commit()
except Exception as e:
repo_db.rollback()
utils.get_logger('__name__').error(
'could not generate token for user', email=self.email, user_id=self.user_id,
exc_info=e)
raise e
if create:
repo_db.begin()
try:
# TODO this has to change, e.g. trade for JWTs
token = ''.join(random.choices(User._token_chars, k=64))
session = Session(token=token, user=self)
repo_db.add(session)
repo_db.commit()
except Exception as e:
repo_db.rollback()
utils.get_logger('__name__').error(
'could not generate token for user', email=self.email, user_id=self.user_id,
exc_info=e)
raise e
else:
raise LoginException(
'No session, user probably not logged in at NOMAD-coe repository GUI')
return session.token.encode('utf-8')
......@@ -176,7 +179,7 @@ class User(Base): # type: ignore
@property
def token(self):
return self.get_auth_token().decode('utf-8')
return self.get_auth_token(create=False).decode('utf-8')
@property
def is_admin(self) -> bool:
......
......@@ -18,6 +18,7 @@ from passlib.hash import bcrypt
from datetime import datetime
from nomad.coe_repo import User, Calc, Upload, create_handle
from nomad.coe_repo.user import Session
from nomad.coe_repo.calc import PublishContext
from nomad import processing, parsing, datamodel
......@@ -46,13 +47,16 @@ def test_password_authorize(test_user):
assert_user(user, test_user)
def test_generate_token(postgres_infra):
user = User.create_user(
'test@email.com', 'password', False, user_id=6000,
first_name='test', last_name='test',
affiliation=dict(name='institut', address='address'),
generate_token=False)
assert user.get_auth_token() is not None
def test_generate_token(postgres, test_user):
postgres.begin()
session = postgres.query(Session).filter_by(user_id=test_user.user_id).first()
if session is not None:
postgres.delete(session)
postgres.commit()
assert test_user.get_auth_token() is not None
postgres.expunge(test_user)
def assert_coe_upload(upload_id: str, upload: datamodel.UploadWithMetadata = None, user_metadata: dict = None):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment