Skip to content

Commit

Permalink
Merge pull request #2170 from uktrade/uat
Browse files Browse the repository at this point in the history
Prod Release
  • Loading branch information
markj0hnst0n authored Sep 5, 2024
2 parents 10bd303 + f302086 commit f31bfab
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 18 deletions.
28 changes: 26 additions & 2 deletions api/applications/tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import factory

import factory.fuzzy
from faker import Faker

from api.applications.enums import ApplicationExportType, ApplicationExportLicenceOfficialType
Expand All @@ -14,7 +14,8 @@
GoodOnApplicationInternalDocument,
StandardApplication,
)
from api.cases.enums import CaseTypeEnum
from api.cases.enums import AdviceLevel, AdviceType, CaseTypeEnum
from api.cases.models import Advice
from api.external_data.models import Denial, DenialEntity, SanctionMatch
from api.documents.tests.factories import DocumentFactory
from api.staticdata.statuses.models import CaseStatus
Expand Down Expand Up @@ -240,3 +241,26 @@ def _create(cls, model_class, *args, **kwargs):
PartyOnApplicationFactory(application=obj, party=ThirdPartyFactory(organisation=obj.organisation))

return obj


class AdviceFactory(factory.django.DjangoModelFactory):
user = factory.SubFactory(GovUserFactory)
case = factory.SubFactory(StandardApplicationFactory)
type = factory.fuzzy.FuzzyChoice(AdviceType.choices, getter=lambda t: t[0])
level = factory.fuzzy.FuzzyChoice(AdviceLevel.choices, getter=lambda t: t[0])

class Meta:
model = Advice


class FinalAdviceOnApplicationFactory(StandardApplicationFactory):

@classmethod
def _create(cls, model_class, *args, **kwargs):
obj = model_class(*args, **kwargs)
obj.status = get_case_status_by_status(CaseStatusEnum.UNDER_FINAL_REVIEW)
obj.save()

AdviceFactory(case=obj, level=AdviceLevel.FINAL)

return obj
1 change: 1 addition & 0 deletions api/conf/exporter_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

urlpatterns = [
path("applications/", include("api.applications.exporter.urls")),
path("static/", include("api.staticdata.exporter.urls")),
]
4 changes: 1 addition & 3 deletions api/organisations/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,7 @@ def get_flags(self, instance):
return list(instance.flags.values("id", "name", "colour", "label", "priority"))

def get_documents(self, instance):
queryset = instance.document_on_organisations.order_by("document_type", "-expiry_date").distinct(
"document_type"
)
queryset = instance.document_on_organisations.order_by("document_type", "-created_at").distinct("document_type")
serializer = DocumentOnOrganisationSerializer(queryset, many=True)
return serializer.data

Expand Down
73 changes: 60 additions & 13 deletions api/organisations/tests/test_documents.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime, timedelta

from unittest import mock

