def test_api_key(db): usera = User(id=uuid.uuid4().bytes, username='******') db.add(usera) userb = User(id=uuid.uuid4().bytes, username='******') db.add(userb) db.commit() assert len(db.query(User).all()) >= 2 db.add(ApiKey(key=keya, user_id=usera.id, owner_id=usera.id)) db.add(ApiKey(key=keyb, user_id=userb.id, owner_id=userb.id)) db.commit() channel1 = Channel(name="testchannel", private=False) channel2 = Channel(name="privatechannel", private=True) channel_member = ChannelMember(channel=channel2, user=usera, role='OWNER') for el in [channel1, channel2, channel_member]: db.add(el) db.commit() client = TestClient(app) response = client.get('/') assert (len(response.text)) response = client.get('/api/channels') response = client.get('/api/channels', headers={"X-Api-Key": keya}) assert (len(response.json()) == 2) response = client.get('/api/channels', headers={"X-Api-Key": keyb}) assert (len(response.json()) == 1)
def __init__(self, db): self.keya = "akey" self.keyb = "bkey" self.usera = User(id=uuid.uuid4().bytes, username='******') Profile(name='usera', user=self.usera, avatar_url='') db.add(self.usera) self.userb = User(id=uuid.uuid4().bytes, username='******') Profile(name='userb', user=self.userb, avatar_url='') db.add(self.userb) self.userc = User(id=uuid.uuid4().bytes, username='******', role="owner") Profile(name='userc', user=self.userc, avatar_url='') db.add(self.userc) assert len(db.query(User).all()) == 3 db.add(ApiKey(key=self.keya, user_id=self.usera.id, owner_id=self.usera.id)) db.add(ApiKey(key=self.keyb, user_id=self.userb.id, owner_id=self.userb.id)) self.channel1 = Channel(name="testchannel", private=False) self.channel2 = Channel(name="privatechannel", private=True) self.package1 = Package(name="Package1", channel=self.channel1) self.package2 = Package(name="Package2", channel=self.channel2) self.channel_member = ChannelMember( channel=self.channel2, user=self.usera, role='maintainer' ) self.channel_member_userc = ChannelMember( channel=self.channel2, user=self.userc, role='owner' ) self.package_member = PackageMember( channel=self.channel2, user=self.userc, package=self.package2, role="owner" ) for el in [ self.channel1, self.channel2, self.channel_member, self.channel_member_userc, self.package1, self.package2, self.package_member, ]: db.add(el) db.commit() self.db = db
def patch_channel( channel_data: rest_models.Channel, dao: Dao = Depends(get_dao), auth: authorization.Rules = Depends(get_rules), channel: db_models.Channel = Depends(get_channel_or_fail), db=Depends(get_db), ): auth.assert_update_channel_info(channel.name) user_attrs = channel_data.dict(exclude_unset=True) if "size_limit" in user_attrs: auth.assert_set_channel_size_limit() changeable_attrs = ["private", "size_limit", "metadata"] for attr_ in user_attrs.keys(): if attr_ not in changeable_attrs: raise HTTPException( status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=f"attribute '{attr_}' of channel can not be changed", ) for attr_, value_ in user_attrs.items(): if attr_ == "metadata": metadata = channel.load_channel_metadata() metadata.update(value_) setattr(channel, "channel_metadata", json.dumps(metadata)) else: setattr(channel, attr_, value_) db.commit() return channel
def local_channel(db): channel = Channel(name="test_local_channel") db.add(channel) db.commit() yield channel db.delete(channel) db.commit()
def proxy_channel(db): channel = Channel(name="test_proxy_channel", mirror_channel_url="http://host", mirror_mode="proxy") db.add(channel) db.commit() yield channel db.delete(channel) db.commit()
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()
def __init__(self, db): self.keya = "akey" self.keyb = "bkey" self.usera = User(id=uuid.uuid4().bytes, username='******') Profile(name='usera', user=self.usera, avatar_url='') db.add(self.usera) self.userb = User(id=uuid.uuid4().bytes, username='******') Profile(name='userb', user=self.userb, avatar_url='') db.add(self.userb) self.userc = User(id=uuid.uuid4().bytes, username='******', role="owner") Profile(name='userc', user=self.userc, avatar_url='') db.add(self.userc) assert len(db.query(User).all()) == 3 self.keya_obj = ApiKey( key=self.keya, time_created=date.today(), expire_at=date(2030, 1, 1), user_id=self.usera.id, owner_id=self.usera.id, ) db.add(self.keya_obj) db.add( ApiKey( key=self.keyb, time_created=date.today(), expire_at=date(2030, 1, 1), user_id=self.userb.id, owner_id=self.userb.id, ) ) self.channel1 = Channel(name="testchannel", private=False) self.channel2 = Channel(name="privatechannel", private=True) self.package1 = Package(name="package1", channel=self.channel1) self.package2 = Package(name="package2", channel=self.channel2) self.package_version_1 = PackageVersion( id=uuid.uuid4().bytes, channel_name=self.channel1.name, package_name=self.package1.name, package_format='tarbz2', platform='noarch', version="0.0.1", build_number="0", build_string="", filename="filename.tar.bz2", info="{}", uploader_id=self.usera.id, size=101, ) self.package_version_2 = PackageVersion( id=uuid.uuid4().bytes, channel_name=self.channel2.name, package_name=self.package2.name, package_format='tarbz2', platform='noarch', version="0.0.1", build_number="0", build_string="", filename="filename2.tar.bz2", info="{}", uploader_id=self.usera.id, size=101, ) self.channel_member = ChannelMember( channel=self.channel2, user=self.usera, role='maintainer' ) self.channel_member_userc = ChannelMember( channel=self.channel2, user=self.userc, role='owner' ) self.package_member = PackageMember( channel=self.channel2, user=self.userc, package=self.package2, role="owner" ) for el in [ self.channel1, self.channel2, self.channel_member, self.channel_member_userc, self.package1, self.package2, self.package_version_1, self.package_version_2, self.package_member, ]: db.add(el) db.commit() self.db = db
def channel(dao: "quetz.dao.Dao", channel_name, user): channel_data = Channel(name=channel_name, private=False) channel = dao.create_channel(channel_data, user.id, "owner") return channel
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()
def channel(dao, user, channel_name): channel_data = Channel(name=channel_name, private=False) channel = dao.create_channel(channel_data, user.id, "owner") return channel
def channel(db): channel = Channel(name="user-with-role", private=True) db.add(channel) db.commit() return channel
def execute_channel_action( self, action: str, channel: db_models.Channel, start_at: Optional[datetime] = None, repeat_every_seconds: Optional[int] = None, ): auth = self.auth channel_name = channel.name channel_metadata = channel.load_channel_metadata() assert_channel_action(action, channel) user_id = auth.assert_user() if action == ChannelActionEnum.synchronize: auth.assert_synchronize_mirror(channel_name) extra_args = dict( channel_name=channel_name, includelist=channel_metadata.get('includelist', None), excludelist=channel_metadata.get('excludelist', None), ) task = self.jobs_dao.create_job( action.encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) elif action == ChannelActionEnum.synchronize_repodata: auth.assert_synchronize_mirror(channel_name) extra_args = dict( channel_name=channel_name, use_repodata=True, includelist=channel_metadata.get('includelist', None), excludelist=channel_metadata.get('excludelist', None), ) task = self.jobs_dao.create_job( action.encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) elif action == ChannelActionEnum.validate_packages: auth.assert_validate_package_cache(channel_name) extra_args = dict(channel_name=channel.name) task = self.jobs_dao.create_job( action.encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) elif action == ChannelActionEnum.generate_indexes: auth.assert_reindex_channel(channel_name) extra_args = dict(channel_name=channel.name) task = self.jobs_dao.create_job( action.encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) elif action == ChannelActionEnum.reindex: auth.assert_reindex_channel(channel_name) extra_args = dict(channel_name=channel_name, ) task = self.jobs_dao.create_job( action.encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) elif action == ChannelActionEnum.synchronize_metrics: auth.assert_reindex_channel(channel_name) extra_args = dict(channel_name=channel.name) task = self.jobs_dao.create_job( action.encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) elif action in [ ChannelActionEnum.cleanup, ChannelActionEnum.cleanup_dry_run ]: auth.assert_channel_db_cleanup(channel_name) dry_run = action == ChannelActionEnum.cleanup_dry_run extra_args = dict( channel_name=channel_name, dry_run=dry_run, ) task = self.jobs_dao.create_job( f"db_{action}".encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) task = self.jobs_dao.create_job( f"pkgstore_{action}".encode('ascii'), user_id, extra_args=extra_args, start_at=start_at, repeat_every_seconds=repeat_every_seconds, ) else: raise HTTPException( status_code=status.HTTP_501_NOT_IMPLEMENTED, detail= (f"Action {action} on channel {channel.name} is not implemented" ), ) return task
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) 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)