def test_view_flag_filter(self):
        flag_1 = FlagFactory(name="Name_1",
                             level="Destination",
                             team=self.gov_user.team,
                             priority=9)
        flag_2 = FlagFactory(name="Name_2",
                             level="Destination",
                             team=self.gov_user.team,
                             priority=10)

        application_1 = StandardApplicationFactory(submitted_at=timezone.now())
        application_1.flags.add(flag_1)
        application_2 = StandardApplicationFactory(submitted_at=timezone.now())
        application_2.flags.add(flag_2)

        url = reverse("cases:search")
        url_1 = f"{url}?flags={flag_1.id}"
        url_2 = f"{url}?flags={flag_2.id}"
        url_3 = f"{url}?flags={flag_1.id}&flags={flag_2.id}"

        response_1 = self.client.get(url_1, **self.gov_headers)
        response_2 = self.client.get(url_2, **self.gov_headers)
        response_3 = self.client.get(url_3, **self.gov_headers)

        data_1 = response_1.json()
        data_2 = response_2.json()
        data_3 = response_3.json()

        self.assertEqual(data_1["count"], 1)
        self.assertEqual(data_2["count"], 1)
        self.assertEqual(data_3["count"], 2)
        self.assertEqual(data_1["results"]["cases"][0]["id"],
                         str(application_1.id))
        self.assertEqual(data_2["results"]["cases"][0]["id"],
                         str(application_2.id))
    def test_filter_with_case_reference_code(self):
        application_1 = StandardApplicationFactory()
        application_2 = StandardApplicationFactory()

        qs_1 = Case.objects.search(case_reference=application_1.reference_code)
        qs_2 = Case.objects.search(
            case_reference=application_1.reference_code[4:])
        qs_3 = Case.objects.search(case_reference=application_2.reference_code)
        qs_4 = Case.objects.search(
            case_reference=application_2.reference_code[4:])

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_3.count(), 1)
        self.assertEqual(qs_4.count(), 1)

        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_1.pk)
        self.assertEqual(qs_3.first().pk, application_2.pk)
        self.assertEqual(qs_4.first().pk, application_2.pk)

        # Search on common substring of both application references
        match = SequenceMatcher(
            None, application_1.reference_code,
            application_2.reference_code).find_longest_match(
                0, len(application_1.reference_code), 0,
                len(application_2.reference_code))

        qs_5 = Case.objects.search(
            case_reference=application_1.reference_code[match.a:match.a +
                                                        match.size])

        self.assertEqual(qs_5.count(), 2)
    def test_filter_finalised_at_range(self):
        day_1 = timezone.datetime(day=10, month=10, year=2020)
        day_2 = timezone.datetime(day=11, month=10, year=2020)
        day_3 = timezone.datetime(day=12, month=10, year=2020)
        day_4 = timezone.datetime(day=13, month=10, year=2020)
        day_5 = timezone.datetime(day=14, month=10, year=2020)
        day_6 = timezone.datetime(day=15, month=10, year=2020)
        day_7 = timezone.datetime(day=16, month=10, year=2020)

        application_1 = StandardApplicationFactory(submitted_at=day_2)
        application_2 = StandardApplicationFactory(submitted_at=day_4)
        application_3 = StandardApplicationFactory(submitted_at=day_6)

        qs_1 = Case.objects.search(submitted_from=day_1.date(),
                                   submitted_to=day_3.date())
        qs_2 = Case.objects.search(submitted_from=day_3.date(),
                                   submitted_to=day_5.date())
        qs_3 = Case.objects.search(submitted_from=day_5.date(),
                                   submitted_to=day_7.date())
        qs_4 = Case.objects.search(submitted_from=day_2.date(),
                                   submitted_to=day_6.date())
        qs_5 = Case.objects.search(submitted_from=day_1.date())
        qs_6 = Case.objects.search(submitted_to=day_7.date())

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_3.count(), 1)
        self.assertEqual(qs_4.count(), 3)
        self.assertEqual(qs_5.count(), 3)
        self.assertEqual(qs_6.count(), 3)
        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_2.pk)
        self.assertEqual(qs_3.first().pk, application_3.pk)
    def test_filter_by_goods_related_description(self):
        application_1 = StandardApplicationFactory()
        good_1 = GoodFactory(
            organisation=application_1.organisation,
            description="Desc 1",
            comment="Comment 1",
            report_summary="Report Summary 1",
        )
        GoodOnApplicationFactory(application=application_1, good=good_1)
        application_2 = StandardApplicationFactory()
        good_2 = GoodFactory(organisation=application_2.organisation,
                             description="afdaf",
                             comment="asdfsadf",
                             report_summary="asdfdsf")
        GoodOnApplicationFactory(application=application_2, good=good_2)

        application_3 = StandardApplicationFactory()
        goods_type = GoodsTypeFactory(application=application_3)

        qs_1 = Case.objects.search(
            goods_related_description=good_1.description)
        qs_2 = Case.objects.search(goods_related_description=good_1.comment)
        qs_3 = Case.objects.search(
            goods_related_description=good_1.report_summary)
        qs_4 = Case.objects.search(
            goods_related_description=goods_type.description)

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_3.count(), 1)
        self.assertEqual(qs_4.count(), 1)
        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_1.pk)
        self.assertEqual(qs_3.first().pk, application_1.pk)
        self.assertEqual(qs_4.first().pk, application_3.pk)
    def test_filter_by_flags(self):
        flag_1 = FlagFactory(name="Name_1",
                             level="Destination",
                             team=self.gov_user.team,
                             priority=9)
        flag_2 = FlagFactory(name="Name_2",
                             level="Destination",
                             team=self.gov_user.team,
                             priority=10)
        flag_3 = FlagFactory(name="Name_3",
                             level="good",
                             team=self.gov_user.team,
                             priority=1)
        application_1 = StandardApplicationFactory()
        application_1.flags.add(flag_1)
        application_2 = StandardApplicationFactory()
        application_2.flags.add(flag_2)
        application_3 = StandardApplicationFactory()
        application_3.flags.add(flag_2)
        application_4 = StandardApplicationFactory()
        GoodOnApplicationFactory(
            good=GoodFactory(organisation=application_4.organisation,
                             flags=[flag_3]),
            application=application_4,
        )

        qs_1 = Case.objects.search(flags=[flag_1.id])
        qs_2 = Case.objects.search(flags=[flag_2.id])
        qs_3 = Case.objects.search(flags=[flag_3.id])

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 2)
        self.assertEqual(qs_3.count(), 1)
        self.assertEqual(qs_3.first().pk, application_4.pk)
    def test_filter_sla_days_range(self):
        application_1 = StandardApplicationFactory(sla_remaining_days=1)
        application_2 = StandardApplicationFactory(sla_remaining_days=3)
        application_3 = StandardApplicationFactory(sla_remaining_days=5)

        qs_1 = Case.objects.search(min_sla_days_remaining=0,
                                   max_sla_days_remaining=2)
        qs_2 = Case.objects.search(min_sla_days_remaining=2,
                                   max_sla_days_remaining=4)
        qs_3 = Case.objects.search(min_sla_days_remaining=4,
                                   max_sla_days_remaining=6)
        qs_4 = Case.objects.search(min_sla_days_remaining=0,
                                   max_sla_days_remaining=6)
        qs_5 = Case.objects.search(max_sla_days_remaining=2)
        qs_6 = Case.objects.search(min_sla_days_remaining=4)

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_3.count(), 1)
        self.assertEqual(qs_4.count(), 3)
        self.assertEqual(qs_5.count(), 1)
        self.assertEqual(qs_6.count(), 1)
        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_2.pk)
        self.assertEqual(qs_3.first().pk, application_3.pk)
        self.assertEqual(qs_5.first().pk, application_1.pk)
        self.assertEqual(qs_6.first().pk, application_3.pk)
    def test_filter_with_organisation_name(self):
        application_1 = StandardApplicationFactory()
        application_2 = StandardApplicationFactory()

        qs_1 = Case.objects.search(
            organisation_name=application_1.organisation.name)
        qs_2 = Case.objects.search(
            organisation_name=application_2.organisation.name)

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_2.pk)
    def test_filter_with_exporter_application_reference(self):
        name_1 = "Ref 1"
        name_2 = "Ref 2"
        application_1 = StandardApplicationFactory(name=name_1)
        application_2 = StandardApplicationFactory(name=name_2)

        qs_1 = Case.objects.search(exporter_application_reference=name_1)
        qs_2 = Case.objects.search(exporter_application_reference=name_2)
        qs_3 = Case.objects.search(exporter_application_reference="Ref")

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_3.count(), 2)

        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_2.pk)
    def test_filter_by_finalised_date_range(self):
        day_1 = timezone.datetime(day=10, month=10, year=2020)
        day_2 = timezone.datetime(day=11, month=10, year=2020)
        day_3 = timezone.datetime(day=12, month=10, year=2020)
        day_4 = timezone.datetime(day=13, month=10, year=2020)
        day_5 = timezone.datetime(day=14, month=10, year=2020)

        application_1 = StandardApplicationFactory()
        application_1.status = get_case_status_by_status(
            CaseStatusEnum.FINALISED)
        application_1.save()
        good = GoodFactory(organisation=application_1.organisation)
        FinalAdviceFactory(user=self.gov_user,
                           team=self.team,
                           case=application_1,
                           good=good,
                           type=AdviceType.APPROVE,
                           created_at=day_2)

        application_2 = StandardApplicationFactory()
        application_2.status = get_case_status_by_status(
            CaseStatusEnum.FINALISED)
        application_2.save()
        good = GoodFactory(organisation=application_2.organisation)
        FinalAdviceFactory(user=self.gov_user,
                           team=self.team,
                           case=application_2,
                           good=good,
                           type=AdviceType.APPROVE,
                           created_at=day_4)

        qs_1 = Case.objects.search(finalised_from=day_1, finalised_to=day_3)
        qs_2 = Case.objects.search(finalised_from=day_3, finalised_to=day_5)
        qs_3 = Case.objects.search(finalised_from=day_1)
        qs_4 = Case.objects.search(finalised_to=day_5)
        qs_5 = Case.objects.search(finalised_to=day_1)
        qs_6 = Case.objects.search(finalised_from=day_5)

        self.assertEqual(qs_1.count(), 1)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_3.count(), 2)
        self.assertEqual(qs_4.count(), 2)
        self.assertEqual(qs_5.count(), 0)
        self.assertEqual(qs_6.count(), 0)
        self.assertEqual(qs_1.first().pk, application_1.pk)
        self.assertEqual(qs_2.first().pk, application_2.pk)
