Exemplo n.º 1
0
    def setUp(self):
        super().setUp()
        self.setUpSecondaryOrg()

        # create some classifiers
        self.c1 = Classifier.create(self.org,
                                    self.admin,
                                    WitType.slug,
                                    "Booker", {},
                                    sync=False)
        self.c1.intents.create(name="book_flight",
                               external_id="book_flight",
                               created_on=timezone.now(),
                               is_active=True)
        self.c1.intents.create(name="book_hotel",
                               external_id="book_hotel",
                               created_on=timezone.now(),
                               is_active=False)
        self.c1.intents.create(name="book_car",
                               external_id="book_car",
                               created_on=timezone.now(),
                               is_active=True)

        self.c2 = Classifier.create(self.org,
                                    self.admin,
                                    WitType.slug,
                                    "Old Booker", {},
                                    sync=False)
        self.c2.is_active = False
        self.c2.save()
Exemplo n.º 2
0
    def test_sync(self):
        # create classifier but don't sync the intents
        c = Classifier.create(
            self.org,
            self.user,
            BothubType.slug,
            "Booker",
            {
                BothubType.CONFIG_ACCESS_TOKEN: "123456789",
                BothubType.INTENT_URL: "https://nlp.bothub.it/info/"
            },
            sync=False,
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')

            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 1)

            mock_get.side_effect = RequestException("Network is unreachable",
                                                    response=MockResponse(
                                                        100, ""))
            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 2)

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            intents = c.get_type().get_active_intents_from_api(c)

            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 3)
            self.assertEqual(3, len(intents))
            intent = intents[0]
            self.assertEqual("intent", intent.name)
            self.assertEqual("intent", intent.external_id)
Exemplo n.º 3
0
    def test_trim_logs_task(self):
        c1 = Classifier.create(self.org,
                               self.admin,
                               WitType.slug,
                               "Booker", {},
                               sync=False)

        HTTPLog.objects.create(
            classifier=c1,
            url="http://org1.bar/zap/?text=" + ("0123456789" * 30),
            request="GET /zap",
            response=" OK 200",
            is_error=False,
            log_type=HTTPLog.INTENTS_SYNCED,
            request_time=10,
            org=self.org,
            created_on=timezone.now() - timedelta(days=7),
        )
        l2 = HTTPLog.objects.create(
            classifier=c1,
            url="http://org2.bar/zap",
            request="GET /zap",
            response=" OK 200",
            is_error=False,
            log_type=HTTPLog.CLASSIFIER_CALLED,
            request_time=10,
            org=self.org,
        )

        trim_http_logs_task()

        # should only have one log remaining and should be l2
        self.assertEqual(1, HTTPLog.objects.all().count())
        self.assertTrue(HTTPLog.objects.filter(id=l2.id))
Exemplo n.º 4
0
    def test_sync(self):
        c = Classifier.create(
            self.org,
            self.user,
            WitType.slug,
            "Booker",
            {
                WitType.CONFIG_APP_ID: "12345",
                WitType.CONFIG_ACCESS_TOKEN: "sesame"
            },
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 2)
            with self.assertRaises(Exception):
                c.get_type().get_active_intents_from_api(c)
                self.assertEqual(
                    HTTPLog.objects.filter(classifier=c).count(), 3)

            mock_get.side_effect = RequestException("Network is unreachable",
                                                    response=MockResponse(
                                                        100, ""))
            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 4)

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            intents = c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 5)
            self.assertEqual(2, len(intents))
            car = intents[0]
            self.assertEqual("book_car", car.name)
            self.assertEqual("book_car", car.external_id)
Exemplo n.º 5
0
    def test_sync(self):
        c = Classifier.create(
            self.org,
            self.user,
            LuisType.slug,
            "Booker",
            {
                LuisType.CONFIG_APP_ID: "12345",
                LuisType.CONFIG_PRIMARY_KEY: "sesame",
                LuisType.CONFIG_ENDPOINT_URL: "http://luis.api",
                LuisType.CONFIG_VERSION: "0.1",
            },
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 1)
            with self.assertRaises(Exception):
                c.get_type().get_active_intents_from_api(c)
                self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 2)

            mock_get.side_effect = RequestException("Network is unreachable", response=MockResponse(100, ""))
            c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 3)

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            intents = c.get_type().get_active_intents_from_api(c)
            self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 4)
            self.assertEqual(2, len(intents))
            car = intents[0]
            self.assertEqual("Book Car", car.name)
            self.assertEqual("b1a3c0ad-e912-4b55-a62e-6fcb77751bc5", car.external_id)
