Beispiel #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"))
    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
Beispiel #3
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="")]),
        ]
Beispiel #4
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
Beispiel #5
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("**"))
Beispiel #6
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="")]),
     ]
Beispiel #7
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="")]),
     ]
Beispiel #8
0
    def __init__(self, search_type: str):

        # TODO: In the future, we should separate the rendering and the generation of the results
        if search_type == "monitoring":
            self._generate_results = QuicksearchManager(
                raise_too_many_rows_error=False).generate_results
        elif search_type == "setup":
            self._generate_results = IndexSearcher(get_index_store()).search
        else:
            raise NotImplementedError(f"Renderer not implemented for type '{search_type}'")
Beispiel #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"))
Beispiel #10
0
 def test_sort_search_results(self) -> None:
     assert list(
         IndexSearcher._sort_search_results(
             {
                 "Hosts": [SearchResult(title="host", url="")],
                 "Setup": [SearchResult(title="setup_menu_entry", url="")],
                 "Global settings": [SearchResult(title="global_setting", url="")],
                 "Other topic": [SearchResult(title="other_item", url="")],
                 "Another topic": [SearchResult(title="another_item", url="")],
             }
         )
     ) == [
         ("Setup", [SearchResult(title="setup_menu_entry", url="")]),
         ("Hosts", [SearchResult(title="host", url="")]),
         ("Another topic", [SearchResult(title="another_item", url="")]),
         ("Other topic", [SearchResult(title="other_item", url="")]),
         ("Global settings", [SearchResult(title="global_setting", url="")]),
     ]
 def test_sort_search_results(self):
     assert list(
         IndexSearcher._sort_search_results({
             "Hosts": [SearchResult(title="host", url="")],
             "Setup": [SearchResult(title="setup_menu_entry", url="")],
             "Global settings":
             [SearchResult(title="global_setting", url="")],
             "Other topic": [SearchResult(title="other_item", url="")],
             "Another topic": [SearchResult(title="another_item", url="")],
         })) == [
             ('Setup', [SearchResult(title='setup_menu_entry', url='')]),
             ('Hosts', [SearchResult(title='host', url='')]),
             ('Another topic', [SearchResult(title='another_item',
                                             url='')]),
             ('Other topic', [SearchResult(title='other_item', url='')]),
             ('Global settings',
              [SearchResult(title='global_setting', url='')]),
         ]
Beispiel #12
0
 def test_search_no_index(self) -> None:
     with pytest.raises(IndexNotFoundException):
         list(IndexSearcher().search("change_dep"))
Beispiel #13
0
def fixture_index_searcher(index_builder: IndexBuilder) -> IndexSearcher:
    index_searcher = IndexSearcher()
    index_searcher._redis_client = index_builder._redis_client
    return index_searcher
 def test_search_no_index(self, with_admin_login):
     with pytest.raises(IndexNotFoundException):
         list(IndexSearcher().search("change_dep"))
Beispiel #15
0
 def test_search_no_index(self) -> None:
     with pytest.raises(IndexNotFoundException):
         list(
             IndexSearcher(PermissionsHandler(
                 URLChecker(ModeEditHost))).search("change_dep"))
Beispiel #16
0
def fixture_index_searcher(index_builder: IndexBuilder) -> IndexSearcher:
    index_searcher = IndexSearcher(
        PermissionsHandler(URLChecker[ModeEditHost](ModeEditHost)))
    index_searcher._redis_client = index_builder._redis_client
    return index_searcher