Ejemplo n.º 10
0
    def test_get_licence_gov_view(self):
        application = StandardApplicationFactory()
        licence = LicenceFactory(
            case=application, start_date=timezone.now().date(), status=LicenceStatus.ISSUED, duration=100,
        )
        self.url = reverse("cases:licences", kwargs={"pk": application.id})

        good = GoodFactory(organisation=application.organisation)
        good_advice = FinalAdviceFactory(
            user=self.gov_user, team=self.team, case=application, good=good, type=AdviceType.APPROVE
        )
        good_on_application = GoodOnApplicationFactory(
            application=application, good=good, quantity=100.0, value=1500, unit=Units.KGM
        )
        good_on_licence = GoodOnLicenceFactory(
            good=good_on_application,
            quantity=good_on_application.quantity,
            usage=20.0,
            value=good_on_application.value,
            licence=licence,
        )

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

        self.assertEqual(response_data["id"], str(licence.id))
        self.assertEqual(response_data["start_date"], licence.start_date.strftime("%Y-%m-%d"))
        self.assertEqual(response_data["duration"], licence.duration)
        self.assertEqual(response_data["goods_on_licence"][0]["good_on_application_id"], str(good_on_application.id))
        self.assertEqual(response_data["goods_on_licence"][0]["usage"], good_on_licence.usage)
        self.assertEqual(response_data["goods_on_licence"][0]["description"], good.description)
        self.assertEqual(response_data["goods_on_licence"][0]["units"]["key"], good_on_application.unit)
        self.assertEqual(response_data["goods_on_licence"][0]["applied_for_quantity"], good_on_application.quantity)
        self.assertEqual(response_data["goods_on_licence"][0]["applied_for_value"], good_on_application.value)
        self.assertEqual(response_data["goods_on_licence"][0]["licenced_quantity"], good_on_licence.quantity)
        self.assertEqual(response_data["goods_on_licence"][0]["licenced_value"], good_on_licence.value)
        self.assertEqual(
            response_data["goods_on_licence"][0]["applied_for_value_per_item"],
            good_on_application.value / good_on_application.quantity,
        )
        self.assertEqual(
            response_data["goods_on_licence"][0]["licenced_value_per_item"],
            good_on_licence.value / good_on_licence.quantity,
        )
        self.assertEqual(
            len(response_data["goods_on_licence"][0]["control_list_entries"]), good.control_list_entries.count()
        )
        self.assertEqual(response_data["goods_on_licence"][0]["advice"]["type"]["key"], good_advice.type)
        self.assertEqual(response_data["goods_on_licence"][0]["advice"]["text"], good_advice.text)
        self.assertEqual(response_data["goods_on_licence"][0]["advice"]["proviso"], good_advice.proviso)
    def test_filter_by_final_advice(self):
        application = StandardApplicationFactory()
        good = GoodFactory(organisation=application.organisation)
        FinalAdviceFactory(user=self.gov_user,
                           team=self.team,
                           case=application,
                           good=good,
                           type=AdviceType.APPROVE)

        qs_1 = Case.objects.search(final_advice_type=AdviceType.CONFLICTING)
        qs_2 = Case.objects.search(final_advice_type=AdviceType.APPROVE)

        self.assertEqual(qs_1.count(), 0)
        self.assertEqual(qs_2.count(), 1)
        self.assertEqual(qs_2.first().pk, application.pk)
