コード例 #1
0
    def test_tree_without_providing_client_key_and_secret(self):
        request = self.factory.get('/chat/tree/')
        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code,
                         status.HTTP_500_INTERNAL_SERVER_ERROR)

        request = self.factory.get(
            '/chat/tree',
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)

        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code,
                         status.HTTP_405_METHOD_NOT_ALLOWED)
コード例 #2
0
    def test_handle_facebook_without_referral(self):
        json_data = {
            "questions": [{
                "text": "hillary",
                "quick_replies": "yes, no"
            }, {
                "text": "billiard",
                "quick_replies": "1, 2"
            }],
            "answers": [{
                "name": "maddy",
                "type": "A",
                "text": "catty",
                "description": "physics"
            }, {
                "name": "patty",
                "type": "A",
                "text": "sreadily",
                "description": "open source"
            }],
            "transitions": [{
                "current_state": "hillary",
                "answer": "maddy",
                "next_state": "billiard"
            }, {
                "current_state": "billiard",
                "answer": "patty",
                "next_state": ""
            }],
            "tree": [{
                "trigger": "assscssary",
                "root_state": "hillary",
                "completion_text": "Thanks man ..!!"
            }]
        }
        request = self.factory.post(
            '/chat/tree/',
            format='json',
            data=json_data,
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)

        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        payload = u'{"object":"page","entry":[{"id":"128922990987384","time":1501144011354,"messaging":[{"sender":{"id":"969833033053167"},"recipient":{"id":"128922990987384"},"timestamp":1501144011318,"message":{"mid":"mid.$cAAAvslv3zhdjtG0aNldgyfQL-AkK","seq":153102,"text":"hi there"}}]}]}'

        tree = Tree.objects.first()
        session = Session.objects.create(tree=tree,
                                         recipient_email='*****@*****.**',
                                         tree_id='1',
                                         recipient_id='969833033053167',
                                         callback_url='http://blah.com')

        handle_facebook_callback(payload)

        question_answer_pair_count = QuestionAnswerPair.objects.all().count()
        self.assertEqual(question_answer_pair_count, 1)
コード例 #3
0
    def test_tree_without_providing_all_fields(self):
        request = self.factory.post(
            '/chat/tree',
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)
        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
コード例 #4
0
    def test_handle_facebook_callback_with_referral(self):
        json_data = {
            "questions": [{
                "text": "hillary",
                "quick_replies": "yes, no"
            }, {
                "text": "billiard",
                "quick_replies": "1, 2"
            }],
            "answers": [{
                "name": "maddy",
                "type": "A",
                "text": "catty",
                "description": "physics"
            }, {
                "name": "patty",
                "type": "A",
                "text": "sreadily",
                "description": "open source"
            }],
            "transitions": [{
                "current_state": "hillary",
                "answer": "maddy",
                "next_state": "billiard"
            }, {
                "current_state": "billiard",
                "answer": "patty",
                "next_state": ""
            }],
            "tree": [{
                "trigger": "assscssary",
                "root_state": "hillary",
                "completion_text": "Thanks man ..!!"
            }]
        }
        request = self.factory.post(
            '/chat/tree/',
            format='json',
            data=json_data,
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)

        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        tree = Tree.objects.first()
        session = Session.objects.create(tree=tree,
                                         recipient_email='*****@*****.**',
                                         tree_id='1')
        handle_facebook_callback(self.payload)

        session = Session.objects.first()

        self.assertEqual(session.recipient_id, '969833033053167')
コード例 #5
0
    def test_start_test(self):
        json_data = {
            "questions": [{
                "text": "hillary",
                "quick_replies": "yes, no"
            }, {
                "text": "billiard",
                "quick_replies": "1, 2"
            }],
            "answers": [{
                "name": "maddy",
                "type": "A",
                "text": "catty",
                "description": "physics"
            }, {
                "name": "patty",
                "type": "A",
                "text": "sreadily",
                "description": "open source"
            }],
            "transitions": [{
                "current_state": "hillary",
                "answer": "maddy",
                "next_state": "billiard"
            }, {
                "current_state": "billiard",
                "answer": "patty",
                "next_state": ""
            }],
            "tree": [{
                "trigger": "assscssary",
                "root_state": "hillary",
                "completion_text": "Thanks man ..!!"
            }]
        }
        request = self.factory.post(
            '/chat/tree/',
            format='json',
            data=json_data,
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)

        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        current_state = Tree.objects.first().root_state
        user_input = 'catty'
        state = self.bot.get_next_state(current_state, user_input)
        self.assertEqual(state.name, 'billiard')
コード例 #6
0
    def test_by_providing_valid_tree_id(self):
        json_data = {
            "questions": [{
                "text": "hillary",
                "quick_replies": "yes, no"
            }, {
                "text": "billiard",
                "quick_replies": "1, 2"
            }],
            "answers": [{
                "name": "maddy",
                "type": "A",
                "text": "catty",
                "description": "physics"
            }, {
                "name": "patty",
                "type": "A",
                "text": "sreadily",
                "description": "open source"
            }],
            "transitions": [{
                "current_state": "hillary",
                "answer": "maddy",
                "next_state": "billiard"
            }, {
                "current_state": "billiard",
                "answer": "patty",
                "next_state": ""
            }],
            "tree": [{
                "trigger": "assscssary",
                "root_state": "hillary",
                "completion_text": "Thanks man ..!!"
            }]
        }
        request = self.factory.post(
            '/chat/tree/',
            format='json',
            data=json_data,
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)

        response = TreeViewSet.as_view()(request)

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        tree = Tree.objects.first()
        data = {
            "callback_url": "http://blah.com",
            "tree": tree.id,
            "recipient_email": "*****@*****.**",
            "recipient_phone": "7655666888"
        }
        request = self.factory.post(
            '/chat/initiate',
            format='json',
            data=data,
            HTTP_CHATBOT_CLIENT_KEY=self.client.key,
            HTTP_CHATBOT_CLIENT_SECRET=self.client.secret)
        response = InitiateChat.as_view()(request)
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

        session = Session.objects.first()

        self.assertEqual(session.status, 'L')