Example #1
0
    def write_theme_same_image_test(self):
        """
        Test that we don't try to overwrite a theme background image with itself
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode(
            )

            # WHEN: Calling _write_theme with path to the same image, but the path written slightly different
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            # Do replacement from end of string to avoid problems with path start
            file_name2 = file_name1[::-1].replace(os.sep, os.sep + os.sep,
                                                  2)[::-1]
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertFalse(mocked_copyfile.called,
                             'shutil.copyfile should not be called')
Example #2
0
    def test_write_theme_same_image(self):
        """
        Test that we don't try to overwrite a theme background image with itself
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode()

            # WHEN: Calling _write_theme with path to the same image, but the path written slightly different
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            # Do replacement from end of string to avoid problems with path start
            file_name2 = file_name1[::-1].replace(os.sep, os.sep + os.sep, 2)[::-1]
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertFalse(mocked_copyfile.called, 'shutil.copyfile should not be called')
Example #3
0
    def unzip_theme_test(self):
        """
        Test that unzipping of themes works
        """
        # GIVEN: A theme file, a output folder and some mocked out internal functions
        with patch('openlp.core.ui.thememanager.critical_error_message_box') \
                as mocked_critical_error_message_box:
            theme_manager = ThemeManager(None)
            theme_manager._create_theme_from_xml = MagicMock()
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            folder = mkdtemp()
            theme_file = os.path.join(TEST_RESOURCES_PATH, 'themes',
                                      'Moss_on_tree.otz')

            # WHEN: We try to unzip it
            theme_manager.unzip_theme(theme_file, folder)

            # THEN: Files should be unpacked
            self.assertTrue(
                os.path.exists(
                    os.path.join(folder, 'Moss on tree', 'Moss on tree.xml')))
            self.assertEqual(mocked_critical_error_message_box.call_count, 0,
                             'No errors should have happened')
            shutil.rmtree(folder)
Example #4
0
    def write_theme_diff_images_test(self):
        """
        Test that we do overwrite a theme background image when a new is submitted
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode(
            )

            # WHEN: Calling _write_theme with path to different images
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            file_name2 = os.path.join(TEST_RESOURCES_PATH, 'church2.jpg')
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertTrue(mocked_copyfile.called,
                            'shutil.copyfile should be called')
Example #5
0
    def test_write_theme_diff_images(self):
        """
        Test that we do overwrite a theme background image when a new is submitted
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, shutil.copyfile,
        #        theme, check_directory_exists and thememanager-attributes.
        with patch('builtins.open') as mocked_open, \
                patch('openlp.core.ui.thememanager.shutil.copyfile') as mocked_copyfile, \
                patch('openlp.core.ui.thememanager.check_directory_exists') as mocked_check_directory_exists:
            mocked_open.return_value = MagicMock()
            theme_manager = ThemeManager(None)
            theme_manager.old_background_image = None
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            mocked_theme = MagicMock()
            mocked_theme.theme_name = 'themename'
            mocked_theme.extract_formatted_xml = MagicMock()
            mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode()

            # WHEN: Calling _write_theme with path to different images
            file_name1 = os.path.join(TEST_RESOURCES_PATH, 'church.jpg')
            file_name2 = os.path.join(TEST_RESOURCES_PATH, 'church2.jpg')
            theme_manager._write_theme(mocked_theme, file_name1, file_name2)

            # THEN: The mocked_copyfile should not have been called
            self.assertTrue(mocked_copyfile.called, 'shutil.copyfile should be called')
Example #6
0
    def test_write_theme_special_char_name(self):
        """
        Test that we can save themes with special characters in the name
        """
        # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.theme_path = Path(self.temp_folder)
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'theme 愛 name'
        mocked_theme.export_theme.return_value = "{}"

        # WHEN: Calling _write_theme with a theme with a name with special characters in it
        theme_manager._write_theme(mocked_theme, None, None)

        # THEN: It should have been created
        assert os.path.exists(os.path.join(self.temp_folder, 'theme 愛 name', 'theme 愛 name.json')) is True, \
            'Theme with special characters should have been created!'
Example #7
0
    def test_write_theme_special_char_name(self):
        """
        Test that we can save themes with special characters in the name
        """
        # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.path = self.temp_folder
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'theme 愛 name'
        mocked_theme.extract_formatted_xml = MagicMock()
        mocked_theme.extract_formatted_xml.return_value = 'fake theme 愛 XML'.encode()

        # WHEN: Calling _write_theme with a theme with a name with special characters in it
        theme_manager._write_theme(mocked_theme, None, None)

        # THEN: It should have been created
        self.assertTrue(os.path.exists(os.path.join(self.temp_folder, 'theme 愛 name', 'theme 愛 name.xml')),
                        'Theme with special characters should have been created!')
