Beispiel #1
0
    def test_get_args(self):
        @functions.signature(str,
                             str,
                             int,
                             atypes.ArrayType(int),
                             types.listtype,
                             body=NestedObj)
        def myfunc(self, first, second, third, foo, nested):
            pass

        funcdef = functions.FunctionDefinition.get(myfunc)
        params = multidict.MultiDict(foo='0,1, 2, three', second='2')
        mimetype = 'application/json'
        body = b'{"o": {"id": 1234, "name": "an object"}}'
        fromargs = ['one']
        fromkwargs = {'third': '1'}

        newargs, newkwargs = args.get_args(funcdef, fromargs, fromkwargs,
                                           params, body, mimetype)
        self.assertEqual([], newargs)
        n = newkwargs.pop('nested')
        self.assertEqual(
            {
                'first': 'one',
                'foo': ['0', '1', '2', 'three'],
                'second': 2,
                'third': [1]
            }, newkwargs)
        self.assertEqual(1234, n.o.id)
        self.assertEqual('an object', n.o.name)

        # check_arguments missing mandatory argument 'second'
        params = multidict.MultiDict(foo='0,1, 2, three', )
        self.assertRaises(exception.MissingArgument, args.get_args, funcdef,
                          fromargs, fromkwargs, params, body, mimetype)
Beispiel #2
0
    def test_queries_multiple_uris(self, storage):
        """
        Uses a `terms` filter against target.scope to filter for URI.

        When multiple "uri" fields are supplied, the normalized URIs of all of
        them should be collected into a set and sent in the query.
        """
        request = mock.Mock()
        params = multidict.MultiDict()
        params.add("uri", "http://example.com")
        params.add("uri", "http://example.net")
        storage.expand_uri.side_effect = [
            ["http://giraffes.com/", "https://elephants.com/"],
            ["http://tigers.com/", "https://elephants.com/"],
        ]

        urifilter = query.UriFilter(request)

        result = urifilter(params)
        query_uris = result["terms"]["target.scope"]

        storage.expand_uri.assert_any_call(request.db, "http://example.com")
        storage.expand_uri.assert_any_call(request.db, "http://example.net")
        assert sorted(query_uris) == sorted(["httpx://giraffes.com",
                                             "httpx://elephants.com",
                                             "httpx://tigers.com"])
Beispiel #3
0
 def _get_instance(self, **kwargs):
     if kwargs:
         data = multidict.MultiDict(kwargs)
     else:
         data = self.data.copy()
     def tracker(*args, **kwargs): pass
     return self.klass(data, __tracker=tracker, __name='tracker')
 def test_add(self):
     d = multidict.MultiDict({"a": "1"})
     d.add("a", "2")
     self.assertEqual(list(d.getall("a")), ["1", "2"])
     d = self._get_instance()
     d.add("b", "3")
     self.assertEqual(list(d.getall("b")), ["1", "3"])
Beispiel #5
0
 def test_add(self):
     d = multidict.MultiDict({'a': '1'}) 
     d.add('a', '2')
     self.assertEquals(list(d.getall('a')), ['1', '2'])
     d = self._get_instance()
     d.add('b', '3')
     self.assertEqual(list(d.getall('b')), ['1', '3'])
Beispiel #6
0
def test_tagsmatcher_aliases_tag_to_tags():
    """'tag' params should be transformed into 'tags' queries.

    'tag' is aliased to 'tags' because users often type tag instead of tags.

    """
    params = multidict.MultiDict()
    params.add('tag', 'foo')
    params.add('tag', 'bar')

    result = query.TagsMatcher()(params)

    assert result == {
        'bool': {
            'must': [
                {
                    'match': {
                        'tags': {
                            'query': 'foo',
                            'operator': 'and'
                        }
                    }
                },
                {
                    'match': {
                        'tags': {
                            'query': 'bar',
                            'operator': 'and'
                        }
                    }
                },
            ]
        }
    }
