def test_run_prefix__assigning_samples_failed(mocker): """Test run prefix when assigning samples fails.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**MOCKED_UPLOADS), ) mocked_add_samples_to_project = mocker.patch.object( APIClient, "add_samples_to_project", side_effect=APIClientError(message="", status_code=404), ) res = runner.invoke( run_prefix, [ str(uuid4()), "gncv://batch", "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_get_sample_sheet.assert_called_once() mocked_add_samples_to_project.assert_called_once() assert "You do not have the sufficient permission" not in res.output assert "There was an error assigning" in res.output
def test_run_prefix__not_owned_project(mocker): """Test run prefix failure when project is not owned.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", side_effect=APIClientError(message="", status_code=403), ) mocked_add_samples_to_project = mocker.patch.object( APIClient, "add_samples_to_project", ) res = runner.invoke( run_prefix, [ str(uuid4()), "gncv://batch", "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_get_sample_sheet.assert_called_once() mocked_add_samples_to_project.assert_not_called() assert "You do not have the sufficient permission" in res.output
def test_create_merged_vcf__not_owned_project(mocker): """Test create merged file failure when project is not owned.""" mocked_response = {"detail": "Not found."} runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_create_merged_vcf = mocker.patch.object( APIClient, "create_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message="", status_code=403), ) res = runner.invoke( create_merged_vcf, [ str(uuid4()), "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_create_merged_vcf.assert_called_once() assert "You do not have the sufficient permission" in res.output
def test_list_projects_no_permission(mocker, credentials): """Test projects no permission available to show them.""" runner = CliRunner() mocked_get_projects = mocker.patch.object( APIClient, "list_projects", side_effect=APIClientError( message="API Client Error: Not Found: Not found.", status_code=403), return_value={"detail": "Not found"}, ) mocked_get_pipeline_capabilities = mocker.patch.object( APIClient, "get_pipeline_capabilities") res = runner.invoke(list_projects, credentials) assert res.exit_code == 1 mocked_get_projects.assert_called_once() mocked_get_pipeline_capabilities.assert_not_called() output_line = io.BytesIO() sys.stdout = output_line echo("\n".join([ "ERROR: You do not have the sufficient permission " "level required to perform this operation.", "ERROR: API Client Error: Not Found: Not found.", "Aborted!", ])) assert output_line.getvalue() == res.output.encode()
def test_s3_import__no_pipeline_capabilities(mocker): """Test S3 import failure when project has no pipeline capabilities. """ runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_import_s3_projects = mocker.patch.object( APIClient, "s3_uri_import", side_effect=APIClientError( message="Project configuration must be set before adding " "samples to a project.", status_code=400, ), ) res = runner.invoke( s3_import, [ "s3://bucket/path/", str(uuid4()), "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_import_s3_projects.assert_called_once() assert "There was an error importing from S3." in res.output assert ( "Project configuration must be set before adding samples to a project." in res.output)
def test_get_metadata__not_owned_sample(mocker): """Test get metadata failure when sample is not owned. This will return empty response to handle cases when using the CLI tool. """ mocked_response = {"detail": "Not found."} runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_get_metadata = mocker.patch.object( APIClient, "get_metadata", return_value=mocked_response, side_effect=APIClientError(message="", status_code=403), ) res = runner.invoke( get_metadata, [ str(uuid4()), "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_get_metadata.assert_called_once() assert "You do not have the sufficient permission" in res.output
def test_list_projects_no_project(mocker, credentials): """Test project samples throw an error if no project available.""" runner = CliRunner() mocked_get_project_samples = mocker.patch.object( APIClient, "get_project_samples", side_effect=APIClientError( message="API Client Error: Not Found: Not found.", status_code=404), return_value={"detail": "Not found"}, ) project_id = str(uuid4()) res = runner.invoke( list_project_samples, [project_id, *credentials], ) assert res.exit_code == 1 mocked_get_project_samples.assert_called_once() output_line = io.BytesIO() sys.stdout = output_line echo_data("\n".join([ "ERROR: Project {} does not exist.".format(project_id), "ERROR: API Client Error: Not Found: Not found.", "Aborted!", ])) assert output_line.getvalue() == res.output.encode()
def test_biosamples_list__no_permission(mocker): """Test BaseSpace Biosamples no permission available to show them.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_list_biosamples = mocker.patch.object( APIClient, "list_biosamples", side_effect=APIClientError( message="API Client Error: Not Found: Not found.", status_code=403), return_value={"detail": "Not found"}, ) res = runner.invoke( biosamples_list, ["1234", "--email", "*****@*****.**", "--password", "123"], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_list_biosamples.assert_called_once() output_line = io.BytesIO() sys.stdout = output_line echo("\n".join([ "ERROR: There was an error listing Biosamples.", "ERROR: You do not have the sufficient permission " "level required to perform this operation.", "ERROR: API Client Error: Not Found: Not found.", "Aborted!", ])) assert output_line.getvalue() == res.output.encode()
def test_status_merged_vcf__empty(mocker): """Test create merged file failure when project is responding empty. This can be due to no merged files or no permission required for the project or no project at all.""" project_id = str(uuid4()) mocked_response = {"detail": "Not found."} runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_retrieve_merged_vcf = mocker.patch.object( APIClient, "retrieve_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message="", status_code=404), ) res = runner.invoke( status_merged_vcf, [ project_id, "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_retrieve_merged_vcf.assert_called_once() message = ("Project {} does not exist or there are no " "running jobs associated with it.".format(project_id)) assert message in res.output
def test_create_merged_vcf__already_running(mocker): """Test create merged file failure when the job is alredy running.""" project_id = str(uuid4()) message = "Merge VCFs for project {} is already running".format(project_id) mocked_response = [message] runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_create_merged_vcf = mocker.patch.object( APIClient, "create_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message=message, status_code=400), ) res = runner.invoke( create_merged_vcf, [ project_id, "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_create_merged_vcf.assert_called_once() assert "is already running" in res.output
def test_create_merged_vcf__needs_two_samples(mocker): """Test create merged file failure when there's only one sample.""" project_id = str(uuid4()) mocked_response = ["should have at least 2 samples"] runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_create_merged_vcf = mocker.patch.object( APIClient, "create_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message=mocked_response[0], status_code=400), ) res = runner.invoke( create_merged_vcf, [ project_id, "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_create_merged_vcf.assert_called_once() assert "should have at least 2 samples" in res.output
def test_create_merged_vcf__no_impute_files(mocker): """Test create merged file failure when there are no impute files.""" project_id = str(uuid4()) mocked_response = ["must have impute VCF files associated"] runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_create_merged_vcf = mocker.patch.object( APIClient, "create_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message=mocked_response[0], status_code=400), ) res = runner.invoke( create_merged_vcf, [ project_id, "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_create_merged_vcf.assert_called_once() assert "must have impute VCF files associated" in res.output
def test_create_merged_vcf__up_to_date(mocker): """Test create merged file failure when attempting to create another one that is the same. """ project_id = str(uuid4()) message = "Merged VCF file for project {} is up-to-date".format(project_id) mocked_response = [message] runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_create_merged_vcf = mocker.patch.object( APIClient, "create_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message=message, status_code=400), ) res = runner.invoke( create_merged_vcf, [ project_id, "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_create_merged_vcf.assert_called_once() assert "is up-to-date" in res.output
def test_create_merged_vcf__not_able(mocker): """Test create merged file failure when it cannot be done.""" project_id = str(uuid4()) mocked_response = [ "You attempted to merge VCFs for a project that cannot do that" ] runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_create_merged_vcf = mocker.patch.object( APIClient, "create_merged_vcf", return_value=mocked_response, side_effect=APIClientError(message=mocked_response[0], status_code=400), ) res = runner.invoke( create_merged_vcf, [ project_id, "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_create_merged_vcf.assert_called_once() assert ("You attempted to merge VCFs for a project that cannot do that" in res.output)
def test_create_project_batches__duplicate_client_ids(credentials, mocker): """Test batch creation failure when there are samples that share same client id. """ runner = CliRunner() mocked_create_project_batch = mocker.patch.object( APIClient, "create_project_batch", side_effect=APIClientError(message="", status_code=400), ) res = runner.invoke( create_project_batch, [ str(uuid4()), *credentials, "--batch-type", "hd777k", "--batch-name", "foo bar", ], ) assert res.exit_code == 0 mocked_create_project_batch.assert_called_once() assert "There was an error creating project batches" in res.output
def test_s3_import__not_owned_project(mocker): """Test S3 import failure when project is not owned.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_import_s3_projects = mocker.patch.object( APIClient, "s3_uri_import", side_effect=APIClientError(message="", status_code=403), ) res = runner.invoke( s3_import, [ "s3://bucket/path/", str(uuid4()), "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_import_s3_projects.assert_called_once() assert "You do not have the sufficient permission" in res.output
def _request( endpoint, params, *args, # pylint: disable=unused-argument **kwargs, # pylint: disable=unused-argument ): if endpoint == ApiEndpoints.GET_JWT.value: if "otp_token" not in params: raise APIClientError( {"otp_token": ["Please enter your OTP token."]}, 401) return {"access": "access", "refresh": "refresh"} return {}
def _request( endpoint, *args, # pylint: disable=unused-argument **kwargs, # pylint: disable=unused-argument ): if ApiEndpoints.UPLOAD_DETAILS.value == endpoint: nonlocal force_refresh_jwt if force_refresh_jwt: force_refresh_jwt = False raise APIClientError("Test error.", 401) return { "id": str(uuid4()), "last_status": {"id": str(uuid4()), "status": ""}, "s3": {"bucket": "test", "object_name": "test"}, } if ApiEndpoints.REFRESH_JWT.value == endpoint: return {"access": ""} return {}
def test_list_does_not_exist(mocker, credentials): """Test user cannot get to uploads.""" runner = CliRunner() mocked_get_projects = mocker.patch.object( APIClient, "get_sample_sheet", side_effect=APIClientError( message="API Client Error: Not Found: Not found.", status_code=404), return_value={"detail": "Not found"}, ) res = runner.invoke(list_uploads, credentials) assert isinstance(res.exception, SystemExit) assert res.exit_code == 1 mocked_get_projects.assert_called_once() assert ("\n".join([ "ERROR: Uploads do not exist.", "ERROR: API Client Error: Not Found: Not found.", "Aborted!\n", ]) == res.output)
def test_delete_project_samples__not_owned_project(credentials, mocker): """Test delete project samples failure when project is not owned.""" runner = CliRunner() mocked_delete_project_samples = mocker.patch.object( APIClient, "delete_project_samples", side_effect=APIClientError(message="", status_code=403), ) res = runner.invoke( delete_project_samples, [ str(uuid4()), *credentials, "--sample-ids", "11111111-1111-1111-1111-111111111111,22222222-2222-2222-2222-222222222222", # noqa ], ) assert res.exit_code == 1 mocked_delete_project_samples.assert_called_once() assert "You do not have the sufficient permission" in res.output
def test_s3_autoimport_create_no_permission(credentials, mocker, project_id): """Test that user cannot create an automated import if no permissions.""" runner = CliRunner() mocked_autoimport_from_s3 = mocker.patch.object( APIClient, "autoimport_from_s3", side_effect=APIClientError(message="", status_code=403), ) res = runner.invoke( create, [ project_id, "identifier", *credentials, ], ) assert res.exit_code == 1 mocked_autoimport_from_s3.assert_called_once() assert "There was an error creating an import job" in res.output
def test_create_project_batches__not_owned_project(credentials, mocker): """Test batch creation failure when project is not owned.""" runner = CliRunner() mocked_create_project_batch = mocker.patch.object( APIClient, "create_project_batch", side_effect=APIClientError(message="", status_code=403), ) res = runner.invoke( create_project_batch, [ str(uuid4()), *credentials, "--batch-type", "hd777k", "--batch-name", "foo bar", ], ) assert res.exit_code == 1 mocked_create_project_batch.assert_called_once() assert "You do not have the sufficient permission" in res.output
def test_delete_project_samples__sample_not_in_project(credentials, mocker, project_id, recording, vcr): """Test delete project samples with sample not in project.""" runner = CliRunner() if not recording: # Mock delete_project_samples only if using the cassettes, since we # mock the return value. delete_project_samples_response = get_vcr_response( "/api/v2/project-delete-samples/", vcr, operator.contains, just_body=False, ) mocked_delete_project_samples = mocker.patch.object( APIClient, "delete_project_samples", side_effect=APIClientError( message=delete_project_samples_response["body"]["string"], status_code=delete_project_samples_response["status"]["code"], ), ) res = runner.invoke( delete_project_samples, [ project_id, *credentials, "--sample-ids", "11111111-1111-1111-1111-111111111111", ], ) assert res.exit_code == 0 if not recording: mocked_delete_project_samples.assert_called_once() assert "All sample ids must be part of the current project." in res.output