예제 #1
0
def test_export_profile_with_existing_file(profile):
    export_path = "exported"
    exported_file = f"{export_path}.zip"
    tentacles_config = os.path.join(get_profile_path(),
                                    "tentacles_config.json")
    spec_tentacles_config = os.path.join(get_profile_path(), "specific_config")
    other_profile = os.path.join(test_config.TEST_FOLDER, "other_profile")
    with _cleaned_tentacles(export_path,
                            exported_file,
                            tentacles_config,
                            dir1=spec_tentacles_config,
                            dir2=other_profile):
        # create fake tentacles config
        shutil.copy(profile.config_file(), tentacles_config)
        os.mkdir(spec_tentacles_config)
        shutil.copy(profile.config_file(),
                    os.path.join(spec_tentacles_config, "t1.json"))
        shutil.copy(profile.config_file(),
                    os.path.join(spec_tentacles_config, "t2.json"))
        shutil.copy(profile.config_file(),
                    f"{export_path}.{constants.PROFILE_EXPORT_FORMAT}")
        with mock.patch.object(os, "remove", mock.Mock()) as remove_mock:
            profiles.export_profile(profile, export_path)
            remove_mock.assert_called_once_with(
                f"{export_path}.{constants.PROFILE_EXPORT_FORMAT}")
        assert os.path.isfile(exported_file)
        with zipfile.ZipFile(exported_file) as zipped:
            zipped.extractall(other_profile)
        # ensure all files got zipped
        for root, dirs, files in os.walk(profile.path):
            dir_path = os.path.join(
                other_profile, "specific_config"
            ) if "specific_config" in root else other_profile
            assert all(
                os.path.isfile(os.path.join(dir_path, f)) for f in files)
예제 #2
0
def test_import_profile(profile):
    export_path = os.path.join(test_config.TEST_FOLDER, "super_profile")
    exported_file = f"{export_path}.zip"
    spec_tentacles_config = os.path.join(get_profile_path(), "specific_config")
    tentacles_config = os.path.join(get_profile_path(),
                                    "tentacles_config.json")
    other_profile = os.path.join(constants.USER_PROFILES_FOLDER,
                                 "imported_super_profile")
    with _cleaned_tentacles(export_path,
                            exported_file,
                            tentacles_config,
                            dir1=other_profile,
                            dir2=constants.USER_FOLDER,
                            dir3=spec_tentacles_config):
        # create fake tentacles config
        shutil.copy(profile.config_file(), tentacles_config)
        os.mkdir(spec_tentacles_config)
        shutil.copy(profile.config_file(),
                    os.path.join(spec_tentacles_config, "t1.json"))
        shutil.copy(profile.config_file(),
                    os.path.join(spec_tentacles_config, "t2.json"))
        profiles.export_profile(profile, export_path)
        imported_profile_path = os.path.join(constants.USER_PROFILES_FOLDER,
                                             "imported_super_profile")
        with mock.patch.object(profile_sharing, "_ensure_unique_profile_id", mock.Mock()) \
                as _ensure_unique_profile_id_mock:
            profiles.import_profile(exported_file)
            _ensure_unique_profile_id_mock.assert_called_once()
        assert os.path.isdir(imported_profile_path)
        # ensure all files got imported
        for root, dirs, files in os.walk(profile.path):
            dir_path = os.path.join(
                other_profile, "specific_config"
            ) if "specific_config" in root else other_profile
            assert all(
                os.path.isfile(os.path.join(dir_path, f)) for f in files)
        profiles.import_profile(exported_file)
        assert os.path.isdir(f"{imported_profile_path}_2")
        with mock.patch.object(shutil, "rmtree",
                               mock.Mock()) as shutil_rmtree_mock:
            profiles.import_profile(exported_file, replace_if_exists=True)
            shutil_rmtree_mock.assert_called_once()
        assert os.path.isdir(imported_profile_path)
        assert not os.path.isdir(f"{imported_profile_path}_3")
예제 #3
0
def test_get_unique_profile_folder(profile):
    assert _get_unique_profile_folder(
        profile.config_file()) == f"{profile.config_file()}_2"
    other_file = f"{profile.config_file()}_2"
    other_file_2 = f"{profile.config_file()}_3"
    other_file_3 = f"{profile.config_file()}_5"
    with _cleaned_tentacles(other_file, other_file_2, other_file_3):
        shutil.copy(profile.config_file(), other_file)
        assert _get_unique_profile_folder(
            profile.config_file()) == f"{profile.config_file()}_3"
        shutil.copy(profile.config_file(), other_file_2)
        assert _get_unique_profile_folder(
            profile.config_file()) == f"{profile.config_file()}_4"
        shutil.copy(profile.config_file(), other_file_3)
        assert _get_unique_profile_folder(
            profile.config_file()) == f"{profile.config_file()}_4"
예제 #4
0
def test_config_file(profile):
    assert profile.config_file() == os.path.join(get_profile_path(),
                                                 constants.PROFILE_CONFIG_FILE)