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 _assert_dto(self, data, licence, old_licence=None): if old_licence: self.assertEqual(len(data), 10) self.assertEqual(data["old_id"], str(old_licence.id)) else: self.assertEqual(len(data), 9) self.assertEqual(data["id"], str(licence.id)) self.assertEqual(data["reference"], licence.reference_code) self.assertEqual(data["type"], licence.case.case_type.reference) self.assertEqual( data["action"], licence_status_to_hmrc_integration_action.get(licence.status)) self.assertEqual(data["start_date"], licence.start_date.strftime("%Y-%m-%d")) self.assertEqual( data["end_date"], add_months(licence.start_date, licence.duration, "%Y-%m-%d")) self._assert_organisation(data, licence.case.organisation) if licence.case.case_type.sub_type == CaseTypeSubTypeEnum.STANDARD: self._assert_end_user(data, licence.case.end_user.party) self._assert_goods_on_licence(data, licence.goods.all()) self.assertEqual(data["id"], str(licence.id)) elif licence.case.case_type.id in CaseTypeEnum.OPEN_GENERAL_LICENCE_IDS: self._assert_countries( data, licence.case.opengenerallicencecase.open_general_licence. countries.order_by("name")) elif licence.case.case_type.sub_type == CaseTypeSubTypeEnum.OPEN: self._assert_countries( data, get_approved_countries(licence.case.baseapplication)) self._assert_goods_types( data, get_approved_goods_types(licence.case.baseapplication))
def setUp(self): super().setUp() self.standard_application = self.create_standard_application_case( self.organisation) self.create_advice(self.gov_user, self.standard_application, "good", AdviceType.APPROVE, AdviceLevel.FINAL) status = LicenceStatus.ISSUED self.hmrc_integration_status = licence_status_to_hmrc_integration_action.get( status) self.standard_licence = self.create_licence(self.standard_application, status=status)
def test_licence_exhaust_calls_schedule_licence_for_hmrc_integration( self, schedule_licence_for_hmrc_integration): schedule_licence_for_hmrc_integration.return_value = None self.standard_licence.exhaust() schedule_licence_for_hmrc_integration.assert_called_with( str(self.standard_licence.id), licence_status_to_hmrc_integration_action.get( self.standard_licence.status), )
def test_save_licence_calls_schedule_licence_for_hmrc_integration( self, schedule_licence_for_hmrc_integration): schedule_licence_for_hmrc_integration.return_value = None self.standard_licence.save(send_status_change_to_hmrc=True) schedule_licence_for_hmrc_integration.assert_called_with( str(self.standard_licence.id), licence_status_to_hmrc_integration_action.get( self.standard_licence.status), )
def schedule_not_sent_licences(): """Send licence info to HMRC integration""" from api.licences.models import Licence from api.licences.tasks import schedule_licence_for_hmrc_integration licences_not_sent = Licence.objects.filter( hmrc_integration_sent_at__isnull=True).exclude( status=LicenceStatus.DRAFT) for licence in licences_not_sent: schedule_licence_for_hmrc_integration( str(licence.id), licence_status_to_hmrc_integration_action.get(licence.status))
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_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 send_to_hmrc_integration(self): from api.licences.tasks import schedule_licence_for_hmrc_integration schedule_licence_for_hmrc_integration( str(self.id), licence_status_to_hmrc_integration_action.get(self.status))
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if not (hasattr(self.instance.case, "baseapplication") and self.instance.case.baseapplication.end_user): self.fields.pop("end_user") if (hasattr(self.instance.case, "baseapplication") and not (hasattr(self.instance.case.baseapplication, "openapplication") and self.instance.case.baseapplication. openapplication.application_countries.exists()) and not hasattr(self.instance.case, "opengenerallicencecase")): self.fields.pop("countries") self.action = licence_status_to_hmrc_integration_action.get( self.instance.status) if self.action != HMRCIntegrationActionEnum.UPDATE: self.fields.pop("old_id") def get_action(self, _): return self.action def get_old_id(self, instance): return str( Licence.objects.filter(case=instance.case, status=LicenceStatus.CANCELLED).order_by( "created_at").values_list( "id", flat=True).last()) def get_countries(self, instance): if hasattr(instance.case, "baseapplication") and hasattr(