Beispiel #1
0
    def test_submit_F680_clearance_with_location_failure(self):
        SiteOnApplication(site=self.organisation.primary_site,
                          application=self.draft).save()

        response = self.client.put(self.url, **self.exporter_headers)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.json()["errors"]["location"],
                         [strings.Applications.F680.LOCATIONS])
Beispiel #2
0
    def create_hmrc_query(
        self,
        organisation: Organisation,
        reference_name="HMRC Query",
        safe_document=True,
        have_goods_departed=False,
    ):
        application = HmrcQuery(
            name=reference_name,
            case_type_id=CaseTypeEnum.HMRC.id,
            activity="Trade",
            usage="Trade",
            organisation=organisation,
            hmrc_organisation=self.hmrc_organisation,
            reasoning="I Am Easy to Find",
            status=get_case_status_by_status(CaseStatusEnum.DRAFT),
            have_goods_departed=have_goods_departed,
            submitted_by=self.hmrc_exporter_user,
        )
        application.save()

        end_user = self.create_party("End User", organisation,
                                     PartyType.END_USER, application)
        consignee = self.create_party("Consignee", organisation,
                                      PartyType.CONSIGNEE, application)
        third_party = self.create_party("Third party", organisation,
                                        PartyType.THIRD_PARTY, application)

        self.assertEqual(end_user, application.end_user.party)
        self.assertEqual(consignee, application.consignee.party)
        self.assertEqual(third_party, application.third_parties.get().party)

        goods_type = GoodsTypeFactory(application=application)

        # Set the application party documents
        self.create_document_for_party(application.end_user.party,
                                       safe=safe_document)
        self.create_document_for_party(application.consignee.party,
                                       safe=safe_document)
        self.create_document_for_party(application.third_parties.first().party,
                                       safe=safe_document)

        self.create_document_for_goods_type(goods_type)

        # Add a site to the application
        SiteOnApplication(site=organisation.primary_site,
                          application=application).save()

        return application
    def test_ensure_user_cannot_see_applications_they_only_have_partial_access_to_(self):
        """
        Ensure that the exporter cannot see applications with sites that they don't have access to AND
        sites that they are assigned to.
        """
        relationship = get_user_organisation_relationship(self.exporter_user, self.organisation)
        relationship.sites.set([self.organisation.primary_site])
        site_2 = SiteFactory(organisation=self.organisation)
        application = self.create_draft_standard_application(self.organisation)
        SiteOnApplication(site=site_2, application=application).save()

        response = self.client.get(self.url, **self.exporter_headers)
        response_data = response.json()["results"]

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(len(response_data), 0)
Beispiel #4
0
    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