Beispiel #7
0
def test_tagsmatcher_aliases_tag_to_tags():
    """'tag' params should be transformed into 'tags' queries.

    'tag' is aliased to 'tags' because users often type tag instead of tags.

    """
    params = multidict.MultiDict()
    params.add('tag', 'foo')
    params.add('tag', 'bar')

    result = query.TagsMatcher()(params)

    assert list(result.keys()) == ['bool']
    assert list(result['bool'].keys()) == ['must']
    assert len(result['bool']['must']) == 2
    assert {
        'match': {
            'tags': {
                'query': 'foo',
                'operator': 'and'
            }
        }
    } in result['bool']['must']
    assert {
        'match': {
            'tags': {
                'query': 'bar',
                'operator': 'and'
            }
        }
    } in result['bool']['must']
Beispiel #8
0
    def test_from_param(self):
        # datetime param
        expected = datetime.datetime(2015, 8, 13, 11, 38, 9, 496475)
        self.assertEqual(
            expected,
            args.from_param(datetime.datetime, '2015-08-13T11:38:09.496475'))
        self.assertIsNone(args.from_param(datetime.datetime, None))

        # usertype param
        self.assertEqual(['0', '1', '2', 'three'],
                         args.from_param(types.listtype, '0,1, 2, three'))

        # array param
        atype = atypes.ArrayType(int)
        self.assertEqual([0, 1, 1234, None],
                         args.from_param(atype, [0, '1', '1_234', None]))
        self.assertIsNone(args.from_param(atype, None))

        # string param
        self.assertEqual('foo', args.from_param(str, 'foo'))
        self.assertIsNone(args.from_param(str, None))

        # string param with from_params
        hit_paths = set()
        params = multidict.MultiDict(foo='bar', )
        self.assertEqual('bar', args.from_params(str, params, 'foo',
                                                 hit_paths))
        self.assertEqual({'foo'}, hit_paths)
Beispiel #9
0
    def test_supports_filtering_for_multiple_users(self):
        userfilter = query.UserFilter()

        params = multidict.MultiDict()
        params.add("user", "alice")
        params.add("user", "luke")

        assert userfilter(params) == {"terms": {"user": ["alice", "luke"]}}
Beispiel #10
0
 def _get_instance(self, environ=None, **kwargs):
     if environ is None:
         environ = {}
     if kwargs:
         data = multidict.MultiDict(kwargs)
     else:
         data = self.data.copy()
     return self.klass(data, environ)
    def generate_response(self, start_response, status, headers, body):
        headers = multidict.MultiDict(headers)
        content_type = headers.get('Content-Type')
        if content_type and content_type.startswith('text/html'):
            body = self.generate_body(body)
            headers['Content-Length'] = str(len(body))

        start_response(status, list(headers.iteritems()))(body)
Beispiel #12
0
 def test_array_from_params(self):
     hit_paths = set()
     datatype = atypes.ArrayType(str)
     params = multidict.MultiDict(foo='bar', one='two')
     self.assertEqual(['bar'],
                      args.from_params(datatype, params, 'foo', hit_paths))
     self.assertEqual({'foo'}, hit_paths)
     self.assertEqual(['two'],
                      args.array_from_params(datatype, params, 'one',
                                             hit_paths))
     self.assertEqual({'foo', 'one'}, hit_paths)
Beispiel #13
0
 def test_usertype_from_params(self):
     hit_paths = set()
     datatype = types.listtype
     params = multidict.MultiDict(foo='0,1, 2, three', )
     self.assertEqual(['0', '1', '2', 'three'],
                      args.usertype_from_params(datatype, params, 'foo',
                                                hit_paths))
     self.assertEqual(['0', '1', '2', 'three'],
                      args.from_params(datatype, params, 'foo', hit_paths))
     self.assertEqual(
         atypes.Unset,
         args.usertype_from_params(datatype, params, 'bar', hit_paths))
    def on_unenroll_submit(cls, student, handler, parameters_list):
        # pylint: disable=abstract-class-instantiated
        parameters = multidict.MultiDict(parameters_list)
        if parameters.get(cls.DATA_REMOVAL_FIELD_NAME, 'False') != 'True':
            return False

        # Paint first page of our hijacking flow.
        handler.template_value['unenroll_parameters'] = transforms.dumps(
            parameters_list)
        DataRemovalConfirmationHandler.class_get(handler)

        # Tell unenroll_post_continue that we are hijacking the page flow,
        # so it should not render its own page.
        return True