Example #8
0
    def test_unzip_theme(self):
        """
        Test that unzipping of themes works
        """
        # GIVEN: A theme file, a output folder and some mocked out internal functions
        with patch('openlp.core.ui.thememanager.critical_error_message_box') \
                as mocked_critical_error_message_box:
            theme_manager = ThemeManager(None)
            theme_manager._create_theme_from_xml = MagicMock()
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.path = ''
            folder = mkdtemp()
            theme_file = os.path.join(TEST_RESOURCES_PATH, 'themes', 'Moss_on_tree.otz')

            # WHEN: We try to unzip it
            theme_manager.unzip_theme(theme_file, folder)

            # THEN: Files should be unpacked
            self.assertTrue(os.path.exists(os.path.join(folder, 'Moss on tree', 'Moss on tree.xml')))
            self.assertEqual(mocked_critical_error_message_box.call_count, 0, 'No errors should have happened')
            shutil.rmtree(folder)
Example #9
0
    def test_unzip_theme(self):
        """
        Test that unzipping of themes works
        """
        # GIVEN: A theme file, a output folder and some mocked out internal functions
        with patch('openlp.core.ui.thememanager.critical_error_message_box') \
                as mocked_critical_error_message_box:
            theme_manager = ThemeManager(None)
            theme_manager._create_theme_from_xml = MagicMock()
            theme_manager.generate_and_save_image = MagicMock()
            theme_manager.theme_path = None
            folder_path = Path(mkdtemp())
            theme_file_path = RESOURCE_PATH / 'themes' / 'Moss_on_tree.otz'

            # WHEN: We try to unzip it
            theme_manager.unzip_theme(theme_file_path, folder_path)

            # THEN: Files should be unpacked
            assert (folder_path / 'Moss on tree' /
                    'Moss on tree.xml').exists() is True
            assert mocked_critical_error_message_box.call_count == 0, 'No errors should have happened'
            folder_path.rmtree()
Example #10
0
    def test_write_theme_diff_images(self, mocked_create_paths,
                                     mocked_copyfile):
        """
        Test that we do overwrite a theme background image when a new is submitted
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, copyfile,
        #        theme, create_paths and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.theme_path = MagicMock()
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'themename'
        mocked_theme.filename = "filename"

        # WHEN: Calling _write_theme with path to different images
        file_path_1 = RESOURCE_PATH / 'church.jpg'
        file_path_2 = RESOURCE_PATH / 'church2.jpg'
        theme_manager._write_theme(mocked_theme, file_path_1, file_path_2)

        # THEN: The mocked_copyfile should not have been called
        assert mocked_copyfile.called is True, 'copyfile should be called'
Example #11
0
    def test_write_theme_same_image(self, mocked_create_paths,
                                    mocked_copyfile):
        """
        Test that we don't try to overwrite a theme background image with itself
        """
        # GIVEN: A new theme manager instance, with mocked builtins.open, copyfile,
        #        theme, create_paths and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.theme_path = MagicMock()
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'themename'
        mocked_theme.extract_formatted_xml = MagicMock()
        mocked_theme.extract_formatted_xml.return_value = 'fake_theme_xml'.encode(
        )

        # WHEN: Calling _write_theme with path to the same image, but the path written slightly different
        file_path_1 = RESOURCE_PATH / 'church.jpg'
        theme_manager._write_theme(mocked_theme, file_path_1, file_path_1)

        # THEN: The mocked_copyfile should not have been called
        assert mocked_copyfile.called is False, 'copyfile should not be called'
Example #12
0
    def write_theme_special_char_name_test(self):
        """
        Test that we can save themes with special characters in the name
        """
        # GIVEN: A new theme manager instance, with mocked theme and thememanager-attributes.
        theme_manager = ThemeManager(None)
        theme_manager.old_background_image = None
        theme_manager.generate_and_save_image = MagicMock()
        theme_manager.path = self.temp_folder
        mocked_theme = MagicMock()
        mocked_theme.theme_name = 'theme 愛 name'
        mocked_theme.extract_formatted_xml = MagicMock()
        mocked_theme.extract_formatted_xml.return_value = 'fake theme 愛 XML'.encode(
        )

        # WHEN: Calling _write_theme with a theme with a name with special characters in it
        theme_manager._write_theme(mocked_theme, None, None)

        # THEN: It should have been created
        self.assertTrue(
            os.path.exists(
                os.path.join(self.temp_folder, 'theme 愛 name',
                             'theme 愛 name.xml')),
            'Theme with special characters should have been created!')