Beispiel #5
0
    def create_mod_clearance_application(
        self,
        organisation,
        case_type,
        reference_name="MOD Clearance Draft",
        safe_document=True,
        additional_information=True,
    ):
        if case_type == CaseTypeEnum.F680:
            model = F680ClearanceApplication
        elif case_type == CaseTypeEnum.GIFTING:
            model = GiftingClearanceApplication
        elif case_type == CaseTypeEnum.EXHIBITION:
            model = ExhibitionClearanceApplication
        else:
            raise BaseException(
                "Invalid case type when creating test MOD Clearance application"
            )

        application = model.objects.create(
            name=reference_name,
            activity="Trade",
            usage="Trade",
            organisation=organisation,
            case_type_id=case_type.id,
            status=get_case_status_by_status(CaseStatusEnum.DRAFT),
            clearance_level=PvGrading.UK_UNCLASSIFIED
            if case_type == CaseTypeEnum.F680 else None,
            submitted_by=self.exporter_user,
        )

        if case_type == CaseTypeEnum.EXHIBITION:
            application.title = "title"
            application.required_by_date = "2021-07-20"
            application.first_exhibition_date = "2022-08-19"
            application.save()
            # must be refreshed to return data in same format as database call
            application.refresh_from_db()
        elif case_type == CaseTypeEnum.F680:
            application.types.add(F680ClearanceType.objects.first())
            self.create_party("End User", organisation, PartyType.END_USER,
                              application)
            self.create_party("Third party", organisation,
                              PartyType.THIRD_PARTY, application)
            self.add_party_documents(
                application,
                safe_document,
                consignee=case_type == CaseTypeEnum.EXHIBITION)
            if additional_information:
                self.add_additional_information(application)
            application.intended_end_use = "intended end use here"
            application.save()
        else:
            self.create_party("End User", organisation, PartyType.END_USER,
                              application)
            self.create_party("Third party", organisation,
                              PartyType.THIRD_PARTY, application)
            self.add_party_documents(
                application,
                safe_document,
                consignee=case_type == CaseTypeEnum.EXHIBITION)

        if case_type not in [
                CaseTypeEnum.F680, CaseTypeEnum.EXHIBITION,
                CaseTypeEnum.GIFTING
        ]:
            self.create_party("Consignee", organisation, PartyType.CONSIGNEE,
                              application)

        # Add a good to the standard application
        self.good_on_application = GoodOnApplication.objects.create(
            good=GoodFactory(organisation=organisation,
                             is_good_controlled=True),
            application=application,
            quantity=10,
            unit=Units.NAR,
            value=500,
        )

        self.create_application_document(application)

        if case_type == CaseTypeEnum.EXHIBITION:
            # Add a site to the application
            SiteOnApplication(site=organisation.primary_site,
                              application=application).save()

        return application
Beispiel #6
0
    def create_draft_standard_application(
        self,
        organisation: Organisation,
        reference_name="Standard Draft",
        safe_document=True,
        parties=True,
        site=True,
        case_type_id=CaseTypeEnum.SIEL.id,
        add_a_good=True,
        user: ExporterUser = None,
        good=None,
    ):
        if not user:
            user = UserOrganisationRelationship.objects.filter(
                organisation_id=organisation.id).first().user

        application = StandardApplication(
            name=reference_name,
            export_type=ApplicationExportType.PERMANENT,
            case_type_id=case_type_id,
            have_you_been_informed=ApplicationExportLicenceOfficialType.YES,
            reference_number_on_information_form="",
            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,
            is_eu_military=False,
            is_compliant_limitations_eu=None,
            intended_end_use="this is our intended end use",
            is_shipped_waybill_or_lading=True,
            non_waybill_or_lading_route_details=None,
            status_id="00000000-0000-0000-0000-000000000000",
            submitted_by=user,
        )

        application.save()

        if add_a_good:
            # Add a good to the standard application
            self.good_on_application = GoodOnApplication.objects.create(
                good=good if good else GoodFactory(organisation=organisation,
                                                   is_good_controlled=True),
                application=application,
                quantity=10,
                unit=Units.NAR,
                value=500,
            )

        if parties:
            self.create_party("End User", organisation, PartyType.END_USER,
                              application)
            self.create_party("Consignee", organisation, PartyType.CONSIGNEE,
                              application)
            self.create_party("Third party", organisation,
                              PartyType.THIRD_PARTY, application)
            # Set the application party documents

            self.add_party_documents(application, safe_document)

        self.create_application_document(application)

        # Add a site to the application
        if site:
            SiteOnApplication(site=organisation.primary_site,
                              application=application).save()

        return application
from test_helpers.clients import DataTestClient


class ComplianceCreateTests(DataTestClient):
    def tests_OIEL_type(self):
        case = self.create_open_application_case(self.organisation)

        generate_compliance_site_case(case)

        self.assertTrue(ComplianceSiteCase.objects.exists())

    def tests_multi_site(self):
        case = self.create_open_application_case(self.organisation)

        # create and add 2nd site to application
        SiteOnApplication(site=SiteFactory(organisation=self.organisation),
                          application=case).save()

        generate_compliance_site_case(case)

        self.assertEqual(ComplianceSiteCase.objects.count(), 2)

    def tests_multi_site_same_record_holder(self):
        case = self.create_open_application_case(self.organisation)

        # create and add 2nd site to application
        new_site = SiteFactory(organisation=self.organisation)
        new_site.site_records_located_at = self.organisation.primary_site
        new_site.save()
        SiteOnApplication(site=new_site, application=case).save()

        generate_compliance_site_case(case)
