def test_users(): engine = create_engine("sqlite:///:memory:", echo=True) session = scoped_session( sessionmaker(autocommit=False, autoflush=False, bind=engine)) Base.metadata.create_all(engine) repo = UserRepository( config=Config(security=SecurityConfig(admin_pwd="admin")), session=session, ) a = User( name="a", password=Password("a"), ) b = User(name="b", password=Password("b")) a = repo.save(a) b = repo.save(b) assert b.id c = repo.get(a.id) assert a == c assert a.password.check("a") assert b == repo.get_by_name("b") repo.delete(a.id) assert repo.get(a.id) is None
def __init__(self, config: Config, session: Session) -> None: self.session = session # init seed admin user from conf admin_user = self.get_by_name("admin") if admin_user is None: self.save( User( id=1, name="admin", password=Password(config.security.admin_pwd), )) elif not admin_user.password.check( config.security.admin_pwd): # type: ignore admin_user.password = Password( config.security.admin_pwd) # type: ignore self.save(admin_user)
def test_authenticate(): users = Mock() users.get_by_name.return_value = User(id=0, password=Password("pwd")) users.get.return_value = User(id=0, name="linus") ldap = Mock() ldap.login.return_value = None ldap.get.return_value = None roles = Mock() roles.get_all_by_user.return_value = [ Role(type=RoleType.READER, group=Group(id="group", name="group")) ] exp = JWTUser( id=0, impersonator=0, type="users", groups=[JWTGroup(id="group", name="group", role=RoleType.READER)], ) service = LoginService( user_repo=users, bot_repo=Mock(), group_repo=Mock(), role_repo=roles, ldap=ldap, event_bus=Mock(), ) assert exp == service.authenticate("dupond", "pwd") users.get_by_name.assert_called_once_with("dupond") roles.get_all_by_user.assert_called_once_with(0)
def create_user(self, create: UserCreateDTO, param: RequestParameters) -> Identity: if param.user and param.user.is_site_admin(): if self.users.get_by_name(create.name): raise UserAlreadyExistError() return self.users.save( User(name=create.name, password=Password(create.password))) else: raise UserHasNotPermissionError()
def test_user_save() -> None: user = User(id=0, name="a", password=Password("b")) service = Mock() service.save_user.return_value = user app = create_app(service) client = app.test_client() res = client.put( "/users/0", headers=create_auth_token(app), json=user.to_dict(), ) assert res.status_code == 200 service.save_user.assert_called_once_with(user, PARAMS) assert res.json == user.to_dict()
def test_create_user(): create = UserCreateDTO(name="hello", password="******") ldap = Mock() ldap.save.return_value = None users = Mock() users.save.return_value = User(id=3, name="hello") users.get_by_name.return_value = None service = LoginService( user_repo=users, bot_repo=Mock(), group_repo=Mock(), role_repo=Mock(), ldap=ldap, event_bus=Mock(), ) service.create_user(create, param=SADMIN) users.save.assert_called_once_with( User(name="hello", password=Password("world")) )
def test_password(): assert Password("pwd").check("pwd")