Example #1
0
    def test_profiles(self):
        """Test the properties related to retrieving, creating, updating and removing profiles."""
        config = Config(self.config_filepath, self.config_dictionary)

        # Each item returned by config.profiles should be a Profile instance
        for profile in config.profiles:
            self.assertIsInstance(profile, Profile)
            self.assertTrue(
                profile.dictionary,
                self.config_dictionary[Config.KEY_PROFILES][profile.name])

        # The profile_names property should return the keys of the profiles dictionary
        self.assertEqual(config.profile_names,
                         list(self.config_dictionary[Config.KEY_PROFILES]))

        # Test get_profile
        self.assertEqual(
            config.get_profile(self.profile_name).dictionary,
            self.profile.dictionary)

        # Update a profile
        updated_profile = create_mock_profile(self.profile_name)
        config.update_profile(updated_profile)
        self.assertEqual(
            config.get_profile(self.profile_name).dictionary,
            updated_profile.dictionary)

        # Removing an unexisting profile should raise
        with self.assertRaises(exceptions.ProfileConfigurationError):
            config.remove_profile('non_existing_profile')

        # Removing an existing should work and in this test case none should remain
        config.remove_profile(self.profile_name)
        self.assertEqual(config.profiles, [])
        self.assertEqual(config.profile_names, [])
Example #2
0
    def test_from_file(self):
        """Test the `Config.from_file` class method.

        Regression test for #3790: make sure configuration is written to disk after it is loaded and migrated.
        """

        # If the config file does not exist, a completely new file is created with a migrated config
        filepath_nonexisting = os.path.join(self.config_filebase,
                                            'config_nonexisting.json')
        config = Config.from_file(filepath_nonexisting)

        # Make sure that the migrated config is written to disk, by loading it from disk and comparing to the content
        # of the in memory config object.
        self.compare_config_in_memory_and_on_disk(config, filepath_nonexisting)

        # Now repeat the test for an existing file. The previous filepath now *does* exist and is migrated
        filepath_existing = filepath_nonexisting
        config = Config.from_file(filepath_existing)

        self.compare_config_in_memory_and_on_disk(config, filepath_existing)

        # Finally, we test that an existing configuration file with an outdated schema is migrated and written to disk
        with tempfile.NamedTemporaryFile() as handle:

            # Write content of configuration with old schema to disk
            filepath = os.path.join(os.path.dirname(__file__), 'migrations',
                                    'test_samples', 'input', '0.json')
            with open(filepath, 'rb') as source:
                handle.write(source.read())
                handle.flush()

            config = Config.from_file(handle.name)
            self.compare_config_in_memory_and_on_disk(config, handle.name)
Example #3
0
    def test_store(self):
        """Test that the store method writes the configuration properly to disk."""
        config = Config(self.config_filepath, self.config_dictionary)
        config.store()

        with open(config.filepath, 'r') as handle:
            config_recreated = Config(config.filepath, json.load(handle))

            self.assertEqual(config.dictionary, config_recreated.dictionary)
Example #4
0
    def test_from_file_no_migrate(self):
        """Test that ``Config.from_file`` does not overwrite if the content was not migrated."""
        from time import sleep

        # Construct the ``Config`` instance and write it to disk
        Config(self.config_filepath, self.config_dictionary).store()

        timestamp = os.path.getmtime(self.config_filepath)
        Config.from_file(self.config_filepath)

        # Sleep a second, because for some operating systems the time resolution is of the order of a second
        sleep(1)

        assert os.path.getmtime(self.config_filepath) == timestamp
Example #5
0
    def test_setting_versions(self):
        """Test the version setters."""
        config = Config(self.config_filepath, self.config_dictionary)

        self.assertEqual(config.version, CURRENT_CONFIG_VERSION)
        self.assertEqual(config.version_oldest_compatible, OLDEST_COMPATIBLE_CONFIG_VERSION)

        new_config_version = 1000
        new_compatible_version = 999

        config.version = new_config_version
        config.version_oldest_compatible = new_compatible_version

        self.assertEqual(config.version, new_config_version)
        self.assertEqual(config.version_oldest_compatible, new_compatible_version)
Example #6
0
    def test_construct_empty_dictionary(self):
        """Constructing with empty dictionary should create basic skeleton."""
        config = Config(self.config_filepath, {})

        self.assertTrue(Config.KEY_PROFILES in config.dictionary)
        self.assertTrue(Config.KEY_VERSION in config.dictionary)
        self.assertTrue(Config.KEY_VERSION_CURRENT in config.dictionary[Config.KEY_VERSION])
        self.assertTrue(Config.KEY_VERSION_OLDEST_COMPATIBLE in config.dictionary[Config.KEY_VERSION])
