test_navbars_and_footers.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. from django.test import Client
  2. from wagtail.test.utils import WagtailPageTests
  3. from wagtail.models import Site
  4. from wagtailcrx.tests.testapp.models import WebPage
  5. from wagtailcrx.models.snippet_models import Footer, Navbar
  6. from wagtailcrx.models.wagtailsettings_models import (
  7. LayoutSettings,
  8. NavbarOrderable,
  9. FooterOrderable,
  10. )
  11. class NavbarFooterTestCase(WagtailPageTests):
  12. """
  13. Test that the relevant navbar chooser settings appear in the homepage HTML.
  14. """
  15. model = WebPage
  16. def setUp(self):
  17. # HTTP client.
  18. self.client = Client()
  19. # Use home page and default site.
  20. self.site = Site.objects.filter(is_default_site=True)[0]
  21. self.homepage = WebPage.objects.get(url_path="/home/")
  22. # create 2 nav snippets
  23. self.navbar = Navbar.objects.create(name="Nav1", custom_id="Nav1")
  24. self.navbar2 = Navbar.objects.create(name="Nav2", custom_id="Nav2")
  25. self.footer = Footer.objects.create(name="Footer1", custom_id="Footer1")
  26. self.footer2 = Footer.objects.create(name="Footer2", custom_id="Footer2")
  27. # Populate settings.
  28. self.settings = LayoutSettings.for_site(self.site)
  29. # layout = self.settings
  30. self.navbarorderable = NavbarOrderable.objects.create(
  31. sort_order=0,
  32. navbar_chooser=LayoutSettings.objects.get(id=self.settings.id),
  33. navbar=Navbar.objects.get(id=self.navbar.id),
  34. )
  35. self.footerorderable = FooterOrderable.objects.create(
  36. sort_order=0,
  37. footer_chooser=LayoutSettings.objects.get(id=self.settings.id),
  38. footer=Footer.objects.get(id=self.footer.id),
  39. )
  40. # save settings
  41. self.settings.save()
  42. def test_get(self):
  43. """
  44. Tests to make sure the page serves a 200 from a GET request.
  45. """
  46. response = self.client.get(self.homepage.url, follow=True)
  47. self.assertEqual(response.status_code, 200)
  48. def test_navbar(self):
  49. """
  50. Make sure navbar is on homepage.
  51. """
  52. response = self.client.get(self.homepage.url, follow=True)
  53. # Checks if specified HTML is within response
  54. # https://docs.djangoproject.com/en/3.2/topics/testing/tools/#django.test.SimpleTestCase.assertContains
  55. self.assertContains(
  56. response,
  57. text=f'<ul class="navbar-nav" id="{self.navbar.custom_id}">',
  58. status_code=200,
  59. html=True,
  60. )
  61. self.assertNotContains(
  62. response,
  63. text=f'<ul class="navbar-nav" id="{self.navbar2.custom_id}">',
  64. status_code=200,
  65. html=True,
  66. )
  67. def test_multi_navbars(self):
  68. """
  69. Adds another navbar and checks if it shows on page.
  70. """
  71. self.navbarorderable2 = NavbarOrderable.objects.create(
  72. sort_order=1,
  73. navbar_chooser=LayoutSettings.objects.get(id=self.settings.id),
  74. navbar=Navbar.objects.get(id=self.navbar2.id),
  75. )
  76. # get the navbar (orderable)
  77. self.settings.save()
  78. # update settings for using 2 navs, then check that both navbars show and in right order
  79. response = self.client.get(self.homepage.url, follow=True)
  80. self.assertContains(
  81. response,
  82. text=f'<ul class="navbar-nav" id="{self.navbar.custom_id}">'
  83. f'</ul><ul class="navbar-nav" id="{self.navbar2.custom_id}">',
  84. status_code=200,
  85. html=True,
  86. )
  87. def test_footer(self):
  88. """
  89. Make sure footer is on homepage.
  90. """
  91. response = self.client.get(self.homepage.url, follow=True)
  92. self.assertContains(
  93. response, text=f'<div id="{self.footer.custom_id}">', status_code=200, html=True
  94. )
  95. self.assertNotContains(
  96. response, text=f'<div id="{self.footer2.custom_id}">', status_code=200, html=True
  97. )
  98. def test_multi_footers(self):
  99. """
  100. Adds another footer to settings and checks if it shows on page.
  101. """
  102. self.footerorderable2 = FooterOrderable.objects.create(
  103. sort_order=1,
  104. footer_chooser=LayoutSettings.objects.get(id=self.settings.id),
  105. footer=Footer.objects.get(id=self.footer2.id),
  106. )
  107. # get the footer (orderable)
  108. self.settings.save()
  109. # update settings for using 2 footers, then check that both footers show
  110. response = self.client.get(self.homepage.url, follow=True)
  111. self.assertContains(
  112. response,
  113. text=f'<div id="{self.footer.custom_id}"></div><div id="{self.footer2.custom_id}">',
  114. status_code=200,
  115. html=True,
  116. )