Пример #1
0
def test_int_minmax_optional():
    messages = Int().message
    iv = Int(min=5, max=10, if_empty=None)
    assert iv.to_python("") == None
    assert iv.to_python(None) == None
    assert iv.to_python('7') == 7
    assert validate(iv, "1") == messages('tooLow', None, min=5)
    assert validate(iv, "15") == messages('tooHigh', None, max=10)
Пример #2
0
def add_auth(app, app_conf, prefix='auth.'):
    """
    Add authentication and authorization middleware to the ``app``.

    :param app_conf: The application's local configuration. Normally specified
                     in the [app:<name>] section of the Paste ini file
                     (where <name> defaults to main).

    :param prefix: Prefix for the config related to the auth.
    :type prefix: :class:`str`

    """
    # Cookie form plugin
    form_plugin = make_redirecting_plugin(
                    login_form_path='/login',
                    login_handler_path='/login_handler',
                    logout_handler_path='/logout',
                    rememberer_name='cookie',
            )
    if prefix+'cookie_secret' not in app_conf:
        raise Exception("Missing config option: %s" % prefix+'cookie_secret')

    cookie_secret = app_conf.get(prefix+'cookie_secret')
    cookie_timeout = Int.to_python(app_conf.get(prefix+'cookie_timeout'))
    cookie_reissue_time = Int.to_python(app_conf.get(prefix+'cookie_reissue_time'))

    # Perform type conversion, sice timeout and reisue_time must be int or None
    if cookie_timeout is not None:
        cookie_timeout = int(cookie_timeout)
    if cookie_reissue_time is not None:
        cookie_reissue_time = int(cookie_reissue_time)

    return setup_sql_auth(app, AuthUser, AuthGroup, AuthPermission, Session,
                          form_plugin=form_plugin,
                          cookie_secret=cookie_secret,
                          cookie_timeout=cookie_timeout,
                          cookie_reissue_time=cookie_reissue_time)
Пример #3
0
def test_int_min():
    messages = Int().message
    iv = Int(min=5)
    assert iv.to_python("5") == 5
    assert validate(iv, "1") == messages('tooLow', None, min=5)
Пример #4
0
def test_int_max():
    messages = Int().message
    iv = Int(max=10)
    assert iv.to_python("10") == 10
    assert validate(iv, "15") == messages('tooHigh', None, max=10)
Пример #5
0
def test_arg_validation():
    class TestView(View):
        def init(self):
            self.add_processor('a', int)
            self.add_processor('b', int)
            self.add_processor('c', int)
            self.add_processor('d', Int)
            self.add_processor('f[]', int, pass_as='f')
            self.add_processor('g[]', pass_as='g')
            # this makes sure that pass_as is handled corretly even if the
            # value isn't sent
            self.add_processor('h[]', pass_as='h')
            self.add_processor('z', int)
            try:
                # try a bad processor type
                self.add_processor('e', 5)
                assert False
            except TypeError as e:
                if 'processor must be a Formencode validator or a callable' != str(
                        e):
                    raise  # pragma: no cover

        def default(self, a, c, d, f, g, b=5, z=None):
            eq_(a, 1)
            eq_(c, 2)
            eq_(d, 3)
            # if an argument fails validation it is completely ignored, as if
            # the client did not send it.  Therefore, we can set default
            # values and they will apply if the argument is not sent or if the
            # value that is sent fails validation
            eq_(b, 5)
            # the argument wasn't sent at all
            eq_(z, None)
            # these arguments have a different name in the get string
            eq_(f, 6)
            eq_(g, 'hi')

    TestView({'c': u'2'}).process()

    # try multiple validators for the same item
    class TestView(View):
        def init(self):
            self.add_processor('e', (String, Email))

        def default(self, e):
            eq_(e, '*****@*****.**')

    TestView({}).process()

    class TestView(View):
        def init(self):
            self.add_processor('e', (Number, Email))

        def default(self, e=None):
            eq_(e, None)

    TestView({}).process()

    # the next test makes sure we don't alter FormEncode validator classes
    # Int should accept a None value because its empty and by default Int should
    # allow empty values.  However, b/c of a bug in processor handling, the
    # required value could alter the Int class.
    Int.to_python(None)

    class TestView(View):
        def init(self):
            self.add_processor('a', Int, required=True)

        def default(self, a):
            eq_(a, 1)

    TestView({}).process()
    # As long as this doesn't throw a ValueError b/c None is empty, the Int
    # class was not altered.
    Int.to_python(None)
Пример #6
0
def test_arg_validation():
    class TestView(View):
        def init(self):
            self.add_processor('a', int)
            self.add_processor('b', int)
            self.add_processor('c', int)
            self.add_processor('d', Int)
            self.add_processor('f[]', int, pass_as='f')
            self.add_processor('g[]', pass_as='g')
            # this makes sure that pass_as is handled corretly even if the
            # value isn't sent
            self.add_processor('h[]', pass_as='h')
            self.add_processor('z', int)
            try:
                # try a bad processor type
                self.add_processor('e', 5)
                assert False
            except TypeError as e:
                if 'processor must be a Formencode validator or a callable' != str(e):
                    raise  # pragma: no cover

        def default(self, a, c, d, f, g, b=5, z=None):
            eq_(a, 1)
            eq_(c, 2)
            eq_(d, 3)
            # if an argument fails validation it is completely ignored, as if
            # the client did not send it.  Therefore, we can set default
            # values and they will apply if the argument is not sent or if the
            # value that is sent fails validation
            eq_(b, 5)
            # the argument wasn't sent at all
            eq_(z, None)
            # these arguments have a different name in the get string
            eq_(f, 6)
            eq_(g, 'hi')
    TestView({'c': u'2'}).process()

    # try multiple validators for the same item
    class TestView(View):
        def init(self):
            self.add_processor('e', (String, Email))

        def default(self, e):
            eq_(e, '*****@*****.**')
    TestView({}).process()

    class TestView(View):
        def init(self):
            self.add_processor('e', (Number, Email))

        def default(self, e=None):
            eq_(e, None)
    TestView({}).process()

    # the next test makes sure we don't alter FormEncode validator classes
    # Int should accept a None value because its empty and by default Int should
    # allow empty values.  However, b/c of a bug in processor handling, the
    # required value could alter the Int class.
    Int.to_python(None)

    class TestView(View):
        def init(self):
            self.add_processor('a', Int, required=True)

        def default(self, a):
            eq_(a, 1)
    TestView({}).process()
    # As long as this doesn't throw a ValueError b/c None is empty, the Int
    # class was not altered.
    Int.to_python(None)