|
1 | 1 | from django.urls import reverse
|
2 | 2 |
|
3 |
| -from temba.campaigns.models import Campaign |
| 3 | +from temba.campaigns.models import Campaign, CampaignEvent |
| 4 | +from temba.contacts.models import ContactField |
4 | 5 | from temba.flows.models import Flow
|
| 6 | +from temba.tests import mock_mailroom |
| 7 | +from temba.triggers.models import Trigger |
5 | 8 |
|
6 | 9 | from . import APITest
|
7 | 10 |
|
8 | 11 |
|
9 | 12 | class DefinitionsEndpointTest(APITest):
|
10 |
| - def test_endpoint(self): |
| 13 | + @mock_mailroom |
| 14 | + def test_endpoint(self, mr_mocks): |
11 | 15 | endpoint_url = reverse("api.v2.definitions") + ".json"
|
12 | 16 |
|
13 | 17 | self.assertGetNotPermitted(endpoint_url, [None, self.agent])
|
14 | 18 | self.assertPostNotAllowed(endpoint_url)
|
15 | 19 | self.assertDeleteNotAllowed(endpoint_url)
|
16 | 20 |
|
17 |
| - self.import_file("test_flows/subflow.json") |
18 |
| - flow = Flow.objects.get(name="Parent Flow") |
19 |
| - |
20 |
| - # all flow dependencies and we should get the child flow |
21 |
| - self.assertGet( |
22 |
| - endpoint_url + f"?flow={flow.uuid}", |
23 |
| - [self.editor], |
24 |
| - raw=lambda j: {f["name"] for f in j["flows"]} == {"Child Flow", "Parent Flow"}, |
25 |
| - ) |
26 |
| - |
27 |
| - # export just the parent flow |
28 |
| - self.assertGet( |
29 |
| - endpoint_url + f"?flow={flow.uuid}&dependencies=none", |
30 |
| - [self.editor], |
31 |
| - raw=lambda j: {f["name"] for f in j["flows"]} == {"Parent Flow"}, |
| 21 | + # create a flow with subflow dependencies |
| 22 | + flow1 = self.create_flow("Parent Flow") |
| 23 | + flow2 = self.create_flow("Child Flow 1") |
| 24 | + flow3 = self.create_flow("Child Flow 2") |
| 25 | + flow1.flow_dependencies.add(flow2, flow3) |
| 26 | + |
| 27 | + # that's used in a campaign |
| 28 | + field = self.create_field("registered", "Registered", ContactField.TYPE_DATETIME) |
| 29 | + group = self.create_group("Others", []) |
| 30 | + campaign1 = Campaign.create(self.org, self.admin, "Reminders", group) |
| 31 | + CampaignEvent.create_flow_event(self.org, self.admin, campaign1, field, 1, "D", flow1, -1) |
| 32 | + |
| 33 | + # and has a trigger |
| 34 | + Trigger.create( |
| 35 | + self.org, self.editor, Trigger.TYPE_KEYWORD, flow1, keywords=["test"], match_type=Trigger.MATCH_FIRST_WORD |
32 | 36 | )
|
33 | 37 |
|
34 |
| - # import the clinic app which has campaigns |
35 |
| - self.import_file("test_flows/the_clinic.json") |
36 |
| - |
37 |
| - # our catchall flow, all alone |
38 |
| - flow = Flow.objects.get(name="Catch All") |
| 38 | + # nothing specified, nothing exported |
39 | 39 | self.assertGet(
|
40 |
| - endpoint_url + f"?flow={flow.uuid}&dependencies=none", |
| 40 | + endpoint_url, |
41 | 41 | [self.editor],
|
42 |
| - raw=lambda j: len(j["flows"]) == 1 and len(j["campaigns"]) == 0 and len(j["triggers"]) == 0, |
| 42 | + raw=lambda j: len(j["flows"]) == 0 and len(j["campaigns"]) == 0 and len(j["triggers"]) == 0, |
43 | 43 | )
|
44 | 44 |
|
45 |
| - # with its trigger dependency |
| 45 | + # flow + all dependencies by default |
46 | 46 | self.assertGet(
|
47 |
| - endpoint_url + f"?flow={flow.uuid}", |
48 |
| - [self.editor], |
49 |
| - raw=lambda j: len(j["flows"]) == 1 and len(j["campaigns"]) == 0 and len(j["triggers"]) == 1, |
50 |
| - ) |
51 |
| - |
52 |
| - # our registration flow, all alone |
53 |
| - flow = Flow.objects.get(name="Register Patient") |
54 |
| - self.assertGet( |
55 |
| - endpoint_url + f"?flow={flow.uuid}&dependencies=none", |
56 |
| - [self.editor], |
57 |
| - raw=lambda j: len(j["flows"]) == 1 and len(j["campaigns"]) == 0 and len(j["triggers"]) == 0, |
58 |
| - ) |
59 |
| - |
60 |
| - # touches a lot of stuff |
61 |
| - self.assertGet( |
62 |
| - endpoint_url + f"?flow={flow.uuid}", |
| 47 | + endpoint_url + f"?flow={flow1.uuid}", |
63 | 48 | [self.editor],
|
64 |
| - raw=lambda j: len(j["flows"]) == 6 and len(j["campaigns"]) == 1 and len(j["triggers"]) == 2, |
| 49 | + raw=lambda j: {f["name"] for f in j["flows"]} == {"Parent Flow", "Child Flow 1", "Child Flow 2"} |
| 50 | + and len(j["campaigns"]) == 1 |
| 51 | + and len(j["triggers"]) == 1, |
65 | 52 | )
|
66 | 53 |
|
67 |
| - # ignore campaign dependencies |
| 54 | + # flow + all dependencies explicitly |
68 | 55 | self.assertGet(
|
69 |
| - endpoint_url + f"?flow={flow.uuid}&dependencies=flows", |
| 56 | + endpoint_url + f"?flow={flow1.uuid}&dependencies=all", |
70 | 57 | [self.editor],
|
71 |
| - raw=lambda j: len(j["flows"]) == 2 and len(j["campaigns"]) == 0 and len(j["triggers"]) == 1, |
| 58 | + raw=lambda j: {f["name"] for f in j["flows"]} == {"Parent Flow", "Child Flow 1", "Child Flow 2"} |
| 59 | + and len(j["campaigns"]) == 1 |
| 60 | + and len(j["triggers"]) == 1, |
72 | 61 | )
|
73 | 62 |
|
74 |
| - # add our missed call flow |
75 |
| - missed_call = Flow.objects.get(name="Missed Call") |
| 63 | + # flow + no dependencies |
76 | 64 | self.assertGet(
|
77 |
| - endpoint_url + f"?flow={flow.uuid}&flow={missed_call.uuid}&dependencies=all", |
| 65 | + endpoint_url + f"?flow={flow1.uuid}&dependencies=none", |
78 | 66 | [self.editor],
|
79 |
| - raw=lambda j: len(j["flows"]) == 7 and len(j["campaigns"]) == 1 and len(j["triggers"]) == 3, |
| 67 | + raw=lambda j: {f["name"] for f in j["flows"]} == {"Parent Flow"} |
| 68 | + and len(j["campaigns"]) == 0 |
| 69 | + and len(j["triggers"]) == 0, |
80 | 70 | )
|
81 | 71 |
|
82 |
| - campaign = Campaign.objects.get(name="Appointment Schedule") |
| 72 | + # flow + just flow dependencies (includes triggers) |
83 | 73 | self.assertGet(
|
84 |
| - endpoint_url + f"?campaign={campaign.uuid}&dependencies=none", |
| 74 | + endpoint_url + f"?flow={flow1.uuid}&dependencies=flows", |
85 | 75 | [self.editor],
|
86 |
| - raw=lambda j: len(j["flows"]) == 0 and len(j["campaigns"]) == 1 and len(j["triggers"]) == 0, |
| 76 | + raw=lambda j: {f["name"] for f in j["flows"]} == {"Parent Flow", "Child Flow 1", "Child Flow 2"} |
| 77 | + and len(j["campaigns"]) == 0 |
| 78 | + and len(j["triggers"]) == 1, |
87 | 79 | )
|
88 | 80 |
|
| 81 | + # campaign + all dependencies |
89 | 82 | self.assertGet(
|
90 |
| - endpoint_url + f"?campaign={campaign.uuid}", |
| 83 | + endpoint_url + f"?campaign={campaign1.uuid}", |
91 | 84 | [self.editor],
|
92 |
| - raw=lambda j: len(j["flows"]) == 6 and len(j["campaigns"]) == 1 and len(j["triggers"]) == 2, |
| 85 | + raw=lambda j: len(j["flows"]) == 3 and len(j["campaigns"]) == 1 and len(j["triggers"]) == 1, |
93 | 86 | )
|
94 | 87 |
|
95 | 88 | # test an invalid value for dependencies
|
96 | 89 | self.assertGet(
|
97 |
| - endpoint_url + f"?flow={flow.uuid}&dependencies=xx", |
| 90 | + endpoint_url + f"?flow={flow1.uuid}&dependencies=xx", |
98 | 91 | [self.editor],
|
99 | 92 | errors={None: "dependencies must be one of none, flows, all"},
|
100 | 93 | )
|
|
0 commit comments