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 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_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_case_routed_by_second_tier_if_tier_one_conditions_not_met(self): queue_2 = Queue(team=self.team) queue_2.save() flag = FlagFactory(team=self.team) rule = self.create_routing_rule( team_id=self.team.id, queue_id=self.queue.id, tier=5, status_id=CaseStatus.objects.get(status="submitted").id, additional_rules=[RoutingRulesAdditionalFields.FLAGS], ) rule.flags_to_include.add(flag) self.create_routing_rule( team_id=self.team.id, queue_id=queue_2.id, tier=6, status_id=CaseStatus.objects.get(status="submitted").id, additional_rules=[], ) case = self.create_open_application_case(organisation=self.organisation) run_routing_rules(case) self.assertNotIn(self.queue, set(case.queues.all())) self.assertIn(queue_2, set(case.queues.all()))
def test_gov_user_can_create_flagging_rule_destination(self): application = self.create_standard_application_case(self.organisation) country_id = (PartyOnApplication.objects.filter( application_id=application).values_list("party__country_id", flat=True).first()) self.gov_user.role = self.super_user_role self.gov_user.save() flag = FlagFactory(level=FlagLevels.DESTINATION, team=self.team) data = { "level": FlagLevels.DESTINATION, "flag": str(flag.id), "matching_values": [country_id] } response = self.client.post(self.url, data, **self.gov_headers) response_data = response.json() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_data["level"], FlagLevels.DESTINATION) self.assertEqual(response_data["flag"], str(flag.id)) self.assertEqual(response_data["matching_values"], [country_id]) rule = FlaggingRule.objects.get() self.assertEqual(rule.level, "Destination") self.assertEqual(rule.flag, flag) self.assertEqual(rule.matching_values, [country_id])
def test_gov_user_can_create_flagging_rule_good(self): application = self.create_standard_application_case(self.organisation) goa = GoodOnApplication.objects.filter( application_id=application.id).first() clc1 = goa.good.control_list_entries.first() clc2 = goa.good.control_list_entries.last() self.gov_user.role = self.super_user_role self.gov_user.save() flag = FlagFactory(level=FlagLevels.GOOD, team=self.team) data = { "level": FlagLevels.GOOD, "flag": str(flag.id), "matching_values": [clc1.rating, clc2.rating], "is_for_verified_goods_only": "True", } response = self.client.post(self.url, data, **self.gov_headers) response_data = response.json() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_data["level"], FlagLevels.GOOD) self.assertEqual(response_data["flag"], str(flag.id)) self.assertEqual(response_data["matching_values"], [clc1.rating, clc2.rating]) self.assertTrue(response_data["is_for_verified_goods_only"]) rule = FlaggingRule.objects.get() self.assertEqual(rule.level, FlagLevels.GOOD) self.assertEqual(rule.flag, flag) self.assertEqual(rule.matching_values, [clc1.rating, clc2.rating])
def setUp(self): super().setUp() self.report_summary = self.create_picklist_item( "Report Summary", self.team, PicklistType.REPORT_SUMMARY, PickListStatus.ACTIVE) self.good_1 = GoodFactory( organisation=self.organisation, flags=[FlagFactory(level=FlagLevels.GOOD, team=self.team)]) self.good_2 = GoodFactory(organisation=self.organisation) role = Role(name="review_goods") role.permissions.set([constants.GovPermissions.REVIEW_GOODS.name]) role.save() self.gov_user.role = role self.gov_user.save() self.application = self.create_draft_standard_application( organisation=self.organisation) self.good_on_application_1 = GoodOnApplication.objects.create( good=self.good_1, application=self.application, quantity=10, unit=Units.NAR, value=500) self.good_on_application_2 = GoodOnApplication.objects.create( good=self.good_2, application=self.application, quantity=10, unit=Units.NAR, value=500) self.case = self.submit_application(self.application) self.url = reverse_lazy("goods:control_list_entries", kwargs={"case_pk": self.case.id})
def test_rules_not_run_if_flags_match(self): flag_1 = FlagFactory(team=self.team) flag_2 = FlagFactory(team=self.team) rule = self.create_routing_rule( team_id=self.team.id, queue_id=self.queue.id, tier=5, status_id=CaseStatus.objects.get(status="submitted").id, additional_rules=[RoutingRulesAdditionalFields.FLAGS], ) rule.flags_to_include.set([flag_1, flag_2]) case = self.create_open_application_case(organisation=self.organisation) case.flags.set([flag_1, flag_2]) run_routing_rules(case) self.assertIn(self.queue, set(case.queues.all())) self.assertEqual(case.status, CaseStatus.objects.get(status="submitted"))
def test_flags_are_returned_correctly(self): application = self.create_draft_open_application(self.organisation) falg = FlagFactory(team=self.team) flag = FlagFactory(team=self.team) Country.objects.get(id="FR").flags.set([falg.id, flag.id]) data = { "countries": ["FR"], "contract_types": ["navy", "army"], "other_contract_type_text": "", } url = reverse("applications:contract_types", kwargs={"pk": application.id}) self.client.put(url, data, **self.exporter_headers) ordered_flags = get_ordered_flags(application, self.team) self.assertIn("Navy", str(ordered_flags)) self.assertIn("Army", str(ordered_flags))
def test_inactive_flag_rule_returns_empty_list(self): flag = FlagFactory(status=FlagStatuses.DEACTIVATED, team=self.team) routing_rule = self.create_routing_rule( team_id=self.team.id, queue_id=self.queue.id, tier=5, status_id=CaseStatus.objects.last().id, additional_rules=[*[k for k, v in RoutingRulesAdditionalFields.choices]], ) routing_rule.flags_to_include.add(flag) parameter_sets = routing_rule.parameter_sets() self.assertEqual(len(parameter_sets), 0)
def test_approve_application_blocking_flags_failure(self): flag = FlagFactory(level=FlagLevels.CASE, team=self.team, blocks_approval=True) self.standard_application.flags.add(flag) self._set_user_permission([ GovPermissions.MANAGE_LICENCE_FINAL_ADVICE, GovPermissions.MANAGE_LICENCE_DURATION ]) data = {"action": AdviceType.APPROVE, "duration": 60} data.update(self.post_date) response = self.client.put(self.url, data=data, **self.gov_headers) response_data = response.json() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(response_data["errors"], [ f"{strings.Applications.Finalise.Error.BLOCKING_FLAGS}{flag.name}" ])
def test_gov_user_can_see_all_flags(self): # create 16 flags, 2 of each level for two different teams with different priorities other_team = TeamFactory() third_team = TeamFactory() flags = [] for team in [self.team, other_team, third_team]: for level in [FlagLevels.GOOD, FlagLevels.DESTINATION, FlagLevels.CASE, FlagLevels.ORGANISATION]: for priority in [0, 1]: flag = FlagFactory( name=level + team.name + str(priority), level=level, priority=priority, team=team ) flags.append(flag) case = self.create_standard_application_case(organisation=self.organisation) case.flags.set([flag for flag in flags if flag.level == FlagLevels.CASE]) good = GoodOnApplication.objects.get(application=case).good good.flags.set([flag for flag in flags if flag.level == FlagLevels.GOOD]) end_user = PartyOnApplication.objects.get(application=case, party__type=PartyType.END_USER).party end_user.flags.set([flag for flag in flags if flag.level == FlagLevels.DESTINATION]) self.organisation.flags.set([flag for flag in flags if flag.level == FlagLevels.ORGANISATION]) ordered_flags = get_ordered_flags(case, self.team) # This is the order of the original flags when displayed on a case expected_order = [0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 9, 17, 10, 18, 11, 19, 12, 20, 13, 21, 14, 22, 15, 23] for i in range(0, 24): if i <= 7: self.assertIn(flags[expected_order[i]].name, str(ordered_flags[i])) else: # We don't know about the order here by team, it doesn't matter as long as its by priority and type correctly if i % 2 == 0: self.assertIn(flags[expected_order[i]].name, str(ordered_flags[i]) + str(ordered_flags[i + 1])) else: self.assertIn(flags[expected_order[i]].name, str(ordered_flags[i]) + str(ordered_flags[i - 1])) i += 1
def test_flagging_rule_can_change_verified_answer(self): self.gov_user.role = self.super_user_role self.gov_user.save() flag = FlagFactory(level=FlagLevels.GOOD, team=self.team) flagging_rule = self.create_flagging_rule( level="Good", flag=flag, team=self.team, matching_values=["ML1"], is_for_verified_goods_only="False") data = { "is_for_verified_goods_only": "True", } url = reverse("flags:flagging_rule", kwargs={"pk": flagging_rule.id}) self.client.put(url, data, **self.gov_headers) flagging_rule.refresh_from_db() self.assertTrue(flagging_rule.is_for_verified_goods_only)
def test_missing_data_create_good_rule_failure(self): application = self.create_standard_application_case(self.organisation) control_list_entry = (GoodOnApplication.objects.filter( application_id=application.id).values_list( "good__control_list_entries__rating", flat=True).first()) self.gov_user.role = self.super_user_role self.gov_user.save() flag = FlagFactory(level=FlagLevels.GOOD, team=self.team) data = { "level": FlagLevels.GOOD, "flag": str(flag.id), "matching_values": [control_list_entry] } response = self.client.post(self.url, data, **self.gov_headers) response_data = response.json() self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(response_data["errors"]["is_for_verified_goods_only"], [strings.FlaggingRules.NO_ANSWER_VERIFIED_ONLY])
def test_gov_user_can_create_flagging_rule_case(self): self.create_standard_application_case(self.organisation) open_application = self.create_draft_open_application( self.organisation) self.submit_application(open_application) self.create_draft_open_application(self.organisation) self.gov_user.role = self.super_user_role self.gov_user.save() flag = FlagFactory(level=FlagLevels.CASE, team=self.team) data = { "level": FlagLevels.CASE, "flag": str(flag.id), "matching_values": [CaseTypeReferenceEnum.SIEL, CaseTypeReferenceEnum.OGEL], } response = self.client.post(self.url, data, **self.gov_headers) response_data = response.json() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response_data["level"], FlagLevels.CASE) self.assertEqual(response_data["flag"], str(flag.id)) self.assertEqual( response_data["matching_values"], [CaseTypeReferenceEnum.SIEL, CaseTypeReferenceEnum.OGEL]) rule = FlaggingRule.objects.get() self.assertEqual(rule.level, "Case") self.assertEqual(rule.flag, flag) self.assertEqual( rule.matching_values, [CaseTypeReferenceEnum.SIEL, CaseTypeReferenceEnum.OGEL])
def create_flag(name: str, level: str, team: Team): warnings.warn( "create_flag is a deprecated function. Use a FlagFactory instead", category=DeprecationWarning, stacklevel=2) return FlagFactory(name=name, level=level, team=team)
def test_countries_ordered_as_expected_on_standard_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) standard_application = self.create_draft_standard_application( self.organisation) # Third parties first_tp = standard_application.third_parties.first() first_tp.party.flags.set([highest_priority_flag]) second_tp = self.create_party("party 2", self.organisation, PartyType.THIRD_PARTY, standard_application) second_tp.flags.set([lowest_priority_flag]) third_tp = self.create_party("party 3", self.organisation, PartyType.THIRD_PARTY, standard_application) fourth_tp = self.create_party("party 4", self.organisation, PartyType.THIRD_PARTY, standard_application) fourth_tp.flags.set([lowest_priority_flag]) # Ultimate end users first_ueu = self.create_party("party 1", self.organisation, PartyType.ULTIMATE_END_USER, standard_application) first_ueu.flags.set([highest_priority_flag]) second_ueu = self.create_party("party 2", self.organisation, PartyType.ULTIMATE_END_USER, standard_application) second_ueu.flags.set([lowest_priority_flag]) third_ueu = self.create_party("party 3", self.organisation, PartyType.ULTIMATE_END_USER, standard_application) fourth_ueu = self.create_party("party 4", self.organisation, PartyType.ULTIMATE_END_USER, standard_application) fourth_ueu.flags.set([lowest_priority_flag]) case = self.submit_application(standard_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_third_parties = [ third_party["id"] for third_party in case_application["third_parties"] ] ordered_ultimate_end_users = [ ueu["id"] for ueu in case_application["ultimate_end_users"] ] # Third parties and ultimate end users are ordered by destination flag priority and for # parties of these types without flags, they are alphabetised self.assertEqual( ordered_third_parties, [ str(first_tp.party.id), str(second_tp.id), str(fourth_tp.id), str(third_tp.id) ], ) self.assertEqual( ordered_ultimate_end_users, [ str(first_ueu.id), str(second_ueu.id), str(fourth_ueu.id), str(third_ueu.id) ], )