Skip to content

Commit

Permalink
[#1788] Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pi-sigma committed Oct 10, 2023
1 parent 267a8c5 commit 3e2e563
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 158 deletions.
6 changes: 4 additions & 2 deletions src/open_inwoner/cms/cases/views/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@


# TODO add OuterCaseAccessMixin back
class OuterCaseListView(CommonPageMixin, BaseBreadcrumbMixin, TemplateView):
class OuterCaseListView(
OuterCaseAccessMixin, CommonPageMixin, BaseBreadcrumbMixin, TemplateView
):
"""View on case list while being loaded"""

template_name = "pages/cases/list_outer.html"
Expand All @@ -33,7 +35,7 @@ def get_context_data(self, **kwargs):

# TODO: add CaseAccessMixin back
class InnerCaseListView(
RequiresHtmxMixin, CommonPageMixin, CaseListMixin, TemplateView
RequiresHtmxMixin, CommonPageMixin, CaseAccessMixin, CaseListMixin, TemplateView
):
"""View on case list"""

Expand Down
234 changes: 79 additions & 155 deletions src/open_inwoner/openzaak/tests/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class CaseListAccessTests(AssertRedirectsMixin, ClearCachesMixin, WebTest):
outer_urls = [
reverse_lazy("cases:open_cases"),
reverse_lazy("cases:closed_cases"),
reverse_lazy("cases:index"),
# reverse_lazy("cases:closed_cases"),
]
inner_urls = [
reverse_lazy("cases:open_cases_content"),
reverse_lazy("cases:closed_cases_content"),
reverse_lazy("cases:cases_content"),
# reverse_lazy("cases:closed_cases_content"),
]

@classmethod
Expand Down Expand Up @@ -133,8 +133,9 @@ def test_no_cases_are_retrieved_when_http_500(self, m):
@requests_mock.Mocker()
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
class CaseListViewTests(ClearCachesMixin, WebTest):
inner_url_open = reverse_lazy("cases:open_cases_content")
inner_url_closed = reverse_lazy("cases:closed_cases_content")
# inner_url_open = reverse_lazy("cases:open_cases_content")
# inner_url_closed = reverse_lazy("cases:closed_cases_content")
inner_url = reverse_lazy("cases:cases_content")

@classmethod
def setUpTestData(cls):
Expand Down Expand Up @@ -314,11 +315,11 @@ def _setUpMocks(self, m):
]:
m.get(resource["url"], json=resource)

def test_list_open_cases(self, m):
def test_list_cases(self, m):
self._setUpMocks(m)

response = self.app.get(
self.inner_url_open, user=self.user, headers={"HX-Request": "true"}
self.inner_url, user=self.user, headers={"HX-Request": "true"}
)

self.assertListEqual(
Expand All @@ -340,13 +341,19 @@ def test_list_open_cases(self, m):
"description": self.zaaktype["omschrijving"],
"current_status": self.status_type1["omschrijving"],
},
{
"uuid": self.zaak3["uuid"],
"start_date": datetime.date.fromisoformat(self.zaak3["startdatum"]),
"end_date": datetime.date.fromisoformat(self.zaak3["einddatum"]),
"identificatie": self.zaak3["identificatie"],
"description": self.zaaktype["omschrijving"],
"current_status": self.status_type2["omschrijving"],
},
],
)
# don't show closed cases
self.assertNotContains(response, self.zaak3["identificatie"])
self.assertNotContains(response, self.zaak3["omschrijving"])
self.assertNotContains(response, self.zaak_intern["identificatie"])
# don't show internal cases
self.assertNotContains(response, self.zaak_intern["omschrijving"])
self.assertNotContains(response, self.zaak_intern["identificatie"])

# check zaken request query parameters
list_zaken_req = [
Expand All @@ -367,152 +374,62 @@ def test_list_open_cases(self, m):
},
)

def test_list_open_cases_reformats_zaak_identificatie(self, m):
def test_list_cases_reformats_zaak_identificatie(self, m):
self._setUpMocks(m)

with patch(
"open_inwoner.cms.cases.views.mixins.format_zaak_identificatie",
wraps=format_zaak_identificatie,
) as spy_format:
self.app.get(
self.inner_url_open, user=self.user, headers={"HX-Request": "true"}
)
self.app.get(self.inner_url, user=self.user, headers={"HX-Request": "true"})

spy_format.assert_called()
self.assertEqual(spy_format.call_count, 2)
self.assertEqual(spy_format.call_count, 3)

