def test_reinstate_licence_success(self, request):
        request.return_value = MockResponse("", 201)
        # Create Case with Licence
        standard_application, standard_licence_1 = self._create_licence_for_submission(
            self.create_standard_application_case)
        standard_licence_1.status = LicenceStatus.ISSUED
        standard_licence_1.save()
        # Create second Licence for Case
        _, standard_licence_2 = self._create_licence_for_submission(
            self.create_standard_application_case)
        # Temporarily manipulate second Licence so it can be used to build 'expected data' in the request
        standard_licence_2.case = standard_application
        standard_licence_2.status = LicenceStatus.REINSTATED
        standard_licence_2.save()
        # Build the 'expected data' from second Licence
        expected_reinstate_json = HMRCIntegrationLicenceSerializer(
            standard_licence_2).data
        expected_reinstate_json["action"] = HMRCIntegrationActionEnum.UPDATE
        # The request to HMRC Integration should contain the old Licence's ID
        expected_reinstate_json["old_id"] = str(standard_licence_1.id)
        # Revert second Licence back to a status required for `/cases/finalise/`
        standard_licence_2.status = LicenceStatus.DRAFT
        standard_licence_2.save()

        url = reverse("cases:finalise", kwargs={"pk": standard_application.id})
        response = self.client.put(url, data={}, **self.gov_headers)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        request.assert_called_once_with(
            "POST",
            f"{LITE_HMRC_INTEGRATION_URL}{SEND_LICENCE_ENDPOINT}",
            json={"licence": expected_reinstate_json},
            headers=ANY,
            timeout=LITE_HMRC_REQUEST_TIMEOUT,
        )
    def test_revoke_licence_success(self, request):
        request.return_value = MockResponse("", 201)
        standard_application, standard_licence = self._create_licence_for_submission(
            self.create_standard_application_case)
        # Set Case to Finalised so it can be revoked
        standard_application.status = get_case_status_by_status(
            CaseStatusEnum.FINALISED)
        standard_application.save()
        # Give Gov User permission to change status of a Finalised Case
        self.gov_user.role.permissions.add(
            GovPermissions.REOPEN_CLOSED_CASES.name)
        standard_licence.status = LicenceStatus.ISSUED
        standard_licence.save()
        expected_insert_json = HMRCIntegrationLicenceSerializer(
            standard_licence).data
        expected_insert_json["action"] = HMRCIntegrationActionEnum.CANCEL

        url = reverse("applications:manage_status",
                      kwargs={"pk": standard_application.id})
        response = self.client.put(url,
                                   data={"status": CaseStatusEnum.REVOKED},
                                   **self.gov_headers)

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        request.assert_called_once_with(
            "POST",
            f"{LITE_HMRC_INTEGRATION_URL}{SEND_LICENCE_ENDPOINT}",
            json={"licence": expected_insert_json},
            headers=ANY,
            timeout=LITE_HMRC_REQUEST_TIMEOUT,
        )
    def test_standard_application(self, status):
        action = licence_status_to_hmrc_integration_action.get(status)
        standard_application = self.create_standard_application_case(
            self.organisation)
        self.create_advice(self.gov_user, standard_application, "good",
                           AdviceType.APPROVE, AdviceLevel.FINAL)
        standard_licence = self.create_licence(standard_application,
                                               status=status)
        good_on_application = standard_application.goods.first()
        GoodOnLicenceFactory(
            good=good_on_application,
            licence=standard_licence,
            quantity=good_on_application.quantity,
            usage=20.0,
            value=good_on_application.value,
        )
        old_licence = None
        if action == HMRCIntegrationActionEnum.UPDATE:
            old_licence = self.create_licence(standard_application,
                                              status=LicenceStatus.CANCELLED)
            standard_application.licences.add(old_licence)

        data = HMRCIntegrationLicenceSerializer(standard_licence).data

        self._assert_dto(data, standard_licence, old_licence=old_licence)
