def add_user(username, roles=[], auth=False, first_name=None, last_name=None): user = User.query.filter(User.username == username).first() if user is None: user = User(username=username, first_name=first_name, last_name=last_name) if auth: TornadoStorage.user.create_social_auth(user, user.username, 'google-oauth2') for rolename in roles: role = Role.query.get(rolename) if role not in user.roles: user.roles.append(role) DBSession().add(user) DBSession().flush() # Add user to sitewide public group public_group = Group.query.filter( Group.name == cfg["misc"]["public_group_name"]).first() if public_group is None: public_group = Group(name=cfg["misc"]["public_group_name"]) DBSession().add(public_group) DBSession().flush() user.groups.append(public_group) DBSession().commit() return User.query.filter(User.username == username).first()
def add_super_user(username): """Initializes a super user with full permissions.""" setup_permissions() # make sure permissions already exist super_user = User.query.filter(User.username == username).first() if super_user is None: super_user = User(username=username) social = TornadoStorage.user.create_social_auth( super_user, super_user.username, 'google-oauth2') admin_role = Role.query.get('Super admin') if admin_role not in super_user.roles: super_user.roles.append(admin_role) DBSession().add(super_user) DBSession().commit()
def source_notification_user(public_group): uid = str(uuid.uuid4()) username = f"{uid}@cesium.ml.org" user = User( username=username, contact_email=username, contact_phone="+12345678910", groups=[public_group], roles=[models.Role.query.get("Full user")], preferences={ "allowEmailNotifications": True, "allowSMSNotifications": True }, ) DBSession().add(user) DBSession().commit() return user
def add_user(username, roles=[], auth=False): user = User.query.filter(User.username == username).first() if user is None: user = User(username=username) if auth: social = TornadoStorage.user.create_social_auth( user, user.username, 'google-oauth2') for rolename in roles: role = Role.query.get(rolename) if role not in user.roles: user.roles.append(role) DBSession().add(user) DBSession().commit() return User.query.filter(User.username == username).first()
def add_user(username, roles=[], auth=False, first_name=None, last_name=None): with DBSession() as session: user = session.scalars( sa.select(User).where(User.username == username)).first() if user is None: user = User(username=username, first_name=first_name, last_name=last_name) if auth: TornadoStorage.user.create_social_auth(user, user.username, 'google-oauth2') for rolename in roles: role = session.scalars( sa.select(Role).where(Role.id == rolename)).first() if role not in user.roles: user.roles.append(role) session.add(user) session.flush() # Add user to sitewide public group public_group_name = cfg['misc.public_group_name'] if public_group_name: public_group = session.scalars( sa.select(Group).where( Group.name == public_group_name)).first() if public_group is None: public_group = Group(name=public_group_name) session.add(public_group) session.flush() user.groups.append(public_group) session.commit() return User.query.filter(User.username == username).first()
for model in Base.metadata.tables: print(" -", model) if src.get("create_permissions", False): with status(f"Creating permissions"): setup_permissions() if src.get("users") is not None: with status(f"Creating users & sitewide public group"): DBSession().add(Group(name=cfg["misc"]["public_group_name"])) DBSession().commit() users = [] for user in src.get('users', []): users.append( User(username=user["username"], role_ids=user["role_ids"]) ) DBSession().add_all(users) for user in users: DBSession().add( TornadoStorage.user.create_social_auth(user, user.username, "google-oauth2") ) users_dict = {user.username: user.id for user in users} with status("Creating tokens"): tokens = {} for user in src.get('users', []): for t in user.get("tokens", []): tokens[t["name"]] = create_token( t["permissions"],
) import astroplan print("Loading test configuration from _test_config.yaml") basedir = pathlib.Path(os.path.dirname(__file__)) cfg = load_config([(basedir / "../../test_config.yaml").absolute()]) set_server_url(f'http://localhost:{cfg["ports.app"]}') print("Setting test database to:", cfg["database"]) models.init_db(**cfg["database"]) # Add a "test factory" User so that all factory-generated comments have a # proper author, if it doesn't already exist (the user may already be in # there if running the test server and running tests individually) if not DBSession.query(User).filter(User.username == "test factory").scalar(): DBSession.add(User(username="******")) DBSession.commit() def pytest_runtest_setup(item): # Print timestamp when running each test print(datetime.now().strftime('[%H:%M:%S] '), end='') # set up a hook to be able to check if a test has failed @pytest.hookimpl(tryfirst=True, hookwrapper=True) def pytest_runtest_makereport(item, call): # execute all other hooks to obtain the report object outcome = yield rep = outcome.get_result()
with status("Dropping all tables"): drop_tables() with status("Creating tables"): create_tables() for model in Base.metadata.tables: print(' -', model) with status(f"Creating permissions"): setup_permissions() with status(f"Creating dummy users"): g = Group(name='Stream A') super_admin_user = User(username='******', role_ids=['Super admin']) group_admin_user = User(username='******', role_ids=['Super admin']) DBSession().add_all([ GroupUser(group=g, user=super_admin_user, admin=True), GroupUser(group=g, user=group_admin_user, admin=True) ]) full_user = User(username='******', role_ids=['Full user'], groups=[g]) DBSession().add_all([super_admin_user, group_admin_user, full_user]) for u in [super_admin_user, group_admin_user, full_user]: DBSession().add( TornadoStorage.user.create_social_auth(u, u.username, 'google-oauth2'))
AllocationFactory, InvitationFactory, NotificationFactory, UserNotificationFactory, ThumbnailFactory, GcnFactory, ) from skyportal.tests.fixtures import TMP_DIR # noqa: F401 from skyportal.models import Obj # Add a "test factory" User so that all factory-generated comments have a # proper author, if it doesn't already exist (the user may already be in # there if running the test server and running tests individually) if (not DBSession().execute( sa.select(User).filter(User.username == "test factory")).scalar()): DBSession.add(User(username="******")) DBSession.commit() # Also add the test driver user (testuser-cesium-ml-org) if needed so that the driver # fixture has a user to login as (without needing an invitation token). # With invitations enabled on the test configs, the driver fails to login properly # without this user because the authenticator looks for the user or an # invitation token when neither exists initially on fresh test databases. if (not DBSession().execute( sa.select(User).filter( User.username == "testuser-cesium-ml-org")).scalar()): DBSession.add( User(username="******", oauth_uid="*****@*****.**")) DBSession.commit()
def __init__(self, username, groupname="Public ZTF", create_user=True, create_group=True, create_instrument=True, create_telescope=True): """ username create_user=True, create_group=True, create_instrument=True, create_telescope=True """ self._connect() self.username = username self.g = Group.query.filter(Group.name == groupname).first() if not self.g: self.g = Group(name=groupname) super_admin_user = User.query.filter( User.username == self.username).first() if not super_admin_user: super_admin_user = User(username=self.username, role_ids=['Super admin']) DBSession().add( GroupUser(group=self.g, user=super_admin_user, admin=True)) uu = super_admin_user DBSession().add( TornadoStorage.user.create_social_auth(uu, uu.username, 'google-oauth2')) DBSession().add(super_admin_user) group_admin_user = User.query.filter( User.username == '*****@*****.**').first() if not group_admin_user: group_admin_user = User(username='******', role_ids=['Group admin']) DBSession().add( GroupUser(group=self.g, user=group_admin_user, admin=True)) uu = group_admin_user DBSession().add( TornadoStorage.user.create_social_auth(uu, uu.username, 'google-oauth2')) DBSession().add(group_admin_user) full_user = User.query.filter( User.username == '*****@*****.**').first() if not full_user: full_user = User(username='******', role_ids=['Full user'], groups=[self.g]) uu = full_user DBSession().add( TornadoStorage.user.create_social_auth(uu, uu.username, 'google-oauth2')) DBSession().add_all([full_user]) DBSession().commit() self.t1 = Telescope.query.filter( Telescope.name == 'Palomar 48inch').first() if not self.t1: self.t1 = Telescope(name='Palomar 48inch', nickname='P48', lat=33.3633675, lon=-116.8361345, elevation=1870, diameter=1.2) if create_telescope: DBSession().add(self.t1) self.i1 = Instrument.query.filter( Instrument.name == 'ZTF Camera').first() if not self.i1: self.i1 = Instrument(telescope=self.t1, name='ZTF Camera', type='phot', band='optical') if create_instrument: DBSession().add(self.i1) self.super_admin_user = super_admin_user self.group_admin_user = group_admin_user DBSession().commit()
with status("Force dropping all tables"): drop_tables() with status("Creating tables. If you really want to start from scratch," " do a make db_clear; make db_init"): create_tables() for model in Base.metadata.tables: print(' -', model) with status(f"Creating permissions"): model_util.setup_permissions() if adminuser != '': with status(f"Creating super admin ({adminuser})"): super_admin_user = User(username=results.adminuser, role_ids=['Super admin']) DBSession().add_all([super_admin_user]) for u in [super_admin_user]: DBSession().add( TornadoStorage.user.create_social_auth( u, u.username, 'google-oauth2')) if user != '': with status(f"Creating user ({user})"): user = User(username=results.user, role_ids=['Full user']) DBSession().add_all([user]) for u in [user]: DBSession().add(
init_db(**cfg["database"]) with status("Dropping all tables"): drop_tables() with status("Creating tables"): create_tables() for model in Base.metadata.tables: print(" -", model) with status(f"Creating permissions"): setup_permissions() with status(f"Creating dummy users"): super_admin_user = User(username="******", role_ids=["Super admin"]) group_admin_user = User(username="******", role_ids=["Super admin"]) full_user = User(username="******", role_ids=["Full user"]) view_only_user = User(username="******", role_ids=["View only"]) DBSession().add_all( [super_admin_user, group_admin_user, full_user, view_only_user]) for u in [ super_admin_user, group_admin_user, full_user, view_only_user ]: DBSession().add( TornadoStorage.user.create_social_auth(u, u.username, "google-oauth2"))