Ejemplo n.º 1
0
    def test_request_handler_get_config(self):
        app = Tipfy()
        with app.get_test_context() as request:
            handler = RequestHandler(request)

            self.assertEqual(handler.get_config('resources.i18n', 'locale'), 'en_US')
            self.assertEqual(handler.get_config('resources.i18n', 'locale', 'foo'), 'en_US')
            self.assertEqual(handler.get_config('resources.i18n'), {
                'locale': 'en_US',
                'timezone': 'America/Chicago',
                'required': REQUIRED_VALUE,
            })
Ejemplo n.º 2
0
    def test_request_handler_get_config(self):
        app = Tipfy()
        with app.get_test_context() as request:
            handler = RequestHandler(request)

            self.assertEqual(handler.get_config('resources.i18n', 'locale'),
                             'en_US')
            self.assertEqual(
                handler.get_config('resources.i18n', 'locale', 'foo'), 'en_US')
            self.assertEqual(
                handler.get_config('resources.i18n'), {
                    'locale': 'en_US',
                    'timezone': 'America/Chicago',
                    'required': REQUIRED_VALUE,
                })
Ejemplo n.º 3
0
    def test_render_template_with_i18n(self):
        app = Tipfy(
            config={
                'tipfyext.jinja2': {
                    'templates_dir':
                    templates_dir,
                    'environment_args':
                    dict(
                        autoescape=True,
                        extensions=[
                            'jinja2.ext.autoescape', 'jinja2.ext.with_',
                            'jinja2.ext.i18n'
                        ],
                    ),
                },
                'tipfy.sessions': {
                    'secret_key': 'secret',
                },
            })
        local.request = Request.from_values()
        local.request.app = app
        handler = RequestHandler(local.request)
        jinja2 = Jinja2(app)

        message = 'Hello, i18n World!'
        res = jinja2.render_template(handler,
                                     'template2.html',
                                     message=message)
        self.assertEqual(res, message)
Ejemplo n.º 4
0
    def test_renew_session(self):
        app = Tipfy()
        request = Request.from_values('/')
        local.current_handler = RequestHandler(app, request)

        user = User.create('my_username', 'my_id')
        user.renew_session(max_age=86400)
Ejemplo n.º 5
0
    def test_engine_factory(self):
        def get_jinja2_env():
            app = handler.app
            cfg = app.get_config('tipfyext.jinja2')

            loader = FileSystemLoader(cfg.get('templates_dir'))

            return Environment(loader=loader)

        app = Tipfy(
            config={
                'tipfyext.jinja2': {
                    'templates_dir': templates_dir,
                    'engine_factory': get_jinja2_env,
                }
            })
        local.request = Request.from_values()
        local.request.app = app
        handler = RequestHandler(local.request)
        jinja2 = Jinja2(app)

        message = 'Hello, World!'
        res = jinja2.render_template(handler,
                                     'template1.html',
                                     message=message)
        self.assertEqual(res, message)
Ejemplo n.º 6
0
    def test_format_date_no_format_but_configured(self):
        app = Tipfy(config={
            'tipfy.sessions': {
                'secret_key': 'secret',
            },
            'tipfy.i18n': {
                'timezone': 'UTC',
                'date_formats': {
                    'time':             'medium',
                    'date':             'medium',
                    'datetime':         'medium',
                    'time.short':       None,
                    'time.medium':      None,
                    'time.full':        None,
                    'time.long':        None,
                    'date.short':       None,
                    'date.medium':      'full',
                    'date.full':        None,
                    'date.long':        None,
                    'datetime.short':   None,
                    'datetime.medium':  None,
                    'datetime.full':    None,
                    'datetime.long':    None,
                }
            }
        })
        local.current_handler = RequestHandler(app, Request.from_values('/'))

        value = datetime.datetime(2009, 11, 10, 16, 36, 05)
        self.assertEqual(i18n.format_date(value), u'Tuesday, November 10, 2009')
Ejemplo n.º 7
0
 def _get_app(self, *args, **kwargs):
     app = Tipfy(config={
         'tipfy.sessions': {
             'secret_key': 'secret',
         },
     })
     local.current_handler = handler = RequestHandler(app, Request.from_values(*args, **kwargs))
     return app
Ejemplo n.º 8
0
    def test_check_password(self):
        app = Tipfy()
        request = Request.from_values('/')
        local.current_handler = RequestHandler(app, request)

        user = User.create('my_username', 'my_id', password='******')

        self.assertEqual(user.check_password('foo'), True)
        self.assertEqual(user.check_password('bar'), False)
Ejemplo n.º 9
0
    def test_user_model(self):
        app = get_app()
        app.router.add(Rule('/', name='home', handler=HomeHandler))

        request = Request.from_values('/')
        local.current_handler = RequestHandler(app, request)

        store = AuthStore(local.current_handler)
        self.assertEqual(store.user_model, User)
Ejemplo n.º 10
0
    def test_render_template(self):
        app = Tipfy(config={'tipfyext.mako': {'templates_dir': templates_dir}})
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        mako = Mako(app)

        message = 'Hello, World!'
        res = mako.render_template(local.current_handler, 'template1.html', message=message)
        self.assertEqual(res, message + '\n')
