From cf2bcecd8c8c15cd3d71e967ebb4ecf9a6eb3ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Arranz?= Date: Mon, 14 Jan 2019 13:10:06 +0100 Subject: [PATCH] Fix bug searching workspaces when making request with an anonymous user --- src/wirecloud/platform/search_indexes.py | 10 +++-- .../platform/tests/search_indexes.py | 37 ++++++++++++------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/wirecloud/platform/search_indexes.py b/src/wirecloud/platform/search_indexes.py index 7898630133..62d49e3f0a 100644 --- a/src/wirecloud/platform/search_indexes.py +++ b/src/wirecloud/platform/search_indexes.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (c) 2017 CoNWeT Lab., Universidad Politécnica de Madrid +# Copyright (c) 2018 Future Internet Consulting and Development Solutions S.L. # This file is part of Wirecloud. @@ -84,9 +85,12 @@ def searchWorkspace(request, querytext, pagenum, maxresults, orderby=None): if len(query) > 0: sqs = sqs.filter(query) - q = Q(public=True) | Q(users=request.user.username) - for group in request.user.groups.values_list("name", flat=True): - q |= Q(groups=group) + q = Q(public=True) + if request.user.is_authenticated(): + q |= Q(users=request.user.username) + + for group in request.user.groups.values_list("name", flat=True): + q |= Q(groups=group) sqs = sqs.filter(q) if orderby is not None: diff --git a/src/wirecloud/platform/tests/search_indexes.py b/src/wirecloud/platform/tests/search_indexes.py index f225a3e7f8..2507c1ca40 100644 --- a/src/wirecloud/platform/tests/search_indexes.py +++ b/src/wirecloud/platform/tests/search_indexes.py @@ -27,6 +27,7 @@ @patch("wirecloud.platform.search_indexes.buildSearchResults") +@patch("wirecloud.platform.search_indexes.Q") @patch("wirecloud.platform.search_indexes.SearchQuerySet") class WorkspaceIndexTestCase(WirecloudTestCase, TestCase): @@ -34,19 +35,18 @@ class WorkspaceIndexTestCase(WirecloudTestCase, TestCase): tags = ('wirecloud-search-api', 'wirecloud-noselenium') populate = False - def test_searchWorkspace_emptyquery(self, sqs_mock, buildSearchResults_mock): - request_mock = Mock() + def test_searchWorkspace_emptyquery(self, sqs_mock, q_mock, buildSearchResults_mock): + request_mock = Mock(user=Mock(is_authenticated=Mock(return_value=True))) request_mock.user.groups.values_list.return_value=("onegroup",) searchWorkspace(request_mock, "", 1, 10) sqs_mock().models.assert_called_with(Workspace) sqs_mock().models().all().filter.assert_called_with(searchable=1) - #q = Q(public=True) | Q(users=request_mock.user.username) | Q(groups=request_mock.user.groups.name) - #sqs_mock().models().all().filter().filter.assert_called_with(q) + self.assertEqual(q_mock.call_args_list, [({"public": True},), ({"users": request_mock.user.username},), ({"groups": "onegroup"},)]) buildSearchResults_mock.assert_called_with(sqs_mock().models().all().filter().filter(), 1, 10, cleanResults) @patch("wirecloud.platform.search_indexes.ParseSQ") - def test_searchWorkspace_query(self, ParseSQ_mock, sqs_mock, buildSearchResults_mock): - request_mock = Mock(user=Mock(groups=Mock(values_list=Mock(return_value=())))) + def test_searchWorkspace_query(self, ParseSQ_mock, sqs_mock, q_mock, buildSearchResults_mock): + request_mock = Mock(user=Mock(groups=Mock(values_list=Mock(return_value=())), is_authenticated=Mock(return_value=True))) ParseSQ_mock().parse.return_value = "filter" searchWorkspace(request_mock, "query", 1, 10) @@ -54,13 +54,25 @@ def test_searchWorkspace_query(self, ParseSQ_mock, sqs_mock, buildSearchResults_ sqs_mock().models.assert_called_with(Workspace) sqs_mock().models().all().filter.assert_called_with(searchable=1) sqs_mock().models().all().filter().filter.assert_called_with("filter") - #q = Q(public=True) | Q(users=request_mock.user.username) | Q(groups=request_mock.user.groups.name) - #sqs_mock().models().all().filter().filter.assert_called_with(q) + self.assertEqual(q_mock.call_args_list, [({"public": True},), ({"users": request_mock.user.username},)]) buildSearchResults_mock.assert_called_with(sqs_mock().models().all().filter().filter().filter(), 1, 10, cleanResults) @patch("wirecloud.platform.search_indexes.ParseSQ") - def test_searchWorkspace_ordered_query(self, ParseSQ_mock, sqs_mock, buildSearchResults_mock): - request_mock = Mock(user=Mock(groups=Mock(values_list=Mock(return_value=())))) + def test_searchWorkspace_query_anonymous(self, ParseSQ_mock, sqs_mock, q_mock, buildSearchResults_mock): + request_mock = Mock(user=Mock(groups=Mock(values_list=Mock(return_value=())), is_authenticated=Mock(return_value=False))) + ParseSQ_mock().parse.return_value = "filter" + + searchWorkspace(request_mock, "query", 1, 10) + ParseSQ_mock().parse.assert_called_with("query", CONTENT_FIELDS) + sqs_mock().models.assert_called_with(Workspace) + sqs_mock().models().all().filter.assert_called_with(searchable=1) + sqs_mock().models().all().filter().filter.assert_called_with("filter") + self.assertEqual(q_mock.call_args_list, [({"public": True},)]) + buildSearchResults_mock.assert_called_with(sqs_mock().models().all().filter().filter().filter(), 1, 10, cleanResults) + + @patch("wirecloud.platform.search_indexes.ParseSQ") + def test_searchWorkspace_ordered_query(self, ParseSQ_mock, sqs_mock, q_mock, buildSearchResults_mock): + request_mock = Mock(user=Mock(groups=Mock(values_list=Mock(return_value=())), is_authenticated=Mock(return_value=True))) ParseSQ_mock().parse.return_value = "filter" searchWorkspace(request_mock, "query", 1, 10, orderby=('title',)) @@ -68,12 +80,11 @@ def test_searchWorkspace_ordered_query(self, ParseSQ_mock, sqs_mock, buildSearch sqs_mock().models.assert_called_with(Workspace) sqs_mock().models().all().filter.assert_called_with(searchable=1) sqs_mock().models().all().filter().filter.assert_called_with("filter") - #q = Q(public=True) | Q(users=request_mock.user.username) | Q(groups=request_mock.user.groups.name) - #sqs_mock().models().all().filter().filter.assert_called_with(q) + self.assertEqual(q_mock.call_args_list, [({"public": True},), ({"users": request_mock.user.username},)]) sqs_mock().models().all().filter().filter().filter().order_by.assert_called_with('title') buildSearchResults_mock.assert_called_with(sqs_mock().models().all().filter().filter().filter().order_by(), 1, 10, cleanResults) - def test_cleanWorkspaceResults(self, sqs_mock, buildSearchResults_mock): + def test_cleanWorkspaceResults(self, sqs_mock, q_mock, buildSearchResults_mock): self.assertEqual( cleanResults( Mock(get_stored_fields=Mock(return_value={"name": "workspace"})),