Ejemplo n.º 12
0
class ChangeStatusTests(DataTestClient):
    def setUp(self):
        super().setUp()
        self.case = StandardApplicationFactory()
        self.url = reverse("cases:case", kwargs={"pk": self.case.id})

    def test_optional_note(self):
        """
        When changing status, allow for optional notes to be added
        """

        data = {"status": CaseStatusEnum.WITHDRAWN, "note": faker.word()}

        response = self.client.patch(self.url, data, **self.gov_headers)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Audit.objects.get(verb=AuditType.UPDATED_STATUS).payload["additional_text"], data["note"])

    @parameterized.expand(
        [
            (CaseStatusEnum.SUSPENDED, LicenceStatus.SUSPENDED,),
            (CaseStatusEnum.SURRENDERED, LicenceStatus.SURRENDERED,),
            (CaseStatusEnum.REVOKED, LicenceStatus.REVOKED,),
        ]
    )
    def test_certain_case_statuses_changes_licence_status(self, case_status, licence_status):
        licence = self.create_licence(self.case, status=LicenceStatus.ISSUED)

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

        self.case.refresh_from_db()
        licence.refresh_from_db()

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(self.case.status.status, case_status)
        self.assertEqual(licence.status, licence_status)
    def test_filter_by_good_control_list_entry(self):
        application_1 = StandardApplicationFactory()
        good = GoodFactory(
            organisation=application_1.organisation,
            is_good_controlled=True,
            control_list_entries=["ML1a"],
        )
        GoodOnApplicationFactory(application=application_1, good=good)

        application_2 = OpenApplicationFactory()
        GoodsTypeFactory(application=application_2,
                         is_good_controlled=True,
                         control_list_entries=["ML2a"])

        qs_1 = Case.objects.search(control_list_entry="")
        qs_2 = Case.objects.search(control_list_entry="ML1b")
        qs_3 = Case.objects.search(control_list_entry="ML1a")
        qs_4 = Case.objects.search(control_list_entry="ML2a")

        self.assertEqual(qs_1.count(), 2)
        self.assertEqual(qs_2.count(), 0)
        self.assertEqual(qs_3.count(), 1)
        self.assertEqual(qs_4.count(), 1)
Ejemplo n.º 14
0
 def setUp(self):
     super().setUp()
     self.application = StandardApplicationFactory()
     self.licence = LicenceFactory(
         case=self.application,
         start_date=timezone.now().date(),
         status=LicenceStatus.REVOKED,
         duration=100,
         reference_code="reference",
     )
     self.good = GoodFactory(organisation=self.application.organisation)
     self.good_on_application = GoodOnApplicationFactory(
         application=self.application,
         good=self.good,
         quantity=100.0,
         value=Decimal("1000.00"))
     self.good_on_licence = GoodOnLicenceFactory(
         good=self.good_on_application,
         quantity=self.good_on_application.quantity,
         usage=20.0,
         value=20,
         licence=self.licence,
     )
Ejemplo n.º 15
0
 def setUp(self):
     super().setUp()
     self.case = StandardApplicationFactory()
     self.url = reverse("cases:case", kwargs={"pk": self.case.id})