Ejemplo n.º 11
0
    def test_get_template_attribute(self):
        app = Tipfy(
            config={'tipfyext.jinja2': {
                'templates_dir': templates_dir
            }})
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        jinja2 = Jinja2(app)

        hello = jinja2.get_template_attribute('hello.html', 'hello')
        self.assertEqual(hello('World'), 'Hello, World!')
Ejemplo n.º 12
0
    def setUp(self):
        # Clean up datastore.
        super(TestAcl, self).setUp()

        self.app = Tipfy()
        self.app.config['tipfy']['dev'] = False
        local.current_handler = RequestHandler(self.app, Request.from_values())

        Acl.roles_map = {}
        Acl.roles_lock = CURRENT_VERSION_ID
        _rules_map.clear()
Ejemplo n.º 13
0
    def test_engine_factory3(self):
        app = Tipfy()
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        _globals = {'message': 'Hey there!'}
        filters = {'ho': lambda e: e + ' Ho!'}
        jinja2 = Jinja2(app, _globals=_globals, filters=filters)

        template = jinja2.environment.from_string("""{{ message|ho }}""")

        self.assertEqual(template.render(), 'Hey there! Ho!')
Ejemplo n.º 14
0
    def test_render_response(self):
        app = Tipfy(config={'tipfyext.mako': {'templates_dir': templates_dir}})
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        mako = Mako(app)

        message = 'Hello, World!'
        response = mako.render_response(local.current_handler, 'template1.html', message=message)
        self.assertEqual(isinstance(response, Response), True)
        self.assertEqual(response.mimetype, 'text/html')
        self.assertEqual(response.data, message + '\n')
Ejemplo n.º 15
0
    def test_get_save_session(self):
        local.current_handler = handler = RequestHandler(self._get_app(), Request.from_values())
        store = SessionStore(handler)

        session = store.get_session()
        self.assertEqual(isinstance(session, SecureCookieSession), True)
        self.assertEqual(session, {})

        session['foo'] = 'bar'

        response = Response()
        store.save(response)

        request = Request.from_values('/', headers={'Cookie': '\n'.join(response.headers.getlist('Set-Cookie'))})
        local.current_handler = handler = RequestHandler(self._get_app(), request)
        store = SessionStore(handler)

        session = store.get_session()
        self.assertEqual(isinstance(session, SecureCookieSession), True)
        self.assertEqual(session, {'foo': 'bar'})
Ejemplo n.º 16
0
 def setUp(self):
     app = Tipfy(rules=[
         Rule('/', name='home', handler=RequestHandler)
     ], config={
         'tipfy.sessions': {
             'secret_key': 'secret',
         },
         'tipfy.i18n': {
             'timezone': 'UTC'
         },
     })
     local.current_handler = RequestHandler(app, Request.from_values('/'))
Ejemplo n.º 17
0
 def dispatch(self, *args, **kwargs):
     """Sets up a lot of common context variables as well as session stuff."""
             
     if self.session and self.session.get('key') == None:
         self.session['key'] = key_generator.create_key()
     
     self.context = self.context or {}
     self.context['user']        = auth.get_current_user()
     self.context['signup_url']  = auth.create_signup_url(request.url)
     self.context['login_url']   = auth.create_login_url(request.url)
     self.context['logout_url']  = auth.create_logout_url(request.url)
     
     return RequestHandler.dispatch(self, *args, **kwargs)
     
Ejemplo n.º 18
0
    def test_after_environment_created_using_string(self):
        app = Tipfy(
            config={
                'tipfyext.jinja2': {
                    'after_environment_created':
                    'resources.jinja2_after_environment_created.after_creation'
                }
            })
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        jinja2 = Jinja2(app)

        template = jinja2.environment.from_string("""{{ 'Hey'|ho }}""")
        self.assertEqual(template.render(), 'Hey, Ho!')
Ejemplo n.º 19
0
    def test_set_cookie_encoded(self):
        local.current_handler = handler = RequestHandler(self._get_app(), Request.from_values())
        store = SessionStore(handler)

        store.set_cookie('foo', 'bar', format='json')
        store.set_cookie('baz', 'ding', format='json')

        response = Response()
        store.save(response)

        headers = {'Cookie': '\n'.join(response.headers.getlist('Set-Cookie'))}
        request = Request.from_values('/', headers=headers)

        self.assertEqual(json_b64decode(request.cookies.get('foo')), 'bar')
        self.assertEqual(json_b64decode(request.cookies.get('baz')), 'ding')
Ejemplo n.º 20
0
    def test_render_template(self):
        app = Tipfy(
            config={'tipfyext.jinja2': {
                'templates_dir': templates_dir
            }})
        local.request = Request.from_values()
        local.request.app = app
        handler = RequestHandler(local.request)
        jinja2 = Jinja2(app)

        message = 'Hello, World!'
        res = jinja2.render_template(handler,
                                     'template1.html',
                                     message=message)
        self.assertEqual(res, message)
