Exemple #1
0
def test_config_user_exists(routed_client, db, oauth_server, channel, user,
                            login, default_role, app):
    profile = Profile(user=user)
    identity = Identity(provider=oauth_server.provider,
                        user=user,
                        identity_id="existing_user_id")
    db.add(identity)
    db.add(profile)
    db.commit()

    response = routed_client.get(f'/auth/{oauth_server.provider}/authorize')

    assert response.status_code == 200

    db.refresh(user)

    users = db.query(User).filter(User.username == login).all()
    assert len(users) == 1

    new_user = users[0]

    assert new_user
    assert new_user.username == login
    assert login != "existing_user" or new_user.role == SERVER_OWNER
    assert login == "existing_user" or new_user.role == default_role
    assert new_user.profile
    assert new_user.identities
    assert new_user.identities[0].provider == oauth_server.provider
Exemple #2
0
def some_identity(user, db):
    identity = Identity(provider="some-provider",
                        user=user,
                        identity_id='some-identity')
    db.add(identity)
    db.commit()
    yield identity
    db.delete(identity)
    db.commit()
Exemple #3
0
def other_user(other_user_without_profile, db):
    profile = Profile(name="Other",
                      avatar_url="http:///avatar",
                      user=other_user_without_profile)
    identity = Identity(
        provider="github",
        identity_id="github",
        username="******",
        user=other_user_without_profile,
    )
    db.add(profile)
    db.add(identity)
    db.commit()
    yield other_user_without_profile
Exemple #4
0
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()
Exemple #5
0
def init_test_db():
    config = Config()
    init_db(config.sqlalchemy_database_url)
    db = get_session(config.sqlalchemy_database_url)

    testUsers = []

    try:
        for index, username in enumerate(['alice', 'bob', 'carol', 'dave']):
            user = User(id=uuid.uuid4().bytes, username=username)

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

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

            user.identities.append(identity)
            user.profile = profile
            db.add(user)
            testUsers.append(user)

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

            for package_index in range(random.randint(5, 100)):
                package = Package(
                    name=f'package{package_index}',
                    description=f'Description of package{package_index}',
                )
                channel.packages.append(package)

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

                db.add(package_member)

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

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

                db.add(package_member)

                # create API key
                key = 'E_KaBFstCKI9hTdPM7DQq56GglRHf2HW7tQtq6si370'

                key_user = User(id=uuid.uuid4().bytes)

                api_key = ApiKey(key=key,
                                 description='test API key',
                                 user=key_user,
                                 owner=test_user)
                db.add(api_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=testUsers[random.randint(0,
                                              len(testUsers) - 1)],
                role='owner',
            )

            db.add(channel_member)
        db.commit()
    finally:
        db.close()
Exemple #6
0
def user_with_identity(user, db):
    identity = Identity(user=user, provider="github", identity_id="1")
    db.add(identity)
    db.commit()
    return identity
Exemple #7
0
from quetz.db_models import (User, Identity, Profile, Channel, ChannelMember,
                             Package, PackageMember, ApiKey)

init_db()

db = SessionLocal()

testUsers = []

try:
    for index, username in enumerate(['alice', 'bob', 'carol', 'dave']):
        user = User(id=uuid.uuid4().bytes, username=username)

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

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

        user.identities.append(identity)
        user.profile = profile
        db.add(user)
        testUsers.append(user)

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