def test_remove_feature_image(projects_fixture, image_file_fixture): feature = FeaturesService.fromImage(projects_fixture.id, image_file_fixture, metadata={}) FeaturesService.delete(feature.id) assert db_session.query(Feature).count() == 0 assert db_session.query(FeatureAsset).count() == 0 assert len(os.listdir(get_project_asset_dir(feature.project_id))) == 0
def test_delete_unauthorized(test_client, projects_fixture): u2 = db_session.query(User).get(2) resp = test_client.delete('/projects/1/', headers={'x-jwt-assertion-test': u2.jwt}) assert resp.status_code == 403 proj = db_session.query(Project).get(1) assert proj is not None
def removeUserFromProject( projectId: int, username: str, ) -> None: """ Remove a user from a Project. :param projectId: int :param username: str :return: None """ proj = db_session.query(Project).get(projectId) user = db_session.query(User).filter(User.username == username).first() observable_project = db_session.query(ObservableDataProject) \ .filter(ObservableDataProject.id == projectId).first() if user not in proj.users: raise ApiException("User is not in project") if len(proj.users) == 1: raise ApiException("Unable to remove last user of project") if observable_project: number_of_potential_observers = len( [u for u in proj.users if u.jwt]) if user.jwt and number_of_potential_observers == 1: raise ApiException( "Unable to remove last user of observable project who can observe file system" ) proj.users.remove(user) db_session.commit()
def test_update_tile_servers(test_client, projects_fixture): u1 = db_session.query(User).get(1) resp1 = test_client.post('/projects/1/tile-servers/', json=_get_tile_server_data(), headers={'x-jwt-assertion-test': u1.jwt}) resp2 = test_client.post('/projects/1/tile-servers/', json=_get_tile_server_data(), headers={'x-jwt-assertion-test': u1.jwt}) updated_data = [{ "id": resp1.get_json()['id'], "name": "NewTestName1" }, { "id": resp2.get_json()['id'], "name": "NewTestName2" }] resp = test_client.put('/projects/1/tile-servers/', json=updated_data, headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 my_tsv1 = db_session.query(TileServer).get(1) assert my_tsv1.name == "NewTestName1" my_tsv2 = db_session.query(TileServer).get(2) assert my_tsv2.name == "NewTestName2"
def test_insert_feature_geojson(projects_fixture, feature_properties_file_fixture): features = FeaturesService.fromGeoJSON(projects_fixture.id, feature_properties_file_fixture, metadata={}) feature = features[0] assert len(features) == 1 assert feature.project_id == projects_fixture.id assert db_session.query(Feature).count() == 1 assert db_session.query(FeatureAsset).count() == 0
def test_delete_empty_project(test_client, projects_fixture): u1 = db_session.query(User).get(1) resp = test_client.delete('/projects/1/', headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 proj = db_session.query(Project).get(1) assert proj is None
def test_remove_feature_image_asset(projects_fixture, feature_fixture, image_file_fixture): feature = FeaturesService.createFeatureAsset(projects_fixture.id, feature_fixture.id, FileStorage(image_file_fixture)) FeaturesService.delete(feature.id) assert db_session.query(Feature).count() == 0 assert db_session.query(FeatureAsset).count() == 0 assert len(os.listdir(get_project_asset_dir(feature.project_id))) == 0
def test_delete_point_cloud(test_client, projects_fixture, point_cloud_fixture): u1 = db_session.query(User).get(1) resp = test_client.delete('/projects/1/point-cloud/1/', headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 point_cloud = db_session.query(PointCloud).get(1) assert point_cloud is None
def test_external_data_rapp_missing_geospatial_metadata( userdata, projects_fixture, agave_utils_with_image_file_from_rapp_folder): agave_utils_with_image_file_from_rapp_folder.client_in_utils.getMetaAssociated.return_value = {} u1 = db_session.query(User).filter(User.username == "test1").first() import_from_agave(projects_fixture.tenant_id, u1.id, "testSystem", "/Rapp", projects_fixture.id) features = db_session.query(Feature).all() assert len(features) == 0
def test_create_feature_image_small_image(projects_fixture, image_small_DES_2176_fixture): feature = FeaturesService.fromImage(projects_fixture.id, image_small_DES_2176_fixture, metadata={}) assert feature.project_id == projects_fixture.id assert len(feature.assets) == 1 assert db_session.query(Feature).count() == 1 assert db_session.query(FeatureAsset).count() == 1 assert len(os.listdir(get_project_asset_dir(feature.project_id))) == 2 os.path.isfile(get_asset_path(feature.assets[0].path)) os.path.isfile(os.path.join(get_project_asset_dir(projects_fixture.id), str(feature.assets[0].uuid) + ".thumb.jpeg"))
def test_delete_point_cloud(projects_fixture): u1 = db_session.query(User).get(1) point_cloud = PointCloudService.create(projectId=projects_fixture.id, data=POINT_CLOUD_DATA, user=u1) PointCloudService.delete(pointCloudId=point_cloud.id) assert db_session.query(PointCloud).count() == 0 assert db_session.query(Feature).count() == 0 assert len(os.listdir(get_project_asset_dir(point_cloud.project_id))) == 0
def test_delete_tile_server(test_client, projects_fixture): u1 = db_session.query(User).get(1) test_client.post('/projects/1/tile-servers/', json=_get_tile_server_data(), headers={'x-jwt-assertion-test': u1.jwt}) resp = test_client.delete('/projects/1/tile-servers/1/', headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 proj = db_session.query(TileServer).get(1) assert proj is None
def test_add_point_cloud(projects_fixture): u1 = db_session.query(User).get(1) point_cloud = PointCloudService.create(projectId=projects_fixture.id, data=POINT_CLOUD_DATA, user=u1) assert point_cloud.description == "description" assert point_cloud.conversion_parameters == "--scale 2.0" assert not point_cloud.feature assert point_cloud.project_id == projects_fixture.id assert db_session.query(PointCloud).count() == 1
def test_delete_overlay(test_client, projects_fixture, image_file_fixture): u1 = db_session.query(User).get(1) test_client.post('/projects/1/overlays/', data=_get_overlay_data({"file": image_file_fixture}), headers={'x-jwt-assertion-test': u1.jwt}) u1 = db_session.query(User).get(1) resp = test_client.delete('/projects/1/overlays/1/', headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 proj = db_session.query(Overlay).get(1) assert proj is None
def test_external_data_good_files(userdata, projects_fixture, agave_utils_with_geojson_file): u1 = db_session.query(User).filter(User.username == "test1").first() import_from_agave(projects_fixture.tenant_id, u1.id, "testSystem", "/testPath", projects_fixture.id) features = db_session.query(Feature).all() # the test geojson has 3 features in it assert len(features) == 3 # This should only have been called once, since there is only # one FILE in the listing agave_utils_with_geojson_file.getFile.assert_called_once()
def delete(featureId: int) -> None: """ Delete a Feature and any assets tied to it. :param featureId: int :return: None """ feat = db_session.query(Feature).get(featureId) assets = db_session.query(FeatureAsset).filter( FeatureAsset.feature_id == featureId) for asset in assets: delete_assets(projectId=feat.project_id, uuid=asset.uuid) db_session.delete(feat) db_session.commit()
def get(project_id: Optional[int] = None, uuid: Optional[str] = None) -> Project: """ Get the metadata associated with a project :param project_id: int :param uid: str :return: Project """ if project_id is not None: return db_session.query(Project).get(project_id) elif uuid is not None: return db_session.query(Project).filter( Project.uuid == uuid).first() raise ValueError("project_id or uid is required")
def test_external_data_no_files_except_for_trash( userdata, projects_fixture, agave_utils_listing_with_single_trash_folder_of_image): u1 = db_session.query(User).filter(User.username == "test1").first() import_from_agave(projects_fixture.tenant_id, u1.id, "testSystem", "/", projects_fixture.id) features = db_session.query(Feature).all() # just a .Trash dir so nothing to import and only top level listing should occur assert len(features) == 0 assert agave_utils_listing_with_single_trash_folder_of_image.client_in_external_data.listing.call_count == 1 agave_utils_listing_with_single_trash_folder_of_image.client_in_external_data.getFile.assert_not_called( ) agave_utils_listing_with_single_trash_folder_of_image.client_in_utils.getFile.assert_not_called( )
def test_external_data_rapp(userdata, projects_fixture, agave_utils_with_image_file_from_rapp_folder): u1 = db_session.query(User).filter(User.username == "test1").first() import_from_agave(projects_fixture.tenant_id, u1.id, "testSystem", "/Rapp", projects_fixture.id) features = db_session.query(Feature).all() # should be one feature with a single image asset assert len(features) == 1 assert len(features[0].assets) == 1 assert len(os.listdir(get_project_asset_dir( features[0].project_id))) == 2 # processed image + thumbnail # This should only have been called once, since there is only one FILE in the listing agave_utils_with_image_file_from_rapp_folder.client_in_external_data.getFile.assert_called_once( )
def getAll(user: User) -> List[Notification]: return db_session.query(Notification) \ .filter(Notification.username == user.username)\ .filter(Notification.tenant_id == user.tenant_id)\ .order_by(Notification.created.desc()) \ .limit(100)\ .all()
def get(featureId: int) -> Feature: """ Retreive a single Feature :param featureId: int :return: Feature """ return db_session.query(Feature).get(featureId)
def test_project_data(test_client, projects_fixture): u1 = db_session.query(User).get(1) resp = test_client.get('/projects/', headers={'x-jwt-assertion-test': u1.jwt}) data = resp.get_json() assert data[0]["name"] == "test" assert data[0]["description"] == "description"
def test_post_image_feature_asset(MockAgaveUtils, test_client, projects_fixture, feature_fixture, image_file_fixture): MockAgaveUtils().getFile.return_value = image_file_fixture u1 = db_session.query(User).filter(User.username == "test1").first() resp = test_client.post( '/projects/1/features/1/assets/', json={"system_id": 'test', 'path': '/test/corrected_image.jpg'}, headers={'x-jwt-assertion-test': u1.jwt}) data = resp.get_json() assert resp.status_code == 200 # Have to reload the User from the DB, in app.py in the teardown_appcontext callback # the session is removed, which causes u1 above to be undefined AFTER the request above. u1 = db_session.query(User).get(1) resp2 = test_client.get("/projects/1/features/1/", headers={'x-jwt-assertion-test': u1.jwt}) feat = resp2.get_json() assert len(feat["assets"]) == 1
def test_create_observable_project_already_exists( test_client, projects_fixture, get_system_users_mock, observable_projects_fixture, import_from_agave_mock, agave_utils_with_geojson_file_mock): u1 = db_session.query(User).get(1) data = { 'project': { 'name': "Renamed Project", 'description': "New Description", 'system_id': observable_projects_fixture.system_id, 'system_path': observable_projects_fixture.path, 'system_file': 'testFilename', }, 'observable': True, 'watch_content': False } resp = test_client.post('/projects/', json=data, headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 409 assert "Conflict, a project for this storage system/path already exists" in resp.json[ 'message']
def test_project_permissions(test_client, projects_fixture): u2 = db_session.query(User).get(2) resp = test_client.get('/projects/', headers={'x-jwt-assertion-test': u2.jwt}) data = resp.get_json() assert resp.status_code == 200 assert len(data) == 0
def test_get_project_features_filter_with_date_range(test_client, projects_fixture, feature_fixture): u1 = db_session.query(User).get(1) start_date = (datetime.datetime.now() - datetime.timedelta(minutes=5)).isoformat() end_date = (datetime.datetime.now() + datetime.timedelta(minutes=5)).isoformat() resp = test_client.get('/projects/1/features/', query_string={ 'startDate': start_date, 'endDate': end_date }, headers={'x-jwt-assertion-test': u1.jwt}) data = resp.get_json() assert resp.status_code == 200 assert len(data['features']) == 1 start_date = (datetime.datetime.now() + datetime.timedelta(minutes=1)).isoformat() resp = test_client.get('/projects/1/features/', query_string={ 'startDate': start_date, 'endDate': end_date }, headers={'x-jwt-assertion-test': u1.jwt}) data = resp.get_json() assert resp.status_code == 200 assert len(data['features']) == 0
def test_get_project_using_single_uuid_not_member_of_project( test_client, projects_fixture): u2 = db_session.query(User).get(2) resp = test_client.get('/projects/', query_string='uuid={}'.format( projects_fixture.uuid), headers={'x-jwt-assertion-test': u2.jwt}) assert resp.status_code == 403
def test_update_project(test_client, projects_fixture): u1 = db_session.query(User).get(1) data = { 'project': { 'name': "Renamed Project", 'description': "New Description", 'public': True }, } resp = test_client.put('/projects/1/', json=data, headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 proj = db_session.query(Project).get(1) assert proj.name == "Renamed Project" assert proj.description == "New Description" assert proj.public
def test_get_project_features_single_feature(test_client, projects_fixture, feature_fixture): u1 = db_session.query(User).get(1) resp = test_client.get('/projects/1/features/', headers={'x-jwt-assertion-test': u1.jwt}) data = resp.get_json() assert resp.status_code == 200 assert len(data['features']) != 0
def test_get_point_clouds_listing(test_client, projects_fixture, point_cloud_fixture): u1 = db_session.query(User).get(1) resp = test_client.get('/projects/1/point-cloud/', headers={'x-jwt-assertion-test': u1.jwt}) assert resp.status_code == 200 data = resp.get_json() assert len(data) == 1