Ejemplo n.º 1
0
    def test_dot_error_message_required(self):
        validate = Validator().validate(
            {'user': {
                'id': 1,
                'email': '*****@*****.**',
                'age': 25
            }},
            required(
                ['user.description'],
                messages={'user.description':
                          'You are missing a description'}))

        self.assertEqual(
            validate, {'user.description': ['You are missing a description']})

        validate = Validator().validate(
            {'user': {
                'id': 1,
                'email': '*****@*****.**'
            }},
            required(['user.id', 'user.email', 'user.age'],
                     messages={'user.age': 'You are missing a user age'}))

        self.assertEqual(validate,
                         {'user.age': ['You are missing a user age']})
Ejemplo n.º 2
0
    def test_when(self):
        validate = Validator().validate(
            {
                'email': '*****@*****.**',
                'phone': '123-456-7890'
            },
            when(isnt(required('email'))).then(required('phone')))

        self.assertEqual(len(validate), 0)

        validate = Validator().validate({'email': '*****@*****.**'},
                                        when(exists('email')).then(
                                            required('phone')))

        self.assertEqual(validate['phone'], ['The phone field is required.'])

        validate = Validator().validate({'user': '******'},
                                        when(exists('email')).then(
                                            required('phone')))

        self.assertEqual(len(validate), 0)

        validate = Validator().validate({
            'email': '*****@*****.**',
        },
                                        when(does_not(exists('email'))).then(
                                            required('phone')))

        self.assertEqual(len(validate), 0)
Ejemplo n.º 3
0
    def test_required(self):
        validate = Validator().validate({'test': 1},
                                        required(['user', 'email']))

        self.assertEqual(validate['user'], ['The user field is required.'])
        self.assertEqual(validate['email'], ['The email field is required.'])

        validate = Validator().validate({'test': 1}, required(['test']))

        self.assertEqual(len(validate), 0)
Ejemplo n.º 4
0
    def test_dot_required(self):
        validate = Validator().validate(
            {'user': {
                'email': '*****@*****.**'
            }}, required(['user.id']))

        self.assertEqual(validate,
                         {'user.id': ['The user.id field is required.']})

        validate = Validator().validate({'user': {
            'id': 1
        }}, required(['user.id']))

        self.assertEqual(len(validate), 0)
Ejemplo n.º 5
0
    def test_error_message_required(self):
        validate = Validator().validate(
            {'test': 1},
            required(['user', 'email'],
                     messages={'user': '******'}))

        self.assertEqual(validate['user'], ['there must be a user value'])
        self.assertEqual(validate['email'], ['The email field is required.'])

        validate = Validator().validate(
            {'test': 1},
            required(['user', 'email'],
                     messages={'email': 'there must be an email value'}))

        self.assertEqual(validate['user'], ['The user field is required.'])
        self.assertEqual(validate['email'], ['there must be an email value'])
Ejemplo n.º 6
0
    def test_dot_several_tests(self):
        validate = Validator().validate(
            {'user': {
                'id': 1,
                'email': '*****@*****.**'
            }}, required(['user.id', 'user.email']), numeric(['user.id']))

        self.assertEqual(len(validate), 0)

        validate = Validator().validate(
            {'user': {
                'id': 1,
                'email': '*****@*****.**'
            }}, required(['user.id', 'user.email']), numeric(['user.email']))

        self.assertEqual(validate,
                         {'user.email': ['The user.email must be a numeric.']})
Ejemplo n.º 7
0
    def test_conditional(self):
        validate = Validator().validate({'terms': 'on'},
                                        when(accepted(['terms'])).then(
                                            required(['user'])))

        self.assertEqual(validate, {'user': ['The user field is required.']})

        validate = Validator().validate({'terms': 'test'}, accepted(['terms']))

        self.assertEqual(validate, {'terms': ['The terms must be accepted.']})
Ejemplo n.º 8
0
    def test_exception(self):
        with self.assertRaises(AttributeError) as e:
            validate = Validator().validate({
                'terms': 'on',
            }, required(['user'], raises={'user': AttributeError}))

        try:
            validate = Validator().validate({
                'terms': 'on',
            }, required(['user'], raises={'user': AttributeError}))
        except AttributeError as e:
            self.assertEqual(str(e), 'The user field is required.')

        try:
            validate = Validator().validate({
                'terms': 'on',
            }, required(['user'], raises=True))
        except ValueError as e:
            self.assertEqual(str(e), 'The user field is required.')
Ejemplo n.º 9
0
    def test_several_tests(self):
        validate = Validator().validate({'test': 'hey'}, required(['notin']),
                                        numeric(['notin']))

        self.assertEqual(
            validate, {
                'notin': [
                    'The notin field is required.',
                    'The notin must be a numeric.'
                ]
            })
Ejemplo n.º 10
0
    def test_request_validation_redirects_back_with_session(self):
        wsgi = generate_wsgi()
        self.app.bind('Application', self.app)
        self.app.bind('SessionCookieDriver', SessionCookieDriver)
        self.app.bind('Environ', wsgi)

        request = self.app.make('Request')
        request.load_environ(wsgi)

        request.request_variables = {'id': 1, 'name': 'Joe'}

        errors = request.validate(required('user'))

        request.session = SessionManager(self.app).driver('cookie')
        request.key('UKLAdrye6pZG4psVRPZytukJo2-A_Zxbo0VaqR5oig8=')
        self.assertEqual(
            request.redirect('/login').with_errors(errors).redirect_url,
            '/login')
        self.assertEqual(
            request.redirect('/login').with_errors(errors).session.get(
                'errors'), {'user': ['The user field is required.']})
Ejemplo n.º 11
0
 def rules(self):
     return [required(['username', 'email']), accepted('terms')]
Ejemplo n.º 12
0
    def test_can_use_asterisk(self):
        validate = Validator().validate(
            {
                'user': {
                    'id':
                    1,
                    'addresses': [
                        {
                            'id': 1,
                            'street': 'A Street'
                        },
                        {
                            'id': 2,
                            'street': 'B Street'
                        },
                        {
                            'id': 3,
                            'street': 'C Street'
                        },
                    ],
                    'age':
                    25
                }
            }, required(['user.addresses.*.id']),
            equals('user.addresses.*.id', [1, 2, 3]))

        self.assertEqual(len(validate), 0)

        validate = Validator().validate(
            {
                'user': {
                    'id':
                    1,
                    'addresses': [
                        {
                            'id': 1,
                            'street': 'A Street'
                        },
                        {
                            'id': 2,
                            'street': 'B Street'
                        },
                        {
                            'id': 3,
                            'street': 'C Street'
                        },
                    ],
                    'age':
                    25
                }
            }, required(['user.addresses.*.house']))

        self.assertEqual(
            validate, {
                'user.addresses.*.house':
                ['The user.addresses.*.house field is required.']
            })

        validate = Validator().validate(
            {'user': {
                'id': 1,
                'addresses': [],
                'age': 25
            }}, required(['user.addresses.*.id']))

        self.assertEqual(validate, {
            'user.addresses.*.id':
            ['The user.addresses.*.id field is required.']
        })