Exemplo n.º 1
0
def _init_db(db: Session, config: Config):
    """Initialize the database and add users from config."""

    if config.configured_section("users"):
        dao = Dao(db)
        role_map = [
            (config.users_admins, "owner"),
            (config.users_maintainers, "maintainer"),
            (config.users_members, "member"),
        ]

        for users, role in role_map:
            for username in users:
                logger.info(f"create user {username} with role {role}")
                dao.create_user_with_role(username, role)
Exemplo n.º 2
0
def function_with_dao(dao: Dao):
    dao.create_user_with_role("my-user")
Exemplo n.º 3
0
Arquivo: cli.py Projeto: beenje/quetz
def _fill_test_database(db: Session) -> NoReturn:
    """Create dummy users and channels to allow further testing in dev mode."""
    from quetz.dao import Dao

    test_users = []
    dao = Dao(db)
    try:
        for index, username in enumerate(['alice', 'bob', 'carol', 'dave']):
            user = dao.create_user_with_role(username)

            identity = Identity(
                provider='dummy',
                identity_id=str(index),
                username=username,
            )

            profile = Profile(name=username.capitalize(),
                              avatar_url='/avatar.jpg')

            user.identities.append(identity)  # type: ignore
            user.profile = profile
            db.add(user)
            test_users.append(user)

        for channel_index in range(3):
            channel = Channel(
                name=f'channel{channel_index}',
                description=f'Description of channel{channel_index}',
                private=False,
            )

            for package_index in range(random.randint(5, 10)):
                package = Package(
                    name=f'package{package_index}',
                    summary=f'package {package_index} summary text',
                    description=f'Description of package{package_index}',
                )
                channel.packages.append(package)  # type: ignore

                test_user = test_users[random.randint(0, len(test_users) - 1)]
                package_member = PackageMember(package=package,
                                               channel=channel,
                                               user=test_user,
                                               role='owner')

                db.add(package_member)

            test_user = test_users[random.randint(0, len(test_users) - 1)]

            if channel_index == 0:
                package = Package(name='xtensor',
                                  description='Description of xtensor')
                channel.packages.append(package)  # type: ignore

                package_member = PackageMember(package=package,
                                               channel=channel,
                                               user=test_user,
                                               role='owner')

                db.add(package_member)

                # create API key
                key = uuid.uuid4().hex

                key_user = User(id=uuid.uuid4().bytes)
                api_key = ApiKey(key=key,
                                 description='test API key',
                                 user=test_user,
                                 owner=test_user)
                db.add(api_key)
                print(
                    f'Test API key created for user "{test_user.username}": {key}'
                )

                key_package_member = PackageMember(
                    user=key_user,
                    channel_name=channel.name,
                    package_name=package.name,
                    role='maintainer',
                )
                db.add(key_package_member)

            db.add(channel)

            channel_member = ChannelMember(
                channel=channel,
                user=test_user,
                role='owner',
            )

            db.add(channel_member)
        db.commit()
    finally:
        db.close()
Exemplo n.º 4
0
def user(dao: Dao, db):
    user = dao.create_user_with_role("existing_user", role=SERVER_OWNER)
    yield user
    db.delete(user)
    db.commit()
Exemplo n.º 5
0
def user(dao: Dao):
    user = dao.create_user_with_role("existing_user", role=SERVER_OWNER)
    return user