Skip to content

Commit 324c1d6

Browse files
committed
✅ [#1821] Tests for category list visibility
1 parent ea488d0 commit 324c1d6

File tree

2 files changed

+84
-9
lines changed

2 files changed

+84
-9
lines changed

src/open_inwoner/components/tests/test_header.py

+55-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,32 @@ def setUpTestData(cls):
2323
# PrimaryNavigation.html requires apphook + categories
2424
create_apphook_page(ProductsApphook)
2525
cls.published1 = CategoryFactory(
26-
path="0001", name="First one", slug="first-one"
26+
path="0001",
27+
name="First one",
28+
slug="first-one",
29+
visible_for_anonymous=True,
30+
visible_for_authenticated=True,
2731
)
2832
cls.published2 = CategoryFactory(
29-
path="0002", name="Second one", slug="second-one"
33+
path="0002",
34+
name="Second one",
35+
slug="second-one",
36+
visible_for_anonymous=True,
37+
visible_for_authenticated=False,
38+
)
39+
cls.published3 = CategoryFactory(
40+
path="0003",
41+
name="Third one",
42+
slug="third-one",
43+
visible_for_anonymous=False,
44+
visible_for_authenticated=True,
45+
)
46+
cls.published4 = CategoryFactory(
47+
path="0004",
48+
name="Fourth one",
49+
slug="fourth-one",
50+
visible_for_anonymous=False,
51+
visible_for_authenticated=False,
3052
)
3153

3254
def test_categories_hidden_from_anonymous_users(self):
@@ -55,6 +77,37 @@ def test_categories_not_hidden_from_anonymous_users(self):
5577
self.assertEqual(categories[0].tag, "a")
5678
self.assertEqual(categories[1].tag, "button")
5779

80+
links = [x for x in doc.find("[title='Onderwerpen'] + ul li a").items()]
81+
self.assertEqual(len(links), 4)
82+
self.assertEqual(links[0].attr("href"), self.published1.get_absolute_url())
83+
self.assertEqual(links[1].attr("href"), self.published2.get_absolute_url())
84+
self.assertEqual(links[2].attr("href"), self.published1.get_absolute_url())
85+
self.assertEqual(links[3].attr("href"), self.published2.get_absolute_url())
86+
87+
def test_categories_visibility_for_authenticated_users(self):
88+
config = SiteConfiguration.get_solo()
89+
config.hide_categories_from_anonymous_users = False
90+
config.save()
91+
92+
self.client.force_login(self.user)
93+
94+
response = self.client.get("/", user=self.user)
95+
96+
doc = PyQuery(response.content)
97+
98+
categories = doc.find("[title='Onderwerpen']")
99+
100+
self.assertEqual(len(categories), 2)
101+
self.assertEqual(categories[0].tag, "a")
102+
self.assertEqual(categories[1].tag, "button")
103+
104+
links = [x for x in doc.find("[title='Onderwerpen'] + ul li a").items()]
105+
self.assertEqual(len(links), 4)
106+
self.assertEqual(links[0].attr("href"), self.published1.get_absolute_url())
107+
self.assertEqual(links[1].attr("href"), self.published3.get_absolute_url())
108+
self.assertEqual(links[2].attr("href"), self.published1.get_absolute_url())
109+
self.assertEqual(links[3].attr("href"), self.published3.get_absolute_url())
110+
58111
def test_search_bar_hidden_from_anonymous_users(self):
59112
config = SiteConfiguration.get_solo()
60113
config.hide_search_from_anonymous_users = True

src/open_inwoner/pdc/tests/test_category.py

+29-7
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,38 @@ class TestPublishedCategories(WebTest):
1616
def setUp(self):
1717
self.user = UserFactory()
1818
self.published1 = CategoryFactory(
19-
path="0001", name="First one", slug="first-one"
19+
path="0001",
20+
name="First one",
21+
slug="first-one",
22+
visible_for_anonymous=True,
23+
visible_for_authenticated=True,
2024
)
2125
self.published2 = CategoryFactory(
22-
path="0002", name="Second one", slug="second-one"
26+
path="0002",
27+
name="Second one",
28+
slug="second-one",
29+
visible_for_anonymous=True,
30+
visible_for_authenticated=False,
31+
)
32+
self.published3 = CategoryFactory(
33+
path="0003",
34+
name="Third one",
35+
slug="third-one",
36+
visible_for_anonymous=False,
37+
visible_for_authenticated=True,
38+
)
39+
self.published4 = CategoryFactory(
40+
path="0004",
41+
name="Fourth one",
42+
slug="fourth-one",
43+
visible_for_anonymous=False,
44+
visible_for_authenticated=False,
2345
)
2446
self.draft1 = CategoryFactory(
25-
path="0003", name="Third one", slug="third-one", published=False
47+
path="0005", name="Fifth one", slug="fifth-one", published=False
2648
)
2749
self.draft2 = CategoryFactory(
28-
path="0004", name="Wourth one", slug="wourth-one", published=False
50+
path="0006", name="Sixth one", slug="sixth-one", published=False
2951
)
3052
cms_tools.create_homepage()
3153

@@ -40,7 +62,7 @@ def test_only_published_categories_exist_in_breadcrumbs_when_logged_in(self):
4062
response = self.app.get("/", user=self.user)
4163
self.assertEqual(
4264
list(response.context["menu_categories"]),
43-
[self.published1, self.published2],
65+
[self.published1, self.published3],
4466
)
4567

4668
def test_only_published_categories_exist_in_list_page_when_anonymous(self):
@@ -52,7 +74,7 @@ def test_only_published_categories_exist_in_list_page_when_anonymous(self):
5274
def test_only_published_categories_exist_in_list_page_when_logged_in(self):
5375
response = self.app.get(reverse("products:category_list"), user=self.user)
5476
self.assertEqual(
55-
list(response.context["categories"]), [self.published1, self.published2]
77+
list(response.context["categories"]), [self.published1, self.published3]
5678
)
5779

5880
def test_only_published_subcategories_exist_in_detail_page_when_anonymous(self):
@@ -87,7 +109,7 @@ def test_only_published_categories_exist_in_my_categories_page(self):
87109
response = self.app.get(reverse("profile:categories"), user=self.user)
88110
self.assertEqual(
89111
list(response.context["form"].fields["selected_categories"].queryset.all()),
90-
[self.published1, self.published2],
112+
[self.published1, self.published2, self.published3, self.published4],
91113
)
92114

93115

0 commit comments

Comments
 (0)