def test_loading_non_existent_file(self):
        """
        Tests loading a non-existent JSON file is handled properly.
        """

        sut = JsonFile(path=INVALID_TEST_FILE_PATH)
        sut.load()
        self.assertEqual(sut.Content, {})
    def test_loading_malformed_json_file(self):

        from suisei.murasame.exceptions import InvalidInputError

        # Create a malformed file
        malformed_json = '{invalid json: [}'
        with open(MALFORMED_FILE_PATH, 'w+') as malformed:
            malformed.write(malformed_json)

        # STEP 1 - Trying to load a malformed JSON file results in
        # an exception
        sut = JsonFile(path=MALFORMED_FILE_PATH)
        with self.assertRaises(InvalidInputError):
            sut.load()

        # Create a malformed file and encrypt it
        from suisei.murasame.util import AESCipher
        cipher = AESCipher(get_password())
        content = cipher.encrypt(malformed_json)
        with open(MALFORMED_FILE_PATH, 'wb') as malformed:
            malformed.write(content)

        # STEP #2 - Trying to load an encrypted and malformed JSON file results
        # in an exception

        sut = JsonFile(path=MALFORMED_FILE_PATH, cb_retrieve_key=get_password)
        with self.assertRaises(InvalidInputError):
            sut.load()
    def test_saving_and_loading_encrypted_json_file(self):
        """
        Tests that JSON content can be saved to and loaded from an encrypted
        file on disk.
        """

        sut1 = JsonFile(path=TEST_FILE_PATH, cb_retrieve_key=get_password)
        sut1.Content['test'] = 'test content'
        sut1.save()

        sut2 = JsonFile(path=TEST_FILE_PATH, cb_retrieve_key=get_password)
        sut2.load()
        self.assertEqual(sut2.Content['test'], 'test content')
    def test_saving_and_loading_json_file(self):
        """
        Tests that JSON content can be saved to and loaded from a file on disk.
        """

        sut1 = JsonFile(path=TEST_FILE_PATH)
        sut1.Content['test'] = 'test content'
        sut1.save()
        del sut1

        sut2 = JsonFile(path=TEST_FILE_PATH)
        sut2.load()
        self.assertEqual(sut2.Content['test'], 'test content')
    def test_creation(self):
        """
        Tests that a JsonFile object can be created.
        """

        # STEP 1 - Create file
        sut = JsonFile(path=TEST_FILE_PATH)
        self.assertNotEqual(sut.Path, None)
        self.assertEqual(sut.Content, {})
    def _load_configuration(self) -> None:

        """
        Loads the configuration file from the given path and parses it into
        this object.
        """

        config_path = self._get_config_path()

        from suisei.murasame.util import JsonFile
        configfile = JsonFile(config_path)

        from suisei.murasame.exceptions import InvalidInputError
        try:
            configfile.load()
        except InvalidInputError:
            raise SystemExit('Failed to load the configuration file.')

        self._parse_configuration(configfile.Content)
    def test_savig_file_to_invalid_location(self):
        """
        Tests that the file is not saved to an invalid location.
        """

        # STEP #1 - Unencrypted JSON file
        sut = JsonFile(path='/invalid/path/to/somewhere/file.json')
        with self.assertRaises(RuntimeError):
            sut.save()

        # STEP #2 - Encrypted JSON file
        sut = JsonFile(path='/invalid/path/to/somewhere/file.json',
                       cb_retrieve_key=get_password)
        with self.assertRaises(RuntimeError):
            sut.save()
    def _load_configuration_file(self, config_directory: str) -> dict:
        """
        Loads a configuration file from disk and returns its content. If the
        file doesn't exist, it will create it using the default configuration.

        Args:
            config_directory:       The config directory of the application.
        """

        filename = os.path.abspath('{}/logging.conf'.format(config_directory))
        log_file = JsonFile(filename)

        try:
            log_file.load()
        except InvalidInputError:
            # Failed to load the configuration file, just use the default
            # configuration to create it.
            log_file.owerwrite_content(DEFAULT_LOGGING_CONFIGURATION)
            log_file.save()

        if not log_file.Content:
            log_file.owerwrite_content(DEFAULT_LOGGING_CONFIGURATION)
            log_file.save()

        return log_file.Content