|
| 1 | +import json |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from django.test import override_settings |
1 | 5 | from django.urls import reverse
|
2 | 6 | from django.utils.translation import gettext_lazy as _
|
3 | 7 |
|
| 8 | +import freezegun |
4 | 9 | from django_webtest import WebTest
|
5 | 10 | from maykin_2fa.test import disable_admin_mfa
|
| 11 | +from privates.storages import PrivateMediaFileSystemStorage |
| 12 | +from privates.test import temp_private_root |
| 13 | +from webtest import Upload |
6 | 14 |
|
7 | 15 | from open_inwoner.accounts.tests.factories import UserFactory
|
8 | 16 |
|
9 |
| -from .factories import ZaakTypeConfigFactory, ZaakTypeInformatieObjectTypeConfigFactory |
| 17 | +from .factories import ( |
| 18 | + CatalogusConfigFactory, |
| 19 | + ServiceFactory, |
| 20 | + ZaakTypeConfigFactory, |
| 21 | + ZaakTypeInformatieObjectTypeConfigFactory, |
| 22 | +) |
10 | 23 |
|
11 | 24 |
|
12 | 25 | @disable_admin_mfa()
|
@@ -109,3 +122,136 @@ def test_both_can_be_disabled(self):
|
109 | 122 | )
|
110 | 123 | self.assertFalse(self.ztc.document_upload_enabled)
|
111 | 124 | self.assertFalse(self.ztiotc.document_upload_enabled)
|
| 125 | + |
| 126 | + |
| 127 | +@disable_admin_mfa() |
| 128 | +@temp_private_root() |
| 129 | +@freezegun.freeze_time("2024-08-14 17:50:01") |
| 130 | +@override_settings(CELERY_TASK_ALWAYS_EAGER=True) |
| 131 | +class TestCatalogusConfigExportAdmin(WebTest): |
| 132 | + csrf_checks = False |
| 133 | + |
| 134 | + def setUp(self): |
| 135 | + self.user = UserFactory(is_superuser=True, is_staff=True) |
| 136 | + self.service = ServiceFactory(slug="service-1") |
| 137 | + self.catalogus = CatalogusConfigFactory( |
| 138 | + url="https://foo.maykinmedia.nl", |
| 139 | + domein="DM", |
| 140 | + rsin="123456789", |
| 141 | + service=self.service, |
| 142 | + ) |
| 143 | + self.mock_file = Upload("dump.json", b"foobar", "application/json") |
| 144 | + |
| 145 | + def test_export_action_returns_correct_export(self): |
| 146 | + response = self.app.post( |
| 147 | + reverse( |
| 148 | + "admin:openzaak_catalogusconfig_changelist", |
| 149 | + ), |
| 150 | + { |
| 151 | + "action": "export_catalogus_configs", |
| 152 | + "_selected_action": [self.catalogus.id], |
| 153 | + }, |
| 154 | + user=self.user, |
| 155 | + ) |
| 156 | + |
| 157 | + self.assertEqual( |
| 158 | + response.content_disposition, |
| 159 | + 'attachment; filename="zgw-catalogi-export.json"', |
| 160 | + ) |
| 161 | + self.assertEqual(response.status_code, 200) |
| 162 | + self.assertEqual( |
| 163 | + [json.loads(row) for row in response.text.split("\n")[:-1]], |
| 164 | + [ |
| 165 | + { |
| 166 | + "model": "openzaak.catalogusconfig", |
| 167 | + "fields": { |
| 168 | + "url": "https://foo.maykinmedia.nl", |
| 169 | + "domein": "DM", |
| 170 | + "rsin": "123456789", |
| 171 | + "service": ["service-1"], |
| 172 | + }, |
| 173 | + }, |
| 174 | + ], |
| 175 | + msg="Response should be valid JSONL matching the input object", |
| 176 | + ) |
| 177 | + self.assertFalse( |
| 178 | + PrivateMediaFileSystemStorage().exists( |
| 179 | + "zgw_import_dump_2024-08-14-17-50-01.jsonl" |
| 180 | + ), |
| 181 | + msg="File should always deleted regardless of success or failure", |
| 182 | + ) |
| 183 | + |
| 184 | + @mock.patch( |
| 185 | + "open_inwoner.openzaak.import_export.CatalogusConfigImport.import_from_jsonl_file_in_django_storage" |
| 186 | + ) |
| 187 | + def test_import_flow_reports_success(self, m) -> None: |
| 188 | + form = self.app.get( |
| 189 | + reverse( |
| 190 | + "admin:upload_zgw_import_file", |
| 191 | + ), |
| 192 | + user=self.user, |
| 193 | + ).form |
| 194 | + form["zgw_export_file"] = self.mock_file |
| 195 | + |
| 196 | + response = form.submit().follow() |
| 197 | + |
| 198 | + self.assertEqual(m.call_count, 1) |
| 199 | + self.assertEqual(m.call_args[0][0], "zgw_import_dump_2024-08-14-17-50-01.jsonl") |
| 200 | + self.assertTrue(isinstance(m.call_args[0][1], PrivateMediaFileSystemStorage)) |
| 201 | + |
| 202 | + messages = [str(msg) for msg in response.context["messages"]] |
| 203 | + self.assertEqual( |
| 204 | + messages, |
| 205 | + ["Successfully processed 1 items"], |
| 206 | + ) |
| 207 | + self.assertFalse( |
| 208 | + PrivateMediaFileSystemStorage().exists( |
| 209 | + "zgw_import_dump_2024-08-14-17-50-01.jsonl" |
| 210 | + ), |
| 211 | + msg="File should always deleted regardless of success or failure", |
| 212 | + ) |
| 213 | + self.assertEqual( |
| 214 | + response.request.path, |
| 215 | + reverse( |
| 216 | + "admin:openzaak_catalogusconfig_changelist", |
| 217 | + ), |
| 218 | + ) |
| 219 | + |
| 220 | + @mock.patch( |
| 221 | + "open_inwoner.openzaak.import_export.CatalogusConfigImport.import_from_jsonl_file_in_django_storage" |
| 222 | + ) |
| 223 | + def test_import_flow_errors_reports_failure_to_user(self, m) -> None: |
| 224 | + m.side_effect = Exception("something went wrong") |
| 225 | + form = self.app.get( |
| 226 | + reverse( |
| 227 | + "admin:upload_zgw_import_file", |
| 228 | + ), |
| 229 | + user=self.user, |
| 230 | + ).form |
| 231 | + form["zgw_export_file"] = self.mock_file |
| 232 | + |
| 233 | + response = form.submit() |
| 234 | + |
| 235 | + self.assertEqual(m.call_count, 1) |
| 236 | + self.assertEqual(m.call_args[0][0], "zgw_import_dump_2024-08-14-17-50-01.jsonl") |
| 237 | + self.assertTrue(isinstance(m.call_args[0][1], PrivateMediaFileSystemStorage)) |
| 238 | + |
| 239 | + messages = [str(msg) for msg in response.context["messages"]] |
| 240 | + self.assertEqual( |
| 241 | + messages, |
| 242 | + [ |
| 243 | + "We were unable to process your upload. Please regenerate the file and try again." |
| 244 | + ], |
| 245 | + ) |
| 246 | + self.assertFalse( |
| 247 | + PrivateMediaFileSystemStorage().exists( |
| 248 | + "zgw_import_dump_2024-08-14-17-50-01.jsonl" |
| 249 | + ), |
| 250 | + msg="File should always deleted regardless of success or failure", |
| 251 | + ) |
| 252 | + self.assertEqual( |
| 253 | + response.request.path, |
| 254 | + reverse( |
| 255 | + "admin:upload_zgw_import_file", |
| 256 | + ), |
| 257 | + ) |
0 commit comments