Ejemplo n.º 1
0
    def test_dcd_not_found_if_not_super_user(
        self,
        monkeypatch: MonkeyPatch,
        mock_livestatus: MockLiveStatusConnection,
    ):
        """
        This test ensures that test_index_is_built_as_super_user makes sense, ie. that if we do not
        build as a super user, the entry "Custom host attributes" is not found.
        """

        @contextmanager
        def SuperUserContext() -> Iterator[None]:
            yield

        monkeypatch.setattr(
            search,
            "SuperUserContext",
            SuperUserContext,
        )

        with _UserContext(LoggedInNobody()):
            builder = IndexBuilder(real_match_item_generator_registry)
            with self._livestatus_mock(mock_livestatus):
                builder.build_full_index()

        searcher = IndexSearcher()
        searcher._redis_client = builder._redis_client
        assert not list(searcher.search("custom host attributes"))
Ejemplo n.º 2
0
    def test_update_with_empty_and_search(
        self,
        monkeypatch: MonkeyPatch,
        match_item_generator_registry: MatchItemGeneratorRegistry,
        index_builder: IndexBuilder,
        index_searcher: IndexSearcher,
    ) -> None:
        """
        Test if things can also be deleted from the index during an update
        """

        def empty_match_item_gen():
            yield from ()

        index_builder.build_full_index()

        monkeypatch.setattr(
            match_item_generator_registry["change_dependent"],
            "generate_match_items",
            empty_match_item_gen,
        )

        index_builder.build_changed_sub_indices("some_change_dependent_whatever")
        assert list(index_searcher.search("**")) == [
            ("Localization-dependent", [SearchResult(title="localization_dependent", url="")]),
        ]
Ejemplo n.º 3
0
    def test_language_after_built(
        self,
        monkeypatch: MonkeyPatch,
        index_builder: IndexBuilder,
    ) -> None:

        current_lang = ""

        def localize_with_memory(lang):
            """Needed to remember currently set language"""
            nonlocal current_lang
            current_lang = lang
            localize(lang)

        monkeypatch.setattr(
            search,
            "localize",
            localize_with_memory,
        )
        monkeypatch.setattr(
            search,
            "get_current_language",
            lambda: current_lang,
        )

        start_lang = ""
        localize_with_memory(start_lang)
        index_builder.build_full_index()
        assert current_lang == start_lang
Ejemplo n.º 4
0
 def test_update_and_search_no_update(
     self,
     index_builder: IndexBuilder,
     index_searcher: IndexSearcher,
 ) -> None:
     index_builder._mark_index_as_built()
     index_builder.build_changed_sub_indices("something")
     assert not list(index_searcher.search("**"))
Ejemplo n.º 5
0
 def test_update_and_search_with_update(
     self,
     index_builder: IndexBuilder,
     index_searcher: IndexSearcher,
 ) -> None:
     index_builder._mark_index_as_built()
     index_builder.build_changed_sub_indices("some_change_dependent_whatever")
     assert list(index_searcher.search("**")) == [
         ("Change-dependent", [SearchResult(title="change_dependent", url="")]),
     ]
Ejemplo n.º 6
0
 def test_full_build_and_search(
     self,
     index_builder: IndexBuilder,
     index_searcher: IndexSearcher,
 ) -> None:
     index_builder.build_full_index()
     assert list(index_searcher.search("**")) == [
         ("Change-dependent", [SearchResult(title="change_dependent", url="")]),
         ("Localization-dependent", [SearchResult(title="localization_dependent", url="")]),
     ]
Ejemplo n.º 7
0
    def test_real_search_without_exception(self, mock_livestatus) -> None:
        builder = IndexBuilder(real_match_item_generator_registry)

        with self._livestatus_mock(mock_livestatus):
            builder.build_full_index()

        assert builder.index_is_built(builder._redis_client)

        searcher = IndexSearcher()
        searcher._redis_client = builder._redis_client

        assert len(list(searcher.search("Host"))) > 4
Ejemplo n.º 8
0
    def test_real_search_without_exception(
        self,
        mock_livestatus: MockLiveStatusConnection,
    ) -> None:
        builder = IndexBuilder(real_match_item_generator_registry)

        with self._livestatus_mock(mock_livestatus):
            builder.build_full_index()

        assert builder.index_is_built(builder._redis_client)

        searcher = IndexSearcher(
            PermissionsHandler(URLChecker[ModeEditHost](ModeEditHost)))
        searcher._redis_client = builder._redis_client

        assert len(list(searcher.search("Host"))) > 4
Ejemplo n.º 9
0
    def test_index_is_built_as_super_user(
        self,
        mock_livestatus: MockLiveStatusConnection,
    ):
        """
        We test that the index is always built as a super user.
        """

        with _UserContext(LoggedInNobody()):
            builder = IndexBuilder(real_match_item_generator_registry)
            with self._livestatus_mock(mock_livestatus):
                builder.build_full_index()

        searcher = IndexSearcher()
        searcher._redis_client = builder._redis_client

        # if the search index did not internally use the super user while building, this item would
        # be missing, because the match item generator for the setup menu only yields entries which
        # the current user is allowed to see
        assert list(searcher.search("custom host attributes"))
Ejemplo n.º 10
0
 def test_update_only_not_built(
     self,
     index_builder: IndexBuilder,
 ) -> None:
     index_builder.build_changed_sub_indices("something")
     assert not index_builder.index_is_built(index_builder._redis_client)
Ejemplo n.º 11
0
def fixture_index_builder(
    match_item_generator_registry: MatchItemGeneratorRegistry,
) -> IndexBuilder:
    return IndexBuilder(match_item_generator_registry)
Ejemplo n.º 12
0
def fixture_index_builder(match_item_generator_registry):
    return IndexBuilder(match_item_generator_registry)