def test_add_remove_files(self): """Test case for adding and removing files from collections Add/Remove File from Collection # noqa: E501 """ filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f2 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f3 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f1.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f2.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f3.key), c1.key) files = self.hiarc_collections.get_collection_files(c1.key) assert len(files) == 3 self.hiarc_collections.remove_file_from_collection(c1.key, f3.key) files = self.hiarc_collections.get_collection_files(c1.key) assert len(files) == 2 for f in [f1.key, f2.key, f3.key]: self.hiarc_files.delete_file(f) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f)
def test_get_collections_for_file(self): """Test case for getting collections for a file Collections for File # noqa: E501 """ cfr1 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(cfr1, filepath) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) aftc = hiarc.AddFileToCollectionRequest(f1.key) self.hiarc_collections.add_file_to_collection(aftc, c1.key) c2 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) aftc = hiarc.AddFileToCollectionRequest(f1.key) self.hiarc_collections.add_file_to_collection(aftc, c1.key) c3 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) aftc = hiarc.AddFileToCollectionRequest(f1.key) self.hiarc_collections.add_file_to_collection(aftc, c1.key) collections = self.hiarc_files.get_collections_for_file(f1.key) assert len(collections) == 3 self.hiarc_files.delete_file(f1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f1.key)
def test_get_files_for_user(self): """Test case for getting files of collections for a user Files of Collection for User # noqa: E501 """ filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') u1 = self.hiarc_users.create_user(self.hiarc_util.create_user()) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) f1 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f2 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f3 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f1.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f2.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f3.key), c1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_collections.get_collection_files, c1.key, x_hiarc_user_key=u1.key) self.hiarc_collections.add_user_to_collection( hiarc.AddUserToCollectionRequest(u1.key, hiarc.AccessLevel.READ_ONLY), c1.key) files = self.hiarc_collections.get_collection_files( c1.key, x_hiarc_user_key=u1.key) assert len(files) == 3 ut = self.hiarc_token.create_user_token( hiarc.CreateUserTokenRequest(key=u1.key)) lc = hiarc.CollectionApi( hiarc.ApiClient(configuration=self.hiarc_util. init_hiarc_client_jwt_token(ut.bearer_token))) files = lc.get_collection_files(c1.key) assert len(files) == 3 # reset ApiClient to Admin User hiarc.ApiClient( configuration=self.hiarc_util.init_hiarc_config_admin()) for f in [f1.key, f2.key, f3.key]: self.hiarc_files.delete_file(f) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f)
def test_delete_with_file(self): """Test case for deleting collections with files Delete Collection with File # noqa: E501 """ c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f1.key), c1.key) self.hiarc_collections.delete_collection(c1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_collections.get_collection, c1.key) r = self.hiarc_files.get_file(f1.key) assert f1 == r self.hiarc_files.delete_file(f1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f1.key)
def test_complex_delete(self): """Test case for deleting file in complex situations File Complex Delete # noqa: E501 """ cfr = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f = self.hiarc_files.create_file(cfr, filepath) new_version_filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'NewVersionOfTest.txt') nvr = hiarc.AddVersionToFileRequest(key=f.key) self.hiarc_files.add_version(nvr, new_version_filepath, f.key) nvr = hiarc.AddVersionToFileRequest(key=f.key) self.hiarc_files.add_version(nvr, new_version_filepath, f.key) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) aftc = hiarc.AddFileToCollectionRequest(f.key) self.hiarc_collections.add_file_to_collection(aftc, c1.key) self.hiarc_files.delete_file(f.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f.key)
def test_parent_collection_access_by_group(self): """Test case for accessing a file in parent collection inheritance by group Group File Access in Parent Collection # noqa: E501 """ cfr1 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(cfr1, filepath) u1 = self.hiarc_users.create_user(self.hiarc_util.create_user()) g1 = self.hiarc_groups.create_group(self.hiarc_util.create_group()) self.hiarc_groups.add_user_to_group(g1.key, u1.key) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c2 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c3 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) self.hiarc_collections.add_child_to_collection(c1.key, c2.key) self.hiarc_collections.add_child_to_collection(c2.key, c3.key) # c1 -> c2 -> c3 agtc = hiarc.AddGroupToCollectionRequest(g1.key, hiarc.AccessLevel.READ_ONLY) self.hiarc_collections.add_group_to_collection( agtc, c1.key) # add group to parent collection aftc = hiarc.AddFileToCollectionRequest(f1.key) self.hiarc_collections.add_file_to_collection( aftc, c1.key) # add file to parent collection fetched = self.hiarc_files.get_file(f1.key, x_hiarc_user_key=u1.key) assert fetched == f1 self.hiarc_files.delete_file(f1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f1.key)
def test_child_collection_access_by_user(self): """Test case for accessing a file in child collection inheritance by user User File Access in Child Collection # noqa: E501 """ cfr1 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(cfr1, filepath) u1 = self.hiarc_users.create_user(self.hiarc_util.create_user()) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c2 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c3 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) self.hiarc_collections.add_child_to_collection(c1.key, c2.key) self.hiarc_collections.add_child_to_collection(c2.key, c3.key) # c1 -> c2 -> c3 autc = hiarc.AddUserToCollectionRequest(u1.key, hiarc.AccessLevel.READ_ONLY) self.hiarc_collections.add_user_to_collection( autc, c1.key) # add user to parent collection aftc = hiarc.AddFileToCollectionRequest(f1.key) # add file to nested child collection with parent of c1 self.hiarc_collections.add_file_to_collection(aftc, c3.key) fetched = self.hiarc_files.get_file(f1.key, x_hiarc_user_key=u1.key) assert fetched == f1 self.hiarc_files.delete_file(f1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f1.key)
def test_add_remove_files_as_user(self): """Test case for adding and removing files from collections as user Add/Remove File from Collection as User # noqa: E501 """ u1 = self.hiarc_users.create_user(self.hiarc_util.create_user()) filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) aftcr = hiarc.AddFileToCollectionRequest(f1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_collections.add_file_to_collection, aftcr, c1.key, x_hiarc_user_key=u1.key) self.hiarc_collections.add_user_to_collection( hiarc.AddUserToCollectionRequest(u1.key, hiarc.AccessLevel.READ_ONLY), c1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_collections.add_file_to_collection, aftcr, c1.key, x_hiarc_user_key=u1.key) u2 = self.hiarc_users.create_user(self.hiarc_util.create_user()) self.hiarc_collections.add_user_to_collection( hiarc.AddUserToCollectionRequest(u2.key, hiarc.AccessLevel.READ_WRITE), c1.key) self.hiarc_collections.add_file_to_collection(aftcr, c1.key, x_hiarc_user_key=u2.key) files = self.hiarc_collections.get_collection_files(c1.key) assert len(files) == 1 u3 = self.hiarc_users.create_user(self.hiarc_util.create_user()) self.assertRaises(hiarc.rest.ApiException, self.hiarc_collections.remove_file_from_collection, c1.key, f1.key, x_hiarc_user_key=u3.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_collections.remove_file_from_collection, c1.key, f1.key, x_hiarc_user_key=u1.key) self.hiarc_collections.remove_file_from_collection( c1.key, f1.key, x_hiarc_user_key=u2.key) files = self.hiarc_collections.get_collection_files(c1.key) assert len(files) == 0 self.hiarc_files.delete_file(f1.key) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f1.key)
def test_get_items(self): """Test case for getting items of collections Items of Collection # noqa: E501 """ filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c2 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c3 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c4 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c5 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c6 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) self.hiarc_collections.add_child_to_collection(c1.key, c2.key) self.hiarc_collections.add_child_to_collection(c1.key, c3.key) self.hiarc_collections.add_child_to_collection(c1.key, c4.key) self.hiarc_collections.add_child_to_collection(c2.key, c5.key) self.hiarc_collections.add_child_to_collection(c4.key, c6.key) f1 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f2 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f3 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f4 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) f5 = self.hiarc_files.create_file(self.hiarc_util.create_file(), filepath) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f1.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f2.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f3.key), c1.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f4.key), c2.key) self.hiarc_collections.add_file_to_collection( hiarc.AddFileToCollectionRequest(f5.key), c3.key) items = self.hiarc_collections.get_collection_items(c1.key) children = items.child_collections assert len(children) == 3 assert next((c for c in children if self.hiarc_util.compare_entity_to_entity(c, c2)), None) is not None assert next((c for c in children if self.hiarc_util.compare_entity_to_entity(c, c3)), None) is not None assert next((c for c in children if self.hiarc_util.compare_entity_to_entity(c, c4)), None) is not None assert next( (c for c in children if self.hiarc_util.compare_entity_to_entity(c, c5)), None) is None assert next( (c for c in children if self.hiarc_util.compare_entity_to_entity(c, c6)), None) is None files = items.files assert len(files) == 3 assert next((f for f in files if self.hiarc_util.compare_entity_to_entity(f, f1)), None) is not None assert next((f for f in files if self.hiarc_util.compare_entity_to_entity(f, f2)), None) is not None assert next((f for f in files if self.hiarc_util.compare_entity_to_entity(f, f3)), None) is not None assert next( (f for f in files if self.hiarc_util.compare_entity_to_entity(f, f4)), None) is None assert next( (f for f in files if self.hiarc_util.compare_entity_to_entity(f, f5)), None) is None for f in [f1.key, f2.key, f3.key, f4.key, f5.key]: self.hiarc_files.delete_file(f) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f)
def test_get_allowed_files(self): """Test case for getting allowed files for user Files allowed to access # noqa: E501 """ cfr1 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f1 = self.hiarc_files.create_file(cfr1, filepath) cfr2 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f2 = self.hiarc_files.create_file(cfr2, filepath) cfr3 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f3 = self.hiarc_files.create_file(cfr3, filepath) cfr4 = self.hiarc_util.create_file() filepath = os.path.join(os.getcwd(), self.hiarc_util.TEST_FILE_PATH, 'Test.txt') f4 = self.hiarc_files.create_file(cfr4, filepath) u1 = self.hiarc_users.create_user(self.hiarc_util.create_user()) autf = hiarc.AddUserToFileRequest(u1.key, hiarc.AccessLevel.READ_ONLY) self.hiarc_files.add_user_to_file(autf, f1.key) c1 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) aftc = hiarc.AddFileToCollectionRequest(f2.key) self.hiarc_collections.add_file_to_collection(aftc, c1.key) autc = hiarc.AddUserToCollectionRequest(u1.key, hiarc.AccessLevel.READ_ONLY) self.hiarc_collections.add_user_to_collection(autc, c1.key) c2 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) c3 = self.hiarc_collections.create_collection( self.hiarc_util.create_collection()) self.hiarc_collections.add_child_to_collection(c2.key, c3.key) aftc = hiarc.AddFileToCollectionRequest(f3.key) self.hiarc_collections.add_file_to_collection(aftc, c3.key) self.hiarc_collections.add_user_to_collection(autc, c2.key) files = [f1.key, f2.key, f3.key, f4.key] filter = hiarc.AllowedFilesRequest(files) filtered_files = self.hiarc_files.filter_allowed_files( filter, x_hiarc_user_key=u1.key) self.assertIn(f1.key, filtered_files) self.assertIn(f2.key, filtered_files) self.assertIn(f3.key, filtered_files) self.assertNotIn(f4.key, filtered_files) for f in files: self.hiarc_files.delete_file(f) self.assertRaises(hiarc.rest.ApiException, self.hiarc_files.get_file, f)