Ejemplo n.º 21
0
    def test_after_environment_created(self):
        def after_creation(environment):
            environment.filters['ho'] = lambda x: x + ', Ho!'

        app = Tipfy(config={
            'tipfyext.jinja2': {
                'after_environment_created': after_creation
            }
        })
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        jinja2 = Jinja2(app)

        template = jinja2.environment.from_string("""{{ 'Hey'|ho }}""")
        self.assertEqual(template.render(), 'Hey, Ho!')
Ejemplo n.º 22
0
    def test_render_response(self):
        app = Tipfy(
            config={'tipfyext.jinja2': {
                'templates_dir': templates_dir
            }})
        local.request = Request.from_values()
        local.request.app = app
        handler = RequestHandler(local.request)
        jinja2 = Jinja2(app)

        message = 'Hello, World!'
        response = jinja2.render_response(handler,
                                          'template1.html',
                                          message=message)
        self.assertEqual(isinstance(response, Response), True)
        self.assertEqual(response.mimetype, 'text/html')
        self.assertEqual(response.data, message)
Ejemplo n.º 23
0
    def test_render_response_force_compiled(self):
        app = Tipfy(config={
            'tipfyext.jinja2': {
                'templates_compiled_target': templates_compiled_target,
                'force_use_compiled': True,
            }
        },
                    debug=False)
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        jinja2 = Jinja2(app)

        message = 'Hello, World!'
        response = jinja2.render_response(local.current_handler,
                                          'template1.html',
                                          message=message)
        self.assertEqual(isinstance(response, Response), True)
        self.assertEqual(response.mimetype, 'text/html')
        self.assertEqual(response.data, message)
Ejemplo n.º 24
0
    def test_get_cookie_args(self):
        local.current_handler = handler = RequestHandler(self._get_app(), Request.from_values())
        store = SessionStore(handler)

        self.assertEqual(store.get_cookie_args(), {
            'max_age':     None,
            'domain':      None,
            'path':        '/',
            'secure':      None,
            'httponly':    False,
        })

        self.assertEqual(store.get_cookie_args(max_age=86400, domain='.foo.com'), {
            'max_age':     86400,
            'domain':      '.foo.com',
            'path':        '/',
            'secure':      None,
            'httponly':    False,
        })
Ejemplo n.º 25
0
    def test_translations(self):
        app = Tipfy(
            config={
                'tipfyext.jinja2': {
                    'environment_args': {
                        'extensions': [
                            'jinja2.ext.i18n',
                        ],
                    },
                },
                'tipfy.sessions': {
                    'secret_key': 'foo',
                },
            })
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        jinja2 = Jinja2(app)

        template = jinja2.environment.from_string(
            """{{ _('foo = %(bar)s', bar='foo') }}""")
        self.assertEqual(template.render(), 'foo = foo')
Ejemplo n.º 26
0
    def test_engine_factory2(self):
        old_sys_path = sys.path[:]
        sys.path.insert(0, current_dir)

        app = Tipfy(
            config={
                'tipfyext.jinja2': {
                    'templates_dir': templates_dir,
                    'engine_factory': 'resources.get_jinja2_env',
                }
            })
        request = Request.from_values()
        local.current_handler = handler = RequestHandler(app, request)
        jinja2 = Jinja2(app)

        message = 'Hello, World!'
        res = jinja2.render_template(local.current_handler,
                                     'template1.html',
                                     message=message)
        self.assertEqual(res, message)

        sys.path = old_sys_path
Ejemplo n.º 27
0
    def test_set_delete_cookie(self):
        local.current_handler = handler = RequestHandler(self._get_app(), Request.from_values())
        store = SessionStore(handler)

        store.set_cookie('foo', 'bar')
        store.set_cookie('baz', 'ding')

        response = Response()
        store.save(response)

        headers = {'Cookie': '\n'.join(response.headers.getlist('Set-Cookie'))}
        request = Request.from_values('/', headers=headers)

        self.assertEqual(request.cookies.get('foo'), 'bar')
        self.assertEqual(request.cookies.get('baz'), 'ding')

        store.delete_cookie('foo')
        store.save(response)

        headers = {'Cookie': '\n'.join(response.headers.getlist('Set-Cookie'))}
        request = Request.from_values('/', headers=headers)

        self.assertEqual(request.cookies.get('foo', None), '')
        self.assertEqual(request.cookies['baz'], 'ding')
Ejemplo n.º 28
0
    def test_secure_cookie_store(self):
        local.current_handler = handler = RequestHandler(self._get_app(), Request.from_values())
        store = SessionStore(handler)

        self.assertEqual(isinstance(store.secure_cookie_store, SecureCookieStore), True)
Ejemplo n.º 29
0
    def test_secure_cookie_store_no_secret_key(self):
        local.current_handler = handler = RequestHandler(Tipfy(), Request.from_values())
        store = SessionStore(handler)

        self.assertRaises(KeyError, getattr, store, 'secure_cookie_store')
    def setUp(self):
        DataStoreTestCase.setUp(self)
        MemcacheTestCase.setUp(self)

        app = Tipfy()
        local.current_handler = RequestHandler(app, Request.from_values())