Skip to content

Commit

Permalink
Changements dans les tests pour cibler 'Accueil' au lieu de la page W…
Browse files Browse the repository at this point in the history
…agtail créée par défaut
  • Loading branch information
chloend committed Nov 25, 2024
1 parent 9bf9b11 commit b1def96
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions lemarche/cms/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,67 @@

from django.core.management import call_command
from django.test import Client, TestCase
from wagtail.models import Site
from wagtail.models import Page, Site

from content_manager.models import ContentPage


class CreateContentPagesCommandTests(TestCase):
class CreateContentPagesCommandTest(TestCase):
def setUp(self):
self.root_page = Page.get_root_nodes().first()
self.home_page = Page(title="Accueil")
self.root_page.add_child(instance=self.home_page)
self.home_page.save_revision().publish()

# We change root_page of default site
Site.objects.filter(hostname="localhost", is_default_site=True).update(root_page=self.home_page)

def test_parent_page_setup(self):
"""Test the parent page for the home page is correctly set up"""
self.assertIsNotNone(self.root_page, "La page racine n'a pas été trouvée.")
self.assertIsNotNone(self.home_page, "La page parente 'Accueil' n'a pas été trouvée.")
self.assertTrue(self.home_page.live, "La page parente 'Accueil' n'est pas publiée.")
self.assertIn(
self.home_page, self.root_page.get_children(), "La page 'Accueil' n'est pas un enfant direct de la racine."
)

response = self.client.get(self.home_page.get_url())
self.assertEqual(
response.status_code,
200,
f"La page d'accueil n'est pas accessible (status: {response.status_code}).",
)

def test_create_content_pages_with_json(self):
client = Client()
"""Test the content pages are correctly created from the JSON file"""
with open("lemarche/fixtures/cms_content_pages.json") as f:
pages_data = json.load(f)

call_command("create_content_pages")

# Check that the pages were created
for page_data in pages_data:
slug = page_data["slug"]
self.assertTrue(
ContentPage.objects.filter(slug=slug).exists(), msg=f"La page avec le slug '{slug}' n'a pas été créée."

try:
page = ContentPage.objects.get(slug=slug)
except ContentPage.DoesNotExist:
self.fail(f"La page avec le slug '{slug}' n'a pas été trouvée.")

self.assertEqual(
page.get_parent(), self.home_page, f"La page '{slug}' n'est pas reliée à la page 'Accueil'."
)

response = client.get(f"/{slug}/")
client = Client()
response = client.get(page.get_url())
self.assertEqual(
response.status_code,
200,
msg=f"La page avec le slug '{slug}' n'est pas accessible (status: {response.status_code}).",
f"La page avec le slug '{slug}' n'est pas accessible (status: {response.status_code}).",
)

def test_prevent_duplicate_page_creation(self):
"""Ensure the command does not create duplicate pages"""
home_page = Site.objects.get(is_default_site=True).root_page
home_page.add_child(instance=ContentPage(slug="mentions-legales", title="Mentions légales"))
"""Test the command does not create duplicate pages"""
self.home_page.add_child(instance=ContentPage(slug="mentions-legales", title="Mentions légales"))

call_command("create_content_pages")

Expand Down

0 comments on commit b1def96

Please sign in to comment.