|
1 | 1 | import os |
2 | 2 | import tempfile |
| 3 | +from unittest import mock |
3 | 4 |
|
4 | 5 | from django.conf import settings |
5 | 6 | from django.contrib.auth.models import Group, Permission |
6 | 7 | from django.core import management |
7 | 8 |
|
8 | 9 | from api.factories.country import CountryFactory |
9 | 10 | from api.factories.disaster_type import DisasterTypeFactory |
| 11 | +from api.models import Export |
10 | 12 | from deployments.factories.user import UserFactory |
11 | 13 | from eap.factories import ( |
12 | 14 | EAPRegistrationFactory, |
@@ -1151,3 +1153,64 @@ def test_status_transition(self): |
1151 | 1153 | # Check is the activated timeline is added |
1152 | 1154 | self.eap_registration.refresh_from_db() |
1153 | 1155 | self.assertIsNotNone(self.eap_registration.activated_at) |
| 1156 | + |
| 1157 | + |
| 1158 | +class TestSimplifiedEapPdfExport(APITestCase): |
| 1159 | + def setUp(self): |
| 1160 | + super().setUp() |
| 1161 | + self.country = CountryFactory.create(name="country1", iso3="XXX") |
| 1162 | + self.national_society = CountryFactory.create(name="national_society1", iso3="YYY") |
| 1163 | + self.disaster_type = DisasterTypeFactory.create(name="disaster1") |
| 1164 | + self.partner1 = CountryFactory.create(name="partner1", iso3="ZZZ") |
| 1165 | + self.partner2 = CountryFactory.create(name="partner2", iso3="AAA") |
| 1166 | + |
| 1167 | + self.user = UserFactory.create() |
| 1168 | + |
| 1169 | + self.eap_registration = EAPRegistrationFactory.create( |
| 1170 | + eap_type=EAPType.SIMPLIFIED_EAP, |
| 1171 | + country=self.country, |
| 1172 | + national_society=self.national_society, |
| 1173 | + disaster_type=self.disaster_type, |
| 1174 | + partners=[self.partner1.id, self.partner2.id], |
| 1175 | + created_by=self.user, |
| 1176 | + modified_by=self.user, |
| 1177 | + ) |
| 1178 | + |
| 1179 | + self.simplified_eap = SimplifiedEAPFactory.create( |
| 1180 | + eap_registration=self.eap_registration, |
| 1181 | + created_by=self.user, |
| 1182 | + modified_by=self.user, |
| 1183 | + national_society_contact_title="NS Title Example", |
| 1184 | + ) |
| 1185 | + self.url = "/api/v2/pdf-export/" |
| 1186 | + |
| 1187 | + @mock.patch("api.serializers.generate_url.delay") |
| 1188 | + def test_create_simplified_eap_export(self, mock_generate_url): |
| 1189 | + data = { |
| 1190 | + "export_type": Export.ExportType.SIMPLIFIED_EAP, |
| 1191 | + "export_id": self.simplified_eap.id, |
| 1192 | + "is_pga": False, |
| 1193 | + } |
| 1194 | + |
| 1195 | + self.authenticate(self.user) |
| 1196 | + |
| 1197 | + with self.capture_on_commit_callbacks(execute=True): |
| 1198 | + response = self.client.post(self.url, data, format="json") |
| 1199 | + self.assert_201(response) |
| 1200 | + export = Export.objects.first() |
| 1201 | + self.assertIsNotNone(export) |
| 1202 | + |
| 1203 | + expected_url = ( |
| 1204 | + f"{settings.GO_WEB_INTERNAL_URL}/" f"{Export.ExportType.SIMPLIFIED_EAP}/" f"{self.simplified_eap.id}/export/" |
| 1205 | + ) |
| 1206 | + self.assertEqual(export.url, expected_url) |
| 1207 | + self.assertEqual(response.data["status"], Export.ExportStatus.PENDING) |
| 1208 | + |
| 1209 | + self.assertEqual(mock_generate_url.called, True) |
| 1210 | + title = f"{self.national_society.name}-{self.disaster_type.name}" |
| 1211 | + mock_generate_url.assert_called_once_with( |
| 1212 | + export.url, |
| 1213 | + export.id, |
| 1214 | + self.user.id, |
| 1215 | + title, |
| 1216 | + ) |
0 commit comments