Skip to content

Commit

Permalink
Merge pull request #844 from maykinmedia/feature/1837-category-visibi…
Browse files Browse the repository at this point in the history
…lity-digid-eherkenning

✨ [#1837] Category visibility for DigiD/eHerkenning
  • Loading branch information
stevenbal authored Nov 13, 2023
2 parents 5893b4e + 887d74f commit 90870cf
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/open_inwoner/pdc/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def draft(self):

def visible_for_user(self, user: User):
if user.is_authenticated:
if getattr(user, "bsn", None):
return self.filter(visible_for_citizens=True)
elif getattr(user, "kvk", None):
return self.filter(visible_for_companies=True)
return self.filter(visible_for_authenticated=True)
return self.filter(visible_for_anonymous=True)

Expand Down
91 changes: 90 additions & 1 deletion src/open_inwoner/pdc/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from unittest import skip

from django.test import TestCase, override_settings
from django.urls import reverse

from open_inwoner.accounts.tests.factories import UserFactory
from open_inwoner.accounts.tests.factories import DigidUserFactory, UserFactory
from open_inwoner.configurations.models import SiteConfiguration

from .factories import CategoryFactory
Expand All @@ -16,6 +18,35 @@ def setUpTestData(cls):
cls.user.email = "[email protected]"
cls.user.save()

cls.category1 = CategoryFactory(
name="0001",
visible_for_anonymous=True,
visible_for_authenticated=True,
visible_for_citizens=False,
visible_for_companies=False,
)
cls.category2 = CategoryFactory(
name="0002",
visible_for_anonymous=True,
visible_for_authenticated=False,
visible_for_citizens=True,
visible_for_companies=True,
)
cls.category3 = CategoryFactory(
name="0003",
visible_for_anonymous=False,
visible_for_authenticated=True,
visible_for_citizens=True,
visible_for_companies=False,
)
cls.category4 = CategoryFactory(
name="0004",
visible_for_anonymous=False,
visible_for_authenticated=False,
visible_for_citizens=False,
visible_for_companies=True,
)

def test_category_list_view_access_restricted(self):
config = SiteConfiguration.get_solo()
config.hide_categories_from_anonymous_users = True
Expand Down Expand Up @@ -48,6 +79,64 @@ def test_category_list_view_access_not_restricted(self):

self.assertEqual(response.status_code, 200)

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

url = reverse("products:category_list")

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

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

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

self.client.force_login(self.user)

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

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

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

user = DigidUserFactory()
self.client.force_login(user)

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

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

@skip("eHerkenning is not implemented yet")
def test_category_list_view_visibility_for_eherkenning_user(self):
url = reverse("products:category_list")

# TODO should be eHerkenningUserFactory
user = DigidUserFactory()
self.client.force_login(user)

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

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


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class CategoryDetailViewTest(TestCase):
Expand Down

0 comments on commit 90870cf

Please sign in to comment.