Expand Down Expand Up @@ -29,9 +29,12 @@ def test_create_organisation_document(self, mock_virus_scan, mock_s3_operations_
mock_virus_scan.return_value = False

url = reverse("organisations:documents", kwargs={"pk": self.organisation.pk})

future_date = datetime.now() + timedelta(days=365)
future_date_formatted = future_date.strftime("%Y-%m-%d")
data = {
"document": {"name": "some-document", "s3_key": "some-document", "size": 476},
"expiry_date": "2026-01-01",
"expiry_date": future_date_formatted,
"reference_code": "123",
"document_type": OrganisationDocumentType.FIREARM_SECTION_FIVE,
}
Expand All @@ -46,7 +49,8 @@ def test_create_organisation_document(self, mock_virus_scan, mock_s3_operations_
self.assertEqual(instance.document.s3_key, "some-document")
self.assertEqual(instance.reference_code, "123")
self.assertEqual(instance.document.size, 476)
self.assertEqual(instance.expiry_date, datetime.date(2026, 1, 1))

self.assertEqual(instance.expiry_date.strftime("%Y-%m-%d"), future_date_formatted)
self.assertEqual(instance.document_type, OrganisationDocumentType.FIREARM_SECTION_FIVE)
self.assertEqual(instance.organisation, self.organisation)

Expand All @@ -58,9 +62,10 @@ def test_create_organisation_document_other_organisation(self, mock_virus_scan,
other_organisation, _ = self.create_organisation_with_exporter_user()
url = reverse("organisations:documents", kwargs={"pk": other_organisation.pk})

future_date = datetime.now() + timedelta(days=365)
data = {
"document": {"name": "some-document", "s3_key": "some-document", "size": 476},
"expiry_date": "2026-01-01",
"expiry_date": future_date,
"reference_code": "123",
"document_type": OrganisationDocumentType.FIREARM_SECTION_FIVE,
}
Expand All @@ -74,25 +79,29 @@ def test_list_organisation_documents(self):
document__s3_key="thisisakey",
document__safe=True,
organisation=self.organisation,
expiry_date=datetime.now() + timedelta(days=4 * 365),
)
DocumentOnOrganisationFactory.create(
document__name="some-document-two",
document__s3_key="thisisakey",
document__safe=True,
organisation=self.organisation,
expiry_date=datetime.now() + timedelta(days=3 * 365),
)
DocumentOnOrganisationFactory.create(
document__name="some-document-three",
document__s3_key="thisisakey",
document__safe=True,
organisation=self.organisation,
expiry_date=datetime.now() + timedelta(days=2 * 365),
)
other_organisation, _ = self.create_organisation_with_exporter_user()
DocumentOnOrganisationFactory.create(
document__name="other-organisation-some-document-three",
document__s3_key="thisisakey",
document__safe=True,
organisation=other_organisation,
expiry_date=datetime.now() + timedelta(days=365),
)

url = reverse("organisations:documents", kwargs={"pk": self.organisation.pk})
Expand All @@ -111,9 +120,11 @@ def test_list_organisation_documents_other_organisation(self):
self.assertEqual(response.status_code, 403)

def test_retrieve_organisation_documents(self):
future_date = datetime.now() + timedelta(days=365)
future_date_formatted = future_date.strftime("%d %B %Y")
document_on_application = DocumentOnOrganisationFactory.create(
organisation=self.organisation,
expiry_date=datetime.date(2026, 1, 1),
expiry_date=future_date,
document_type=OrganisationDocumentType.FIREARM_SECTION_FIVE,
reference_code="123",
document__name="some-document-one",
Expand All @@ -128,14 +139,13 @@ def test_retrieve_organisation_documents(self):
)

response = self.client.get(url, **self.exporter_headers)

self.assertEqual(response.status_code, 200)

self.assertEqual(
response.json(),
{
"id": str(document_on_application.pk),
"expiry_date": "01 January 2026",
"expiry_date": future_date_formatted,
"document_type": "section-five-certificate",
"organisation": str(self.organisation.id),
"is_expired": False,
Expand All @@ -151,11 +161,46 @@ def test_retrieve_organisation_documents(self):
},
)

def test_retrieve_documents_on_organisation_details(self):
# We create 2 documents on the organisation

DocumentOnOrganisationFactory.create(
organisation=self.organisation,
expiry_date=datetime.now() + timedelta(days=365),
document_type=OrganisationDocumentType.REGISTERED_FIREARM_DEALER_CERTIFICATE,
reference_code="123",
document__name="some-document-one",
document__s3_key="some-document-one",
document__size=476,
document__safe=True,
created_at=datetime.now() - timedelta(hours=1),
)
DocumentOnOrganisationFactory.create(
organisation=self.organisation,
expiry_date=datetime.now() + timedelta(days=365),
document_type=OrganisationDocumentType.REGISTERED_FIREARM_DEALER_CERTIFICATE,
reference_code="321",
document__name="some-document-two",
document__s3_key="some-document-two",
document__size=476,
document__safe=True,
created_at=datetime.now(),
)

url = reverse("organisations:organisation", kwargs={"pk": self.organisation.pk})

response = self.client.get(url, **self.exporter_headers)

# We check that the first document delivered to organisation details is the most recently created

self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()["documents"][0]["document"]["name"], "some-document-two")

def test_retrieve_organisation_documents_invalid_organisation(self):
other_organisation, _ = self.create_organisation_with_exporter_user()
document_on_application = DocumentOnOrganisationFactory.create(
organisation=other_organisation,
expiry_date=datetime.date(2026, 1, 1),
expiry_date=datetime.now() + timedelta(days=365),
document_type=OrganisationDocumentType.FIREARM_SECTION_FIVE,
reference_code="123",
document__name="some-document-one",
Expand Down Expand Up @@ -215,11 +260,12 @@ def test_update_organisation_documents(self):
"document_on_application_pk": document_on_application.pk,
},
)

