Skip to content

Commit

Permalink
Merge pull request #818 from uktrade/LTD-1135-nlr-doesnt-conflict
Browse files Browse the repository at this point in the history
NLR advice doesn't conflict
  • Loading branch information
eadpearce authored Sep 22, 2021
2 parents 4b659c2 + 87a0bb1 commit 0007bc0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 8 deletions.
10 changes: 5 additions & 5 deletions api/applications/tests/test_finalise_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def test_get_approved_goods_success(self):
self.standard_application,
"good",
AdviceType.APPROVE,
AdviceLevel.USER,
AdviceLevel.FINAL,
advice_text=advice_text,
)

Expand All @@ -245,7 +245,7 @@ def test_get_approved_goods_success(self):
self.standard_application,
"",
AdviceType.REFUSE,
AdviceLevel.USER,
AdviceLevel.FINAL,
good=second_good_on_app.good,
)

Expand All @@ -258,15 +258,15 @@ def test_get_approved_goods_success(self):
self.standard_application,
"",
AdviceType.NO_LICENCE_REQUIRED,
AdviceLevel.USER,
AdviceLevel.FINAL,
good=third_good_on_app.good,
)

response = self.client.get(self.url, **self.gov_headers)
data = response.json()["goods"]

self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(data), 1)
self.assertEqual(len(data), 2)
self.assertEqual(data[0]["id"], str(self.good_on_application.id))
self.assertEqual(data[0]["good"]["id"], str(self.good_on_application.good.id))
self.assertEqual(data[0]["good"]["description"], self.good_on_application.good.description)
Expand All @@ -278,7 +278,7 @@ def test_get_approved_goods_success(self):
def test_get_proviso_goods_success(self):
# Proviso the existing good
advice = self.create_advice(
self.gov_user, self.standard_application, "good", AdviceType.PROVISO, AdviceLevel.USER
self.gov_user, self.standard_application, "good", AdviceType.PROVISO, AdviceLevel.FINAL
)

response = self.client.get(self.url, **self.gov_headers)
Expand Down
4 changes: 4 additions & 0 deletions api/applications/tests/test_matching_denials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import csv
import io
import pytest

from django.urls import reverse
from faker import Faker
Expand Down Expand Up @@ -34,6 +35,9 @@ def setUp(self):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(models.Denial.objects.count(), 5)

@pytest.mark.xfail(reason="This test is flaky and should be rewritten")
# Occasionally causes this error:
# django.db.utils.IntegrityError: duplicate key value violates unique constraint "external_data_denial_reference_05842cee_uniq"
def test_adding_denials_to_application(self):
data = [
{"application": self.application.id, "denial": denial.id, "category": "exact" if (index % 2) else "partial"}
Expand Down
5 changes: 3 additions & 2 deletions api/applications/views/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
)
from api.audit_trail import service as audit_trail_service
from api.audit_trail.enums import AuditType
from api.cases.enums import AdviceType, CaseTypeSubTypeEnum, CaseTypeEnum
from api.cases.enums import AdviceLevel, AdviceType, CaseTypeSubTypeEnum, CaseTypeEnum
from api.cases.generated_documents.models import GeneratedCaseDocument
from api.cases.generated_documents.helpers import auto_generate_case_document
from api.cases.helpers import can_set_status
Expand Down Expand Up @@ -509,7 +509,8 @@ def get(self, request, pk):
approved_goods_on_application = (
GoodOnApplication.objects.filter(
application_id=pk,
good__advice__type__in=[AdviceType.APPROVE, AdviceType.PROVISO],
good__advice__level=AdviceLevel.FINAL,
good__advice__type__in=[AdviceType.APPROVE, AdviceType.PROVISO, AdviceType.NO_LICENCE_REQUIRED],
good__advice__case_id=pk,
good__advice__good_id__isnull=False,
)
Expand Down
11 changes: 11 additions & 0 deletions api/cases/libraries/advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ def construct_coalesced_advice_values(
if getattr(advice, field):
fields[field].add(getattr(advice, field))

advice_types = set([a.type for a in deduplicated_advice])

if len(advice_types) == 1:
advice_type = deduplicated_advice[0].type
elif advice_types == {AdviceType.NO_LICENCE_REQUIRED, AdviceType.APPROVE}:
advice_type = AdviceType.NO_LICENCE_REQUIRED
elif advice_types == {AdviceType.PROVISO, AdviceType.APPROVE}:
advice_type = AdviceType.PROVISO
else:
advice_type = AdviceType.CONFLICTING

pv_grading = (
break_text.join([PvGrading.to_str(pv_grading) for pv_grading in fields["pv_grading"]])
if fields["pv_grading"]
Expand Down
29 changes: 28 additions & 1 deletion api/cases/tests/test_create_final_advice.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from api.teams.models import Team
from api.users.tests.factories import GovUserFactory
from test_helpers.clients import DataTestClient
from api.users.models import GovUser, Role
from api.users.models import Role


class CreateCaseAdviceTests(DataTestClient):
Expand Down Expand Up @@ -52,6 +52,7 @@ def setUp(self):
self.gov_user_3 = GovUserFactory(baseuser_ptr__email="[email protected]", team=team_3, role=role)

self.standard_case_url = reverse("cases:case_final_advice", kwargs={"pk": self.standard_case.id})
self.standard_case_finalise_url = reverse("applications:finalise", kwargs={"pk": self.standard_case.id})

def test_advice_is_concatenated_when_final_advice_first_created(self):
"""
Expand Down Expand Up @@ -95,6 +96,32 @@ def test_create_conflicting_final_advice_shows_all_fields(self):
self.assertEqual(response_data.get("proviso"), "I am easy to proviso")
self.assertCountEqual(["1a", "1b", "1c"], response_data["denial_reasons"])

def test_create_final_advice_nlr_overrides_approval(self):
"""
NLR decision overrides approval
"""
self.create_advice(self.gov_user, self.standard_case, "good", AdviceType.NO_LICENCE_REQUIRED, AdviceLevel.TEAM)
self.create_advice(self.gov_user_3, self.standard_case, "good", AdviceType.APPROVE, AdviceLevel.TEAM)

response = self.client.get(self.standard_case_url, **self.gov_headers)
response_data = response.json()["advice"][0]

self.assertEqual(response_data.get("type").get("key"), "no_licence_required")

def test_return_final_advice(self):
"""
Check the correct advice is returned
"""
self.create_advice(self.gov_user_2, self.standard_case, "good", AdviceType.APPROVE, AdviceLevel.TEAM)
self.create_advice(self.gov_user, self.standard_case, "good", AdviceType.NO_LICENCE_REQUIRED, AdviceLevel.TEAM)
self.create_advice(self.gov_user, self.standard_case, "good", AdviceType.NO_LICENCE_REQUIRED, AdviceLevel.FINAL)
self.create_advice(self.gov_user_2, self.standard_case, "end_user", AdviceType.APPROVE, AdviceLevel.FINAL)

response = self.client.get(self.standard_case_finalise_url, **self.gov_headers)
final_advice_on_goods = response.json()["goods"]

self.assertEqual(len(final_advice_on_goods), 1)

def test_create_final_advice_same_advice_type_different_pv_gradings(self):
"""
Same advice types, different pv gradings
Expand Down

0 comments on commit 0007bc0

Please sign in to comment.