Skip to content

Commit

Permalink
✅ [#1821] Tests for category visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Nov 6, 2023
1 parent ea488d0 commit b66ba9a
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 15 deletions.
54 changes: 48 additions & 6 deletions src/open_inwoner/cms/products/tests/test_plugin_categories.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,35 @@ def setUp(self):

def test_only_highlighted_categories_exist_in_context_when_they_exist(self):
CategoryFactory(name="Should be first")
highlighted_category = CategoryFactory(
name="This should be second", highlighted=True
highlighted_category1 = CategoryFactory(
name="This should be second",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=True,
)
highlighted_category2 = CategoryFactory(
path="0002",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=False,
)
highlighted_category3 = CategoryFactory(
path="0003",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=True,
)
highlighted_category4 = CategoryFactory(
path="0004",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=False,
)

html, context = cms_tools.render_plugin(CategoriesPlugin)
self.assertEqual(
list(context["categories"]),
[highlighted_category],
[highlighted_category1, highlighted_category2],
)

def test_highlighted_categories_are_ordered_by_alphabetically(self):
Expand All @@ -49,15 +70,36 @@ def test_highlighted_categories_are_ordered_by_alphabetically(self):
def test_only_highlighted_categories_are_shown_when_they_exist(self):
user = UserFactory()
category = CategoryFactory(name="Should be first")
highlighted_category = CategoryFactory(
name="This should be second", highlighted=True
highlighted_category1 = CategoryFactory(
name="This should be second",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=True,
)
highlighted_category2 = CategoryFactory(
path="0002",
highlighted=True,
visible_for_anonymous=True,
visible_for_authenticated=False,
)
highlighted_category3 = CategoryFactory(
path="0003",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=True,
)
highlighted_category4 = CategoryFactory(
path="0004",
highlighted=True,
visible_for_anonymous=False,
visible_for_authenticated=False,
)

html, context = cms_tools.render_plugin(CategoriesPlugin, user=user)

self.assertEqual(
list(context["categories"]),
[highlighted_category],
[highlighted_category1, highlighted_category3],
)

def test_category_selected(self):
Expand Down
57 changes: 55 additions & 2 deletions src/open_inwoner/components/tests/test_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,32 @@ def setUpTestData(cls):
# PrimaryNavigation.html requires apphook + categories
create_apphook_page(ProductsApphook)
cls.published1 = CategoryFactory(
path="0001", name="First one", slug="first-one"
path="0001",
name="First one",
slug="first-one",
visible_for_anonymous=True,
visible_for_authenticated=True,
)
cls.published2 = CategoryFactory(
path="0002", name="Second one", slug="second-one"
path="0002",
name="Second one",
slug="second-one",
visible_for_anonymous=True,
visible_for_authenticated=False,
)
cls.published3 = CategoryFactory(
path="0003",
name="Third one",
slug="third-one",
visible_for_anonymous=False,
visible_for_authenticated=True,
)
cls.published4 = CategoryFactory(
path="0004",
name="Fourth one",
slug="fourth-one",
visible_for_anonymous=False,
visible_for_authenticated=False,
)

def test_categories_hidden_from_anonymous_users(self):
Expand Down Expand Up @@ -55,6 +77,37 @@ def test_categories_not_hidden_from_anonymous_users(self):
self.assertEqual(categories[0].tag, "a")
self.assertEqual(categories[1].tag, "button")

links = [x for x in doc.find("[title='Onderwerpen'] + ul li a").items()]
self.assertEqual(len(links), 4)
self.assertEqual(links[0].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[1].attr("href"), self.published2.get_absolute_url())
self.assertEqual(links[2].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[3].attr("href"), self.published2.get_absolute_url())

def test_categories_visibility_for_authenticated_users(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = False
config.save()

self.client.force_login(self.user)

response = self.client.get("/", user=self.user)

doc = PyQuery(response.content)

categories = doc.find("[title='Onderwerpen']")

self.assertEqual(len(categories), 2)
self.assertEqual(categories[0].tag, "a")
self.assertEqual(categories[1].tag, "button")

links = [x for x in doc.find("[title='Onderwerpen'] + ul li a").items()]
self.assertEqual(len(links), 4)
self.assertEqual(links[0].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[1].attr("href"), self.published3.get_absolute_url())
self.assertEqual(links[2].attr("href"), self.published1.get_absolute_url())
self.assertEqual(links[3].attr("href"), self.published3.get_absolute_url())

def test_search_bar_hidden_from_anonymous_users(self):
config = SiteConfiguration.get_solo()
config.hide_search_from_anonymous_users = True
Expand Down
36 changes: 29 additions & 7 deletions src/open_inwoner/pdc/tests/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,38 @@ class TestPublishedCategories(WebTest):
def setUp(self):
self.user = UserFactory()
self.published1 = CategoryFactory(
path="0001", name="First one", slug="first-one"
path="0001",
name="First one",
slug="first-one",
visible_for_anonymous=True,
visible_for_authenticated=True,
)
self.published2 = CategoryFactory(
path="0002", name="Second one", slug="second-one"
path="0002",
name="Second one",
slug="second-one",
visible_for_anonymous=True,
visible_for_authenticated=False,
)
self.published3 = CategoryFactory(
path="0003",
name="Third one",
slug="third-one",
visible_for_anonymous=False,
visible_for_authenticated=True,
)
self.published4 = CategoryFactory(
path="0004",
name="Fourth one",
slug="fourth-one",
visible_for_anonymous=False,
visible_for_authenticated=False,
)
self.draft1 = CategoryFactory(
path="0003", name="Third one", slug="third-one", published=False
path="0005", name="Fifth one", slug="fifth-one", published=False
)
self.draft2 = CategoryFactory(
path="0004", name="Wourth one", slug="wourth-one", published=False
path="0006", name="Sixth one", slug="sixth-one", published=False
)
cms_tools.create_homepage()

Expand All @@ -40,7 +62,7 @@ def test_only_published_categories_exist_in_breadcrumbs_when_logged_in(self):
response = self.app.get("/", user=self.user)
self.assertEqual(
list(response.context["menu_categories"]),
[self.published1, self.published2],
[self.published1, self.published3],
)

def test_only_published_categories_exist_in_list_page_when_anonymous(self):
Expand All @@ -52,7 +74,7 @@ def test_only_published_categories_exist_in_list_page_when_anonymous(self):
def test_only_published_categories_exist_in_list_page_when_logged_in(self):
response = self.app.get(reverse("products:category_list"), user=self.user)
self.assertEqual(
list(response.context["categories"]), [self.published1, self.published2]
list(response.context["categories"]), [self.published1, self.published3]
)

def test_only_published_subcategories_exist_in_detail_page_when_anonymous(self):
Expand Down Expand Up @@ -87,7 +109,7 @@ def test_only_published_categories_exist_in_my_categories_page(self):
response = self.app.get(reverse("profile:categories"), user=self.user)
self.assertEqual(
list(response.context["form"].fields["selected_categories"].queryset.all()),
[self.published1, self.published2],
[self.published1, self.published2, self.published3, self.published4],
)


Expand Down
14 changes: 14 additions & 0 deletions src/open_inwoner/pdc/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def setUpTestData(cls):
cls.category = CategoryFactory.create(
name="test cat",
description="A <em>descriptive</em> description",
visible_for_anonymous=False,
visible_for_authenticated=False,
)

def test_category_detail_view_access_restricted(self):
Expand Down Expand Up @@ -95,6 +97,18 @@ def test_category_detail_view_access_not_restricted(self):

self.assertEqual(response.status_code, 200)

def test_category_detail_view_access_not_restricted_if_invisible(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = False
config.save()

url = reverse("products:category_detail", kwargs={"slug": self.category.slug})

# request with anonymous user
response = self.client.get(url)

self.assertEqual(response.status_code, 200)

def test_category_detail_description_rendered(self):
url = reverse("products:category_detail", kwargs={"slug": self.category.slug})

Expand Down

0 comments on commit b66ba9a

Please sign in to comment.