test_importing_themes.py 6.0 KB

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