Exemplo n.º 1
0
    def test_chained_validators_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([
            ('foo', {
                'validators': [self.bar_validator,
                               self.bat_validator,
                               self.baz_validator],
                'required': True
                })])(self.handler.get)

        # Called without parameters, raises an exception

        self.assertRaises(web.HTTPError,
                          get,
                          self.handler)

        # Called with parameters, verifies that parameter is passed through
        # each validator in order.

        self.handler.request.arguments['foo'] = 'Foobar'
        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar',
                                                       self.handler)
        self.bat_validator.validate.assert_called_with('bar',
                                                       self.handler)
        self.baz_validator.validate.assert_called_with('bat',
                                                       self.handler)

        self.handler.get.assert_called_with(self.handler, data={'foo': 'baz'})
Exemplo n.º 2
0
    def test_chained_validators_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([('foo', {
            'validators':
            [self.bar_validator, self.bat_validator, self.baz_validator],
            'required':
            True
        })])(self.handler.get)

        # Called without parameters, raises an exception

        self.assertRaises(web.HTTPError, get, self.handler)

        # Called with parameters, verifies that parameter is passed through
        # each validator in order.

        self.handler.request.arguments['foo'] = 'Foobar'
        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar', self.handler)
        self.bat_validator.validate.assert_called_with('bar', self.handler)
        self.baz_validator.validate.assert_called_with('bat', self.handler)

        self.handler.get.assert_called_with(self.handler, data={'foo': 'baz'})
Exemplo n.º 3
0
    def test_failing_parameter_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([('foo', {
            'validators': [self.fail_validator],
            'required': True
        })])(self.handler.get)

        # Called without parameters, raises an exception
        self.assertRaises(web.HTTPError, get, self.handler)

        # Called with parameters, raises an exception
        self.handler.request.arguments['foo'] = 'Foobar'
        self.assertRaises(web.HTTPError, get, self.handler)
Exemplo n.º 4
0
    def test_multiple_parameter_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([('foo', {
            'validators': self.bar_validator,
            'required': True
        }), ('spam', {
            'validators': self.bat_validator,
            'required': True
        }), ('eggs', {
            'validators': self.baz_validator,
            'required': False
        })])(self.handler.get)

        # Called without parameters, raises an exception
        self.assertRaises(web.HTTPError, get, self.handler)

        # Called with required parameters, returns validated parameters
        self.handler.request.arguments['foo'] = 'Foobar'
        self.handler.request.arguments['spam'] = 'Canned Meat'

        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar', self.handler)
        self.bat_validator.validate.assert_called_with('Canned Meat',
                                                       self.handler)
        self.handler.get.assert_called_with(self.handler,
                                            data={
                                                'eggs': None,
                                                'foo': 'bar',
                                                'spam': 'bat'
                                            })

        # Called with all parameters, returns validated parameters
        self.handler.request.arguments['foo'] = 'Foobar'
        self.handler.request.arguments['spam'] = 'Canned Meat'
        self.handler.request.arguments['eggs'] = 'Over Easy'

        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar', self.handler)
        self.bat_validator.validate.assert_called_with('Canned Meat',
                                                       self.handler)
        self.handler.get.assert_called_with(self.handler,
                                            data={
                                                'foo': 'bar',
                                                'spam': 'bat',
                                                'eggs': 'baz'
                                            })
Exemplo n.º 5
0
    def test_failing_parameter_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([
            ('foo', {'validators': [self.fail_validator], 'required': True})
            ])(self.handler.get)

        # Called without parameters, raises an exception
        self.assertRaises(web.HTTPError,
                          get,
                          self.handler)

        # Called with parameters, raises an exception
        self.handler.request.arguments['foo'] = 'Foobar'
        self.assertRaises(web.HTTPError,
                          get,
                          self.handler)
Exemplo n.º 6
0
    def test_extra_parameters_dict(self):
        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([('foo', {
            'validators': self.bar_validator,
            'required': True
        })])(self.handler.get)

        # Called without parameters, raises an exception
        self.assertRaises(web.HTTPError, get, self.handler)

        # Called with extra parameters, trims extra validated parameter
        self.handler.request.arguments['foo'] = 'Foobar'
        self.handler.request.arguments['extra'] = 'Do Not Want'
        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar', self.handler)
        self.handler.get.assert_called_with(self.handler, data={'foo': 'bar'})
Exemplo n.º 7
0
    def test_optional_parameter_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([
            ('foo', {'validators': self.bar_validator, 'required': False})
            ])(self.handler.get)

        # Called without parameters, raises an exception
        get(self.handler)
        self.handler.get.assert_called_with(self.handler, data={'foo': None})

        # Called with parameters, returns validated parameter
        self.handler.request.arguments['foo'] = 'Foobar'
        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar',
                                                       self.handler)
        self.handler.get.assert_called_with(self.handler, data={'foo': 'bar'})
Exemplo n.º 8
0
    def test_optional_parameter_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([('foo', {
            'validators': self.bar_validator,
            'required': False
        })])(self.handler.get)

        # Called without parameters, raises an exception
        get(self.handler)
        self.handler.get.assert_called_with(self.handler, data={'foo': None})

        # Called with parameters, returns validated parameter
        self.handler.request.arguments['foo'] = 'Foobar'
        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar', self.handler)
        self.handler.get.assert_called_with(self.handler, data={'foo': 'bar'})
Exemplo n.º 9
0
    def test_extra_parameters_dict(self):
        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([
            ('foo', {'validators': self.bar_validator, 'required': True})
            ])(self.handler.get)

        # Called without parameters, raises an exception
        self.assertRaises(web.HTTPError,
                          get,
                          self.handler)

        # Called with extra parameters, trims extra validated parameter
        self.handler.request.arguments['foo'] = 'Foobar'
        self.handler.request.arguments['extra'] = 'Do Not Want'
        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar',
                                                       self.handler)
        self.handler.get.assert_called_with(self.handler, data={'foo': 'bar'})
Exemplo n.º 10
0
    def test_multiple_parameter_dict(self):

        # Apply the decorator we're testing to the mock get method
        get = params.parse_dict([
            ('foo', {'validators': self.bar_validator, 'required': True}),
            ('spam', {'validators': self.bat_validator, 'required': True}),
            ('eggs', {'validators': self.baz_validator, 'required': False})
            ])(self.handler.get)

        # Called without parameters, raises an exception
        self.assertRaises(web.HTTPError,
                          get,
                          self.handler)

        # Called with required parameters, returns validated parameters
        self.handler.request.arguments['foo'] = 'Foobar'
        self.handler.request.arguments['spam'] = 'Canned Meat'

        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar',
                                                       self.handler)
        self.bat_validator.validate.assert_called_with('Canned Meat',
                                                       self.handler)
        self.handler.get.assert_called_with(self.handler,
                                            data={'eggs': None,
                                                  'foo': 'bar',
                                                  'spam': 'bat'})

        # Called with all parameters, returns validated parameters
        self.handler.request.arguments['foo'] = 'Foobar'
        self.handler.request.arguments['spam'] = 'Canned Meat'
        self.handler.request.arguments['eggs'] = 'Over Easy'

        get(self.handler)

        self.bar_validator.validate.assert_called_with('Foobar', self.handler)
        self.bat_validator.validate.assert_called_with('Canned Meat',
                                                       self.handler)
        self.handler.get.assert_called_with(self.handler,
                                            data={'foo': 'bar',
                                                  'spam': 'bat',
                                                  'eggs': 'baz'})