Пример #1
0
    def test_flow_event(self):
        self.setupChannel()

        org = self.channel.org
        org.save()

        from temba.flows.models import ActionSet, WebhookAction, Flow
        flow = self.create_flow()

        # replace our uuid of 4 with the right thing
        actionset = ActionSet.objects.get(x=4)
        actionset.set_actions_dict(
            [WebhookAction(org.get_webhook_url()).as_json()])
        actionset.save()

        with patch('requests.Session.send') as mock:
            # run a user through this flow
            flow.start([], [self.joe])

            # have joe reply with mauve, which will put him in the other category that triggers the API Action
            sms = self.create_msg(contact=self.joe,
                                  direction='I',
                                  status='H',
                                  text="Mauve")

            mock.return_value = MockResponse(200, "{}")
            Flow.find_and_handle(sms)

            # should have one event created
            event = WebHookEvent.objects.get()

            self.assertEquals('C', event.status)
            self.assertEquals(1, event.try_count)
            self.assertFalse(event.next_attempt)

            result = WebHookResult.objects.get()
            self.assertStringContains("successfully", result.message)
            self.assertEquals(200, result.status_code)

            self.assertTrue(mock.called)

            args = mock.call_args_list[0][0]
            prepared_request = args[0]
            self.assertStringContains(self.channel.org.get_webhook_url(),
                                      prepared_request.url)

            data = parse_qs(prepared_request.body)
            self.assertEquals(self.channel.pk, int(data['channel'][0]))
            self.assertEquals(actionset.uuid, data['step'][0])
            self.assertEquals(flow.pk, int(data['flow'][0]))
            self.assertEquals(self.joe.uuid, data['contact'][0])
            self.assertEquals(unicode(self.joe.get_urn('tel')), data['urn'][0])

            values = json.loads(data['values'][0])

            self.assertEquals('Other', values[0]['category']['base'])
            self.assertEquals('color', values[0]['label'])
            self.assertEquals('Mauve', values[0]['text'])
            self.assertTrue(values[0]['time'])
            self.assertTrue(data['time'])
Пример #2
0
    def test_flow_event(self, mock_send):
        self.setupChannel()

        org = self.channel.org
        org.save()

        flow = self.create_flow(definition=self.COLOR_FLOW_DEFINITION)

        # replace our uuid of 4 with the right thing
        actionset = ActionSet.objects.get(x=4)
        actionset.set_actions_dict(
            [WebhookAction(org.get_webhook_url()).as_json()])
        actionset.save()

        # run a user through this flow
        flow.start([], [self.joe])

        # have joe reply with mauve, which will put him in the other category that triggers the API Action
        sms = self.create_msg(contact=self.joe,
                              direction='I',
                              status='H',
                              text="Mauve",
                              attachments=[
                                  "image/jpeg:http://s3.com/text.jpg",
                                  "audio/mp4:http://s3.com/text.mp4"
                              ])

        mock_send.return_value = MockResponse(200, "{}")
        Flow.find_and_handle(sms)

        # should have one event created
        event = WebHookEvent.objects.get()

        self.assertEquals('C', event.status)
        self.assertEquals(1, event.try_count)
        self.assertFalse(event.next_attempt)

        result = WebHookResult.objects.get()
        self.assertIn("successfully", result.message)
        self.assertEquals(200, result.status_code)
        self.assertEqual(self.joe, result.contact)

        self.assertTrue(mock_send.called)

        args = mock_send.call_args_list[0][0]
        prepared_request = args[0]
        self.assertIn(self.channel.org.get_webhook_url(), prepared_request.url)

        data = parse_qs(prepared_request.body)

        self.assertEqual(data['channel'], [str(self.channel.id)])
        self.assertEqual(data['channel_uuid'], [self.channel.uuid])
        self.assertEqual(data['step'], [actionset.uuid])
        self.assertEqual(data['text'], ["Mauve"])
        self.assertEqual(data['attachments'],
                         ["http://s3.com/text.jpg", "http://s3.com/text.mp4"])
        self.assertEqual(data['flow'], [str(flow.id)])
        self.assertEqual(data['flow_uuid'], [flow.uuid])
        self.assertEqual(data['contact'], [self.joe.uuid])
        self.assertEqual(data['contact_name'], [self.joe.name])
        self.assertEqual(data['urn'], [six.text_type(self.joe.get_urn('tel'))])

        values = json.loads(data['values'][0])

        self.assertEquals('Other', values[0]['category']['base'])
        self.assertEquals('color', values[0]['label'])
        self.assertEquals('Mauve', values[0]['text'])
        self.assertTrue(values[0]['time'])
        self.assertTrue(data['time'])
