Skip to content

Commit

Permalink
Merge pull request #2116 from uktrade/LTD-5248-update-license-endpoin…
Browse files Browse the repository at this point in the history
…t-changes

LTD-5248-update-license-endpoint-changes
  • Loading branch information
depsiatwal authored Aug 5, 2024
2 parents 77d67af + 6e0bab3 commit ce17d0d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
6 changes: 6 additions & 0 deletions api/licences/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def cancel(self, send_status_change_to_hmrc=True):
self.status = LicenceStatus.CANCELLED
self.save(send_status_change_to_hmrc=send_status_change_to_hmrc)

def reinstate(self):
# This supersedes the issue method as it's called explicity on the license
# Hence the user explicty knows which license is being reinstated
self.status = LicenceStatus.REINSTATED
self.save()

def issue(self, send_status_change_to_hmrc=True):
# re-issue the licence if an older version exists
status = LicenceStatus.ISSUED
Expand Down
16 changes: 16 additions & 0 deletions api/licences/serializers/view_licence.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def get_document(self, instance):


class LicenceDetailsSerializer(serializers.ModelSerializer):
# These actions are used to support the Licence status change screen
# Suspened to reinstated don't send HMRC messages as these are only support offline via email
action_dict = {
"reinstated": lambda instance: Licence.reinstate(instance),
"suspended": lambda instance: Licence.suspend(instance),
"revoked": lambda instance: Licence.revoke(instance, send_status_change_to_hmrc=True),
}

class Meta:
model = Licence
Expand All @@ -223,6 +230,15 @@ class Meta:
)
read_only_fields = ["id", "reference_code"]

def update(self, instance, validated_data):
update_action = validated_data.get("status")
try:
action_method = self.action_dict[update_action]
action_method(instance)
except KeyError:
raise serializers.ValidationError(f"Updating licence status: {update_action} not allowed")
return super().update(instance, validated_data)


class LicenceWithGoodsViewSerializer(serializers.Serializer):
id = serializers.UUIDField()
Expand Down
70 changes: 49 additions & 21 deletions api/licences/tests/test_get_licences.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from unittest import mock
from django.urls import reverse
from rest_framework import status
from api.core.constants import Roles
Expand Down Expand Up @@ -232,28 +233,62 @@ def test_get_license_details_exporter_not_allowed(self):
response = self.client.get(self.url, **self.exporter_headers)
assert response.status_code == status.HTTP_403_FORBIDDEN

@parameterized.expand(
[
[{"status": "suspended"}, "suspend"],
[{"status": "reinstated"}, "reinstate"],
]
)
def test_update_license_details_message_success(self, data, expect_model_method):

with mock.patch(f"api.licences.models.Licence.{expect_model_method}") as save_method_mock:

response = self.client.patch(self.url, data, **self.gov_headers)

response_data = response.json()
self.standard_application_licence.refresh_from_db()
expected_data = {
"id": str(self.standard_application_licence.id),
"reference_code": self.standard_application_licence.reference_code,
**data,
}
save_method_mock.assert_called_once()
save_method_mock.assert_called_once_with(self.standard_application_licence)
assert response.status_code == status.HTTP_200_OK
assert response_data == expected_data

def test_update_license_details_revoked_success_send_hmrc(self):
data = {"status": "revoked"}
with mock.patch("api.licences.models.Licence.revoke") as save_method_mock:
response = self.client.patch(self.url, data, **self.gov_headers)
response_data = response.json()
self.standard_application_licence.refresh_from_db()
expected_data = {
"id": str(self.standard_application_licence.id),
"reference_code": self.standard_application_licence.reference_code,
**data,
}
save_method_mock.assert_called_once()
save_method_mock.assert_called_once_with(self.standard_application_licence, send_status_change_to_hmrc=True)
assert response.status_code == status.HTTP_200_OK
assert response_data == expected_data

@parameterized.expand(
[
[{"status": "revoked"}],
[{"status": "issued"}],
[{"status": "suspended"}],
]
)
def test_update_license_details(self, data):
def test_update_license_details_put_fails(self, data):
response = self.client.put(self.url, data, **self.gov_headers)
assert response.status_code == status.HTTP_403_FORBIDDEN

def test_update_license_details_invalid_status(self):
data = {"status": "dummy"}
response = self.client.patch(self.url, data, **self.gov_headers)

response_data = response.json()
self.standard_application_licence.refresh_from_db()
expected_data = {
"id": str(self.standard_application_licence.id),
"reference_code": self.standard_application_licence.reference_code,
**data,
}

assert response.status_code == status.HTTP_200_OK
assert response_data == expected_data

assert response.json()["errors"] == {"status": ['"dummy" is not a valid choice.']}
assert response.status_code == status.HTTP_400_BAD_REQUEST
response = self.client.put(self.url, data, **self.gov_headers)
assert response.status_code == status.HTTP_403_FORBIDDEN

Expand All @@ -266,15 +301,8 @@ def test_update_license_details(self, data):
)
def test_update_license_details_not_updated(self, data):

key = list(data.keys())[0]
old_state = getattr(self.standard_application_licence, key)

response = self.client.patch(self.url, data, **self.gov_headers)
assert response.status_code == status.HTTP_200_OK
self.standard_application_licence.refresh_from_db()
new_state = getattr(self.standard_application_licence, key)

assert old_state == new_state
assert response.status_code == status.HTTP_400_BAD_REQUEST

def test_update_license_details_non_lu_admin_forbidden(self):

Expand Down

0 comments on commit ce17d0d

Please sign in to comment.