Exemplo n.º 6
0
    def test_sync(self):
        c = Classifier.create(
            self.org,
            self.user,
            LuisType.slug,
            "Booker",
            {
                LuisType.CONFIG_APP_ID: "12345",
                LuisType.CONFIG_PRIMARY_KEY: "sesame",
                LuisType.CONFIG_ENDPOINT_URL: "http://luis.api",
                LuisType.CONFIG_VERSION: "0.1",
            },
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')
            logs = []
            with self.assertRaises(Exception):
                LuisType.get_active_intents_from_api(c, logs)
                self.assertEqual(1, len(logs))

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            logs = []
            intents = LuisType.get_active_intents_from_api(c, logs)
            self.assertEqual(1, len(logs))
            self.assertEqual(2, len(intents))
            car = intents[0]
            self.assertEqual("Book Car", car.name)
            self.assertEqual("b1a3c0ad-e912-4b55-a62e-6fcb77751bc5",
                             car.external_id)
Exemplo n.º 7
0
    def test_sync(self):
        c = Classifier.create(
            self.org,
            self.user,
            WitType.slug,
            "Booker",
            {
                WitType.CONFIG_APP_ID: "12345",
                WitType.CONFIG_ACCESS_TOKEN: "sesame"
            },
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')
            logs = []
            with self.assertRaises(Exception):
                WitType.get_active_intents_from_api(c, logs)
                self.assertEqual(1, len(logs))

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            logs = []
            intents = WitType.get_active_intents_from_api(c, logs)
            self.assertEqual(1, len(logs))
            self.assertEqual(2, len(intents))
            car = intents[0]
            self.assertEqual("book_car", car.name)
            self.assertEqual("book_car", car.external_id)
Exemplo n.º 8
0
    def test_sync(self):
        c = Classifier.create(
            self.org,
            self.user,
            BotHubType.slug,
            "Booker",
            {BotHubType.CONFIG_ACCESS_TOKEN: "123456789", BotHubType.INTENT_URL: "https://nlp.bothub.it/info/"},
        )

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(400, '{ "error": "true" }')
            logs = []
            with self.assertRaises(Exception):
                BotHubType.get_active_intents_from_api(c, logs)
                self.assertEqual(1, len(logs))

        with patch("requests.get") as mock_get:
            mock_get.return_value = MockResponse(200, INTENT_RESPONSE)
            logs = []
            intents = BotHubType.get_active_intents_from_api(c, logs)
            self.assertEqual(1, len(logs))
            self.assertEqual(3, len(intents))
            intent = intents[0]
            self.assertEqual("intent", intent.name)
            self.assertEqual("intent", intent.external_id)
Exemplo n.º 9
0
    def form_valid(self, form):
        from .type import BotHubType

        config = {BotHubType.CONFIG_ACCESS_TOKEN: form.cleaned_data["access_token"]}

        self.object = Classifier.create(
            self.org, self.request.user, BotHubType.slug, form.cleaned_data["name"], config
        )

        return super().form_valid(form)
Exemplo n.º 10
0
    def form_valid(self, form):
        from .type import WitType

        config = {
            WitType.CONFIG_ACCESS_TOKEN: form.cleaned_data["access_token"],
            WitType.CONFIG_APP_ID: str(form.cleaned_data["app_id"]),
        }

        self.object = Classifier.create(self.org, self.request.user,
                                        WitType.slug,
                                        form.cleaned_data["name"], config)

        return super().form_valid(form)
Exemplo n.º 11
0
    def form_valid(self, form):
        from .type import LuisType

        config = {
            LuisType.CONFIG_APP_ID: form.cleaned_data["app_id"],
            LuisType.CONFIG_AUTHORING_ENDPOINT: form.cleaned_data["authoring_endpoint"],
            LuisType.CONFIG_AUTHORING_KEY: form.cleaned_data["authoring_key"],
            LuisType.CONFIG_PREDICTION_ENDPOINT: form.cleaned_data["prediction_endpoint"],
            LuisType.CONFIG_PREDICTION_KEY: form.cleaned_data["prediction_key"],
            LuisType.CONFIG_SLOT: form.cleaned_data["slot"],
        }

        self.object = Classifier.create(self.org, self.request.user, LuisType.slug, form.cleaned_data["name"], config)

        return super().form_valid(form)
Exemplo n.º 12
0
    def form_valid(self, form):
        from .type import LuisType

        config = {
            LuisType.CONFIG_APP_ID: form.cleaned_data["app_id"],
            LuisType.CONFIG_VERSION: form.cleaned_data["version"],
            LuisType.CONFIG_PRIMARY_KEY: form.cleaned_data["primary_key"],
            LuisType.CONFIG_ENDPOINT_URL: form.cleaned_data["endpoint_url"],
        }

        self.object = Classifier.create(self.org, self.request.user,
                                        LuisType.slug,
                                        form.cleaned_data["name"], config)

        return super().form_valid(form)
Exemplo n.º 13
0
    def test_sync(self, mock_get):
        # create classifier but don't sync the intents
        c = Classifier.create(
            self.org,
            self.user,
            LuisType.slug,
            "Booker",
            {
                LuisType.CONFIG_APP_ID: "1dc41f72-b9d9-4a38-8999-5acf78e3d17e",
                LuisType.CONFIG_AUTHORING_ENDPOINT: "http://nyaruka-authoring.luis.api",
                LuisType.CONFIG_AUTHORING_KEY: "sesame",
                LuisType.CONFIG_PREDICTION_ENDPOINT: "http://nyaruka.luis.api",
                LuisType.CONFIG_PREDICTION_KEY: "sesame",
                LuisType.CONFIG_SLOT: "production",
            },
            sync=False,
        )

        mock_get.side_effect = [MockResponse(400, '{"error": "yes"}')]

        c.get_type().get_active_intents_from_api(c)
        self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 1)

        mock_get.side_effect = [MockResponse(100, "")]

        c.get_type().get_active_intents_from_api(c)
        self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 2)

        mock_get.side_effect = [
            MockResponse(200, GET_APP_RESPONSE),
            MockResponse(200, GET_VERSION_INTENTS_RESPONSE),
        ]

        intents = c.get_type().get_active_intents_from_api(c)

        self.assertEqual(HTTPLog.objects.filter(classifier=c).count(), 4)
        self.assertEqual(2, len(intents))
        self.assertEqual("Book Flight", intents[0].name)
        self.assertEqual("75d2e81c-e441-45ac", intents[0].external_id)