Example #7
0
    def test_basic_properties(self):
        """Test the basic properties of the Config class."""
        config = Config(self.config_filepath, self.config_dictionary)

        self.assertEqual(config.filepath, self.config_filepath)
        self.assertEqual(config.dirpath, self.config_filebase)
        self.assertEqual(config.version, CURRENT_CONFIG_VERSION)
        self.assertEqual(config.version_oldest_compatible, OLDEST_COMPATIBLE_CONFIG_VERSION)
        self.assertEqual(config.dictionary, self.config_dictionary)
Example #8
0
    def test_option_global_only(self):
        """Test that `global_only` options are only set globally even if a profile specific scope is set."""
        option_name = 'user.email'
        option_value = '*****@*****.**'

        config = Config(self.config_filepath, self.config_dictionary)

        # Setting an option globally should be fine
        config.set_option(option_name, option_value, scope=None)
        self.assertEqual(config.get_option(option_name, scope=None, default=False), option_value)

        # Setting an option profile specific should actually not set it on the profile since it is `global_only`
        config.set_option(option_name, option_value, scope=None)
        self.assertEqual(config.get_option(option_name, scope=self.profile_name, default=False), None)
        self.assertEqual(config.get_option(option_name, scope=None, default=False), option_value)
Example #9
0
    def test_set_option_override(self):
        """Test that `global_only` options are only set globally even if a profile specific scope is set."""
        option_name = 'autofill.user.email'
        option_value_one = '*****@*****.**'
        option_value_two = '*****@*****.**'

        config = Config(self.config_filepath, self.config_dictionary)

        # Setting an option if it does not exist should work
        config.set_option(option_name, option_value_one)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False),
            option_value_one)

        # Setting it again will override it by default
        config.set_option(option_name, option_value_two)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False),
            option_value_two)

        # If we set override to False, it should not override, big surprise
        config.set_option(option_name, option_value_one, override=False)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False),
            option_value_two)
Example #10
0
    def test_option(self):
        """Test the setter, unsetter and getter of configuration options."""
        option_value = 131
        option_name = 'daemon.timeout'
        option = get_option(option_name)
        config = Config(self.config_filepath, self.config_dictionary)

        # Getting option that does not exist, should simply return the option default
        self.assertEqual(
            config.get_option(option_name, scope=self.profile_name),
            option.default)
        self.assertEqual(config.get_option(option_name, scope=None),
                         option.default)

        # Unless we set default=False, in which case it should return None
        self.assertEqual(
            config.get_option(option_name,
                              scope=self.profile_name,
                              default=False), None)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False), None)

        # Setting an option profile configuration wide
        config.set_option(option_name, option_value)

        # Getting configuration wide should get new value but None for profile specific
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False),
            option_value)
        self.assertEqual(
            config.get_option(option_name,
                              scope=self.profile_name,
                              default=False), None)

        # Setting an option profile specific
        config.set_option(option_name, option_value, scope=self.profile_name)
        self.assertEqual(
            config.get_option(option_name, scope=self.profile_name),
            option_value)

        # Unsetting profile specific
        config.unset_option(option_name, scope=self.profile_name)
        self.assertEqual(
            config.get_option(option_name,
                              scope=self.profile_name,
                              default=False), None)

        # Unsetting configuration wide
        config.unset_option(option_name, scope=None)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False), None)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=True),
            option.default)

        # Setting a `None` like option
        option_value = 0
        config.set_option(option_name, option_value)
        self.assertEqual(
            config.get_option(option_name, scope=None, default=False),
            option_value)
Example #11
0
    def test_default_profile(self):
        """Test setting and getting default profile."""
        config = Config(self.config_filepath, self.config_dictionary)

        # If not set should return None
        self.assertEqual(config.default_profile_name, None)

        # Setting it to a profile that does not exist should raise
        with self.assertRaises(exceptions.ProfileConfigurationError):
            config.set_default_profile('non_existing_profile')

        # After setting a default profile, it should return the right name
        config.add_profile(create_mock_profile(self.profile_name))
        config.set_default_profile(self.profile_name)
        self.assertTrue(config.default_profile_name, self.profile_name)

        # Setting it when a default is already set, should not overwrite by default
        alternative_profile_name = 'alternative_profile_name'
        config.add_profile(create_mock_profile(alternative_profile_name))
        config.set_default_profile(self.profile_name)
        self.assertTrue(config.default_profile_name, self.profile_name)

        # But with overwrite=True it should
        config.set_default_profile(self.profile_name, overwrite=True)
        self.assertTrue(config.default_profile_name, alternative_profile_name)