From 16a02851b0b77802de61350bf5fe6508dafb3c3f Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 23 Aug 2024 13:47:37 +0000 Subject: [PATCH 1/2] Only import real flows in tests where it's required --- temba/api/v2/tests.py | 4 +- temba/channels/tests.py | 2 +- temba/contacts/tests.py | 5 +-- temba/flows/tests.py | 94 +++++++++++++++++++---------------------- temba/globals/tests.py | 4 +- temba/mailroom/tests.py | 6 +-- temba/tickets/tests.py | 2 +- 7 files changed, 54 insertions(+), 63 deletions(-) diff --git a/temba/api/v2/tests.py b/temba/api/v2/tests.py index 7adaf2e4d9c..930c66db048 100644 --- a/temba/api/v2/tests.py +++ b/temba/api/v2/tests.py @@ -3184,7 +3184,7 @@ def test_flow_starts(self, mock_async_start): self.assertPostNotPermitted(endpoint_url, [None, self.agent, self.user]) self.assertDeleteNotAllowed(endpoint_url) - flow = self.get_flow("favorites_v13") + flow = self.create_flow("Test") # try to create an empty flow start self.assertPost(endpoint_url, self.editor, {}, errors={"flow": "This field is required."}) @@ -3402,7 +3402,7 @@ def test_flow_starts(self, mock_async_start): { "id": start3.id, "uuid": str(start3.uuid), - "flow": {"uuid": flow.uuid, "name": "Favorites"}, + "flow": {"uuid": flow.uuid, "name": "Test"}, "contacts": [{"uuid": self.joe.uuid, "name": "Joe Blow"}], "groups": [{"uuid": hans_group.uuid, "name": "hans"}], "restart_participants": False, diff --git a/temba/channels/tests.py b/temba/channels/tests.py index 049201b8513..4343f34e3c2 100644 --- a/temba/channels/tests.py +++ b/temba/channels/tests.py @@ -178,7 +178,7 @@ def test_release(self, mr_mocks): channel2 = Channel.create(self.org, self.user, "", "T", "Test Channel", "0785553333") # add channel trigger - flow = self.get_flow("color") + flow = self.create_flow("Test") Trigger.create(self.org, self.admin, Trigger.TYPE_CATCH_ALL, flow, channel=channel1) # create some activity on this channel diff --git a/temba/contacts/tests.py b/temba/contacts/tests.py index ec46dd1c8e4..2bd3c34c1a2 100644 --- a/temba/contacts/tests.py +++ b/temba/contacts/tests.py @@ -986,9 +986,8 @@ def test_delete(self): @mock_mailroom def test_start(self, mr_mocks): sample_flows = list(self.org.flows.order_by("name")) - background_flow = self.get_flow("background") - self.get_flow("media_survey") - archived_flow = self.get_flow("color") + background_flow = self.create_flow("Background") + archived_flow = self.create_flow("Archived") archived_flow.archive(self.admin) contact = self.create_contact("Joe", phone="+593979000111") diff --git a/temba/flows/tests.py b/temba/flows/tests.py index 0d267f37bda..da0f35f24b3 100644 --- a/temba/flows/tests.py +++ b/temba/flows/tests.py @@ -96,7 +96,7 @@ def test_clean_name(self): @patch("temba.mailroom.queue_interrupt") def test_archive(self, mock_queue_interrupt): - flow = self.get_flow("color") + flow = self.create_flow("Test") flow.archive(self.admin) mock_queue_interrupt.assert_called_once_with(self.org, flow=flow) @@ -220,7 +220,7 @@ def test_flow_archive_with_campaign(self): self.assertTrue(flow.is_archived) def test_editor(self): - flow = self.get_flow("color") + flow = self.create_flow("Test") self.login(self.admin) @@ -1245,7 +1245,7 @@ def test_group_send(self): self.get_flow("group_send_flow") def test_flow_delete_of_inactive_flow(self): - flow = self.get_flow("favorites") + flow = self.create_flow("Test") flow.release(self.admin) self.login(self.admin) @@ -1630,7 +1630,7 @@ def test_views(self): create_url = reverse("flows.flow_create") self.create_contact("Eric", phone="+250788382382") - flow = self.get_flow("color") + flow = self.create_flow("Test") # create a flow for another org other_flow = Flow.create(self.org2, self.admin2, "Flow2") @@ -1821,7 +1821,7 @@ def test_views(self): self.assertEqual(language_flow.base_language, "eng") def test_update_messaging_flow(self): - flow = self.get_flow("color_v13") + flow = self.create_flow("Test") update_url = reverse("flows.flow_update", args=[flow.id]) def assert_triggers(expected: list): @@ -1833,9 +1833,9 @@ def assert_triggers(expected: list): update_url, [self.editor, self.admin], form_fields={ - "name": "Colors", + "name": "Test", "keyword_triggers": [], - "expires_after_minutes": 720, + "expires_after_minutes": 10080, "ignore_triggers": False, }, ) @@ -1931,7 +1931,7 @@ def assert_triggers(expected: list): ) def test_update_voice_flow(self): - flow = self.get_flow("ivr") + flow = self.create_flow("IVR Test", flow_type=Flow.TYPE_VOICE) update_url = reverse("flows.flow_update", args=[flow.id]) self.assertRequestDisallowed(update_url, [None, self.user, self.agent, self.admin2]) @@ -1979,7 +1979,7 @@ def test_update_voice_flow(self): self.assertEqual(30, flow.metadata["ivr_retry"]) def test_update_surveyor_flow(self): - flow = self.get_flow("media_survey") + flow = self.create_flow("Survey", flow_type=Flow.TYPE_SURVEY) update_url = reverse("flows.flow_update", args=[flow.id]) # we should only see name and contact creation option on form @@ -1994,7 +1994,7 @@ def test_update_surveyor_flow(self): self.assertEqual("login", flow.metadata.get("contact_creation")) def test_update_background_flow(self): - flow = self.get_flow("background") + flow = self.create_flow("Background", flow_type=Flow.TYPE_BACKGROUND) update_url = reverse("flows.flow_update", args=[flow.id]) # we should only see name on form @@ -2008,14 +2008,14 @@ def test_update_background_flow(self): self.assertEqual("New Name", flow.name) def test_list_views(self): - flow1 = self.get_flow("color_v13") - flow2 = self.get_flow("no_ruleset_flow") + flow1 = self.create_flow("Flow 1") + flow2 = self.create_flow("Flow 2") # archive second flow flow2.is_archived = True flow2.save(update_fields=("is_archived",)) - flow3 = Flow.create(self.org, self.admin, "Flow 3") + flow3 = self.create_flow("Flow 3") self.login(self.admin) @@ -2293,7 +2293,7 @@ def test_save_revisions(self): self.assertResponseError(response, "description", "Your flow has been upgraded to the latest version") def test_inactive_flow(self): - flow = self.get_flow("color_v13") + flow = self.create_flow("Deleted") flow.release(self.admin) self.login(self.admin) @@ -2898,6 +2898,16 @@ def test_results(self): self.assertEqual(24, len(data["hod"])) self.assertEqual(7, len(data["dow"])) + # check that views return 404 for inactive flows + flow = self.create_flow("Deleted") + flow.release(self.admin) + + response = self.client.get(reverse("flows.flow_activity_chart", args=[flow.id])) + self.assertEqual(404, response.status_code) + + response = self.client.get(reverse("flows.flow_category_counts", args=[flow.uuid])) + self.assertEqual(404, response.status_code) + def test_activity(self): flow = self.get_flow("favorites_v13") flow_nodes = flow.get_definition()["nodes"] @@ -2939,24 +2949,6 @@ def test_activity(self): response.json(), ) - def test_activity_chart_of_inactive_flow(self): - flow = self.get_flow("favorites") - flow.release(self.admin) - - self.login(self.admin) - response = self.client.get(reverse("flows.flow_activity_chart", args=[flow.id])) - - self.assertEqual(404, response.status_code) - - def test_category_counts_of_inactive_flow(self): - flow = self.get_flow("favorites") - flow.release(self.admin) - - self.login(self.admin) - response = self.client.get(reverse("flows.flow_category_counts", args=[flow.uuid])) - - self.assertEqual(404, response.status_code) - def test_write_protection(self): flow = self.get_flow("favorites_v13") flow_json = flow.get_definition() @@ -3738,7 +3730,7 @@ def create_session(org, created_on: datetime): def test_trim(self): contact = self.create_contact("Ben Haggerty", phone="+250788123123") - flow = self.get_flow("color") + flow = self.create_flow("Test") # create some runs that have sessions session1 = FlowSession.objects.create( @@ -4953,7 +4945,7 @@ def test_from_archives(self): def test_no_responses(self): today = timezone.now().astimezone(self.org.timezone).date() - flow = self.get_flow("color_v13") + flow = self.create_flow("Test") self.assertEqual(flow.get_run_stats()["total"], 0) @@ -4963,7 +4955,7 @@ def test_no_responses(self): # every sheet has only the head row self.assertEqual(1, len(list(workbook.worksheets[0].rows))) - self.assertEqual(11, len(list(workbook.worksheets[0].columns))) + self.assertEqual(8, len(list(workbook.worksheets[0].columns))) class FlowLabelTest(TembaTest): @@ -5308,28 +5300,28 @@ class FlowRevisionTest(TembaTest): def test_trim_revisions(self): start = timezone.now() - color = self.get_flow("color") - clinic = self.get_flow("the_clinic") + flow1 = self.create_flow("Flow 1") + flow2 = self.create_flow("Flow 2") revision = 100 FlowRevision.objects.all().update(revision=revision) # create a single old clinic revision FlowRevision.objects.create( - flow=clinic, + flow=flow2, definition=dict(), revision=99, created_on=timezone.now() - timedelta(days=7), created_by=self.admin, ) - # make a bunch of revisions for color on the same day + # make a bunch of revisions for flow 1 on the same day created = timezone.now().replace(hour=6) - timedelta(days=1) for i in range(25): revision -= 1 created = created - timedelta(minutes=1) FlowRevision.objects.create( - flow=color, definition=dict(), revision=revision, created_by=self.admin, created_on=created + flow=flow1, definition=dict(), revision=revision, created_by=self.admin, created_on=created ) # then for 5 days prior, make a few more @@ -5339,32 +5331,32 @@ def test_trim_revisions(self): revision -= 1 created = created - timedelta(minutes=1) FlowRevision.objects.create( - flow=color, definition=dict(), revision=revision, created_by=self.admin, created_on=created + flow=flow1, definition=dict(), revision=revision, created_by=self.admin, created_on=created ) # trim our flow revisions, should be left with original (today), 25 from yesterday, 1 per day for 5 days = 31 - self.assertEqual(76, FlowRevision.objects.filter(flow=color).count()) + self.assertEqual(76, FlowRevision.objects.filter(flow=flow1).count()) self.assertEqual(45, FlowRevision.trim(start)) - self.assertEqual(31, FlowRevision.objects.filter(flow=color).count()) + self.assertEqual(31, FlowRevision.objects.filter(flow=flow1).count()) self.assertEqual( 7, - FlowRevision.objects.filter(flow=color) + FlowRevision.objects.filter(flow=flow1) .annotate(created_date=TruncDate("created_on")) .distinct("created_date") .count(), ) # trim our clinic flow manually, should remain unchanged - self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count()) - self.assertEqual(0, FlowRevision.trim_for_flow(clinic.id)) - self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count()) + self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count()) + self.assertEqual(0, FlowRevision.trim_for_flow(flow2.id)) + self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count()) # call our task trim_flow_revisions() - self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count()) - self.assertEqual(31, FlowRevision.objects.filter(flow=color).count()) + self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count()) + self.assertEqual(31, FlowRevision.objects.filter(flow=flow1).count()) # call again (testing reading redis key) trim_flow_revisions() - self.assertEqual(2, FlowRevision.objects.filter(flow=clinic).count()) - self.assertEqual(31, FlowRevision.objects.filter(flow=color).count()) + self.assertEqual(2, FlowRevision.objects.filter(flow=flow2).count()) + self.assertEqual(31, FlowRevision.objects.filter(flow=flow1).count()) diff --git a/temba/globals/tests.py b/temba/globals/tests.py index 43e3315243d..2c937f68d09 100644 --- a/temba/globals/tests.py +++ b/temba/globals/tests.py @@ -29,8 +29,8 @@ def test_model(self): self.assertEqual("Secret Value", global3.name) self.assertEqual("", global3.value) - flow1 = self.get_flow("color") - flow2 = self.get_flow("favorites") + flow1 = self.create_flow("Flow 1") + flow2 = self.create_flow("Flow 2") flow1.global_dependencies.add(global1, global2) flow2.global_dependencies.add(global1) diff --git a/temba/mailroom/tests.py b/temba/mailroom/tests.py index a0e0be3f377..f5f0b6cb34e 100644 --- a/temba/mailroom/tests.py +++ b/temba/mailroom/tests.py @@ -20,7 +20,7 @@ class MailroomQueueTest(TembaTest): def test_queue_flow_start(self): - flow = self.get_flow("favorites") + flow = self.create_flow("Test") jim = self.create_contact("Jim", phone="+12065551212") bobs = self.create_group("Bobs", [self.create_contact("Bob", phone="+12065551313")]) @@ -101,7 +101,7 @@ def test_queue_interrupt_by_contacts(self): ) def test_queue_interrupt_by_flow(self): - flow = self.get_flow("favorites") + flow = self.create_flow("Test") flow.archive(self.admin) self.assert_org_queued(self.org, "batch") @@ -472,7 +472,7 @@ def test_from_flow_run(self): ) def test_from_event_fire(self): - flow = self.get_flow("color_v13") + flow = self.create_flow("Test") group = self.create_group("Reporters", contacts=[]) registered = self.create_field("registered", "Registered", value_type="D") campaign = Campaign.create(self.org, self.admin, "Welcomes", group) diff --git a/temba/tickets/tests.py b/temba/tickets/tests.py index b495ded7c06..b63863b999a 100644 --- a/temba/tickets/tests.py +++ b/temba/tickets/tests.py @@ -365,7 +365,7 @@ def test_list(self): self.assertEqual(("tickets", "mine", "open", str(ticket.uuid)), response.context["temba_referer"]) # contacts in a flow get interrupt menu option instead - flow = self.get_flow("color") + flow = self.create_flow("Test") self.contact.current_flow = flow self.contact.save() deep_link = f"{list_url}all/open/{str(ticket.uuid)}/" From ce2bb0ea99298de9f42d7dc4bad3a0feb73c28de Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Fri, 23 Aug 2024 13:49:57 +0000 Subject: [PATCH 2/2] TembaTest.create_flow should return a flow in latest version without migrating --- temba/tests/base.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/temba/tests/base.py b/temba/tests/base.py index abea0847540..34a7140e44f 100644 --- a/temba/tests/base.py +++ b/temba/tests/base.py @@ -543,7 +543,7 @@ def create_flow(self, name: str, *, flow_type=Flow.TYPE_MESSAGE, nodes=None, is_ "name": name, "type": Flow.GOFLOW_TYPES[flow_type], "revision": 1, - "spec_version": "13.1.0", + "spec_version": Flow.CURRENT_SPEC_VERSION, "expire_after_minutes": Flow.EXPIRES_DEFAULTS[flow_type], "language": "eng", "nodes": nodes, @@ -551,9 +551,7 @@ def create_flow(self, name: str, *, flow_type=Flow.TYPE_MESSAGE, nodes=None, is_ flow.version_number = definition["spec_version"] flow.save() - - json_flow = Flow.migrate_definition(definition, flow) - flow.save_revision(self.admin, json_flow) + flow.save_revision(self.admin, definition) return flow