Пример #1
0
    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({})
Пример #2
0
    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'})
Пример #3
0
    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({})
Пример #4
0
    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({})
Пример #5
0
    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'
Пример #6
0
    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({})
Пример #7
0
    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'
Пример #8
0
    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"})
Пример #9
0
    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'
Пример #10
0
    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
Пример #11
0
    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'})
Пример #12
0
    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
Пример #13
0
    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'
Пример #14
0
    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': '******'})
Пример #15
0
    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"})
Пример #16
0
    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": "******"})
Пример #17
0
    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
Пример #18
0
    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")
Пример #19
0
    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
Пример #20
0
    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
Пример #21
0
    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
Пример #22
0
    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."),
        }
Пример #23
0
    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))
Пример #24
0
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,
    }
Пример #25
0
    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.'),
        }
Пример #26
0
    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.'),
        }
Пример #27
0
    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
Пример #28
0
    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
Пример #29
0
    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
Пример #30
0
    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
Пример #31
0
    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
Пример #32
0
    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
Пример #33
0
    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
Пример #34
0
    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."),
        }
Пример #35
0
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),
    }
Пример #36
0
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),
    }
Пример #37
0
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),
    }
Пример #38
0
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),
    }
Пример #39
0
    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),
        }
Пример #40
0
    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)
Пример #41
0
    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)
Пример #42
0
    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
Пример #43
0
    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
Пример #44
0
    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