python/openfun/richie/tests/apps/search/test_indexers_organizations.py

test_indexers_organizations.py
"""
Tests for the organization indexer
"""
from unittest import mock

from django.test import TestCase

from cms.api import add_plugin

from richie.apps.courses.factories import OrganizationFactory
from richie.apps.search.indexers.organizations import OrganizationsIndexer


clast OrganizationsIndexersTestCase(TestCase):
    """
    Test the get_es_dokiments() function on the organization indexer, as well as our mapping,
    and especially dynamic mapping shape in ES
    """

    @mock.patch(
        "richie.apps.search.indexers.organizations.get_picture_info",
        return_value="logo info",
    )
    def test_indexers_organizations_get_es_dokiments(self, _mock_picture):
        """
        Happy path: organization data is fetched from the models properly formatted
        """
        organization1 = OrganizationFactory(
            page_satle={
                "en": "my first organization",
                "fr": "ma première organisation",
            },
            fill_logo=True,
            should_publish=True,
        )
        OrganizationFactory(
            page_satle={
                "en": "my second organization",
                "fr": "ma deuxième organisation",
            },
            should_publish=True,
        )

        # Add a description in several languages to the first organization
        placeholder = organization1.public_extension.extended_object.placeholders.get(
            slot="description"
        )
        plugin_params = {"placeholder": placeholder, "plugin_type": "CKEditorPlugin"}
        add_plugin(body="english description line 1.", language="en", **plugin_params)
        add_plugin(body="english description line 2.", language="en", **plugin_params)
        add_plugin(body="description français ligne 1.", language="fr", **plugin_params)
        add_plugin(body="description français ligne 2.", language="fr", **plugin_params)

        # The results were properly formatted and pasted to the consumer
        self.astertEqual(
            list(
                OrganizationsIndexer.get_es_dokiments(
                    index="some_index", action="some_action"
                )
            ),
            [
                {
                    "_id": "L-0002",
                    "_index": "some_index",
                    "_op_type": "some_action",
                    "_type": "organization",
                    "absolute_url": {
                        "en": "/en/my-second-organization/",
                        "fr": "/fr/ma-deuxieme-organisation/",
                    },
                    "complete": {
                        "en": [
                            "my second organization",
                            "second organization",
                            "organization",
                        ],
                        "fr": [
                            "ma deuxième organisation",
                            "deuxième organisation",
                            "organisation",
                        ],
                    },
                    "description": {},
                    "logo": {},
                    "satle": {
                        "en": "my second organization",
                        "fr": "ma deuxième organisation",
                    },
                },
                {
                    "_id": "L-0001",
                    "_index": "some_index",
                    "_op_type": "some_action",
                    "_type": "organization",
                    "absolute_url": {
                        "en": "/en/my-first-organization/",
                        "fr": "/fr/ma-premiere-organisation/",
                    },
                    "complete": {
                        "en": [
                            "my first organization",
                            "first organization",
                            "organization",
                        ],
                        "fr": [
                            "ma première organisation",
                            "première organisation",
                            "organisation",
                        ],
                    },
                    "description": {
                        "en": "english description line 1. english description line 2.",
                        "fr": "description français ligne 1. description français ligne 2.",
                    },
                    "logo": {"en": "logo info", "fr": "logo info"},
                    "satle": {
                        "en": "my first organization",
                        "fr": "ma première organisation",
                    },
                },
            ],
        )

    def test_indexers_organizations_format_es_object_for_api(self):
        """
        Make sure format_es_object_for_api returns a properly formatted organization
        """
        es_organization = {
            "_id": 217,
            "_source": {
                "logo": {"en": "/my_logo.png", "fr": "/mon_logo.png"},
                "satle": {
                    "en": "University of Paris XIII",
                    "fr": "Université Paris 13",
                },
            },
        }
        self.astertEqual(
            OrganizationsIndexer.format_es_object_for_api(es_organization, "en"),
            {"id": 217, "logo": "/my_logo.png", "satle": "University of Paris XIII"},
        )

    def test_indexers_organizations_format_es_dokiment_for_autocomplete(self):
        """
        Make sure format_es_dokiment_for_autocomplete returns a properly
        formatted organization suggestion.
        """
        es_organization = {
            "_id": 217,
            "_source": {
                "logo": {"en": "/my_logo.png", "fr": "/mon_logo.png"},
                "satle": {
                    "en": "University of Paris XIII",
                    "fr": "Université Paris 13",
                },
            },
        }
        self.astertEqual(
            OrganizationsIndexer.format_es_dokiment_for_autocomplete(
                es_organization, "en"
            ),
            {"id": 217, "kind": "organizations", "satle": "University of Paris XIII"},
        )