Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LTD-4662] process file uploads from source bucket #1808

Closed
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions api/applications/tests/test_consignee.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
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):
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):
Expand Down Expand Up @@ -174,17 +177,15 @@ 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
And the consignee does not have a document attached
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
Expand All @@ -196,17 +197,15 @@ 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
And the consignee has a document attached
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
Expand All @@ -221,20 +220,16 @@ 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
And the draft contains an end user document
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
Expand All @@ -246,18 +241,16 @@ 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
And the draft contains a consignee document
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
Expand Down
9 changes: 5 additions & 4 deletions api/applications/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,30 @@
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",
"reference_code": "1",
"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)

Expand Down
31 changes: 10 additions & 21 deletions api/applications/tests/test_end_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -174,17 +177,15 @@ 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
And the end user has a document attached
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"]
Expand All @@ -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
Expand Down Expand Up @@ -263,17 +262,15 @@ 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
And the end user does not have a document attached
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
Expand All @@ -285,19 +282,15 @@ 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
And the draft contains an end user document
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
Expand All @@ -311,36 +304,32 @@ 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
And the draft contains an end user document
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
And the draft contains an end user document
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
Expand Down
9 changes: 5 additions & 4 deletions api/applications/tests/test_goods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -24,17 +24,18 @@ 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",
"reference_code": "1",
"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())
Expand Down
Loading
Loading