-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1979 from uktrade/uat
PROD Release
- Loading branch information
Showing
25 changed files
with
779 additions
and
137 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
api/cases/management/commands/removes_flags_and_audits_from_cases.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import logging | ||
|
||
from django.db import transaction | ||
|
||
from django.core.management.base import BaseCommand | ||
from api.cases.libraries.finalise import remove_flags_on_finalisation, remove_flags_from_audit_trail | ||
from api.cases.models import Case | ||
from api.staticdata.statuses.models import CaseStatus | ||
from api.staticdata.statuses.enums import CaseStatusEnum | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Command to remove required flags from Cases and Audit Trails for the past cases" | ||
|
||
def handle(self, *args, **options): | ||
count = 0 | ||
statuses = [ | ||
CaseStatus.objects.get(status=CaseStatusEnum.WITHDRAWN), | ||
CaseStatus.objects.get(status=CaseStatusEnum.FINALISED), | ||
CaseStatus.objects.get(status=CaseStatusEnum.CLOSED), | ||
] | ||
|
||
cases = Case.objects.filter(status__in=statuses) | ||
|
||
with transaction.atomic(): | ||
for case in cases: | ||
remove_flags_on_finalisation(case) | ||
remove_flags_from_audit_trail(case) | ||
count += 1 | ||
|
||
logging.info("Successfully adjusted %s cases.", cases.count()) |
68 changes: 68 additions & 0 deletions
68
api/cases/management/commands/tests/test_removes_flags_and_audits_from_cases.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from django.core.management import call_command | ||
from parameterized import parameterized | ||
|
||
from test_helpers.clients import DataTestClient | ||
|
||
from api.flags.models import Flag | ||
from api.users.models import BaseUser | ||
from api.users.enums import UserType | ||
from api.audit_trail.enums import AuditType | ||
|
||
from api.audit_trail import service as audit_trail_service | ||
|
||
from api.cases.tests.factories import FinalAdviceFactory | ||
from api.cases.enums import AdviceType | ||
from api.staticdata.statuses.enums import CaseStatusEnum | ||
from api.staticdata.statuses.models import CaseStatus | ||
from api.audit_trail.models import Audit | ||
|
||
|
||
class TestCommand(DataTestClient): | ||
def setUp(self): | ||
super().setUp() | ||
self.application = self.create_standard_application_case(self.organisation) | ||
self.original_flag_id = self.application.flags.first().id | ||
|
||
# Add Flag to test with removal after Finalise | ||
self.test_flag = Flag.objects.all().first() | ||
self.test_flag.remove_on_finalised = True | ||
self.test_flag.save() | ||
self.application.flags.add(self.test_flag) | ||
|
||
user = BaseUser(email="[email protected]", first_name="John", last_name="Smith", type=UserType.SYSTEM) | ||
|
||
self.case = self.application.get_case() | ||
|
||
self.test_audit = audit_trail_service.create( | ||
actor=user, | ||
verb=AuditType.ADD_FLAGS, | ||
target=self.case, | ||
payload={ | ||
"added_flags": [self.test_flag.name], | ||
"additional_text": "test", | ||
"added_flags_id": [str(self.test_flag.id)], | ||
}, | ||
) | ||
|
||
FinalAdviceFactory(user=self.gov_user, case=self.application, type=AdviceType.APPROVE) | ||
|
||
@parameterized.expand( | ||
case_status for case_status in [CaseStatusEnum.FINALISED, CaseStatusEnum.WITHDRAWN, CaseStatusEnum.CLOSED] | ||
) | ||
def test_call_command_removes_flags_and_audits_from_cases(self, case_status): | ||
status_var = CaseStatus.objects.get(status=case_status) | ||
self.case.status = status_var | ||
self.case.save() | ||
|
||
audit_queryset = Audit.objects.filter(target_object_id=self.case.id) | ||
|
||
self.assertEqual(self.case.flags.count(), 2) | ||
self.assertEqual(audit_queryset.count(), 2) | ||
|
||
call_command("removes_flags_and_audits_from_cases") | ||
|
||
audit_queryset = Audit.objects.filter(target_object_id=self.case.id) | ||
|
||
self.assertNotIn(self.test_flag, self.case.flags.all()) | ||
self.assertEqual(audit_queryset.count(), 1) | ||
self.assertNotIn(self.test_audit, audit_queryset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import pytest | ||
from unittest import mock | ||
from django.urls import reverse | ||
from api.audit_trail.enums import AuditType | ||
from api.flags.models import Flag | ||
from api.users.enums import UserType | ||
from api.users.models import BaseUser | ||
from rest_framework import status | ||
from parameterized import parameterized | ||
|
||
from api.audit_trail.models import Audit | ||
from api.cases.enums import AdviceType, CaseTypeEnum | ||
|
@@ -12,6 +18,8 @@ | |
from api.staticdata.statuses.enums import CaseStatusEnum | ||
from api.staticdata.statuses.models import CaseStatus | ||
from test_helpers.clients import DataTestClient | ||
from api.audit_trail import service as audit_trail_service | ||
from api.staticdata.statuses.libraries.get_case_status import get_case_status_by_status | ||
|
||
|
||
class RefuseAdviceTests(DataTestClient): | ||
|
@@ -121,6 +129,29 @@ class ApproveAdviceTests(DataTestClient): | |
def setUp(self): | ||
super().setUp() | ||
self.application = self.create_standard_application_case(self.organisation) | ||
self.original_flag_id = self.application.flags.first().id | ||
|
||
# Add Flag to test with removal after Finalise | ||
self.test_flag = Flag.objects.all().first() | ||
self.test_flag.remove_on_finalised = True | ||
self.test_flag.save() | ||
self.application.flags.add(self.test_flag) | ||
|
||
user = BaseUser(email="[email protected]", first_name="John", last_name="Smith", type=UserType.SYSTEM) | ||
|
||
case = self.application.get_case() | ||
|
||
self.test_audit = audit_trail_service.create( | ||
actor=user, | ||
verb=AuditType.ADD_FLAGS, | ||
target=case, | ||
payload={ | ||
"added_flags": [self.test_flag.name], | ||
"additional_text": "test", | ||
"added_flags_id": [str(self.test_flag.id)], | ||
}, | ||
) | ||
|
||
self.url = reverse("cases:finalise", kwargs={"pk": self.application.id}) | ||
FinalAdviceFactory(user=self.gov_user, case=self.application, type=AdviceType.APPROVE) | ||
self.template = self.create_letter_template( | ||
|
@@ -151,3 +182,50 @@ def test_approve_standard_application_success( | |
send_exporter_notifications_func.assert_called() | ||
|
||
assert case.sub_status.name == "Approved" | ||
|
||
@mock.patch("api.cases.views.views.notify_exporter_licence_issued") | ||
@mock.patch("api.cases.generated_documents.models.GeneratedCaseDocument.send_exporter_notifications") | ||
def test_finalised_standard_application_with_flags_removed( | ||
self, | ||
send_exporter_notifications_func, | ||
mock_notify_exporter_licence_issued, | ||
): | ||
self.gov_user.role.permissions.set([GovPermissions.MANAGE_LICENCE_FINAL_ADVICE.name]) | ||
self.create_generated_case_document(self.application, self.template, advice_type=AdviceType.APPROVE) | ||
|
||
self.assertEqual(self.application.flags.count(), 2) | ||
|
||
response = self.client.put(self.url, data={}, **self.gov_headers) | ||
self.application.refresh_from_db() | ||
|
||
self.assertEqual(response.status_code, status.HTTP_201_CREATED) | ||
self.assertEqual(self.application.status, CaseStatus.objects.get(status=CaseStatusEnum.FINALISED)) | ||
for document in GeneratedCaseDocument.objects.filter(advice_type__isnull=False): | ||
self.assertTrue(document.visible_to_exporter) | ||
|
||
# Make sure Flag was removed and Audit trail was removed | ||
case = get_case(self.application.id) | ||
self.assertNotIn(self.test_flag, case.flags.all()) | ||
|
||
audit_queryset = Audit.objects.filter(target_object_id=case.id) | ||
self.assertNotIn(self.test_audit, audit_queryset) | ||
|
||
@parameterized.expand(case_status for case_status in [CaseStatusEnum.WITHDRAWN, CaseStatusEnum.CLOSED]) | ||
def test_standard_application_remove_audit_and_flag_with_statuses(self, case_status): | ||
url = reverse("applications:manage_status", kwargs={"pk": self.application.id}) | ||
data = {"status": case_status} | ||
|
||
self.assertEqual(self.application.flags.count(), 2) | ||
|
||
response = self.client.put(url, data=data, **self.gov_headers) | ||
self.application.refresh_from_db() | ||
|
||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(self.application.status, get_case_status_by_status(case_status)) | ||
|
||
# Make sure Flag was removed and Audit trail was removed | ||
case = get_case(self.application.id) | ||
self.assertNotIn(self.test_flag, case.flags.all()) | ||
|
||
audit_queryset = Audit.objects.filter(target_object_id=case.id) | ||
self.assertNotIn(self.test_audit, audit_queryset) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.