|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +from django.test import Client, TestCase |
| 4 | +from django.urls import reverse |
| 5 | + |
| 6 | +from pydantic import ValidationError |
| 7 | + |
| 8 | +from open_inwoner.accounts.tests.factories import UserFactory |
| 9 | +from open_inwoner.haalcentraal.api_models import BRP2xPersoon |
| 10 | +from open_inwoner.haalcentraal.clients import BRPClient |
| 11 | +from open_inwoner.haalcentraal.config_checks.fetch_brp import ( |
| 12 | + FetchBRPCheck, |
| 13 | + FetchBRPForm, |
| 14 | +) |
| 15 | +from open_inwoner.haalcentraal.exceptions import BRPAPINetworkError |
| 16 | +from open_inwoner.haalcentraal.models import HaalCentraalConfig |
| 17 | + |
| 18 | + |
| 19 | +def make_validation_error() -> ValidationError: |
| 20 | + try: |
| 21 | + BRP2xPersoon.model_validate( |
| 22 | + {"verblijfplaats": {"verblijfadres": {"huisnummer": "not-a-number"}}} |
| 23 | + ) |
| 24 | + except ValidationError as exc: |
| 25 | + return exc |
| 26 | + raise AssertionError("expected a ValidationError") # pragma: no cover |
| 27 | + |
| 28 | + |
| 29 | +class FetchBRPCheckTests(TestCase): |
| 30 | + def setUp(self): |
| 31 | + self.check = FetchBRPCheck() |
| 32 | + |
| 33 | + def test_invalid_bsn(self): |
| 34 | + form = FetchBRPForm(data={"bsn": "123"}) |
| 35 | + self.assertTrue(form.is_valid()) |
| 36 | + |
| 37 | + result = self.check.run(form.cleaned_data) |
| 38 | + |
| 39 | + self.assertFalse(result.success) |
| 40 | + self.assertIn("Ongeldig BSN", result.message) |
| 41 | + |
| 42 | + def test_not_configured(self): |
| 43 | + form = FetchBRPForm(data={"bsn": "123456789"}) |
| 44 | + self.assertTrue(form.is_valid()) |
| 45 | + |
| 46 | + result = self.check.run(form.cleaned_data) |
| 47 | + |
| 48 | + self.assertFalse(result.success) |
| 49 | + self.assertIn("not configured", result.message) |
| 50 | + |
| 51 | + def test_network_error(self): |
| 52 | + form = FetchBRPForm(data={"bsn": "123456789"}) |
| 53 | + self.assertTrue(form.is_valid()) |
| 54 | + |
| 55 | + mock_client = MagicMock() |
| 56 | + mock_client.fetch_brp_data_for_bsn.side_effect = BRPAPINetworkError( |
| 57 | + "Connection failed" |
| 58 | + ) |
| 59 | + |
| 60 | + with patch.object(BRPClient, "from_config", return_value=mock_client): |
| 61 | + result = self.check.run(form.cleaned_data) |
| 62 | + |
| 63 | + self.assertFalse(result.success) |
| 64 | + self.assertIn("Failed to connect", result.message) |
| 65 | + |
| 66 | + def test_validation_error(self): |
| 67 | + form = FetchBRPForm(data={"bsn": "123456789"}) |
| 68 | + self.assertTrue(form.is_valid()) |
| 69 | + |
| 70 | + mock_client = MagicMock() |
| 71 | + mock_client.fetch_brp_data_for_bsn.side_effect = make_validation_error() |
| 72 | + |
| 73 | + with patch.object(BRPClient, "from_config", return_value=mock_client): |
| 74 | + result = self.check.run(form.cleaned_data) |
| 75 | + |
| 76 | + self.assertFalse(result.success) |
| 77 | + self.assertIn("failed validation", result.message) |
| 78 | + |
| 79 | + def test_no_person_found(self): |
| 80 | + form = FetchBRPForm(data={"bsn": "123456789"}) |
| 81 | + self.assertTrue(form.is_valid()) |
| 82 | + |
| 83 | + mock_client = MagicMock() |
| 84 | + mock_client.fetch_brp_data_for_bsn.return_value = None |
| 85 | + mock_client.version = "2.1" |
| 86 | + |
| 87 | + with patch.object(BRPClient, "from_config", return_value=mock_client): |
| 88 | + result = self.check.run(form.cleaned_data) |
| 89 | + |
| 90 | + self.assertFalse(result.success) |
| 91 | + self.assertIn("No person found", result.message) |
| 92 | + |
| 93 | + def test_person_found(self): |
| 94 | + form = FetchBRPForm(data={"bsn": "123456789"}) |
| 95 | + self.assertTrue(form.is_valid()) |
| 96 | + |
| 97 | + mock_client = MagicMock() |
| 98 | + mock_client.fetch_brp_data_for_bsn.return_value = BRP2xPersoon() |
| 99 | + mock_client.version = "2.1" |
| 100 | + |
| 101 | + with patch.object(BRPClient, "from_config", return_value=mock_client): |
| 102 | + result = self.check.run(form.cleaned_data) |
| 103 | + |
| 104 | + self.assertTrue(result.success) |
| 105 | + self.assertIn("BRP data retrieved", result.message) |
| 106 | + self.assertEqual(result.extra["version"], "2.1") |
| 107 | + |
| 108 | + |
| 109 | +class FetchBRPViewTests(TestCase): |
| 110 | + def setUp(self): |
| 111 | + self.client = Client() |
| 112 | + self.superuser = UserFactory(is_superuser=True, is_staff=True) |
| 113 | + self.user = UserFactory() |
| 114 | + self.config = HaalCentraalConfig.get_solo() |
| 115 | + |
| 116 | + def get_url(self): |
| 117 | + return reverse( |
| 118 | + "run_config_check", |
| 119 | + args=["haalcentraal", "haalcentraalconfig", self.config.pk, "fetch_brp"], |
| 120 | + ) |
| 121 | + |
| 122 | + def test_permission_denied_for_normal_user(self): |
| 123 | + self.client.force_login(self.user) |
| 124 | + |
| 125 | + response = self.client.get(self.get_url()) |
| 126 | + |
| 127 | + self.assertEqual(response.status_code, 403) |
| 128 | + |
| 129 | + def test_superuser_can_access(self): |
| 130 | + self.client.force_login(self.superuser) |
| 131 | + |
| 132 | + response = self.client.get(self.get_url()) |
| 133 | + |
| 134 | + self.assertEqual(response.status_code, 200) |
| 135 | + |
| 136 | + def test_post_runs_check(self): |
| 137 | + self.client.force_login(self.superuser) |
| 138 | + |
| 139 | + response = self.client.post(self.get_url(), {"bsn": "123456789"}) |
| 140 | + |
| 141 | + self.assertEqual(response.status_code, 200) |
| 142 | + self.assertContains(response, "not configured") |
| 143 | + |
| 144 | + |
| 145 | +class FetchBRPStandaloneTests(TestCase): |
| 146 | + def setUp(self): |
| 147 | + self.client = Client() |
| 148 | + self.superuser = UserFactory(is_superuser=True, is_staff=True) |
| 149 | + |
| 150 | + def test_standalone_runs_check(self): |
| 151 | + self.client.force_login(self.superuser) |
| 152 | + |
| 153 | + url = reverse("run_config_check_standalone", args=["fetch_brp"]) |
| 154 | + |
| 155 | + response = self.client.post(url, {"bsn": "123456789"}) |
| 156 | + |
| 157 | + self.assertEqual(response.status_code, 200) |
| 158 | + self.assertContains(response, "not configured") |
0 commit comments