def test_list_open_cases_translates_status(self, m):
def test_list_cases_translates_status(self, m):
st1 = StatusTranslationFactory(
status=self.status_type1["omschrijving"],
translation="Translated Status Type",
)
self._setUpMocks(m)
response = self.app.get(
self.inner_url_open, user=self.user, headers={"HX-Request": "true"}
self.inner_url, user=self.user, headers={"HX-Request": "true"}
)
self.assertNotContains(response, st1.status)
self.assertContains(response, st1.translation)

def test_list_open_cases_logs_displayed_case_ids(self, m):
def test_list_cases_logs_displayed_case_ids(self, m):
self._setUpMocks(m)

self.app.get(
self.inner_url_open, user=self.user, headers={"HX-Request": "true"}
)
self.app.get(self.inner_url, user=self.user, headers={"HX-Request": "true"})

# check access logs for displayed cases
logs = list(TimelineLog.objects.all())

case_log = [
l for l in logs if self.zaak1["identificatie"] in l.extra_data["message"]
log
for log in logs
if self.zaak1["identificatie"] in log.extra_data["message"]
]
self.assertEqual(len(case_log), 1)
self.assertEqual(self.user, case_log[0].user)
self.assertEqual(self.user, case_log[0].content_object)

case_log = [
l for l in logs if self.zaak2["identificatie"] in l.extra_data["message"]
log
for log in logs
if self.zaak2["identificatie"] in log.extra_data["message"]
]
self.assertEqual(len(case_log), 1)
self.assertEqual(self.user, case_log[0].user)
self.assertEqual(self.user, case_log[0].content_object)

# no logs for non-displayed cases
# no logs for internal, hence non-displayed cases
for log in logs:
self.assertNotIn(self.zaak3["identificatie"], log.extra_data["message"])

def test_list_closed_cases(self, m):
self._setUpMocks(m)

response = self.app.get(
self.inner_url_closed, user=self.user, headers={"HX-Request": "true"}
)

self.assertListEqual(
response.context.get("cases"),
[
{
"uuid": self.zaak3["uuid"],
"start_date": datetime.date.fromisoformat(self.zaak3["startdatum"]),
"end_date": datetime.date.fromisoformat(self.zaak3["einddatum"]),
"identificatie": self.zaak3["identificatie"],
"description": self.zaaktype["omschrijving"],
"current_status": self.status_type2["omschrijving"],
},
],
)
# don't show closed cases
for open_zaak in [self.zaak1, self.zaak2]:
self.assertNotContains(response, open_zaak["identificatie"])
self.assertNotContains(response, open_zaak["omschrijving"])

# check zaken request query parameters
list_zaken_req = [
req
for req in m.request_history
if req.hostname == "zaken.nl" and req.path == "/api/v1/zaken"
][0]
self.assertEqual(len(list_zaken_req.qs), 2)
self.assertEqual(
list_zaken_req.qs,
{
"rol__betrokkeneidentificatie__natuurlijkpersoon__inpbsn": [
self.user.bsn
],
"maximalevertrouwelijkheidaanduiding": [
VertrouwelijkheidsAanduidingen.beperkt_openbaar
],
},
)

def test_list_closed_cases_reformats_zaak_identificatie(self, m):
self._setUpMocks(m)

