From 8b236ffe2f36d71d4803797dfd476e848aea0e10 Mon Sep 17 00:00:00 2001 From: Steven Bal Date: Fri, 23 Feb 2024 17:06:30 +0100 Subject: [PATCH] :white_check_mark: [#2041] Fix tests for user DB constraints --- src/open_inwoner/accounts/tests/test_contact_views.py | 2 +- src/open_inwoner/accounts/tests/test_profile_views.py | 1 + .../accounts/tests/test_user_constraints.py | 6 ++++-- src/open_inwoner/cms/cases/tests/test_contactform.py | 6 +++++- src/open_inwoner/haalcentraal/tests/test_signal.py | 10 +++++++--- src/open_inwoner/kvk/tests/test_signals.py | 11 +++++++---- src/open_inwoner/openklant/tests/data.py | 8 ++++++++ src/open_inwoner/pdc/tests/test_views.py | 3 +-- 8 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/open_inwoner/accounts/tests/test_contact_views.py b/src/open_inwoner/accounts/tests/test_contact_views.py index 040df99b09..dc9084a50b 100644 --- a/src/open_inwoner/accounts/tests/test_contact_views.py +++ b/src/open_inwoner/accounts/tests/test_contact_views.py @@ -179,7 +179,7 @@ def test_existing_user_contact(self): self.assertEqual(existing_user, pending_invitation) def test_existing_user_contact_with_case_sensitive_email(self): - existing_user = UserFactory(email="user@example.com", bsn="111111111") + existing_user = DigidUserFactory(email="user@example.com", bsn="111111111") response = self.app.get(self.create_url, user=self.user) form = response.forms["contact-form"] diff --git a/src/open_inwoner/accounts/tests/test_profile_views.py b/src/open_inwoner/accounts/tests/test_profile_views.py index 4cec8a6c11..f64dcb5d9d 100644 --- a/src/open_inwoner/accounts/tests/test_profile_views.py +++ b/src/open_inwoner/accounts/tests/test_profile_views.py @@ -207,6 +207,7 @@ def test_info_eherkenning_user(self): housenumber="42", postcode="1234 XY", city="The good place", + kvk="87654321", ) response = self.app.get(self.url, user=user) diff --git a/src/open_inwoner/accounts/tests/test_user_constraints.py b/src/open_inwoner/accounts/tests/test_user_constraints.py index b50dd0f0c7..cb88f77092 100644 --- a/src/open_inwoner/accounts/tests/test_user_constraints.py +++ b/src/open_inwoner/accounts/tests/test_user_constraints.py @@ -1,3 +1,5 @@ +from unittest import skip + from django.db import IntegrityError, transaction from django.test import TestCase @@ -14,12 +16,11 @@ def test_unique_email_when_login_default(self): with self.assertRaises(IntegrityError): UserFactory(email="foo@example.com", login_type=LoginTypeChoices.default) - def test_not_unique_email_when_login_not_default(self): + def test_not_unique_email_when_login_digid_or_eherkenning(self): UserFactory(email="foo@example.com", login_type=LoginTypeChoices.default) UserFactory(email="foo@example.com", login_type=LoginTypeChoices.digid) UserFactory(email="foo@example.com", login_type=LoginTypeChoices.eherkenning) - UserFactory(email="foo@example.com", login_type=LoginTypeChoices.oidc) def test_unique_bsn_when_login_digid(self): UserFactory(bsn="123456782", login_type=LoginTypeChoices.digid) @@ -49,6 +50,7 @@ def test_unique_rsin_when_login_herkenning(self): with self.assertRaises(IntegrityError): UserFactory(rsin="12345678", login_type=LoginTypeChoices.eherkenning) + @skip("Not yet implemented") def test_not_both_kvk_and_rsin_when_login_eherkenning(self): with self.assertRaises(IntegrityError): UserFactory( diff --git a/src/open_inwoner/cms/cases/tests/test_contactform.py b/src/open_inwoner/cms/cases/tests/test_contactform.py index a97a37b372..314f51f3d6 100644 --- a/src/open_inwoner/cms/cases/tests/test_contactform.py +++ b/src/open_inwoner/cms/cases/tests/test_contactform.py @@ -13,6 +13,7 @@ ) from zgw_consumers.constants import APITypes +from open_inwoner.accounts.models import User from open_inwoner.accounts.tests.factories import ( DigidUserFactory, eHerkenningUserFactory, @@ -442,6 +443,9 @@ def test_form_success_with_api_eherkenning_user(self, m): with self.subTest( use_rsin_for_innNnpId_query_parameter=use_rsin_for_innNnpId_query_parameter ): + # FIXME for some reason creating the user outside of the loop + # makes the second iteration fail? + User.objects.filter(kvk="12345678").delete() eherkenning_user = eHerkenningUserFactory( kvk="12345678", rsin="000000000" ) @@ -460,7 +464,7 @@ def test_form_success_with_api_eherkenning_user(self, m): m.get( f"{KLANTEN_ROOT}klanten?subjectNietNatuurlijkPersoon__innNnpId={identifier}", json=paginated_response([self.klant]), - ), + ) response = self.app.get(self.case_detail_url, user=eherkenning_user) diff --git a/src/open_inwoner/haalcentraal/tests/test_signal.py b/src/open_inwoner/haalcentraal/tests/test_signal.py index df61235656..510f7e0a08 100644 --- a/src/open_inwoner/haalcentraal/tests/test_signal.py +++ b/src/open_inwoner/haalcentraal/tests/test_signal.py @@ -1,6 +1,7 @@ import logging -from django.test import TestCase, override_settings +from django.contrib.auth import user_logged_in +from django.test import RequestFactory, TestCase, override_settings from django.utils.translation import gettext as _ import requests_mock @@ -8,6 +9,7 @@ from timeline_logger.models import TimelineLog from open_inwoner.accounts.choices import LoginTypeChoices +from open_inwoner.accounts.models import User from open_inwoner.accounts.tests.factories import UserFactory from open_inwoner.utils.tests.helpers import AssertTimelineLogMixin @@ -106,8 +108,10 @@ def test_user_is_not_updated_when_not_logged_in_via_digid(self, m): user = UserFactory( first_name="", infix="", last_name="", login_type=LoginTypeChoices.default ) - user.bsn = "999993847" - user.save() + + request = RequestFactory().get("/dummy") + request.user = user + user_logged_in.send(User, user=user, request=request) user.refresh_from_db() diff --git a/src/open_inwoner/kvk/tests/test_signals.py b/src/open_inwoner/kvk/tests/test_signals.py index 74f48f9c77..57c505572e 100644 --- a/src/open_inwoner/kvk/tests/test_signals.py +++ b/src/open_inwoner/kvk/tests/test_signals.py @@ -1,7 +1,7 @@ import logging -from django.contrib.auth import get_user_model -from django.test import TestCase +from django.contrib.auth import get_user_model, user_logged_in +from django.test import RequestFactory, TestCase from django.utils.translation import gettext as _ import requests_mock @@ -9,6 +9,7 @@ from timeline_logger.models import TimelineLog from open_inwoner.accounts.choices import LoginTypeChoices +from open_inwoner.accounts.models import User from open_inwoner.accounts.tests.factories import UserFactory from open_inwoner.utils.logentry import LOG_ACTIONS from open_inwoner.utils.test import ClearCachesMixin @@ -53,8 +54,10 @@ def test_user_is_not_updated_when_not_logged_in_via_eherkenning(self, m): user = UserFactory( first_name="", infix="", last_name="", login_type=LoginTypeChoices.default ) - user.bsn = "69599084" - user.save() + + request = RequestFactory().get("/dummy") + request.user = user + user_logged_in.send(User, user=user, request=request) user.refresh_from_db() diff --git a/src/open_inwoner/openklant/tests/data.py b/src/open_inwoner/openklant/tests/data.py index fa65deb559..f79e3ee940 100644 --- a/src/open_inwoner/openklant/tests/data.py +++ b/src/open_inwoner/openklant/tests/data.py @@ -1,6 +1,7 @@ from requests.exceptions import RequestException from zgw_consumers.constants import APITypes +from open_inwoner.accounts.models import User from open_inwoner.accounts.tests.factories import ( DigidUserFactory, eHerkenningUserFactory, @@ -38,10 +39,13 @@ def setUpServices(cls): class MockAPIReadPatchData(MockAPIData): def __init__(self): + User.objects.filter(email="old@example.com").delete() self.user = DigidUserFactory( email="old@example.com", phonenumber="0100000000", ) + + User.objects.filter(kvk="12345678").delete() self.eherkenning_user = eHerkenningUserFactory( email="old2@example.com", kvk="12345678", @@ -103,9 +107,11 @@ def install_mocks_eherkenning(self, m, use_rsin=True) -> "MockAPIReadPatchData": class MockAPIReadData(MockAPIData): def __init__(self): + User.objects.filter(bsn="100000001").delete() self.user = DigidUserFactory( bsn="100000001", ) + User.objects.filter(kvk="12345678").delete() self.eherkenning_user = eHerkenningUserFactory( kvk="12345678", rsin="000000000", @@ -304,9 +310,11 @@ def install_mocks(self, m, link_objectcontactmomenten=False) -> "MockAPIReadData class MockAPICreateData(MockAPIData): def __init__(self): + User.objects.filter(bsn="100000001").delete() self.user = DigidUserFactory( bsn="100000001", ) + User.objects.filter(kvk="12345678").delete() self.eherkenning_user = eHerkenningUserFactory( kvk="12345678", rsin="000000000", diff --git a/src/open_inwoner/pdc/tests/test_views.py b/src/open_inwoner/pdc/tests/test_views.py index 9e9382fd7f..64bd364f30 100644 --- a/src/open_inwoner/pdc/tests/test_views.py +++ b/src/open_inwoner/pdc/tests/test_views.py @@ -202,8 +202,7 @@ def test_category_detail_view_access_restricted_for_digid_user(self): description="A descriptive description", visible_for_citizens=False, ) - user = DigidUserFactory() - self.client.force_login(user) + self.client.force_login(self.user) url = reverse("products:category_detail", kwargs={"slug": category.slug})