future_date = datetime.now() + timedelta(days=365)
future_date_formatted = future_date.strftime("%Y-%m-%d")
response = self.client.put(
url,
data={
"expiry_date": "2030-12-12",
"expiry_date": future_date_formatted,
"reference_code": "567",
},
**self.exporter_headers,
Expand All @@ -228,8 +274,8 @@ def test_update_organisation_documents(self):

document_on_application.refresh_from_db()
self.assertEqual(
document_on_application.expiry_date,
datetime.date(2030, 12, 12),
document_on_application.expiry_date.strftime("%Y-%m-%d"),
future_date_formatted,
)
self.assertEqual(
document_on_application.reference_code,
Expand All @@ -240,6 +286,7 @@ def test_update_organisation_documents_other_organisation(self):
other_organisation, _ = self.create_organisation_with_exporter_user()
document_on_application = DocumentOnOrganisationFactory.create(organisation=other_organisation)

future_date = datetime.now() + timedelta(days=365)
url = reverse(
"organisations:documents",
kwargs={
Expand All @@ -251,7 +298,7 @@ def test_update_organisation_documents_other_organisation(self):
response = self.client.put(
url,
data={
"expiry_date": "2030-12-12",
"expiry_date": future_date,
"reference_code": "567",
},
**self.exporter_headers,
Expand Down
9 changes: 9 additions & 0 deletions api/staticdata/exporter/control_list_entries/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from api.staticdata.control_list_entries.models import ControlListEntry


class ControlListEntriesListSerializer(serializers.ModelSerializer):
class Meta:
model = ControlListEntry
fields = ("rating", "text")
48 changes: 48 additions & 0 deletions api/staticdata/exporter/control_list_entries/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from rest_framework import status
from rest_framework.reverse import reverse

from api.staticdata.control_list_entries.models import ControlListEntry
from api.staticdata.control_list_entries.factories import ControlListEntriesFactory
from test_helpers.clients import DataTestClient


class ControlListEntriesListTests(DataTestClient):
def setUp(self):
self.url = reverse("exporter_staticdata:control_list_entries:control_list_entries")
super().setUp()

def test_list_view_success(self):
response = self.client.get(self.url, **self.exporter_headers)

self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertTrue(len(response.json()) > 0)

for cle in response.json():
self.assertEqual(list(cle.keys()), ["rating", "text"])

def test_list_view_failure_bad_headers(self):
response = self.client.get(self.url, **self.gov_headers)

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_list_view_success_exact_response(self):
# Set up empty CLE db table for this test only
ControlListEntry.objects.all().delete()

cle_1 = ControlListEntriesFactory(rating="ABC123", controlled=True)
cle_2 = ControlListEntriesFactory(rating="1Z101", controlled=True)
cle_3 = ControlListEntriesFactory(rating="ZXYW", controlled=True)

response = self.client.get(self.url, **self.exporter_headers)

self.assertEqual(response.status_code, status.HTTP_200_OK)

self.assertEqual(
response.json(),
[
{"rating": cle_1.rating, "text": cle_1.text},
{"rating": cle_2.rating, "text": cle_2.text},
{"rating": cle_3.rating, "text": cle_3.text},
],
)
9 changes: 9 additions & 0 deletions api/staticdata/exporter/control_list_entries/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from api.staticdata.exporter.control_list_entries import views

app_name = "control_list_entries"

urlpatterns = [
path("", views.ControlListEntriesList.as_view(), name="control_list_entries"),
]
12 changes: 12 additions & 0 deletions api/staticdata/exporter/control_list_entries/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from rest_framework import generics

from api.core.authentication import ExporterAuthentication
from api.staticdata.control_list_entries.models import ControlListEntry
from api.staticdata.exporter.control_list_entries.serializers import ControlListEntriesListSerializer


class ControlListEntriesList(generics.ListAPIView):
authentication_classes = (ExporterAuthentication,)
pagination_class = None
serializer_class = ControlListEntriesListSerializer
queryset = ControlListEntry.objects.filter(controlled=True)
7 changes: 7 additions & 0 deletions api/staticdata/exporter/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path, include

app_name = "exporter_staticdata"

urlpatterns = [
path("control-list-entries/", include("api.staticdata.exporter.control_list_entries.urls")),
]
Loading

0 comments on commit f31bfab

Please sign in to comment.