Exemplo n.º 14
0
    def test_delete(self):
        c = Classifier.create(
            self.org,
            self.user,
            WitType.slug,
            "Booker",
            {
                WitType.CONFIG_APP_ID: "12345",
                WitType.CONFIG_ACCESS_TOKEN: "sesame"
            },
        )

        # delete the classifier
        url = reverse("classifiers.classifier_delete", args=[c.uuid])
        response = self.client.post(url)
        self.assertRedirect(response, "/users/login/")

        self.login(self.admin)
        response = self.client.post(url)

        c.refresh_from_db()
        self.assertFalse(c.is_active)

        # reactivate
        c.is_active = True
        c.save()

        # add a dependency and try again
        flow = self.create_flow()
        flow.classifier_dependencies.add(c)

        with self.assertRaises(ValueError):
            self.client.post(url)

        c.refresh_from_db()
        self.assertTrue(c.is_active)
Exemplo n.º 15
0
    def test_list_orgs(self):
        org = Org.objects.first()
        org_uuid = str(org.uuid)

        classifier = Classifier.create(org,
                                       self.admin,
                                       WitType.slug,
                                       "Booker", {},
                                       sync=False)

        response = self.classifier_list_request(org_uuid=org_uuid)
        messages = [message for message in response]

        self.assertEqual(len(messages), 1)

        message = messages[0]

        self.assertEqual(message.name, "Booker")
        self.assertEqual(message.classifier_type, WitType.slug)
        self.assertEqual(message.uuid, str(classifier.uuid))

        classifier = Classifier.create(org,
                                       self.admin,
                                       LuisType.slug,
                                       "Test", {},
                                       sync=False)

        response = self.classifier_list_request(org_uuid=org_uuid)
        messages = [message for message in response]

        self.assertEqual(len(messages), 2)

        message = messages[0]

        self.assertEqual(message.name, "Test")
        self.assertEqual(message.classifier_type, LuisType.slug)
        self.assertEqual(message.uuid, str(classifier.uuid))

        response = self.classifier_list_request(org_uuid=org_uuid,
                                                classifier_type=LuisType.slug)
        messages = [message for message in response]

        self.assertEqual(len(messages), 1)

        message = messages[0]

        self.assertEqual(message.name, "Test")
        self.assertEqual(message.classifier_type, LuisType.slug)
        self.assertEqual(message.uuid, str(classifier.uuid))

        classifier = Classifier.create(org,
                                       self.admin,
                                       LuisType.slug,
                                       "Test2", {},
                                       sync=False)

        response = self.classifier_list_request(org_uuid=org_uuid,
                                                classifier_type=LuisType.slug)
        messages = [message for message in response]

        self.assertEqual(len(messages), 2)

        response = self.classifier_list_request(org_uuid=org_uuid)
        messages = [message for message in response]

        self.assertEqual(len(messages), 3)
