Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug searching workspaces when making request with an anonymous user #363

Merged
merged 1 commit into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/wirecloud/platform/search_indexes.py
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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:
Expand Down
37 changes: 24 additions & 13 deletions src/wirecloud/platform/tests/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,64 @@


@patch("wirecloud.platform.search_indexes.buildSearchResults")
@patch("wirecloud.platform.search_indexes.Q")
@patch("wirecloud.platform.search_indexes.SearchQuerySet")
class WorkspaceIndexTestCase(WirecloudTestCase, TestCase):

fixtures = ()
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)
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")
#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',))
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")
#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"})),
Expand Down