Example #1
0
    def test_viewsets_categories_search_with_invalid_params(self):
        """
        Error case: the client used an incorrectly formatted request
        """
        factory = APIRequestFactory()
        # The request contains incorrect params: limit should be a positive integer
        request = factory.get("/api/v1.0/subjects/?name=&limit=-2")

        response = CategoriesViewSet.as_view({"get": "list"})(
            request, version="1.0", kind="subjects"
        )

        # The client received a BadRequest response with the relevant data
        self.assertEqual(response.status_code, 400)
        self.assertTrue("limit" in response.data["errors"])
Example #2
0
    def test_viewsets_categories_retrieve_unknown(self):
        """
        Error case: the client is asking for a category that does not exist
        """
        factory = APIRequestFactory()
        request = factory.get("/api/v1.0/categories/43")

        # Act like the ES client would when we attempt to get a non-existent document
        with mock.patch.object(settings.ES_CLIENT,
                               "get",
                               side_effect=NotFoundError):
            response = CategoriesViewSet.as_view({"get":
                                                  "retrieve"})(request,
                                                               43,
                                                               version="1.0")

        # The client received a standard NotFound response
        self.assertEqual(response.status_code, 404)
Example #3
0
    def test_viewsets_categories_retrieve(self):
        """
        Happy path: the client requests an existing category, gets it back
        """
        factory = APIRequestFactory()
        request = factory.get("/api/v1.0/categories/42")

        with mock.patch.object(
                settings.ES_CLIENT,
                "get",
                return_value={
                    "_id": 42,
                    "_source": {
                        "is_meta": True,
                        "logo": {
                            "fr": "/image42.png"
                        },
                        "nb_children": 1,
                        "path": "0001",
                        "title": {
                            "fr": "Some Category"
                        },
                    },
                },
        ):
            # Note: we need to use a separate argument for the ID as that is what the ViewSet uses
            response = CategoriesViewSet.as_view({"get":
                                                  "retrieve"})(request,
                                                               42,
                                                               version="1.0")

        # The client received a proper response with the relevant category
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.data,
            {
                "id": 42,
                "is_meta": True,
                "logo": "/image42.png",
                "nb_children": 1,
                "path": "0001",
                "title": "Some Category",
            },
        )
Example #4
0
    def test_viewsets_categories_search(self, mock_search):
        """
        Happy path: the category is filtering the categories by name
        """
        factory = APIRequestFactory()
        request = factory.get("/api/v1.0/subjects/?query=Science&limit=2")

        mock_search.return_value = {
            "hits": {
                "hits": [
                    {
                        "_id": 21,
                        "_source": {
                            "icon": {"fr": "/icon21.png"},
                            "is_meta": True,
                            "logo": {"fr": "/logo21.png"},
                            "nb_children": 1,
                            "path": "0002",
                            "title": {"fr": "Computer Science"},
                        },
                    },
                    {
                        "_id": 61,
                        "_source": {
                            "icon": {"fr": "/icon61.png"},
                            "is_meta": False,
                            "logo": {"fr": "/logo61.png"},
                            "nb_children": 0,
                            "path": "00020001",
                            "title": {"fr": "Engineering Sciences"},
                        },
                    },
                ],
                "total": 32,
            }
        }

        response = CategoriesViewSet.as_view({"get": "list"})(
            request, version="1.0", kind="subjects"
        )

        # The client received a properly formatted response
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.data,
            {
                "meta": {"count": 2, "offset": 0, "total_count": 32},
                "objects": [
                    {
                        "icon": "/icon21.png",
                        "id": 21,
                        "is_meta": True,
                        "logo": "/logo21.png",
                        "nb_children": 1,
                        "path": "0002",
                        "title": "Computer Science",
                    },
                    {
                        "icon": "/icon61.png",
                        "id": 61,
                        "is_meta": False,
                        "logo": "/logo61.png",
                        "nb_children": 0,
                        "path": "00020001",
                        "title": "Engineering Sciences",
                    },
                ],
            },
        )
        # The ES connector was called with a query that matches the client's request
        mock_search.assert_called_with(
            _source=[
                "absolute_url",
                "icon",
                "is_meta",
                "logo",
                "nb_children",
                "path",
                "title.*",
            ],
            body={
                "query": {
                    "bool": {
                        "must": [
                            {"term": {"kind": "subjects"}},
                            {
                                "multi_match": {
                                    "query": "Science",
                                    "fields": ["title.*"],
                                }
                            },
                        ]
                    }
                }
            },
            doc_type="category",
            from_=0,
            index="richie_categories",
            size=2,
        )