Beispiel #15
0
    def test_default_params_multidict(self):
        """Multiple params go into multiple "match" dicts."""
        builder = query.Builder(ES_VERSION)
        params = multidict.MultiDict()
        params.add("user", "fred")
        params.add("user", "bob")

        q = builder.build(params)

        assert q["query"] == {
            'bool': {'filter': [],
                     'must': [{'match': {'user': '******'}},
                              {'match': {'user': '******'}}]},
        }
Beispiel #16
0
    def test_args_from_params(self):
        @functions.signature(str, str, int, atypes.ArrayType(int),
                             types.listtype)
        def myfunc(self, first, second, third, foo):
            pass

        funcdef = functions.FunctionDefinition.get(myfunc)
        params = multidict.MultiDict(foo='0,1, 2, three',
                                     third='1',
                                     second='2')
        self.assertEqual(([], {
            'foo': ['0', '1', '2', 'three'],
            'second': 2,
            'third': [1]
        }), args.args_from_params(funcdef, params))

        # unexpected param
        params = multidict.MultiDict(bar='baz')
        self.assertRaises(exception.UnknownArgument, args.args_from_params,
                          funcdef, params)

        # no params plus a body
        params = multidict.MultiDict(__body__='')
        self.assertEqual(([], {}), args.args_from_params(funcdef, params))
Beispiel #17
0
def test_anymatcher_multiple_params():
    """Multiple keywords at once are handled correctly."""
    anymatcher = query.AnyMatcher()
    params = multidict.MultiDict()
    params.add("any", "howdy")
    params.add("any", "there")

    result = anymatcher(params)

    assert result == {
        "simple_query_string": {
            "fields": ["quote", "tags", "text", "uri.parts", "user"],
            "query": "howdy there",
        }
    }
Beispiel #18
0
def test_builder_default_params_multidict():
    """Multiple params go into multiple "match" dicts."""
    builder = query.Builder()
    params = multidict.MultiDict()
    params.add("user", "fred")
    params.add("user", "bob")

    q = builder.build(params)

    assert q["query"] == {
        "bool": {
            "must": [
                {"match": {"user": "******"}},
                {"match": {"user": "******"}}
            ]
        }
    }
Beispiel #19
0
    def params(self):
        """Override params property of webob.request.BaseRequest.
        Added an 'encoded_params' attribute in case of PY2 to avoid
        encoding values in next subsequent calls to the params property.
        """
        if six.PY2:
            encoded_params = getattr(self, 'encoded_params', None)
            if encoded_params is None:
                params = super(Request, self).params
                params_dict = multidict.MultiDict()
                for key, value in params.items():
                    params_dict.add(key, encodeutils.safe_encode(value))

                setattr(self, 'encoded_params',
                        multidict.NestedMultiDict(params_dict))
            return self.encoded_params
        return super(Request, self).params
Beispiel #20
0
def test_build_with_keyword():
    """Keywords are returned in the query dict in a "multi_match" clause."""
    params = multidict.MultiDict()
    params.add("any", "howdy")

    q = query.build(request_params=params)

    assert q["query"]["filtered"]["query"] == {
        "bool": {
            "must": [{
                "multi_match": {
                    "fields": ["quote", "tags", "text", "uri.parts", "user"],
                    "query": ["howdy"],
                    "type": "cross_fields"
                }
            }]
        }
    }
    def test_post(self):
        request = webapp2.Request.blank('/blobstore',
                                        method='POST',
                                        POST=multidict.MultiDict([
                                            ('blob_key', 'a'),
                                            ('blob_key', 'b')
                                        ]))
        response = webapp2.Response()
        handler = blobstore_viewer.BlobstoreRequestHandler(request, response)

        self.mox.StubOutWithMock(blobstore, 'delete')
        blobstore.delete(['a', 'b'])

        self.mox.ReplayAll()
        handler.post()
        self.mox.VerifyAll()
        self.assertEqual(302, response.status_int)
        self.assertEqual('http://localhost/blobstore',
                         response.headers.get('Location'))
