def test_get_by_id(self): """Get user by ID.""" user = User("foo", "*****@*****.**") user.save() retrieved = User.get_by_id(user.id) assert retrieved == user
def test_check_password(self): """Check password.""" user = User.create(username="******", email="*****@*****.**", password="******") assert user.check_password("foobarbaz123") is True assert user.check_password("barfoobaz") is False
def save_user_ldap(dn, username, user_info, memberships): """Saves a user that managed to log in with LDAP Group determination method is based on the first 'OU=' section in the user DN, might need tweaking """ existing = User.query.filter_by(username=username).first() if existing: log.warning("User %s is already registered", username) return existing name = user_info.get("cn", "").split() first = last = None if len(name) > 1: first = name[0] last = " ".join(name[1:]) elif len(name) == 1: last = username groupname = None for sec in dn.split(","): if sec.startswith("OU"): groupname = sec.split("=")[1] break else: groupname = "default" groups = Group.query.filter_by(groupname=groupname) if groups.first(): group = groups.first() current_app.logger.info("Found group %s", groupname) else: group = Group.create( groupname=groupname, active=True, ) current_app.logger.info("Created group %s", groupname) user = User.create( username=username, first_name=first, last_name=last, active=True, group_id=group.id, ) return user
def save_user_omero(user_info): username = user_info["username"] existing = User.query.filter_by(username=username).first() if existing: log.warning("User %s is already registered", username) return existing name = user_info.get("fullname", "").split() first = last = None if len(name) > 1: first = name[0] last = " ".join(name[1:]) elif len(name) == 1: last = username groupname = user_info.get("groupname", "default") groups = Group.query.filter_by(groupname=groupname) if groups.first(): group = groups.first() current_app.logger.info("Found group %s", groupname) else: group = Group.create( groupname=groupname, active=True, ) current_app.logger.info("Created group %s", groupname) user = User.create( username=username, first_name=first, last_name=last, active=True, group_id=group.id, ) return user
def test_password_is_nullable(self): """Test null password.""" user = User(username="******", email="*****@*****.**") user.save() assert user.password is None
def test_created_at_defaults_to_datetime(self): """Test creation date.""" user = User(username="******", email="*****@*****.**") user.save() assert bool(user.created_at) assert isinstance(user.created_at, dt.datetime)
def load_user(user_id): """Load user by ID.""" return User.get_by_id(int(user_id))