diff --git a/api/applications/tests/test_consignee.py b/api/applications/tests/test_consignee.py index b70bfd4d90..497372135b 100644 --- a/api/applications/tests/test_consignee.py +++ b/api/applications/tests/test_consignee.py @@ -11,6 +11,7 @@ from api.parties.models import PartyDocument from api.staticdata.countries.helpers import get_country from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class ConsigneeOnDraftTests(DataTestClient): @@ -18,11 +19,13 @@ def setUp(self): super().setUp() self.draft = self.create_draft_standard_application(self.organisation) self.url = reverse("applications:parties", kwargs={"pk": self.draft.id}) + s3_key = "s3_keykey.pdf" self.new_document_data = { "name": "document_name.pdf", - "s3_key": "s3_keykey.pdf", + "s3_key": s3_key, "size": 123456, } + upload_file(s3_key) @parameterized.expand([SubType.GOVERNMENT, SubType.COMMERCIAL, SubType.OTHER]) def test_set_consignee_on_draft_successful(self, data_type): @@ -174,9 +177,8 @@ def test_delete_consignee_on_standard_application_when_application_has_no_consig self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_consignee_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_post_consignee_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains a consignee @@ -184,7 +186,6 @@ def test_post_consignee_document_success(self, mock_virus_scan, mock_s3_operatio When a document is submitted Then a 201 CREATED is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False party = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.CONSIGNEE, deleted_at__isnull=True @@ -196,9 +197,8 @@ def test_post_consignee_document_success(self, mock_virus_scan, mock_s3_operatio self.assertEqual(response.status_code, status.HTTP_201_CREATED) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_get_consignee_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_get_consignee_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains a consignee @@ -206,7 +206,6 @@ def test_get_consignee_document_success(self, mock_virus_scan, mock_s3_operation When the document is retrieved Then the data in the document is the same as the data in the attached consignee document """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False party = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.CONSIGNEE, deleted_at__isnull=True @@ -221,12 +220,9 @@ def test_get_consignee_document_success(self, mock_virus_scan, mock_s3_operation self.assertEqual(response_data["s3_key"], expected["s3_key"]) self.assertEqual(response_data["size"], expected["size"]) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_consignee_document_success( - self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object - ): + def test_delete_consignee_document_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains an end user @@ -234,7 +230,6 @@ def test_delete_consignee_document_success( When there is an attempt to delete the document Then 204 NO CONTENT is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False party = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.CONSIGNEE, deleted_at__isnull=True @@ -246,10 +241,9 @@ def test_delete_consignee_document_success( self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) delete_s3_function.assert_called_once() - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_consignee_success(self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object): + def test_delete_consignee_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains a consignee user @@ -257,7 +251,6 @@ def test_delete_consignee_success(self, delete_s3_function, mock_virus_scan, moc When there is an attempt to delete the consignee Then 200 OK is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False consignee = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.CONSIGNEE, deleted_at__isnull=True diff --git a/api/applications/tests/test_documents.py b/api/applications/tests/test_documents.py index d604fb729e..909a041e00 100644 --- a/api/applications/tests/test_documents.py +++ b/api/applications/tests/test_documents.py @@ -5,21 +5,22 @@ from api.audit_trail.enums import AuditType from api.audit_trail.models import Audit from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class ApplicationDocumentViewTests(DataTestClient): - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.libraries.s3_operations.upload_bytes_file") - def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan, mock_s3_operations_get_object): + def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan): mock_virus_scan.return_value = False application = self.create_draft_standard_application(organisation=self.organisation, user=self.exporter_user) url = reverse("applications:application_documents", kwargs={"pk": application.pk}) + s3_key = "section5_20210223145814.png" data = { "name": "section5.png", - "s3_key": "section5_20210223145814.png", + "s3_key": s3_key, "size": 1, "document_on_organisation": { "expiry_date": "2222-01-01", @@ -27,7 +28,7 @@ def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan, mock_s3_op "document_type": "section-five-certificate", }, } - mock_s3_operations_get_object.return_value = data + upload_file(s3_key) response = self.client.post(url, data, **self.exporter_headers) diff --git a/api/applications/tests/test_end_user.py b/api/applications/tests/test_end_user.py index 219298ed38..63db0af55b 100644 --- a/api/applications/tests/test_end_user.py +++ b/api/applications/tests/test_end_user.py @@ -12,6 +12,7 @@ from api.parties.models import PartyDocument from api.staticdata.countries.helpers import get_country from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class EndUserOnDraftTests(DataTestClient): @@ -33,11 +34,13 @@ def setUp(self): "applications:party_document", kwargs={"pk": self.draft.id, "party_pk": self.draft.end_user.party.id} ) + s3_key = "s3_keykey.pdf" self.new_document_data = { "name": "updated_document_name.pdf", - "s3_key": "s3_keykey.pdf", + "s3_key": s3_key, "size": 123456, } + upload_file(s3_key) @parameterized.expand([SubType.GOVERNMENT, SubType.COMMERCIAL, SubType.OTHER]) def test_set_end_user_on_draft_standard_application_successful(self, data_type): @@ -174,9 +177,8 @@ def test_delete_end_user_on_standard_application_when_application_has_no_end_use self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_get_end_user_document_successful(self, mock_virus_scan, mock_s3_operations_get_object): + def test_get_end_user_document_successful(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains an end user @@ -184,7 +186,6 @@ def test_get_end_user_document_successful(self, mock_virus_scan, mock_s3_operati When the document is retrieved Then the data in the document is the same as the data in the attached end user document """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.get(self.document_url, **self.exporter_headers) response_data = response.json()["document"] @@ -210,16 +211,14 @@ def test_get_document_when_no_end_user_exists_failure(self): self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_document_when_no_end_user_exists_failure(self, mock_virus_scan, mock_s3_operations_get_object): + def test_post_document_when_no_end_user_exists_failure(self, mock_virus_scan): """ Given a standard draft has been created And the draft does not contain an end user When there is an attempt to submit a document Then a 400 BAD REQUEST is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.END_USER, deleted_at__isnull=True @@ -263,9 +262,8 @@ def test_get_end_user_document_when_document_does_not_exist_failure(self): self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code) self.assertEqual(None, response.json()["document"]) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_end_user_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_post_end_user_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains an end user @@ -273,7 +271,6 @@ def test_post_end_user_document_success(self, mock_virus_scan, mock_s3_operation When a document is submitted Then a 201 CREATED is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False party = PartyOnApplication.objects.get( application=self.draft, deleted_at__isnull=True, party__type=PartyType.END_USER @@ -285,11 +282,8 @@ def test_post_end_user_document_success(self, mock_virus_scan, mock_s3_operation self.assertEqual(response.status_code, status.HTTP_201_CREATED) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_end_user_document_when_a_document_already_exists_success( - self, mock_virus_scan, mock_s3_operations_get_object - ): + def test_post_end_user_document_when_a_document_already_exists_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains an end user @@ -297,7 +291,6 @@ def test_post_end_user_document_when_a_document_already_exists_success( When there is an attempt to post a document Then a 400 BAD REQUEST is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False end_user = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.END_USER, deleted_at__isnull=True @@ -311,10 +304,9 @@ def test_post_end_user_document_when_a_document_already_exists_success( self.assertEqual(party_documents.count(), 1) self.assertEqual(party_documents.first().name, "updated_document_name.pdf") - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_end_user_document_success(self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object): + def test_delete_end_user_document_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains an end user @@ -322,17 +314,15 @@ def test_delete_end_user_document_success(self, delete_s3_function, mock_virus_s When there is an attempt to delete the document Then 204 NO CONTENT is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.delete(self.document_url, **self.exporter_headers) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) delete_s3_function.assert_called_once() - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_end_user_success(self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object): + def test_delete_end_user_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains an end user @@ -340,7 +330,6 @@ def test_delete_end_user_success(self, delete_s3_function, mock_virus_scan, mock When there is an attempt to delete the end user Then 204 NO CONTENT is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False end_user = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.END_USER, deleted_at__isnull=True diff --git a/api/applications/tests/test_goods.py b/api/applications/tests/test_goods.py index 4a6daa537e..3130107120 100644 --- a/api/applications/tests/test_goods.py +++ b/api/applications/tests/test_goods.py @@ -5,13 +5,13 @@ from api.audit_trail.models import Audit from api.goods.tests.factories import GoodFactory from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class ApplicationGoodOnApplicationDocumentViewTests(DataTestClient): - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.libraries.s3_operations.upload_bytes_file") - def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan, mock_s3_operations_get_object): + def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan): mock_virus_scan.return_value = False application = self.create_draft_standard_application(organisation=self.organisation, user=self.exporter_user) good = GoodFactory(organisation=self.organisation) @@ -24,9 +24,10 @@ def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan, mock_s3_op }, ) + s3_key = "section5_20210223145814.png" data = { "name": "section5.png", - "s3_key": "section5_20210223145814.png", + "s3_key": s3_key, "size": 1, "document_on_organisation": { "expiry_date": "2222-01-01", @@ -34,7 +35,7 @@ def test_audit_trail_create(self, upload_bytes_func, mock_virus_scan, mock_s3_op "document_type": "section-five-certificate", }, } - mock_s3_operations_get_object.return_value = data + upload_file(s3_key) response = self.client.post(url, data, **self.exporter_headers) self.assertEqual(response.status_code, 201, response.json()) diff --git a/api/applications/tests/test_goods_type_on_application.py b/api/applications/tests/test_goods_type_on_application.py index 8c89e7ef30..276b581cd8 100644 --- a/api/applications/tests/test_goods_type_on_application.py +++ b/api/applications/tests/test_goods_type_on_application.py @@ -8,6 +8,7 @@ from api.staticdata.control_list_entries.helpers import get_control_list_entry from api.staticdata.control_list_entries.models import ControlListEntry from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class GoodsTypeOnApplicationTests(DataTestClient): @@ -33,11 +34,13 @@ def setUp(self): "goods_type_pk": GoodsType.objects.get(application=self.hmrc_query).id, }, ) + s3_key = "s3_keykey.pdf" self.new_document_data = { "name": "document_name.pdf", - "s3_key": "s3_keykey.pdf", + "s3_key": s3_key, "size": 123456, } + upload_file(s3_key) def test_create_goodstype_on_open_application_as_exporter_user_success(self): response = self.client.post(self.url, self.data, **self.exporter_headers) @@ -109,9 +112,8 @@ def test_remove_goodstype_from_open_application_as_exporter_user_success(self): self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(GoodsType.objects.all().count(), initial_goods_types_count - 1) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_goods_type_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_post_goods_type_document_success(self, mock_virus_scan): """ Given a draft HMRC query has been created And the draft contains a goods type @@ -119,7 +121,6 @@ def test_post_goods_type_document_success(self, mock_virus_scan, mock_s3_operati When a document is submitted Then a 201 CREATED is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False GoodsTypeDocument.objects.get(goods_type__application=self.hmrc_query).delete() count = GoodsTypeDocument.objects.count() @@ -129,9 +130,8 @@ def test_post_goods_type_document_success(self, mock_virus_scan, mock_s3_operati self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(count + 1, GoodsTypeDocument.objects.count()) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_get_goods_type_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_get_goods_type_document_success(self, mock_virus_scan): """ Given a draft HMRC query has been created And the draft contains a goods type @@ -139,7 +139,6 @@ def test_get_goods_type_document_success(self, mock_virus_scan, mock_s3_operatio When the document is retrieved Then the data in the document is the same as the data in the attached goods party document """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.get(self.document_url, **self.hmrc_exporter_headers) response_data = response.json()["document"] @@ -148,12 +147,9 @@ def test_get_goods_type_document_success(self, mock_virus_scan, mock_s3_operatio self.assertEqual(response_data["s3_key"], self.new_document_data["s3_key"]) self.assertEqual(response_data["size"], self.new_document_data["size"]) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_goods_type_document_success( - self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object - ): + def test_delete_goods_type_document_success(self, delete_s3_function, mock_virus_scan): """ Given a draft HMRC query has been created And the draft contains a goods type @@ -161,17 +157,15 @@ def test_delete_goods_type_document_success( When there is an attempt to delete the document Then 204 NO CONTENT is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.delete(self.document_url, **self.hmrc_exporter_headers) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) delete_s3_function.assert_called_once() - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_goods_type_success(self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object): + def test_delete_goods_type_success(self, delete_s3_function, mock_virus_scan): """ Given a draft HMRC query has been created And the draft contains a goods type @@ -179,7 +173,6 @@ def test_delete_goods_type_success(self, delete_s3_function, mock_virus_scan, mo When there is an attempt to delete goods type Then 200 OK is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False url = reverse( "applications:application_goodstype", diff --git a/api/applications/tests/test_third_parties.py b/api/applications/tests/test_third_parties.py index 484c609940..af2bed3d15 100644 --- a/api/applications/tests/test_third_parties.py +++ b/api/applications/tests/test_third_parties.py @@ -11,6 +11,7 @@ from api.parties.enums import PartyType, PartyRole, SubType from api.parties.models import Party, PartyDocument from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class ThirdPartiesOnDraft(DataTestClient): @@ -24,11 +25,13 @@ def setUp(self): "applications:party_document", kwargs={"pk": self.draft.id, "party_pk": self.draft.third_parties.first().party.id}, ) + s3_key = "s3_keykey.pdf" self.new_document_data = { "name": "document_name.pdf", - "s3_key": "s3_keykey.pdf", + "s3_key": s3_key, "size": 123456, } + upload_file(s3_key) @pytest.mark.only def test_set_multiple_third_parties_on_draft_successful(self): @@ -170,9 +173,8 @@ def test_delete_third_party_on_standard_application_when_application_has_no_thir self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_third_party_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_post_third_party_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains a third party @@ -180,7 +182,6 @@ def test_post_third_party_document_success(self, mock_virus_scan, mock_s3_operat When a document is submitted Then a 201 CREATED is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False PartyDocument.objects.filter(party=self.draft.third_parties.all().first().party).delete() @@ -188,9 +189,8 @@ def test_post_third_party_document_success(self, mock_virus_scan, mock_s3_operat self.assertEqual(response.status_code, status.HTTP_201_CREATED) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_get_third_party_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_get_third_party_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains a third party @@ -198,7 +198,6 @@ def test_get_third_party_document_success(self, mock_virus_scan, mock_s3_operati When the document is retrieved Then the data in the document is the same as the data in the attached third party document """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.get(self.document_url, **self.exporter_headers) @@ -209,12 +208,9 @@ def test_get_third_party_document_success(self, mock_virus_scan, mock_s3_operati self.assertEqual(response_data["s3_key"], expected["s3_key"]) self.assertEqual(response_data["size"], expected["size"]) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_third_party_document_success( - self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object - ): + def test_delete_third_party_document_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains a third party @@ -222,7 +218,6 @@ def test_delete_third_party_document_success( When there is an attempt to delete the document Then 200 NO CONTENT is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.delete(self.document_url, **self.exporter_headers) @@ -230,10 +225,9 @@ def test_delete_third_party_document_success( self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) delete_s3_function.assert_called_once() - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_third_party_success(self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object): + def test_delete_third_party_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains a third party @@ -241,7 +235,6 @@ def test_delete_third_party_success(self, delete_s3_function, mock_virus_scan, m When there is an attempt to delete third party Then 200 OK """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False self.assertEqual(self.draft.third_parties.count(), 1) diff --git a/api/applications/tests/test_ultimate_end_users.py b/api/applications/tests/test_ultimate_end_users.py index e2151321f9..8b6e2116a3 100644 --- a/api/applications/tests/test_ultimate_end_users.py +++ b/api/applications/tests/test_ultimate_end_users.py @@ -11,6 +11,7 @@ from api.parties.models import Party from api.parties.models import PartyDocument from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class UltimateEndUsersOnDraft(DataTestClient): @@ -28,6 +29,7 @@ def setUp(self): "s3_key": "s3_keykey.pdf", "size": 123456, } + upload_file("s3_keykey.pdf") def test_set_multiple_ultimate_end_users_on_draft_successful(self): PartyOnApplication.objects.filter(application=self.draft, party__type=PartyType.ULTIMATE_END_USER).delete() @@ -140,9 +142,8 @@ def test_delete_ueu_on_standard_application_when_application_has_no_ueu_failure( self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_post_ultimate_end_user_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_post_ultimate_end_user_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains an ultimate end user @@ -150,7 +151,6 @@ def test_post_ultimate_end_user_document_success(self, mock_virus_scan, mock_s3_ When a document is submitted Then a 201 CREATED is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False PartyDocument.objects.filter(party=self.draft.ultimate_end_users.first().party).delete() @@ -158,9 +158,8 @@ def test_post_ultimate_end_user_document_success(self, mock_virus_scan, mock_s3_ self.assertEqual(response.status_code, status.HTTP_201_CREATED) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_get_ultimate_end_user_document_success(self, mock_virus_scan, mock_s3_operations_get_object): + def test_get_ultimate_end_user_document_success(self, mock_virus_scan): """ Given a standard draft has been created And the draft contains an ultimate end user @@ -168,7 +167,6 @@ def test_get_ultimate_end_user_document_success(self, mock_virus_scan, mock_s3_o When the document is retrieved Then the data in the document is the same as the data in the attached ultimate end user document """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.get(self.document_url, **self.exporter_headers) response_data = response.json()["document"] @@ -178,12 +176,9 @@ def test_get_ultimate_end_user_document_success(self, mock_virus_scan, mock_s3_o self.assertEqual(response_data["s3_key"], expected["s3_key"]) self.assertEqual(response_data["size"], expected["size"]) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_ultimate_end_user_document_success( - self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object - ): + def test_delete_ultimate_end_user_document_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains an ultimate end user @@ -191,17 +186,15 @@ def test_delete_ultimate_end_user_document_success( When there is an attempt to delete the document Then 204 NO CONTENT is returned """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False response = self.client.delete(self.document_url, **self.exporter_headers) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) delete_s3_function.assert_called_once() - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") @mock.patch("api.documents.models.Document.delete_s3") - def test_delete_ultimate_end_user_success(self, delete_s3_function, mock_virus_scan, mock_s3_operations_get_object): + def test_delete_ultimate_end_user_success(self, delete_s3_function, mock_virus_scan): """ Given a standard draft has been created And the draft contains an ultimate end user @@ -209,7 +202,6 @@ def test_delete_ultimate_end_user_success(self, delete_s3_function, mock_virus_s When there is an attempt to delete the document Then 200 OK """ - mock_s3_operations_get_object.return_value = self.new_document_data mock_virus_scan.return_value = False self.assertEqual(self.draft.ultimate_end_users.count(), 1) party_on_application = self.draft.ultimate_end_users.first() diff --git a/api/cases/tests/test_case_documents.py b/api/cases/tests/test_case_documents.py index 0ebea43bf5..495392686b 100644 --- a/api/cases/tests/test_case_documents.py +++ b/api/cases/tests/test_case_documents.py @@ -9,8 +9,7 @@ from lite_content.lite_api.strings import Documents from test_helpers.clients import DataTestClient - -from api.documents.libraries.s3_operations import init_s3_client +from test_helpers.file_uploads import upload_file class CaseDocumentsTests(DataTestClient): @@ -38,13 +37,7 @@ def setUp(self): self.case = self.submit_application(self.standard_application) self.file = self.create_case_document(self.case, self.gov_user, "Test") self.path = "cases:document_download" - - s3 = init_s3_client()["processed"] - s3.put_object( - Bucket=settings.FILE_UPLOAD_PROCESSED_BUCKET["AWS_STORAGE_BUCKET_NAME"], - Key=self.file.s3_key, - Body=b"test", - ) + upload_file(self.file.s3_key) def test_download_case_document_success(self): url = reverse(self.path, kwargs={"case_pk": self.case.id, "document_pk": self.file.id}) diff --git a/api/cases/tests/test_case_ecju_queries.py b/api/cases/tests/test_case_ecju_queries.py index 07d40d6137..d11d66f914 100644 --- a/api/cases/tests/test_case_ecju_queries.py +++ b/api/cases/tests/test_case_ecju_queries.py @@ -23,6 +23,7 @@ from api.staticdata.statuses.libraries.get_case_status import get_case_status_by_status from test_helpers.clients import DataTestClient from api.users.tests.factories import ExporterUserFactory +from test_helpers.file_uploads import upload_file faker = Faker() @@ -424,20 +425,20 @@ def test_query_create(self, mock_notify): class ECJUQueriesResponseTests(DataTestClient): - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def add_query_document(self, case_id, query_id, expected_status, mock_virus_scan, mock_s3_operations_get_object): + def add_query_document(self, case_id, query_id, expected_status, mock_virus_scan): mock_virus_scan.return_value = False url = reverse("cases:case_ecju_query_add_document", kwargs={"pk": case_id, "query_pk": query_id}) file_name = faker.file_name() + s3_key = f"{file_name}_{faker.uuid4()}" data = { "name": file_name, - "s3_key": f"{file_name}_{faker.uuid4()}", + "s3_key": s3_key, "description": faker.text(), "size": 1 << 10, "virus_scanned_at": timezone.now(), } - mock_s3_operations_get_object.return_value = data + upload_file(s3_key) response = self.client.post(url, data, **self.exporter_headers) self.assertEqual(response.status_code, expected_status) return response.json() diff --git a/api/conftest.py b/api/conftest.py index 1d593b51d5..9194eca462 100644 --- a/api/conftest.py +++ b/api/conftest.py @@ -132,7 +132,7 @@ def setup(settings): settings.HAWK_AUTHENTICATION_ENABLED = False -@pytest.fixture(autouse=True) +@pytest.fixture(autouse=True, scope="session") def mock_aws_calls(): with mock_aws(): clients = init_s3_client() diff --git a/api/documents/tests/test_document_stream.py b/api/documents/tests/test_document_stream.py index a5463a86a9..9a0ef5a60a 100644 --- a/api/documents/tests/test_document_stream.py +++ b/api/documents/tests/test_document_stream.py @@ -6,20 +6,15 @@ from django.urls import reverse from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file from api.conf import settings -from api.documents.libraries.s3_operations import init_s3_client class DocumentStream(DataTestClient): def setUp(self): super().setUp() - s3 = init_s3_client()["processed"] - s3.put_object( - Bucket=settings.FILE_UPLOAD_PROCESSED_BUCKET["AWS_STORAGE_BUCKET_NAME"], - Key="thisisakey", - Body=b"test", - ) + upload_file("thisisakey") def test_document_stream_as_caseworker(self): # given there is a case document diff --git a/api/organisations/tests/test_documents.py b/api/organisations/tests/test_documents.py index 3e73a50dc5..2f54026f77 100644 --- a/api/organisations/tests/test_documents.py +++ b/api/organisations/tests/test_documents.py @@ -7,7 +7,7 @@ from api.organisations.enums import OrganisationDocumentType from api.organisations.models import DocumentOnOrganisation from test_helpers.clients import DataTestClient -from api.documents.libraries.s3_operations import init_s3_client +from test_helpers.file_uploads import upload_file class OrganisationDocumentViewTests(DataTestClient): @@ -21,12 +21,7 @@ def setUp(self): def create_document_on_organisation(self, name): url = reverse("organisations:documents", kwargs={"pk": self.organisation.pk}) - s3 = init_s3_client()["staged"] - s3.put_object( - Bucket=settings.FILE_UPLOAD_STAGED_BUCKET["AWS_STORAGE_BUCKET_NAME"], - Key=name, - Body=b"test", - ) + upload_file(name) data = { "document": {"name": name, "s3_key": name, "size": 476}, "expiry_date": "2026-01-01", @@ -35,10 +30,8 @@ def create_document_on_organisation(self, name): } return self.client.post(url, data, **self.exporter_headers) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_create_organisation_document(self, mock_virus_scan, mock_s3_operations_get_object): - mock_s3_operations_get_object.return_value = self.document_data + def test_create_organisation_document(self, mock_virus_scan): mock_virus_scan.return_value = False response = self.create_document_on_organisation("some-document") @@ -55,10 +48,8 @@ def test_create_organisation_document(self, mock_virus_scan, mock_s3_operations_ self.assertEqual(instance.document_type, OrganisationDocumentType.FIREARM_SECTION_FIVE) self.assertEqual(instance.organisation, self.organisation) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_list_organisation_documents(self, mock_virus_scan, mock_s3_operations_get_object): - mock_s3_operations_get_object.return_value = self.document_data + def test_list_organisation_documents(self, mock_virus_scan): mock_virus_scan.return_value = False self.assertEqual(self.create_document_on_organisation("some-document-one").status_code, 201) self.assertEqual(self.create_document_on_organisation("some-document-two").status_code, 201) @@ -108,10 +99,8 @@ def test_retrieve_organisation_documents(self, mock_virus_scan): }, ) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_delete_organisation_documents(self, mock_virus_scan, mock_s3_operations_get_object): - mock_s3_operations_get_object.return_value = self.document_data + def test_delete_organisation_documents(self, mock_virus_scan): mock_virus_scan.return_value = False response = self.create_document_on_organisation("some-document-one") self.assertEqual(response.status_code, 201) @@ -131,10 +120,8 @@ def test_delete_organisation_documents(self, mock_virus_scan, mock_s3_operations with self.assertRaises(DocumentOnOrganisation.DoesNotExist): DocumentOnOrganisation.objects.get(pk=document_on_application_pk) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_update_organisation_documents(self, mock_virus_scan, mock_s3_operations_get_object): - mock_s3_operations_get_object.return_value = self.document_data + def test_update_organisation_documents(self, mock_virus_scan): mock_virus_scan.return_value = False response = self.create_document_on_organisation("some-document-one") self.assertEqual(response.status_code, 201) diff --git a/api/test_more_documents.py b/api/test_more_documents.py index 1b3dc1e4b9..fc7cc3d0e3 100644 --- a/api/test_more_documents.py +++ b/api/test_more_documents.py @@ -9,6 +9,7 @@ from api.applications.libraries.case_status_helpers import get_case_statuses from api.staticdata.statuses.libraries.get_case_status import get_case_status_by_status from test_helpers.clients import DataTestClient +from test_helpers.file_uploads import upload_file class DraftDocumentTests(DataTestClient): @@ -35,12 +36,11 @@ def setUp(self): "size": 476, "description": "banana cake 2", } + upload_file(self.test_filename) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_upload_document_on_unsubmitted_application(self, mock_virus_scan, mock_s3_operations_get_object): + def test_upload_document_on_unsubmitted_application(self, mock_virus_scan): """Test success in adding a document to an unsubmitted application.""" - mock_s3_operations_get_object.return_value = self.data mock_virus_scan.return_value = False self.client.post(self.url_draft, data=self.data, **self.exporter_headers) @@ -58,11 +58,9 @@ def test_upload_document_on_unsubmitted_application(self, mock_virus_scan, mock_ self.assertEqual(len(response_data), 2) self.assertTrue(self.data in response_data) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_upload_multiple_documents_on_unsubmitted_application(self, mock_virus_scan, mock_s3_operations_get_object): + def test_upload_multiple_documents_on_unsubmitted_application(self, mock_virus_scan): """Test success in adding multiple documents to an unsubmitted application.""" - mock_s3_operations_get_object.return_value = self.data mock_virus_scan.return_value = False data = [self.data, self.data2] self.client.post(self.url_draft, data=self.data, **self.exporter_headers) @@ -119,12 +117,8 @@ def test_get_individual_draft_document(self): self.assertEqual(response.json()["document"]["size"], application_document.size) @parameterized.expand(get_case_statuses(read_only=False)) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") - def test_add_document_when_application_in_editable_state_success( - self, editable_status, mock_virus_scan, mock_s3_operations_get_object - ): - mock_s3_operations_get_object.return_value = self.data + def test_add_document_when_application_in_editable_state_success(self, editable_status, mock_virus_scan): mock_virus_scan.return_value = False application = self.create_draft_standard_application(self.organisation) application.status = get_case_status_by_status(editable_status) @@ -137,13 +131,11 @@ def test_add_document_when_application_in_editable_state_success( self.assertEqual(application.applicationdocument_set.count(), 2) @parameterized.expand(get_case_statuses(read_only=False)) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.models.Document.delete_s3") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") def test_delete_document_when_application_in_editable_state_success( - self, editable_status, mock_virus_scan, mock_delete_s3, mock_s3_operations_get_object + self, editable_status, mock_virus_scan, mock_delete_s3 ): - mock_s3_operations_get_object.return_value = self.data mock_virus_scan.return_value = False application = self.create_draft_standard_application(self.organisation) application.status = get_case_status_by_status(editable_status) @@ -175,13 +167,11 @@ def test_add_document_when_application_in_read_only_state_failure(self, read_onl self.assertEqual(application.applicationdocument_set.count(), 1) @parameterized.expand(get_case_statuses(read_only=True)) - @mock.patch("api.documents.libraries.s3_operations.get_object") @mock.patch("api.documents.models.Document.delete_s3") @mock.patch("api.documents.libraries.av_operations.scan_file_for_viruses") def test_delete_document_when_application_in_read_only_state_failure( - self, read_only_status, mock_virus_scan, mock_delete_s3, mock_s3_operations_get_object + self, read_only_status, mock_virus_scan, mock_delete_s3 ): - mock_s3_operations_get_object.return_value = self.data mock_virus_scan.return_value = False application = self.create_draft_standard_application(self.organisation) application.status = get_case_status_by_status(read_only_status) diff --git a/test_helpers/file_uploads.py b/test_helpers/file_uploads.py new file mode 100644 index 0000000000..5b466a4007 --- /dev/null +++ b/test_helpers/file_uploads.py @@ -0,0 +1,18 @@ +from django.conf import settings + +from api.documents.libraries.s3_operations import init_s3_client + + +def upload_file(s3_key, content=b"test"): + """ + Emulates how the frontend does file uploads by uploading to the processed + S3 bucket. + TODO: When we switch the frontend to uploading to the staged bucket instead, + we should change that here. + """ + s3 = init_s3_client()["processed"] + s3.put_object( + Bucket=settings.FILE_UPLOAD_PROCESSED_BUCKET["AWS_STORAGE_BUCKET_NAME"], + Key=s3_key, + Body=content, + )