Skip to content

Commit

Permalink
Merge pull request #1046 from maykinmedia/fix/2143-categories-for-admins
Browse files Browse the repository at this point in the history
🐛 [#2143] Show all categories to staff users
  • Loading branch information
stevenbal authored Feb 26, 2024
2 parents b6ad694 + 2695b62 commit d57cca2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
5 changes: 4 additions & 1 deletion src/open_inwoner/pdc/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ def draft(self):

def visible_for_user(self, user: User):
if user.is_authenticated:
if user.bsn:
# Show all categories to staff users
if user.is_staff:
return self
elif user.bsn:
return self.filter(visible_for_citizens=True)
elif user.kvk:
return self.filter(visible_for_companies=True)
Expand Down
66 changes: 42 additions & 24 deletions src/open_inwoner/pdc/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,28 @@

@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class CategoryListViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
cls.user.set_password("12345")
cls.user.email = "[email protected]"
cls.user.save()

cls.category1 = CategoryFactory(
def setUp(self):
super().setUp()

self.category_anonymous = CategoryFactory(
name="0001",
visible_for_anonymous=True,
visible_for_citizens=False,
visible_for_companies=False,
)
cls.category2 = CategoryFactory(
self.category_all = CategoryFactory(
name="0002",
visible_for_anonymous=True,
visible_for_citizens=True,
visible_for_companies=True,
)
cls.category3 = CategoryFactory(
self.category_citizens = CategoryFactory(
name="0003",
visible_for_anonymous=False,
visible_for_citizens=True,
visible_for_companies=False,
)
cls.category4 = CategoryFactory(
self.category_companies = CategoryFactory(
name="0004",
visible_for_anonymous=False,
visible_for_citizens=False,
Expand All @@ -61,14 +57,16 @@ def test_category_list_view_access_restricted(self):

url = reverse("products:category_list")

user = UserFactory()

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

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, "/accounts/login/?next=/products/")

# request with user logged in
self.client.login(email=self.user.email, password="12345")
self.client.force_login(user=user)

response = self.client.get(url)

Expand Down Expand Up @@ -98,7 +96,8 @@ def test_category_list_view_visibility_for_anonymous_user(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(
list(response.context["object_list"]), [self.category1, self.category2]
list(response.context["object_list"]),
[self.category_anonymous, self.category_all],
)

def test_category_list_view_visibility_for_digid_user(self):
Expand All @@ -112,7 +111,8 @@ def test_category_list_view_visibility_for_digid_user(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(
list(response.context["object_list"]), [self.category2, self.category3]
list(response.context["object_list"]),
[self.category_all, self.category_citizens],
)

@override_settings(MIDDLEWARE=PATCHED_MIDDLEWARE)
Expand All @@ -126,20 +126,38 @@ def test_category_list_view_visibility_for_eherkenning_user(self):

self.assertEqual(response.status_code, 200)
self.assertEqual(
list(response.context["object_list"]), [self.category2, self.category4]
list(response.context["object_list"]),
[self.category_all, self.category_companies],
)

def test_category_list_view_visibility_for_staff_user(self):
url = reverse("products:category_list")

user = UserFactory(is_staff=True)
self.client.force_login(user)

response = self.client.get(url)

self.assertEqual(response.status_code, 200)
self.assertEqual(
list(response.context["object_list"]),
[
self.category_anonymous,
self.category_all,
self.category_citizens,
self.category_companies,
],
)


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class CategoryDetailViewTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = DigidUserFactory()
cls.user.set_password("12345")
cls.user.email = "[email protected]"
cls.user.save()

cls.category = CategoryFactory.create(
def setUp(self):
super().setUp()

self.user = DigidUserFactory()

self.category = CategoryFactory.create(
name="test cat",
description="A <em>descriptive</em> description",
visible_for_anonymous=False,
Expand All @@ -159,7 +177,7 @@ def test_category_detail_view_access_restricted(self):
self.assertEqual(response.url, "/accounts/login/?next=/products/test-cat/")

# request with user logged in
self.client.login(email=self.user.email, password="12345")
self.client.force_login(user=self.user)

response = self.client.get(url)

Expand Down

0 comments on commit d57cca2

Please sign in to comment.