|
1 | | -from django.test import TestCase |
| 1 | +import pytest |
| 2 | +from django.core.exceptions import ValidationError |
| 3 | +from django.test import TestCase, override_settings |
| 4 | +from wagtail.images.models import Image |
| 5 | +from wagtail.images.tests.utils import get_test_image_file |
| 6 | + |
| 7 | +from home.models import ( |
| 8 | + CaseStudiesPage, |
| 9 | + FeaturedCaseStudy, |
| 10 | + GenericPage, |
| 11 | + HeroCarouselImage, |
| 12 | + Label, |
| 13 | +) |
| 14 | + |
| 15 | +from .test_models import LandingPageTestMixin |
2 | 16 |
|
3 | 17 |
|
4 | 18 | class TestHome(TestCase): |
5 | 19 | def test_status_code__success(self): |
6 | 20 | home_page_get = self.client.get("/") |
7 | 21 | assert home_page_get.status_code == 200 |
| 22 | + |
| 23 | + |
| 24 | +class FeaturedCaseStudiesTestMixin(LandingPageTestMixin): |
| 25 | + """Home page plus a Case Studies section with featured entries.""" |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + super().setUp() |
| 29 | + self.case_studies_index = CaseStudiesPage(title="Case Studies", slug="case-studies") |
| 30 | + self.home.add_child(instance=self.case_studies_index) |
| 31 | + |
| 32 | + self.case_study_1 = GenericPage( |
| 33 | + title="AI-Driven Shoreline Extraction", |
| 34 | + slug="shoreline", |
| 35 | + intro="<p>Neural networks mapping land-water boundaries.</p>", |
| 36 | + ) |
| 37 | + self.case_studies_index.add_child(instance=self.case_study_1) |
| 38 | + |
| 39 | + self.case_study_2 = GenericPage(title="Rapid Tree Canopy Detection", slug="canopy") |
| 40 | + self.case_studies_index.add_child(instance=self.case_study_2) |
| 41 | + |
| 42 | + self.label = Label.objects.create(name="Government", color="navy") |
| 43 | + for sort_order, page in enumerate([self.case_study_1, self.case_study_2]): |
| 44 | + FeaturedCaseStudy.objects.create( |
| 45 | + page=self.home, |
| 46 | + case_study=page.page_ptr, |
| 47 | + label=self.label, |
| 48 | + sort_order=sort_order, |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +@override_settings(WAGTAIL_CACHE=False) |
| 53 | +class TestHomePage(FeaturedCaseStudiesTestMixin, TestCase): |
| 54 | + def test_homepage_serves_redesign(self): |
| 55 | + response = self.client.get(self.home.url) |
| 56 | + assert response.status_code == 200 |
| 57 | + self.assertTemplateUsed(response, "home/home_page.html") |
| 58 | + self.assertContains(response, "overview-band") |
| 59 | + self.assertContains(response, "platform-tile") |
| 60 | + |
| 61 | + def test_design_param_is_ignored(self): |
| 62 | + response = self.client.get(self.home.url, {"design": "2"}) |
| 63 | + assert response.status_code == 200 |
| 64 | + self.assertTemplateUsed(response, "home/home_page.html") |
| 65 | + |
| 66 | + def test_carousel_renders_featured_case_studies(self): |
| 67 | + response = self.client.get(self.home.url) |
| 68 | + self.assertContains(response, "case-carousel") |
| 69 | + self.assertContains(response, self.case_study_1.title) |
| 70 | + self.assertContains(response, self.case_study_2.title) |
| 71 | + self.assertContains(response, "case-carousel__badge--navy") |
| 72 | + |
| 73 | + def test_platform_tiles_render_aims(self): |
| 74 | + target = GenericPage(title="Tile Target", slug="tile-target") |
| 75 | + self.home.add_child(instance=target) |
| 76 | + self.home.aim_1_title = "Data Catalogue" |
| 77 | + self.home.aim_1_description = "<p>A searchable catalogue of datasets.</p>" |
| 78 | + self.home.aim_1_page = target |
| 79 | + self.home.save() |
| 80 | + |
| 81 | + response = self.client.get(self.home.url) |
| 82 | + self.assertContains(response, "Data Catalogue") |
| 83 | + self.assertContains(response, "A searchable catalogue of datasets.") |
| 84 | + self.assertContains(response, f'href="{target.url}"') |
| 85 | + self.assertContains(response, "Find out more") |
| 86 | + |
| 87 | + def test_audience_section_renders(self): |
| 88 | + response = self.client.get(self.home.url) |
| 89 | + self.assertContains(response, "Who is the Earth Observation DataHub for?") |
| 90 | + self.assertContains(response, 'class="audience-card"', count=3) |
| 91 | + self.assertContains(response, "Government and Public Sector") |
| 92 | + self.assertContains(response, "Industry & Commercial Operators") |
| 93 | + self.assertContains(response, "Academia & Researchers") |
| 94 | + |
| 95 | + def test_platform_tile_renders_image_when_set(self): |
| 96 | + self.home.aim_1_image.save("aim1.png", get_test_image_file(), save=True) |
| 97 | + |
| 98 | + response = self.client.get(self.home.url) |
| 99 | + self.assertContains(response, "platform-tile__image", count=1) |
| 100 | + # Tiles without an image simply show title and description. |
| 101 | + self.assertContains(response, 'class="platform-tile"', count=4) |
| 102 | + |
| 103 | + def test_platform_skips_empty_aim_slots(self): |
| 104 | + self.home.aim_4_title = "" |
| 105 | + self.home.save() |
| 106 | + |
| 107 | + response = self.client.get(self.home.url) |
| 108 | + self.assertContains(response, 'class="platform-tile"', count=3) |
| 109 | + |
| 110 | + |
| 111 | +@override_settings(WAGTAIL_CACHE=False) |
| 112 | +class TestHeroCarousel(FeaturedCaseStudiesTestMixin, TestCase): |
| 113 | + def test_hero_carousel_renders_picked_imagery(self): |
| 114 | + for i in range(2): |
| 115 | + image = Image.objects.create(title=f"Hero {i}", file=get_test_image_file()) |
| 116 | + HeroCarouselImage.objects.create( |
| 117 | + page=self.home, |
| 118 | + image=image, |
| 119 | + caption=f"Caption {i}", |
| 120 | + sort_order=i, |
| 121 | + ) |
| 122 | + |
| 123 | + response = self.client.get(self.home.url) |
| 124 | + assert response.status_code == 200 |
| 125 | + self.assertContains(response, "data-hero-carousel") |
| 126 | + self.assertContains(response, "hero-carousel__bg", count=2) |
| 127 | + self.assertContains(response, "Caption 0") |
| 128 | + # The live-site hero text stays on top of the imagery. |
| 129 | + self.assertContains(response, "We provide a single point of access to") |
| 130 | + |
| 131 | + def test_hero_falls_back_to_static_image_without_picks(self): |
| 132 | + response = self.client.get(self.home.url) |
| 133 | + assert response.status_code == 200 |
| 134 | + self.assertNotContains(response, "data-hero-carousel") |
| 135 | + self.assertContains(response, 'class="hero"') |
| 136 | + self.assertContains(response, "We provide a single point of access to") |
| 137 | + |
| 138 | + |
| 139 | +@override_settings(WAGTAIL_CACHE=False) |
| 140 | +class TestFeaturedCaseStudy(FeaturedCaseStudiesTestMixin, TestCase): |
| 141 | + def test_display_summary_falls_back_to_intro(self): |
| 142 | + featured = self.home.featured_case_studies.first() |
| 143 | + assert "Neural networks mapping land-water boundaries." in featured.display_summary |
| 144 | + assert "<p>" not in featured.display_summary |
| 145 | + |
| 146 | + def test_display_summary_prefers_override(self): |
| 147 | + featured = self.home.featured_case_studies.first() |
| 148 | + featured.summary_override = "Override summary." |
| 149 | + assert featured.display_summary == "Override summary." |
| 150 | + |
| 151 | + def test_display_image_falls_back_to_hero_image(self): |
| 152 | + hero = Image.objects.create(title="Hero", file=get_test_image_file()) |
| 153 | + self.case_study_1.hero_image = hero |
| 154 | + self.case_study_1.save() |
| 155 | + |
| 156 | + featured = self.home.featured_case_studies.first() |
| 157 | + assert featured.display_image == hero |
| 158 | + |
| 159 | + def test_display_image_prefers_override(self): |
| 160 | + hero = Image.objects.create(title="Hero", file=get_test_image_file()) |
| 161 | + override = Image.objects.create(title="Override", file=get_test_image_file()) |
| 162 | + self.case_study_1.hero_image = hero |
| 163 | + self.case_study_1.save() |
| 164 | + |
| 165 | + featured = self.home.featured_case_studies.first() |
| 166 | + featured.image_override = override |
| 167 | + assert featured.display_image == override |
| 168 | + |
| 169 | + def test_display_image_none_without_images(self): |
| 170 | + featured = self.home.featured_case_studies.first() |
| 171 | + assert featured.display_image is None |
| 172 | + |
| 173 | + def test_clean_rejects_pages_outside_case_studies_section(self): |
| 174 | + outside = GenericPage(title="Outside", slug="outside") |
| 175 | + self.home.add_child(instance=outside) |
| 176 | + |
| 177 | + featured = FeaturedCaseStudy(page=self.home, case_study=outside.page_ptr) |
| 178 | + with pytest.raises(ValidationError): |
| 179 | + featured.clean() |
| 180 | + |
| 181 | + def test_clean_accepts_case_study_pages(self): |
| 182 | + featured = self.home.featured_case_studies.first() |
| 183 | + featured.clean() |
0 commit comments