with patch(
"open_inwoner.cms.cases.views.mixins.format_zaak_identificatie",
wraps=format_zaak_identificatie,
) as spy_format:
self.app.get(
self.inner_url_closed, user=self.user, headers={"HX-Request": "true"}
self.assertNotIn(
self.zaak_intern["identificatie"], log.extra_data["message"]
)

spy_format.assert_called()
self.assertEqual(spy_format.call_count, 1)

def test_list_closed_cases_translates_status(self, m):
st1 = StatusTranslationFactory(
status=self.status_type2["omschrijving"],
translation="Translated Status Type",
)
self._setUpMocks(m)
response = self.app.get(
self.inner_url_closed, user=self.user, headers={"HX-Request": "true"}
)
self.assertNotContains(response, st1.status)
self.assertContains(response, st1.translation)

def test_list_closed_cases_logs_displayed_case_ids(self, m):
self._setUpMocks(m)

self.app.get(
self.inner_url_closed, user=self.user, headers={"HX-Request": "true"}
)

# check access logs for displayed cases
logs = list(TimelineLog.objects.all())

case_log = [
l for l in logs if self.zaak3["identificatie"] in l.extra_data["message"]
]
self.assertEqual(len(case_log), 1)
self.assertEqual(self.user, case_log[0].user)
self.assertEqual(self.user, case_log[0].content_object)

# no logs for non-displayed cases
for log in logs:
self.assertNotIn(self.zaak1["identificatie"], log.extra_data["message"])
self.assertNotIn(self.zaak2["identificatie"], log.extra_data["message"])

@patch.object(CaseListMixin, "paginate_by", 1)
def test_list_cases_paginated(self, m):
"""
Expand All @@ -522,7 +439,7 @@ def test_list_cases_paginated(self, m):

# 1. test first page
response_1 = self.app.get(
self.inner_url_open, user=self.user, headers={"HX-Request": "true"}
self.inner_url, user=self.user, headers={"HX-Request": "true"}
)

self.assertListEqual(
Expand All @@ -542,7 +459,7 @@ def test_list_cases_paginated(self, m):
self.assertContains(response_1, "?page=2")

# 2. test next page
next_page = f"{self.inner_url_open}?page=2"
next_page = f"{self.inner_url}?page=2"
response_2 = self.app.get(
next_page, user=self.user, headers={"HX-Request": "true"}
)
Expand All @@ -567,45 +484,52 @@ def test_list_cases_paginated(self, m):
def test_list_cases_paginated_logs_displayed_case_ids(self, m):
self._setUpMocks(m)

# 1. test first page
response = self.app.get(
self.inner_url_open, user=self.user, headers={"HX-Request": "true"}
)
self.assertEqual(response.context.get("cases")[0]["uuid"], self.zaak2["uuid"])
with self.subTest("first page"):
response = self.app.get(
self.inner_url, user=self.user, headers={"HX-Request": "true"}
)
self.assertEqual(
response.context.get("cases")[0]["uuid"], self.zaak2["uuid"]
)

# check access logs for displayed cases
logs = list(TimelineLog.objects.all())
# check access logs for displayed cases
logs = list(TimelineLog.objects.all())

case_log = [
l for l in logs if self.zaak2["identificatie"] in l.extra_data["message"]
]
self.assertEqual(len(case_log), 1)
self.assertEqual(self.user, case_log[0].user)
self.assertEqual(self.user, case_log[0].content_object)
case_log = [
log
for log in logs
if self.zaak2["identificatie"] in log.extra_data["message"]
]
self.assertEqual(len(case_log), 1)
self.assertEqual(self.user, case_log[0].user)
self.assertEqual(self.user, case_log[0].content_object)

# no logs for non-displayed cases
for log in logs:
self.assertNotIn(self.zaak1["identificatie"], log.extra_data["message"])
self.assertNotIn(self.zaak3["identificatie"], log.extra_data["message"])
# no logs for non-displayed cases
for log in logs:
self.assertNotIn(self.zaak1["identificatie"], log.extra_data["message"])
self.assertNotIn(self.zaak3["identificatie"], log.extra_data["message"])

# clear logs for testing
TimelineLog.objects.all().delete()

# 2. test next page
next_page = f"{self.inner_url_open}?page=2"
response = self.app.get(
next_page, user=self.user, headers={"HX-Request": "true"}
)
self.assertEqual(response.context.get("cases")[0]["uuid"], self.zaak1["uuid"])

# check access logs for displayed cases
logs = list(TimelineLog.objects.all())
case_log = [
l for l in logs if self.zaak1["identificatie"] in l.extra_data["message"]
]
self.assertEqual(len(case_log), 1)
with self.subTest("next page"):
next_page = f"{self.inner_url}?page=2"
response = self.app.get(
next_page, user=self.user, headers={"HX-Request": "true"}
)
self.assertEqual(
response.context.get("cases")[0]["uuid"], self.zaak1["uuid"]
)

# no logs for non-displayed cases (after we cleared just above)
for log in logs:
self.assertNotIn(self.zaak2["identificatie"], log.extra_data["message"])
self.assertNotIn(self.zaak3["identificatie"], log.extra_data["message"])
# check access logs for displayed cases
logs = list(TimelineLog.objects.all())
case_log = [
log
for log in logs
if self.zaak1["identificatie"] in log.extra_data["message"]
]
self.assertEqual(len(case_log), 1)

# no logs for non-displayed cases (after we cleared just above)
for log in logs:
self.assertNotIn(self.zaak2["identificatie"], log.extra_data["message"])
self.assertNotIn(self.zaak3["identificatie"], log.extra_data["message"])
1 change: 0 additions & 1 deletion src/open_inwoner/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
path("apimock/", include("open_inwoner.apimock.urls")),
# TODO move search to products cms app?
path("", include("open_inwoner.search.urls", namespace="search")),
# path("uitkeringen/", include("open_inwoner.cms.ssd.urls", namespace="ssd")),
re_path(r"^", include("cms.urls")),
]

Expand Down

0 comments on commit 3e2e563

Please sign in to comment.