def send_licence(licence: Licence, action: str):
    """Sends licence information to HMRC Integration"""

    logging.info(
        f"Sending licence '{licence.id}', action '{action}' to HMRC Integration"
    )

    url = f"{LITE_HMRC_INTEGRATION_URL}{SEND_LICENCE_ENDPOINT}"
    data = {"licence": HMRCIntegrationLicenceSerializer(licence).data}

    response = post(url,
                    data,
                    hawk_credentials=HAWK_LITE_API_CREDENTIALS,
                    timeout=LITE_HMRC_REQUEST_TIMEOUT)

    if response.status_code not in [
            status.HTTP_200_OK, status.HTTP_201_CREATED
    ]:
        raise HMRCIntegrationException(
            f"An unexpected response was received when sending licence '{licence.id}', action '{action}' to HMRC "
            f"Integration -> status={response.status_code}, message={response.text}"
        )

    if response.status_code == status.HTTP_201_CREATED:
        licence.hmrc_integration_sent_at = timezone.now()
        licence.save()

    logging.info(
        f"Successfully sent licence '{licence.id}', action '{action}' to HMRC Integration"
    )
    def test_ogl_application(self, status):
        action = licence_status_to_hmrc_integration_action.get(status)
        open_general_licence = OpenGeneralLicenceFactory(
            case_type=CaseType.objects.get(id=CaseTypeEnum.OGEL.id))
        open_general_licence_case = OpenGeneralLicenceCaseFactory(
            open_general_licence=open_general_licence,
            site=self.organisation.primary_site,
            organisation=self.organisation,
        )
        open_general_licence_licence = Licence.objects.get(
            case=open_general_licence_case)

        if action == HMRCIntegrationActionEnum.UPDATE:
            # Cancel the original & issue a new OGL Licence
            open_general_licence_licence.cancel()
            new_licence = issue_open_general_licence(open_general_licence_case)
            data = HMRCIntegrationLicenceSerializer(new_licence).data
            self._assert_dto(data,
                             new_licence,
                             old_licence=open_general_licence_licence)
        else:
            data = HMRCIntegrationLicenceSerializer(
                open_general_licence_licence).data
            self._assert_dto(data, open_general_licence_licence)
    def test_approve_open_application_licence_success(self, request):
        request.return_value = MockResponse("", 201)
        open_application, open_licence = self._create_licence_for_submission(
            self.create_open_application_case)
        expected_insert_json = HMRCIntegrationLicenceSerializer(
            open_licence).data
        expected_insert_json["action"] = HMRCIntegrationActionEnum.INSERT

        url = reverse("cases:finalise", kwargs={"pk": open_application.id})
        response = self.client.put(url, data={}, **self.gov_headers)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        request.assert_called_once_with(
            "POST",
            f"{LITE_HMRC_INTEGRATION_URL}{SEND_LICENCE_ENDPOINT}",
            json={"licence": expected_insert_json},
            headers=ANY,
            timeout=LITE_HMRC_REQUEST_TIMEOUT,
        )
    def test_open_application(self, status):
        action = licence_status_to_hmrc_integration_action.get(status)
        open_application = self.create_open_application_case(self.organisation)
        open_application.goods_type.first().countries.set(
            [Country.objects.first()])
        GoodCountryDecisionFactory(
            case=open_application,
            country=Country.objects.first(),
            goods_type=open_application.goods_type.first(),
            approve=True,
        )
        open_licence = self.create_licence(open_application, status=status)
        old_licence = None
        if action == HMRCIntegrationActionEnum.UPDATE:
            old_licence = self.create_licence(open_application,
                                              status=LicenceStatus.CANCELLED)
            open_application.licences.add(old_licence)

        data = HMRCIntegrationLicenceSerializer(open_licence).data

        self._assert_dto(data, open_licence, old_licence)
    def test_create_ogel_licence_success(self, request):
        request.return_value = MockResponse("", 201)
        open_general_licence = OpenGeneralLicenceFactory(
            case_type=CaseType.objects.get(id=CaseTypeEnum.OGEL.id))
        open_general_licence_case = OpenGeneralLicenceCaseFactory(
            open_general_licence=open_general_licence,
            site=self.organisation.primary_site,
            organisation=self.organisation,
        )
        open_general_licence_licence = Licence.objects.get(
            case=open_general_licence_case)
        expected_insert_json = HMRCIntegrationLicenceSerializer(
            open_general_licence_licence).data
        expected_insert_json["action"] = HMRCIntegrationActionEnum.INSERT

        request.assert_called_with(
            "POST",
            f"{LITE_HMRC_INTEGRATION_URL}{SEND_LICENCE_ENDPOINT}",
            json={"licence": expected_insert_json},
            headers=ANY,
            timeout=LITE_HMRC_REQUEST_TIMEOUT,
        )