Beispiel #22
0
def test_build_with_multiple_keywords():
    """Multiple keywords at once are handled correctly."""
    params = multidict.MultiDict()
    params.add("any", "howdy")
    params.add("any", "there")

    q = query.build(request_params=params)

    assert q["query"]["filtered"]["query"] == {
        "bool": {
            "must": [{
                "multi_match": {
                    "fields": ["quote", "tags", "text", "uri.parts", "user"],
                    "query": ["howdy", "there"],
                    "type": "cross_fields"
                }
            }]
        }
    }
Beispiel #23
0
    def test_accepts_url_aliases(self, storage):
        request = mock.Mock()
        params = multidict.MultiDict()
        params.add("uri", "http://example.com")
        params.add("url", "http://example.net")
        storage.expand_uri.side_effect = [
            ["http://giraffes.com/", "https://elephants.com/"],
            ["http://tigers.com/", "https://elephants.com/"],
        ]

        urifilter = query.UriFilter(request)

        result = urifilter(params)
        query_uris = result["terms"]["target.scope"]

        storage.expand_uri.assert_any_call(request.db, "http://example.com")
        storage.expand_uri.assert_any_call(request.db, "http://example.net")
        assert sorted(query_uris) == sorted(["httpx://giraffes.com",
                                             "httpx://elephants.com",
                                             "httpx://tigers.com"])
Beispiel #24
0
def test_build_query_with_multiple_text_params():
    """Multiple "test" request params produce multiple "match" clauses."""
    params = multidict.MultiDict()
    params.add("text", "foo")
    params.add("text", "bar")
    query = search.build_query(request_params=params)

    assert query["query"] == {
        "bool": {
            "must": [{
                "match": {
                    "text": "foo"
                }
            }, {
                "match": {
                    "text": "bar"
                }
            }]
        }
    }
Beispiel #25
0
def test_build_with_multiple_quote_params():
    """Multiple "quote" request params produce multiple "match" clauses."""
    params = multidict.MultiDict()
    params.add("quote", "foo")
    params.add("quote", "bar")
    q = query.build(request_params=params)

    assert q["query"]["filtered"]["query"] == {
        "bool": {
            "must": [{
                "match": {
                    "quote": "foo"
                }
            }, {
                "match": {
                    "quote": "bar"
                }
            }]
        }
    }
Beispiel #26
0
def test_build_for_multiple_users():
    """Multiple "user" params go into multiple "match" dicts."""
    params = multidict.MultiDict()
    params.add("user", "fred")
    params.add("user", "bob")

    q = query.build(request_params=params)

    assert q["query"]["filtered"]["query"] == {
        "bool": {
            "must": [{
                "match": {
                    "user": "******"
                }
            }, {
                "match": {
                    "user": "******"
                }
            }]
        }
    }
Beispiel #27
0
def test_build_for_multiple_tags():
    """Multiple "tags" params go into multiple "match" dicts."""
    params = multidict.MultiDict()
    params.add("tags", "foo")
    params.add("tags", "bar")

    q = query.build(request_params=params)

    assert q["query"]["filtered"]["query"] == {
        "bool": {
            "must": [{
                "match": {
                    "tags": "foo"
                }
            }, {
                "match": {
                    "tags": "bar"
                }
            }]
        }
    }
Beispiel #28
0
 def setUp(self):
     self._list = [("a", text_("\xe9")), ("a", "e"), ("a", "f"), ("b", "1")]
     self.data = multidict.MultiDict(self._list)
     self.d = self._get_instance()
Beispiel #29
0
 def setUp(self):
     self._list = [('a', text_('\xe9')), ('a', 'e'), ('a', 'f'), ('b', '1')]
     self.data = multidict.MultiDict(self._list)
     self.d = self._get_instance()
Beispiel #30
0
 def _get_instance(self, **kwargs):
     if kwargs:
         data = multidict.MultiDict(kwargs)
     else:
         data = self.data.copy()
     return self.klass(data, {})