test_checks.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. from pathlib import Path
  2. from unittest import mock
  3. from django.conf import DEFAULT_STORAGE_ALIAS, STATICFILES_STORAGE_ALIAS, settings
  4. from django.contrib.staticfiles.checks import E005, check_finders, check_storages
  5. from django.contrib.staticfiles.finders import BaseFinder, get_finder
  6. from django.core.checks import Error, Warning
  7. from django.test import SimpleTestCase, override_settings
  8. from .cases import CollectionTestCase
  9. from .settings import TEST_ROOT
  10. class FindersCheckTests(CollectionTestCase):
  11. run_collectstatic_in_setUp = False
  12. def test_base_finder_check_not_implemented(self):
  13. finder = BaseFinder()
  14. msg = (
  15. "subclasses may provide a check() method to verify the finder is "
  16. "configured correctly."
  17. )
  18. with self.assertRaisesMessage(NotImplementedError, msg):
  19. finder.check()
  20. def test_check_finders(self):
  21. """check_finders() concatenates all errors."""
  22. error1 = Error("1")
  23. error2 = Error("2")
  24. error3 = Error("3")
  25. def get_finders():
  26. class Finder1(BaseFinder):
  27. def check(self, **kwargs):
  28. return [error1]
  29. class Finder2(BaseFinder):
  30. def check(self, **kwargs):
  31. return []
  32. class Finder3(BaseFinder):
  33. def check(self, **kwargs):
  34. return [error2, error3]
  35. class Finder4(BaseFinder):
  36. pass
  37. return [Finder1(), Finder2(), Finder3(), Finder4()]
  38. with mock.patch("django.contrib.staticfiles.checks.get_finders", get_finders):
  39. errors = check_finders(None)
  40. self.assertEqual(errors, [error1, error2, error3])
  41. def test_no_errors_with_test_settings(self):
  42. self.assertEqual(check_finders(None), [])
  43. @override_settings(STATICFILES_DIRS="a string")
  44. def test_dirs_not_tuple_or_list(self):
  45. self.assertEqual(
  46. check_finders(None),
  47. [
  48. Error(
  49. "The STATICFILES_DIRS setting is not a tuple or list.",
  50. hint="Perhaps you forgot a trailing comma?",
  51. id="staticfiles.E001",
  52. )
  53. ],
  54. )
  55. def test_dirs_contains_static_root(self):
  56. with self.settings(STATICFILES_DIRS=[settings.STATIC_ROOT]):
  57. self.assertEqual(
  58. check_finders(None),
  59. [
  60. Error(
  61. "The STATICFILES_DIRS setting should not contain the "
  62. "STATIC_ROOT setting.",
  63. id="staticfiles.E002",
  64. )
  65. ],
  66. )
  67. def test_dirs_contains_static_root_in_tuple(self):
  68. with self.settings(STATICFILES_DIRS=[("prefix", settings.STATIC_ROOT)]):
  69. self.assertEqual(
  70. check_finders(None),
  71. [
  72. Error(
  73. "The STATICFILES_DIRS setting should not contain the "
  74. "STATIC_ROOT setting.",
  75. id="staticfiles.E002",
  76. )
  77. ],
  78. )
  79. def test_prefix_contains_trailing_slash(self):
  80. static_dir = Path(TEST_ROOT) / "project" / "documents"
  81. with self.settings(STATICFILES_DIRS=[("prefix/", static_dir)]):
  82. self.assertEqual(
  83. check_finders(None),
  84. [
  85. Error(
  86. "The prefix 'prefix/' in the STATICFILES_DIRS setting must "
  87. "not end with a slash.",
  88. id="staticfiles.E003",
  89. ),
  90. ],
  91. )
  92. def test_nonexistent_directories(self):
  93. with self.settings(
  94. STATICFILES_DIRS=[
  95. "/fake/path",
  96. ("prefix", "/fake/prefixed/path"),
  97. ]
  98. ):
  99. self.assertEqual(
  100. check_finders(None),
  101. [
  102. Warning(
  103. "The directory '/fake/path' in the STATICFILES_DIRS "
  104. "setting does not exist.",
  105. id="staticfiles.W004",
  106. ),
  107. Warning(
  108. "The directory '/fake/prefixed/path' in the "
  109. "STATICFILES_DIRS setting does not exist.",
  110. id="staticfiles.W004",
  111. ),
  112. ],
  113. )
  114. # Nonexistent directories are skipped.
  115. finder = get_finder("django.contrib.staticfiles.finders.FileSystemFinder")
  116. self.assertEqual(list(finder.list(None)), [])
  117. class StoragesCheckTests(SimpleTestCase):
  118. @override_settings(STORAGES={})
  119. def test_error_empty_storages(self):
  120. errors = check_storages(None)
  121. self.assertEqual(errors, [E005])
  122. @override_settings(
  123. STORAGES={
  124. DEFAULT_STORAGE_ALIAS: {
  125. "BACKEND": "django.core.files.storage.FileSystemStorage",
  126. },
  127. "example": {
  128. "BACKEND": "ignore.me",
  129. },
  130. }
  131. )
  132. def test_error_missing_staticfiles(self):
  133. errors = check_storages(None)
  134. self.assertEqual(errors, [E005])
  135. @override_settings(
  136. STORAGES={
  137. STATICFILES_STORAGE_ALIAS: {
  138. "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
  139. },
  140. }
  141. )
  142. def test_staticfiles_no_errors(self):
  143. errors = check_storages(None)
  144. self.assertEqual(errors, [])