def test_removes_group_term_from_query(self, pyramid_request, unparse): query = MultiDict({'group': 'abcd1234'}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({})
def test_preserves_other_query_terms_for_group_search(self, pyramid_request, unparse): query = MultiDict({'group': 'abcd1234', 'tag': 'foo'}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({'tag': 'foo'})
def test_removes_group_term_from_query(self, group, pyramid_request, unparse): query = MultiDict({"group": group.pubid}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({})
def test_removes_user_term_from_query(self, pyramid_request, unparse): query = MultiDict({"user": "******"}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({})
def test_redirects_to_group_search_page_if_one_group_in_query(self, pyramid_request, unparse): query = MultiDict({'group': 'abcd1234'}) with pytest.raises(HTTPFound) as e: check_url(pyramid_request, query, unparse=unparse) assert e.value.location == '/act/groups/abcd1234?q=UNPARSED_QUERY'
def test_redirects_to_user_search_page_if_one_user_in_query(self, pyramid_request, unparse): query = MultiDict({'user': '******'}) with pytest.raises(HTTPFound) as e: check_url(pyramid_request, query, unparse=unparse) assert e.value.location == '/act/users/jose?q=UNPARSED_QUERY'
def test_preserves_other_query_terms_for_user_search( self, pyramid_request, unparse): query = MultiDict({"user": "******", "tag": "foo"}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({"tag": "foo"})
def test_redirects_to_user_search_page_if_one_user_in_query( self, pyramid_request, unparse): query = MultiDict({'user': '******'}) with pytest.raises(HTTPFound) as e: check_url(pyramid_request, query, unparse=unparse) assert e.value.location == '/act/users/jose?q=UNPARSED_QUERY'
def test_does_not_remove_group_term_from_query_if_group_does_not_exist( self, pyramid_request, unparse): query = MultiDict({"group": "does_not_exist"}) check_url(pyramid_request, query, unparse=unparse) assert query.get("group") == "does_not_exist" assert not unparse.called
def test_preserves_other_query_terms_for_user_search( self, pyramid_request, unparse): query = MultiDict({'user': '******', 'tag': 'foo'}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({'tag': 'foo'})
def test_does_not_remove_group_term_from_query_if_group_does_not_exist( self, pyramid_request, unparse): query = MultiDict({'group': 'does_not_exist'}) check_url(pyramid_request, query, unparse=unparse) assert query.get('group') == 'does_not_exist' assert not unparse.called
def test_redirects_to_group_search_page_if_one_group_in_query( self, pyramid_request, unparse): query = MultiDict({'group': 'abcd1234'}) with pytest.raises(HTTPFound) as e: check_url(pyramid_request, query, unparse=unparse) assert e.value.location == '/act/groups/abcd1234?q=UNPARSED_QUERY'
def test_preserves_user_query_terms_for_group_search( self, group, pyramid_request, unparse): query = MultiDict({'group': group.pubid, 'user': '******'}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({'user': '******'})
def test_preserves_other_query_terms_for_user_search( self, pyramid_request, unparse ): query = MultiDict({"user": "******", "tag": "foo"}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({"tag": "foo"})
def test_preserves_user_query_terms_for_group_search( self, group, pyramid_request, unparse ): query = MultiDict({"group": group.pubid, "user": "******"}) with pytest.raises(HTTPFound): check_url(pyramid_request, query, unparse=unparse) unparse.assert_called_once_with({"user": "******"})
def test_does_not_remove_group_term_from_query_if_group_does_not_exist( self, pyramid_request, unparse ): query = MultiDict({"group": "does_not_exist"}) check_url(pyramid_request, query, unparse=unparse) assert query.get("group") == "does_not_exist" assert not unparse.called
def test_redirects_to_group_search_page_if_one_group_in_query( self, group, pyramid_request, unparse): query = MultiDict({"group": group.pubid}) with pytest.raises(HTTPFound) as e: check_url(pyramid_request, query, unparse=unparse) assert e.value.location == ( f"/act/groups/{group.pubid}/{group.slug}?q=UNPARSED_QUERY")
def test_does_not_remove_user_term_from_query_if_user_does_not_exist( self, pyramid_request, unparse, user_service): query = MultiDict({'user': '******'}) user_service.fetch.return_value = None check_url(pyramid_request, query, unparse=unparse) assert query.get('user') == 'jose' assert not unparse.called
def test_does_not_remove_group_term_from_query_if_group_does_not_exist(self, pyramid_request, unparse): query = MultiDict({'group': 'does_not_exist'}) check_url(pyramid_request, query, unparse=unparse) assert query.get('group') == 'does_not_exist' assert not unparse.called
def test_does_not_remove_user_term_from_query_if_user_does_not_exist( self, pyramid_request, unparse, user_service ): query = MultiDict({"user": "******"}) user_service.fetch.return_value = None check_url(pyramid_request, query, unparse=unparse) assert query.get("user") == "jose" assert not unparse.called
def search(self): # Make a copy of the query params to be consumed by search. query_params = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, query_params) page_size = self.request.params.get("page_size", PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, query_params, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ "name": group.name, "pubid": group.pubid }) def tag_link(tag): tag = parser.unparse({"tag": tag}) return self.request.route_url("activity.search", _query=[("q", tag)]) def username_from_id(userid): parts = split_user(userid) return parts["username"] def user_link(userid): username = username_from_id(userid) return self.request.route_url("activity.user_search", username=username) return { "search_results": results, "groups_suggestions": groups_suggestions, "page": paginate(self.request, results.total, page_size=page_size), "pretty_link": pretty_link, "q": self.request.params.get("q", ""), "tag_link": tag_link, "user_link": user_link, "username_from_id": username_from_id, # The message that is shown (only) if there's no search results. "zero_message": _("No annotations matched your search."), }
def test_redirects_to_group_search_page_if_one_group_in_query(self, group, pyramid_request, unparse): query = MultiDict({'group': group.pubid}) with pytest.raises(HTTPFound) as e: check_url(pyramid_request, query, unparse=unparse) assert e.value.location == ( '/act/groups/{pubid}/{slug}?q=UNPARSED_QUERY'.format( pubid=group.pubid, slug=group.slug))
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) groups_suggestions = [] if request.authenticated_user: for group in request.authenticated_user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username=username_from_id(userid) return request.route_url('activity.user_search', username=username) return { 'aggregations': result.aggregations, 'groups_suggestions': groups_suggestions, 'page': paginate(request, result.total, page_size=page_size), 'pretty_link': pretty_link, 'q': request.params.get('q', ''), 'tag_link': tag_link, 'timeframes': result.timeframes, 'total': result.total, 'user_link': user_link, 'username_from_id': username_from_id, }
def search(self): # Make a copy of the query params to be consumed by search. q = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), }
def search(self): q = query.extract(self.request) # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), }
def test_does_nothing_if_not_on_search_page(self, pyramid_request, unparse): pyramid_request.matched_route.name = 'activity.group_search' query = MultiDict({'group': 'abcd1234'}) result = check_url(pyramid_request, query, unparse=unparse) assert result is None
def test_does_nothing_with_non_matching_queries(self, pyramid_request, unparse): query = MultiDict({'tag': 'foo'}) result = check_url(pyramid_request, query, unparse=unparse) assert result is None
def test_does_not_redirect_to_user_page_if_user_does_not_exist( self, pyramid_request, user_service ): query = MultiDict({"user": "******"}) user_service.fetch.return_value = None assert check_url(pyramid_request, query) is None
def test_does_nothing_if_not_on_search_page(self, pyramid_request, unparse): pyramid_request.matched_route.name = "group_read" query = MultiDict({"group": "abcd1234"}) result = check_url(pyramid_request, query, unparse=unparse) assert result is None
def search(self): # Make a copy of the query params to be consumed by search. q = self.parsed_query_params.copy() # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get("page_size", PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({"name": group.name, "pubid": group.pubid}) def tag_link(tag): q = parser.unparse({"tag": tag}) return self.request.route_url("activity.search", _query=[("q", q)]) def username_from_id(userid): parts = split_user(userid) return parts["username"] def user_link(userid): username = username_from_id(userid) return self.request.route_url("activity.user_search", username=username) return { "search_results": results, "groups_suggestions": groups_suggestions, "page": paginate(self.request, results.total, page_size=page_size), "pretty_link": pretty_link, "q": self.request.params.get("q", ""), "tag_link": tag_link, "user_link": user_link, "username_from_id": username_from_id, # The message that is shown (only) if there's no search results. "zero_message": _("No annotations matched your search."), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) # Fetch results result = query.execute(request, q, page_size=PAGE_SIZE) return { 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=PAGE_SIZE), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) if q is None: return {} # Check whether a redirect is required query.check_url(request, q) # Fetch results result = query.execute(request, q) return { 'q': request.params['q'], 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) for user in result.aggregations.get('users', []): user['username'] = util.user.split_user(user['user'])['username'] user['userid'] = user['user'] user['faceted_by'] = _faceted_by_user(request, user['username'], q) del user['user'] groups_suggestions = [] if request.authenticated_user: for group in request.authenticated_user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) return { 'total': result.total, 'aggregations': result.aggregations, 'groups_suggestions': groups_suggestions, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=page_size), }
def search(request): if not request.feature('search_page'): raise httpexceptions.HTTPNotFound() q = query.extract(request) # Check whether a redirect is required query.check_url(request, q) page_size = request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results result = query.execute(request, q, page_size=page_size) return { 'total': result.total, 'aggregations': result.aggregations, 'timeframes': result.timeframes, 'page': paginate(request, result.total, page_size=page_size), }
def search(self): # If the user is just logged-in in 10 seconds, create JWT grant tokens # for each JWT clients. user = self.request.user grant_tokens = [] if user is not None and \ datetime.utcnow() < user.last_login_date + timedelta(seconds=10): clients = self.request.db.query(AuthClient) \ .order_by(AuthClient.name.asc()) \ .all() for client in clients: if client.secret is not None: grant_tokens.append( self._create_grant_token(user.username, client)) q = query.extract(self.request) # Check whether a redirect is required. query.check_url(self.request, q) page_size = self.request.params.get('page_size', PAGE_SIZE) try: page_size = int(page_size) except ValueError: page_size = PAGE_SIZE # Fetch results. results = query.execute(self.request, q, page_size=page_size) groups_suggestions = [] if self.request.user: for group in self.request.user.groups: groups_suggestions.append({ 'name': group.name, 'pubid': group.pubid }) def tag_link(tag): q = parser.unparse({'tag': tag}) return self.request.route_url('activity.search', _query=[('q', q)]) def username_from_id(userid): parts = split_user(userid) return parts['username'] def user_link(userid): username = username_from_id(userid) return self.request.route_url('activity.user_search', username=username) return { 'search_results': results, 'groups_suggestions': groups_suggestions, 'page': paginate(self.request, results.total, page_size=page_size), 'pretty_link': pretty_link, 'q': self.request.params.get('q', ''), 'tag_link': tag_link, 'user_link': user_link, 'username_from_id': username_from_id, # The message that is shown (only) if there's no search results. 'zero_message': _('No annotations matched your search.'), 'grant_tokens': enumerate(grant_tokens), }
def test_doesnt_raise_with_non_matching_queries(self, pyramid_request, unparse): query = MultiDict({"tag": "foo"}) assert not check_url(pyramid_request, query, unparse=unparse)
def test_doesnt_raise_if_not_on_search_page(self, pyramid_request, unparse): pyramid_request.matched_route.name = "group_read" query = MultiDict({"group": "abcd1234"}) assert not check_url(pyramid_request, query, unparse=unparse)
def test_does_not_redirect_to_group_page_if_group_does_not_exist( self, pyramid_request, unparse ): query = MultiDict({"group": "does_not_exist"}) assert check_url(pyramid_request, query, unparse=unparse) is None
def test_does_nothing_with_non_matching_queries(self, pyramid_request, unparse): query = MultiDict({"tag": "foo"}) result = check_url(pyramid_request, query, unparse=unparse) assert result is None
def test_does_not_redirect_to_group_page_if_group_does_not_exist( self, pyramid_request, unparse): query = MultiDict({'group': 'does_not_exist'}) assert check_url(pyramid_request, query, unparse=unparse) is None