def test_group_exceptions(set_exp_dataset_config): """Test ability to handle group-related exceptions. """ from mydata.conf import settings from mydata.models.group import Group # Test retrieving a valid group record (using the Group model's # get_group_by_name method) and ensure that no exception is raised: with requests_mock.Mocker() as mocker: mock_get_group(mocker, settings.general.mytardis_url, "TestFacility-Group1") group = Group.get_group_by_name("TestFacility-Group1") assert group.name == "TestFacility-Group1" assert group.get_value_for_key("name") == group.name # Try to look up group record with an invalid API key, # which should give 401 (Unauthorized). api_key = settings.general.api_key settings.general.api_key = "invalid" with requests_mock.Mocker() as mocker: get_group_url = ( "%s/api/v1/group/?format=json&name=TestFacility-Group1" ) % settings.general.mytardis_url mocker.get(get_group_url, status_code=401) with pytest.raises(HTTPError) as excinfo: _ = Group.get_group_by_name("TestFacility-Group1") assert excinfo.value.response.status_code == 401 settings.general.api_key = api_key with requests_mock.Mocker() as mocker: get_group_url = ("%s/api/v1/group/?format=json&name=INVALID_GROUP" ) % settings.general.mytardis_url mocker.get(get_group_url, text=EMPTY_LIST_RESPONSE) group = Group.get_group_by_name("INVALID_GROUP") assert not group
def test_scan_group_dataset_folders(set_group_dataset_config): """Test ability to scan the Group / Dataset folder structure. """ from mydata.conf import settings from mydata.tasks.folders import scan_folders groups = [] folders = [] # We don't need callback for finding users folders: found_exp = None found_user = None def found_group(group): groups.append(group) def found_dataset(folder): folders.append(folder) with requests_mock.Mocker() as mocker: mock_testfacility_user_response(mocker, settings.general.mytardis_url) for group_name in ("TestFacility-Group1", "TestFacility-Group2"): mock_get_group(mocker, settings.general.mytardis_url, group_name) mock_test_facility_response(mocker, settings.general.mytardis_url) mock_test_instrument_response(mocker, settings.general.mytardis_url) scan_folders(found_user, found_group, found_exp, found_dataset) assert sorted([group.name for group in groups]) == [ "TestFacility-Group1", "TestFacility-Group2", ] assert sorted([folder.name for folder in folders]) == ["Birds", "Flowers"] assert sum([folder.num_files for folder in folders]) == 5
def test_scan_group_dataset(set_group_dataset_config): from mydata.commands.scan import scan_cmd from mydata.conf import settings with requests_mock.Mocker() as mocker: mock_testfacility_user_response(mocker, settings.general.mytardis_url) for group_name in ("TestFacility-Group1", "TestFacility-Group2"): mock_get_group(mocker, settings.general.mytardis_url, group_name) mock_test_facility_response(mocker, settings.general.mytardis_url) mock_test_instrument_response(mocker, settings.general.mytardis_url) runner = CliRunner() result = runner.invoke(scan_cmd, []) assert result.exit_code == 0 assert result.output == "%s\n" % textwrap.dedent(""" Scanning tests/testdata/testdata-group-dataset/ using the "User Group / Dataset" folder structure... Found group folder: Group1 Found group folder: Group2 Found 2 dataset folders in tests/testdata/testdata-group-dataset/ """)
def test_objectacl_exceptions(set_exp_dataset_config): """Test ability to handle ObjectACL-related exceptions. """ # In a normal MyData run, the settings singleton would only be initialized # once, but when running a series of unit tests, we need to ensure that # settings is initialized for each test from the MYDATA_CONFIG_PATH # environment variable. from mydata.conf import settings from mydata.models.objectacl import ObjectACL from mydata.models.experiment import Experiment from mydata.models.folder import Folder from mydata.models.group import Group with requests_mock.Mocker() as mocker: mock_testfacility_user_response(mocker, settings.general.mytardis_url) owner = settings.general.default_owner dataset_folder_name = "Flowers" exp_folder_name = "Exp1" location = os.path.join(settings.general.data_directory, exp_folder_name) # Test sharing experiment with user, and ensure that no exception # is raised: user_folder_name = owner.username group_folder_name = None folder = Folder(dataset_folder_name, location, user_folder_name, group_folder_name, owner) folder.experiment_title = "Existing Experiment" with requests_mock.Mocker() as mocker: get_exp_url = ( "%s/api/v1/mydata_experiment/?format=json&title=Existing%%20Experiment" "&folder_structure=Experiment%%20/%%20Dataset&user_folder_name=testfacility" ) % settings.general.mytardis_url mocker.get(get_exp_url, text=EXISTING_EXP_RESPONSE) experiment = Experiment.get_exp_for_folder(folder) assert experiment.title == "Existing Experiment" with requests_mock.Mocker() as mocker: post_acl_url = "%s/api/v1/objectacl/" % settings.general.mytardis_url mocker.post(post_acl_url, status_code=201) ObjectACL.share_exp_with_user(experiment, owner) # Test sharing experiment with group, and ensure that no exception # is raised: with requests_mock.Mocker() as mocker: mock_get_group(mocker, settings.general.mytardis_url, "TestFacility-Group1") group = Group.get_group_by_name("TestFacility-Group1") post_acl_url = "%s/api/v1/objectacl/" % settings.general.mytardis_url mocker.post(post_acl_url, status_code=201) ObjectACL.share_exp_with_group(experiment, group, is_owner=True) # Try to create a user ObjectACL record with # an invalid API key, which should give 401 (Unauthorized) api_key = settings.general.api_key settings.general.api_key = "invalid" with requests_mock.Mocker() as mocker: post_acl_url = "%s/api/v1/objectacl/" % settings.general.mytardis_url mocker.post(post_acl_url, status_code=401) with pytest.raises(HTTPError) as excinfo: ObjectACL.share_exp_with_user(experiment, owner) assert excinfo.value.response.status_code == 401 settings.general.api_key = api_key # Try to create a group ObjectACL record with # an invalid API key, which should give 401 (Unauthorized) api_key = settings.general.api_key settings.general.api_key = "invalid" with requests_mock.Mocker() as mocker: post_acl_url = "%s/api/v1/objectacl/" % settings.general.mytardis_url mocker.post(post_acl_url, status_code=401) with pytest.raises(HTTPError) as excinfo: ObjectACL.share_exp_with_group(experiment, group, is_owner=True) assert excinfo.value.response.status_code == 401 settings.general.api_key = api_key