Beispiel #8
0
def add_sites_to_application(user: ExporterUser, new_sites: Union[QuerySet,
                                                                  List[Site]],
                             application: BaseApplication):
    """
    Add sites to an application, handle validation and audit
    """
    sites_on_application = SiteOnApplication.objects.filter(
        application=application)

    if not new_sites:
        raise ValidationError({"sites": ["Select at least one site"]})

    # Users don't specify their goods locations if they've already departed
    if getattr(application, "have_goods_departed", False):
        raise ValidationError(
            {"sites": [Applications.Generic.GOODS_ALREADY_DEPARTED]})

    # Sites can't be set if the case is in a read only state
    if application.status.status in get_case_statuses(read_only=True):
        raise ValidationError({
            "sites":
            [f"Application status {application.status.status} is read-only"]
        })

    # Transhipment and Trade Control applications can't have sites based in certain countries
    if application.case_type.id in [
            *CaseTypeEnum.trade_control_case_type_ids(), CaseTypeEnum.SITL.id
    ]:
        banned_sites = new_sites.filter(
            address__country_id__in=
            TRANSHIPMENT_AND_TRADE_CONTROL_BANNED_COUNTRIES).values_list(
                "address__country_id", flat=True)

        if banned_sites:
            raise ValidationError({
                "sites": [
                    ExternalLocations.Errors.COUNTRY_ON_APPLICATION %
                    (", ".join(banned_sites), application.case_type.reference)
                ]
            })

    # It's possible for users to modify sites as long as sites they add are in countries
    # already on the application
    if not application.is_major_editable():
        old_countries = sites_on_application.values_list(
            "site__address__country")
        difference = new_sites.exclude(address__country__in=old_countries)

        if difference:
            raise ValidationError({
                "sites":
                ["Sites have to be in the same country on minor edits"]
            })

    removed_sites_on_application = sites_on_application.exclude(
        site__id__in=new_sites)
    added_sites = new_sites.exclude(
        id__in=sites_on_application.values_list("site__id", flat=True))
    removed_locations = ExternalLocationOnApplication.objects.filter(
        application=application)

    _set_activity(
        user=user,
        application=application,
        removed_locations=removed_locations,
        removed_sites=removed_sites_on_application,
        added_sites=added_sites,
    )

    # Check if site has been removed and if it is no longer used on other applications set "is_used_on_application" to False
    # Only do this for minor/major edits
    if application.status.status != CaseStatusEnum.DRAFT:
        removed_site_ids = removed_sites_on_application.values_list("site_id",
                                                                    flat=True)
        if removed_site_ids:
            sites_still_on_other_applications = (
                SiteOnApplication.objects.exclude(
                    application=application).exclude(
                        application__status__status="draft").filter(
                            site__id__in=removed_site_ids))
            removed_sites_no_longer_on_other_applications = removed_sites_on_application.exclude(
                site__id__in=sites_still_on_other_applications.values_list(
                    "site_id", flat=True))

            removed_sites = Site.objects.filter(
                id__in=removed_sites_no_longer_on_other_applications.
                values_list("site_id", flat=True))
            removed_sites.update(is_used_on_application=False)

        # Set "is_used_on_application" to True for sites that have been added
        added_sites.update(is_used_on_application=True)

    # Save the new sites
    SiteOnApplication.objects.bulk_create([
        SiteOnApplication(site=site, application=application)
        for site in added_sites
    ])

    removed_sites_on_application.delete()
    removed_locations.delete()

    # Update application activity
    application.activity = TRADING
    application.save()