test_importing_themes.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import os
  2. import pytest
  3. from django.urls import reverse
  4. from ....test import assert_contains
  5. from ...models import Theme
  6. import_link = reverse("misago:admin:themes:import")
  7. class MockThemeExport:
  8. def __init__(self, response):
  9. self.name = "theme-export.zip"
  10. self.content_type = response["content-type"]
  11. self.size = response["content-length"]
  12. self._response = response
  13. def read(self):
  14. return self._response.getvalue()
  15. @pytest.fixture
  16. def reimport_theme(admin_client):
  17. def export_import_theme(theme, extra_data=None):
  18. export_link = reverse("misago:admin:themes:export", kwargs={"pk": theme.pk})
  19. theme_export = MockThemeExport(admin_client.post(export_link))
  20. data = extra_data or {}
  21. data["upload"] = theme_export
  22. admin_client.post(import_link, data)
  23. return Theme.objects.filter(pk__gt=theme.pk).last()
  24. return export_import_theme
  25. def assert_filenames_are_same(src, dst):
  26. source_filename = os.path.split(src.path)[-1]
  27. imported_filename = os.path.split(dst.path)[-1]
  28. assert source_filename == imported_filename
  29. def test_import_theme_form_is_displayed(admin_client):
  30. response = admin_client.get(import_link)
  31. assert_contains(response, "Import theme")
  32. def test_theme_import_fails_if_export_was_not_uploaded(admin_client):
  33. admin_client.post(import_link, {"upload": None})
  34. assert Theme.objects.count() == 1
  35. def test_empty_theme_export_can_be_imported_back(reimport_theme, theme):
  36. assert reimport_theme(theme)
  37. def test_theme_can_be_imported_with_custom_name(reimport_theme, theme):
  38. imported_theme = reimport_theme(theme, {"name": "Imported theme"})
  39. assert imported_theme.name == "Imported theme"
  40. def test_theme_can_be_imported_without_parent(reimport_theme, theme):
  41. imported_theme = reimport_theme(theme)
  42. assert imported_theme.parent is None
  43. def test_theme_can_be_imported_with_custom_parent(reimport_theme, theme):
  44. imported_theme = reimport_theme(theme, {"parent": theme.pk})
  45. assert imported_theme.parent == theme
  46. def test_theme_can_be_imported_with_default_theme_asparent(
  47. reimport_theme, theme, default_theme
  48. ):
  49. imported_theme = reimport_theme(theme, {"parent": default_theme.pk})
  50. assert imported_theme.parent == default_theme
  51. def test_theme_import_fails_if_parent_is_nonexisisting(
  52. reimport_theme, theme, nonexisting_theme
  53. ):
  54. assert not reimport_theme(theme, {"parent": nonexisting_theme.pk})
  55. def test_importing_theme_under_parent_rebuilds_themes_tree(reimport_theme, theme):
  56. imported_theme = reimport_theme(theme, {"parent": theme.pk})
  57. theme.refresh_from_db()
  58. assert imported_theme.tree_id == theme.tree_id
  59. assert theme.lft == 1
  60. assert theme.rght == 4
  61. assert imported_theme.lft == 2
  62. assert imported_theme.rght == 3
  63. def test_theme_details_are_exported_and_imported_back(reimport_theme, theme):
  64. theme.name = "Exported Theme"
  65. theme.version = "0.1.2 FINAL"
  66. theme.author = "John Doe"
  67. theme.url = "https://example.com"
  68. theme.save()
  69. imported_theme = reimport_theme(theme)
  70. assert imported_theme.name == theme.name
  71. assert imported_theme.version == theme.version
  72. assert imported_theme.author == theme.author
  73. assert imported_theme.url == theme.url
  74. def test_imported_theme_has_own_dirname(reimport_theme, theme):
  75. imported_theme = reimport_theme(theme)
  76. assert theme.dirname != imported_theme.dirname
  77. def test_css_file_is_exported_and_imported_back(reimport_theme, theme, css):
  78. imported_theme = reimport_theme(theme)
  79. imported_css = imported_theme.css.last()
  80. assert imported_css.name == css.name
  81. assert imported_css.source_hash == css.source_hash
  82. assert imported_css.size == css.size
  83. assert_filenames_are_same(css.source_file, imported_css.source_file)
  84. def test_css_link_is_exported_and_imported_back(reimport_theme, theme, css_link):
  85. imported_theme = reimport_theme(theme)
  86. imported_css = imported_theme.css.last()
  87. assert imported_css.name == css_link.name
  88. assert imported_css.url == css_link.url
  89. def test_importing_css_link_triggers_getting_remote_css_size_task(
  90. reimport_theme, theme, css_link, mock_update_remote_css_size
  91. ):
  92. reimport_theme(theme)
  93. mock_update_remote_css_size.assert_called_once()
  94. def test_theme_export_containing_css_file_and_link_can_be_imported_back(
  95. reimport_theme, theme, css, css_link
  96. ):
  97. imported_theme = reimport_theme(theme)
  98. css_names = list(imported_theme.css.values_list("name", flat=True))
  99. assert css_names == [css.name, css_link.name]
  100. def test_theme_css_order_is_preserved_after_import(
  101. reimport_theme, theme, css, css_link
  102. ):
  103. css_link.order = 1
  104. css_link.save()
  105. css.order = 2
  106. css.save()
  107. imported_theme = reimport_theme(theme)
  108. css_names = list(imported_theme.css.values_list("name", flat=True))
  109. assert css_names == [css_link.name, css.name]
  110. def test_theme_export_containing_media_file_can_be_imported_back(
  111. reimport_theme, theme, media
  112. ):
  113. imported_theme = reimport_theme(theme)
  114. imported_media = imported_theme.media.last()
  115. assert imported_media.name == media.name
  116. assert imported_media.hash == media.hash
  117. assert imported_media.type == media.type
  118. assert imported_media.size == media.size
  119. assert_filenames_are_same(media.file, imported_media.file)
  120. def test_theme_export_containing_image_file_can_be_imported_back(
  121. reimport_theme, theme, image
  122. ):
  123. imported_theme = reimport_theme(theme)
  124. imported_image = imported_theme.media.last()
  125. assert imported_image.name == image.name
  126. assert imported_image.hash == image.hash
  127. assert imported_image.type == image.type
  128. assert imported_image.width == image.width
  129. assert imported_image.height == image.height
  130. assert imported_image.size == image.size
  131. assert_filenames_are_same(image.file, imported_image.file)
  132. def test_theme_export_containing_different_files_can_be_imported_back(
  133. reimport_theme, theme, css, css_link, media, image
  134. ):
  135. imported_theme = reimport_theme(theme)
  136. css_names = list(imported_theme.css.values_list("name", flat=True))
  137. assert css_names == [css.name, css_link.name]
  138. media_names = list(imported_theme.media.values_list("name", flat=True))
  139. assert media_names == [image.name, media.name]
  140. def test_importing_theme_triggers_css_build(
  141. reimport_theme, theme, css_link, mock_build_theme_css
  142. ):
  143. imported_theme = reimport_theme(theme)
  144. mock_build_theme_css.assert_called_once_with(imported_theme.pk)