Skip to content

Commit

Permalink
✅ [#2041] Fix tests for user DB constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Mar 15, 2024
1 parent 89f4ac2 commit 83e4b1f
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/open_inwoner/accounts/tests/test_contact_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]", bsn="111111111")
existing_user = DigidUserFactory(email="[email protected]", bsn="111111111")
response = self.app.get(self.create_url, user=self.user)
form = response.forms["contact-form"]

Expand Down
1 change: 1 addition & 0 deletions src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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)

Expand Down
6 changes: 4 additions & 2 deletions src/open_inwoner/accounts/tests/test_user_constraints.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import skip

from django.db import IntegrityError, transaction
from django.test import TestCase

Expand All @@ -14,12 +16,11 @@ def test_unique_email_when_login_default(self):
with self.assertRaises(IntegrityError):
UserFactory(email="[email protected]", 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="[email protected]", login_type=LoginTypeChoices.default)

UserFactory(email="[email protected]", login_type=LoginTypeChoices.digid)
UserFactory(email="[email protected]", login_type=LoginTypeChoices.eherkenning)
UserFactory(email="[email protected]", login_type=LoginTypeChoices.oidc)

def test_unique_bsn_when_login_digid(self):
UserFactory(bsn="123456782", login_type=LoginTypeChoices.digid)
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 5 additions & 1 deletion src/open_inwoner/cms/cases/tests/test_contactform.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
)
from zgw_consumers.constants import APITypes

from open_inwoner.accounts.models import User
from open_inwoner.accounts.tests.factories import (
DigidUserFactory,
eHerkenningUserFactory,
Expand Down Expand Up @@ -462,6 +463,9 @@ def test_form_success_with_api_eherkenning_user(self, m, contactmoment_mock):
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"
)
Expand All @@ -480,7 +484,7 @@ def test_form_success_with_api_eherkenning_user(self, m, contactmoment_mock):
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)

Expand Down
10 changes: 7 additions & 3 deletions src/open_inwoner/haalcentraal/tests/test_signal.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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
from freezegun import freeze_time
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

Expand Down Expand Up @@ -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()

Expand Down
11 changes: 7 additions & 4 deletions src/open_inwoner/kvk/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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
from freezegun import freeze_time
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
Expand Down Expand Up @@ -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()

Expand Down
8 changes: 8 additions & 0 deletions src/open_inwoner/openklant/tests/data.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -38,10 +39,13 @@ def setUpServices(cls):

class MockAPIReadPatchData(MockAPIData):
def __init__(self):
User.objects.filter(email="[email protected]").delete()
self.user = DigidUserFactory(
email="[email protected]",
phonenumber="0100000000",
)

User.objects.filter(kvk="12345678").delete()
self.eherkenning_user = eHerkenningUserFactory(
email="[email protected]",
kvk="12345678",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions src/open_inwoner/pdc/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ def test_category_detail_view_access_restricted_for_digid_user(self):
description="A <em>descriptive</em> 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})

Expand Down

0 comments on commit 83e4b1f

Please sign in to comment.