Пример #3
0
    def test_flow_event(self, mock_send):
        self.setupChannel()

        org = self.channel.org
        org.save()

        flow = self.get_flow('color')

        # replace our uuid of 4 with the right thing
        actionset = ActionSet.objects.get(x=4)
        actionset.set_actions_dict(
            [WebhookAction(str(uuid4()), org.get_webhook_url()).as_json()])
        actionset.save()

        # run a user through this flow
        flow.start([], [self.joe])

        # have joe reply with mauve, which will put him in the other category that triggers the API Action
        sms = self.create_msg(contact=self.joe,
                              direction='I',
                              status='H',
                              text="Mauve",
                              attachments=[
                                  "image/jpeg:http://s3.com/text.jpg",
                                  "audio/mp4:http://s3.com/text.mp4"
                              ])

        mock_send.return_value = MockResponse(200, "{}")
        Flow.find_and_handle(sms)

        # should have one event created
        event = WebHookEvent.objects.get()

        self.assertEqual('C', event.status)
        self.assertEqual(1, event.try_count)
        self.assertFalse(event.next_attempt)

        result = WebHookResult.objects.get()
        self.assertIn("successfully", result.message)
        self.assertEqual(200, result.status_code)
        self.assertEqual(self.joe, result.contact)

        self.assertTrue(mock_send.called)

        args = mock_send.call_args_list[0][0]
        prepared_request = args[0]
        self.assertIn(self.channel.org.get_webhook_url(), prepared_request.url)

        data = json.loads(prepared_request.body)

        self.assertEqual(data['channel'], {
            'uuid': str(self.channel.uuid),
            'name': self.channel.name
        })
        self.assertEqual(
            data['contact'], {
                'uuid': str(self.joe.uuid),
                'name': self.joe.name,
                'urn': six.text_type(self.joe.get_urn('tel'))
            })
        self.assertEqual(data['flow'], {
            'uuid': str(flow.uuid),
            'name': flow.name
        })
        self.assertEqual(
            data['input'], {
                'urn':
                'tel:+250788123123',
                'text':
                "Mauve",
                'attachments': [
                    "image/jpeg:http://s3.com/text.jpg",
                    "audio/mp4:http://s3.com/text.mp4"
                ]
            })
        self.assertEqual(data['results']['color']['category'], 'Other')
        self.assertEqual(data['results']['color']['name'], 'color')
        self.assertEqual(data['results']['color']['value'], 'Mauve')
        self.assertEqual(data['results']['color']['input'], 'Mauve')
Пример #4
0
    def test_flow_event(self, mock_send):
        self.setupChannel()

        org = self.channel.org
        org.save()

        flow = self.get_flow("color")

        # replace our uuid of 4 with the right thing
        actionset = ActionSet.objects.get(x=4)
        actionset.actions = [
            WebhookAction(str(uuid4()), org.get_webhook_url()).as_json()
        ]
        actionset.save()

        # run a user through this flow
        flow.start([], [self.joe])

        # have joe reply with mauve, which will put him in the other category that triggers the API Action
        sms = self.create_msg(
            contact=self.joe,
            direction="I",
            status="H",
            text="Mauve",
            attachments=[
                "image/jpeg:http://s3.com/text.jpg",
                "audio/mp4:http://s3.com/text.mp4"
            ],
        )

        mock_send.return_value = MockResponse(200, "{}")
        Flow.find_and_handle(sms)

        # should have one event created
        event = WebHookEvent.objects.get()

        self.assertEqual("C", event.status)
        self.assertEqual(1, event.try_count)
        self.assertFalse(event.next_attempt)

        result = WebHookResult.objects.get()
        self.assertIn("successfully", result.message)
        self.assertEqual(200, result.status_code)
        self.assertEqual(self.joe, result.contact)

        self.assertTrue(mock_send.called)

        args = mock_send.call_args_list[0][0]
        prepared_request = args[0]
        self.assertIn(self.channel.org.get_webhook_url(), prepared_request.url)

        data = json.loads(prepared_request.body)

        self.assertEqual(data["channel"], {
            "uuid": str(self.channel.uuid),
            "name": self.channel.name
        })
        self.assertEqual(
            data["contact"], {
                "uuid": str(self.joe.uuid),
                "name": self.joe.name,
                "urn": str(self.joe.get_urn("tel"))
            })
        self.assertEqual(data["flow"], {
            "uuid": str(flow.uuid),
            "name": flow.name,
            "revision": 1
        })
        self.assertEqual(
            data["input"],
            {
                "urn":
                "tel:+250788123123",
                "text":
                "Mauve",
                "attachments": [
                    "image/jpeg:http://s3.com/text.jpg",
                    "audio/mp4:http://s3.com/text.mp4"
                ],
            },
        )
        self.assertEqual(
            data["results"],
            {
                "color": {
                    "category":
                    "Other",
                    "node_uuid":
                    matchers.UUID4String(),
                    "name":
                    "color",
                    "value":
                    "Mauve\nhttp://s3.com/text.jpg\nhttp://s3.com/text.mp4",
                    "created_on":
                    matchers.ISODate(),
                    "input":
                    "Mauve\nhttp://s3.com/text.jpg\nhttp://s3.com/text.mp4",
                }
            },
        )