def test_list_user_packages( user, client, other_user, db, private_channel, private_package, http_user, expected_role, endpoint, ): member = PackageMember(channel=private_channel, package=private_package, user=user, role="member") db.add(member) db.commit() response = client.get(f"/api/dummylogin/{http_user}") assert response.status_code == 200 response = client.get(endpoint.format(http_user=http_user)) assert response.status_code == 200 data = response.json() if "result" in data: data = data["result"] assert data == [{ "name": private_package.name, "channel_name": private_channel.name, "role": expected_role, }]
def test_list_keys_with_package_roles( auth_client, api_keys, db, user, other_user, private_channel, private_package, ): package_member = PackageMember(channel=private_channel, package=private_package, user=user, role="maintainer") db.add(package_member) db.commit() response = auth_client.get("/api/api-keys") assert response.status_code == 200 returned_keys = { key["description"]: key["roles"] for key in response.json() } assert len(returned_keys) == 2 assert returned_keys["user_is_user"] is None assert returned_keys["key"] is None
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 test_get_package_version_permissions( auth_client, user, private_package_version, user_package_role, user_channel_role, private_channel, db, private_package, private, user_server_role, ): private_channel.private = private user.role = user_server_role if user_channel_role: channel_member = ChannelMember( channel=private_channel, user=user, role=user_channel_role ) db.add(channel_member) if user_package_role: package_member = PackageMember( channel=private_channel, user=user, package=private_package, role=user_package_role, ) db.add(package_member) db.commit() filename = private_package_version.filename platform = private_package_version.platform channel_name = private_package_version.channel_name package_name = private_package_version.package_name response = auth_client.get( f"/api/channels/{channel_name}/" f"packages/{package_name}/versions/{platform}/{filename}" ) if not private: assert response.status_code == 200 elif user_server_role in [OWNER, MAINTAINER]: assert response.status_code == 200 elif user_channel_role in [OWNER, MAINTAINER, MEMBER]: assert response.status_code == 200 elif user_package_role in [OWNER, MAINTAINER, MEMBER]: assert response.status_code == 200 else: assert response.status_code == 403
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 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()
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=f'xtensor', description=f'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')