Exemplo n.º 16
0
    def test_classifier(self):
        # create some classifiers
        c1 = Classifier.create(self.org,
                               self.admin,
                               WitType.slug,
                               "Booker", {},
                               sync=False)
        c1.intents.create(name="book_flight",
                          external_id="book_flight",
                          created_on=timezone.now(),
                          is_active=True)
        c1.intents.create(name="book_hotel",
                          external_id="book_hotel",
                          created_on=timezone.now(),
                          is_active=False)
        c1.intents.create(name="book_car",
                          external_id="book_car",
                          created_on=timezone.now(),
                          is_active=True)

        c2 = Classifier.create(self.org,
                               self.admin,
                               WitType.slug,
                               "Old Booker", {},
                               sync=False)
        c2.is_active = False
        c2.save()

        log_url = reverse("request_logs.httplog_classifier", args=[c1.uuid])
        response = self.client.get(log_url)
        self.assertLoginRedirect(response)

        self.login(self.admin)

        # create some logs
        l1 = HTTPLog.objects.create(
            classifier=c1,
            url="http://org1.bar/zap",
            request="GET /zap",
            response=" OK 200",
            is_error=False,
            log_type=HTTPLog.INTENTS_SYNCED,
            request_time=10,
            org=self.org,
        )
        l2 = HTTPLog.objects.create(
            classifier=c2,
            url="http://org2.bar/zap",
            request="GET /zap",
            response=" OK 200",
            is_error=False,
            log_type=HTTPLog.CLASSIFIER_CALLED,
            request_time=10,
            org=self.org,
        )

        response = self.client.get(log_url)
        self.assertEqual(1, len(response.context["object_list"]))
        self.assertContains(response, "Intents Synced")
        self.assertNotContains(response, "Classifier Called")

        log_url = reverse("request_logs.httplog_read", args=[l1.id])
        self.assertContains(response, log_url)

        response = self.client.get(log_url)
        self.assertContains(response, "200")
        self.assertContains(response, "http://org1.bar/zap")
        self.assertNotContains(response, "http://org2.bar/zap")

        self.login(self.admin2)
        response = self.client.get(log_url)
        self.assertLoginRedirect(response)

        # move l1 to be from a week ago
        l1.created_on = timezone.now() - timedelta(days=7)
        l1.save(update_fields=["created_on"])

        trim_http_logs_task()

        # should only have one log remaining and should be l2
        self.assertEqual(1, HTTPLog.objects.all().count())
        self.assertTrue(HTTPLog.objects.filter(id=l2.id))

        # release l2
        l2.release()
        self.assertFalse(HTTPLog.objects.filter(id=l2.id))
Exemplo n.º 17
0
    def test_classifier(self):
        c1 = Classifier.create(self.org,
                               self.admin,
                               WitType.slug,
                               "Booker", {},
                               sync=False)
        c2 = Classifier.create(self.org,
                               self.admin,
                               WitType.slug,
                               "Old Booker", {},
                               sync=False)
        c2.is_active = False
        c2.save()

        l1 = HTTPLog.objects.create(
            classifier=c1,
            url="http://org1.bar/zap/?text=" + ("0123456789" * 30),
            request="GET /zap",
            response=" OK 200",
            is_error=False,
            log_type=HTTPLog.INTENTS_SYNCED,
            request_time=10,
            org=self.org,
        )
        HTTPLog.objects.create(
            classifier=c2,
            url="http://org2.bar/zap",
            request="GET /zap",
            response=" OK 200",
            is_error=False,
            log_type=HTTPLog.CLASSIFIER_CALLED,
            request_time=10,
            org=self.org,
        )

        list_url = reverse("request_logs.httplog_classifier", args=[c1.uuid])
        log_url = reverse("request_logs.httplog_read", args=[l1.id])

        response = self.assertListFetch(list_url,
                                        allow_viewers=False,
                                        allow_editors=False,
                                        allow_org2=False,
                                        context_objects=[l1])
        self.assertContains(response, "Intents Synced")
        self.assertContains(response, log_url)
        self.assertNotContains(response, "Classifier Called")

        # view the individual log item
        response = self.assertReadFetch(log_url,
                                        allow_viewers=False,
                                        allow_editors=False,
                                        context_object=l1)
        self.assertContains(response, "200")
        self.assertContains(response, "http://org1.bar/zap")
        self.assertNotContains(response, "http://org2.bar/zap")

        # can't list logs for deleted classifier
        response = self.requestView(
            reverse("request_logs.httplog_classifier", args=[c2.uuid]),
            self.admin)
        self.assertEqual(404, response.status_code)