def setUp(self): super().setUp() self.open_draft = self.create_draft_open_application(self.organisation) self.goods_types = GoodsType.objects.filter( application=self.open_draft).order_by("id") self.goods_type_1 = self.goods_types[0] self.goods_type_2 = self.goods_types[1] # Add a country to the draft self.country_1 = get_country("ES") self.country_2 = get_country("US") self.country_3 = get_country("FR") self.all_countries = [self.country_1, self.country_2, self.country_3] for country in self.all_countries: CountryOnApplication(application=self.open_draft, country=country).save() self.good_url = reverse( "applications:application_goodstype", kwargs={ "pk": self.open_draft.id, "goodstype_pk": self.goods_type_1.id }, ) self.good_country_url = reverse( "applications:application_goodstype_assign_countries", kwargs={"pk": self.open_draft.id}, )
def test_remove_countries_from_a_submitted_application_success(self): CountryOnApplication(application=self.draft, country=get_country("US")).save() self.submit_application(self.draft) countries_on_app_before = CountryOnApplication.objects.filter( application=self.draft).count() data = {"countries": [get_country("FR").pk]} response = self.client.post(self.url, data, **self.exporter_headers) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual( CountryOnApplication.objects.filter( application=self.draft).count(), countries_on_app_before - 1, )
def test_set_end_user_on_draft_standard_application_successful(self, data_type): data = { "name": "Government", "address": "Westminster, London SW1A 0AA", "country": "GB", "sub_type": data_type, "website": "https://www.gov.uk", "type": PartyType.END_USER, } if data_type == SubType.OTHER: data["sub_type_other"] = "Other" response = self.client.post(self.url, data, **self.exporter_headers) party_on_application = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.END_USER, deleted_at__isnull=True, ) self.draft.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(party_on_application.party.name, data["name"]) self.assertEqual(party_on_application.party.address, data["address"]) self.assertEqual(party_on_application.party.country, get_country(data["country"])) self.assertEqual(party_on_application.party.sub_type, data_type) self.assertEqual(party_on_application.party.sub_type_other, data.get("sub_type_other")) self.assertEqual(party_on_application.party.website, data["website"])
def create_party(name, organisation, party_type, application=None, pk=None, country_code="GB", role=PartyRole.AGENT): if not pk: pk = uuid.uuid4() data = { "id": pk, "name": name, "organisation": organisation, "address": "42 Road, London, Buckinghamshire", "website": "www." + name + ".com", "sub_type": SubType.GOVERNMENT, "type": party_type, "country": get_country(country_code), } if party_type == PartyType.THIRD_PARTY: data["role"] = role party = Party(**data) party.save() if application and party_type == PartyType.ADDITIONAL_CONTACT: application.additional_contacts.add(party) elif application: # Attach party to application application.add_party(party) return party
def test_set_consignee_on_draft_successful(self, data_type): """ Given a standard draft has been created And the draft does not yet contain a consignee When a new consignee is added Then the consignee is successfully added to the draft """ data = { "name": "Government of Paraguay", "address": "Asuncion", "country": "PY", "sub_type": data_type, "website": "https://www.gov.py", "type": PartyType.CONSIGNEE, } if data_type == SubType.OTHER: data["sub_type_other"] = "Other" response = self.client.post(self.url, data, **self.exporter_headers) party_on_application = PartyOnApplication.objects.get( application=self.draft, party__type=PartyType.CONSIGNEE, deleted_at__isnull=True, ) self.draft.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(party_on_application.party.name, data["name"]) self.assertEqual(party_on_application.party.address, data["address"]) self.assertEqual(party_on_application.party.country, get_country(data["country"])) self.assertEqual(party_on_application.party.sub_type, data_type) self.assertEqual(party_on_application.party.website, data["website"])
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) organisation_type = self.initial_data.get("type") or self.instance.type in_uk = self.initial_data.get("location", "united_kingdom") == "united_kingdom" if self.instance: in_uk = self.instance.primary_site.address.country == get_country( "GB") if organisation_type != OrganisationType.HMRC and in_uk: self.fields["eori_number"].allow_blank = False self.fields["eori_number"].allow_null = False if organisation_type == OrganisationType.COMMERCIAL and in_uk: self.fields["vat_number"].allow_blank = False self.fields["vat_number"].allow_null = False self.fields["registration_number"].allow_blank = False self.fields["registration_number"].allow_null = False self.fields["sic_number"].allow_blank = False self.fields["sic_number"].allow_null = False if "data" in kwargs: if "user" in kwargs["data"]: kwargs["data"]["user"]["sites"] = kwargs["data"]["user"].get( "sites", [])
def test_cannot_change_countries_on_goodstype_on_cryptographic_application(self): country_1 = get_country("ES") country_2 = get_country("US") country_3 = get_country("FR") application = self.create_draft_open_application(organisation=self.organisation) application.goodstype_category = GoodsTypeCategory.CRYPTOGRAPHIC application.save() goodstype = GoodsType.objects.filter(application=application).first() initial_countries_count = goodstype.countries.count() data = {str(goodstype.id): [country_1.id, country_2.id, country_3.id]} url = reverse("applications:application_goodstype_assign_countries", kwargs={"pk": application.id}) response = self.client.put(url, data, **self.exporter_headers) goodstype.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(goodstype.countries.count(), initial_countries_count)
def test_countries_ordered_as_expected_on_open_application(self): highest_priority_flag = FlagFactory(name="highest priority flag", level="Destination", team=self.gov_user.team, priority=0) lowest_priority_flag = FlagFactory(name="lowest priority flag", level="Destination", team=self.gov_user.team, priority=10) open_application = self.create_draft_open_application( self.organisation) # Countries with flags added portugal = get_country("PT") portugal.flags.set([highest_priority_flag]) andorra = get_country("AD") andorra.flags.set([lowest_priority_flag]) benin = get_country("BJ") benin.flags.set([lowest_priority_flag]) # Countries without flags added # Add additional countries to the application ad = CountryOnApplication(application=open_application, country=get_country("AD")) ad.save() bj = CountryOnApplication(application=open_application, country=get_country("BJ")) bj.save() at = CountryOnApplication(application=open_application, country=get_country("AT")) at.save() pt = CountryOnApplication(application=open_application, country=get_country("PT")) pt.save() # FR already on draft open application fr = CountryOnApplication.objects.get(application=open_application, country_id="FR") case = self.submit_application(open_application) url = reverse("cases:case", kwargs={"pk": case.id}) response = self.client.get(url, **self.gov_headers) self.assertEqual(response.status_code, status.HTTP_200_OK) case_application = response.json()["case"]["data"] ordered_countries = [ destination["id"] for destination in case_application["destinations"]["data"] ] # Countries are ordered by flag priority and for countries without flags, they are alphabetised self.assertEqual( ordered_countries, [str(pt.id), str(ad.id), str(bj.id), str(at.id), str(fr.id)])
def create_external_location(name, org, country="GB"): external_location = ExternalLocation( name=name, address="20 Questions Road, Enigma", country=get_country(country), organisation=org, ) external_location.save() return external_location
def test_all_countries_added_media(self): data = { "name": "Test", "export_type": ApplicationExportType.PERMANENT, "application_type": CaseTypeReferenceEnum.OIEL, "goodstype_category": GoodsTypeCategory.MEDIA, } response = self.client.post(self.url, data, **self.exporter_headers) self.assertEqual(response.status_code, status.HTTP_201_CREATED) returned_coa = CountryOnApplication.objects.filter( application=OpenApplication.objects.first()) # Ensure the UK is not in the list of media destinations self.assertNotIn(get_country("GB"), returned_coa) self.assertEqual(returned_coa.count(), Country.exclude_special_countries.count() - 1)
def test_add_site_to_a_submitted_application_failure(self): """ Cannot add additional site to a submitted application unless the additional site is located in a country that is already on the application """ site_to_add = SiteFactory(organisation=self.organisation, address__country=get_country("PL")) data = {"sites": [self.primary_site.id, site_to_add.id]} self.submit_application(self.application) response = self.client.post(self.url, data, **self.exporter_headers) self.application.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( SiteOnApplication.objects.filter( application=self.application).count(), 1)
def seed_default_organisations(cls): for organisation in COMMERCIAL_ORGANISATIONS + HMRC_ORGANISATIONS: org, created = Organisation.objects.get_or_create( name__iexact=organisation["name"], defaults={ "name": organisation["name"], "type": organisation["type"], "eori_number": "1234567890AAA", "sic_number": "2345", "vat_number": "GB123456789", "registration_number": organisation["reg_no"], "status": OrganisationStatus.ACTIVE, }, ) if created: address = Address.objects.create( address_line_1="42 Question Road", address_line_2="", country=get_country("GB"), city="London", region="Greater London", postcode="SW1A 0AA", ) site = Site.objects.create(name="Headquarters", organisation=org, address=address) site.site_records_located_at = site site.save() org.primary_site = site org.save() cls.print_created_or_updated( Organisation, { "name": organisation["name"], "type": organisation["type"] }, is_created=True, )
def create_draft_open_application(self, organisation: Organisation, reference_name="Open Draft", case_type_id=CaseTypeEnum.OIEL.id): application = OpenApplication( name=reference_name, case_type_id=case_type_id, export_type=ApplicationExportType.PERMANENT, activity="Trade", usage="Trade", organisation=organisation, status=get_case_status_by_status(CaseStatusEnum.DRAFT), is_military_end_use_controls=False, is_informed_wmd=False, is_suspected_wmd=False, intended_end_use="intended end use is none of your business", is_shipped_waybill_or_lading=True, non_waybill_or_lading_route_details=None, status_id="00000000-0000-0000-0000-000000000000", submitted_by=self.exporter_user, ) application.save() # Add a goods description GoodsTypeFactory(application=application, is_good_controlled=True) GoodsTypeFactory(application=application, is_good_controlled=True) # Add a country to the application - GB cannot be a destination on licences! CountryOnApplication(application=application, country=get_country("FR")).save() # Add a site to the application SiteOnApplication(site=organisation.primary_site, application=application).save() return application
def post(self, request, pk): """ Add countries to an open licence application. """ application = get_application(pk) if application.goodstype_category in GoodsTypeCategory.IMMUTABLE_DESTINATIONS: raise BadRequestError( detail= "You cannot do this action for this type of open application") data = request.data country_ids = data.get("countries") # Validate that there are countries if not country_ids: return JsonResponse( data={ "errors": { "countries": ["You have to pick at least one country"] } }, status=status.HTTP_400_BAD_REQUEST, ) if not is_case_status_draft( application.status.status ) and application.status.status in get_case_statuses(read_only=True): return JsonResponse( data={ "errors": { "external_locations": [ f"Application status {application.status.status} is read-only." ] } }, status=status.HTTP_400_BAD_REQUEST, ) else: previous_countries = CountryOnApplication.objects.filter( application=application) previous_country_ids = [ str(previous_country_id) for previous_country_id in previous_countries.values_list("country__id", flat=True) ] new_countries = [] if (is_case_status_draft(application.status.status) or application.status.status == CaseStatusEnum.APPLICANT_EDITING): new_countries = [ get_country(country_id) for country_id in country_ids if country_id not in previous_country_ids ] else: for country_id in country_ids: if previous_country_ids and country_id not in previous_country_ids: return JsonResponse( data={ "errors": { "countries": [ "Go back and change your answer from ‘Change a site, or delete " "a good, third party or country’ to ’Change something else’." ] } }, status=status.HTTP_400_BAD_REQUEST, ) # Get countries to be removed removed_country_ids = list( set(previous_country_ids) - set(country_ids)) removed_countries = previous_countries.filter( country__id__in=removed_country_ids) # Append new Countries to application (only in unsubmitted/applicant editing statuses) CountryOnApplication.objects.bulk_create([ CountryOnApplication(country=country, application=application) for country in new_countries ]) countries_data = CountrySerializer(new_countries, many=True).data case = Case.objects.get(id=application.id) if new_countries: audit_trail_service.create( actor=request.user, verb=AuditType.ADD_COUNTRIES_TO_APPLICATION, target=case, payload={ "countries": [country.name for country in new_countries] }, ) if removed_countries: audit_trail_service.create( actor=request.user, verb=AuditType.REMOVED_COUNTRIES_FROM_APPLICATION, target=case, payload={ "countries": [ country.country.name for country in removed_countries ] }, ) removed_countries.delete() return JsonResponse(data={"countries": countries_data}, status=status.HTTP_201_CREATED)