def test_real_logout(self): user = User.create('foo', 'foo_id', auth_remember=True) app = self.get_app() request = Request.from_values('/') local.current_handler = RequestHandler(app, request) store = MultiAuthStore(local.current_handler) store.login_with_auth_id('foo_id', remember=False) response = Response() local.current_handler.session_store.save(response) request = Request.from_values('/', headers={ 'Cookie': '\n'.join(response.headers.getlist('Set-Cookie')), }) local.current_handler = RequestHandler(app, request) store = MultiAuthStore(local.current_handler) self.assertNotEqual(store.user, None) self.assertEqual(store.user.username, 'foo') store.logout() response = Response() local.current_handler.session_store.save(response) request = Request.from_values('/', headers={ 'Cookie': '\n'.join(response.headers.getlist('Set-Cookie')), }) local.current_handler = RequestHandler(app, request) store = MultiAuthStore(local.current_handler) #self.assertEqual(store.user, None) self.assertEqual(store.session, None)
def test_redirect_relative_uris(self): app = Tipfy() class Handler(RequestHandler): pass request = Request.from_values('/foo/bar/') handler = Handler(app, request) response = handler.redirect('/baz') self.assertEqual(response.headers['Location'], 'http://localhost/baz') response = handler.redirect('./baz') self.assertEqual(response.headers['Location'], 'http://localhost/foo/bar/baz') response = handler.redirect('../baz') self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz') request = Request.from_values('/foo/bar') handler = Handler(app, request) response = handler.redirect('/baz') self.assertEqual(response.headers['Location'], 'http://localhost/baz') response = handler.redirect('./baz') self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz') response = handler.redirect('../baz') self.assertEqual(response.headers['Location'], 'http://localhost/baz')
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')
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')
def test_render_json_response(self): local.current_handler = HomeHandler(Tipfy(), Request.from_values()) response = render_json_response({'foo': 'bar'}) self.assertEqual(isinstance(response, Response), True) self.assertEqual(response.mimetype, 'application/json') self.assertEqual(response.data, '{"foo": "bar"}')
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)
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)
def test_make_response(self): app = Tipfy() request = Request.from_values() # Empty. response = app.make_response(request) self.assertEqual(isinstance(response, app.response_class), True) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, '') # From Response. response = app.make_response(request, Response('Hello, World!')) self.assertEqual(isinstance(response, app.response_class), True) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'Hello, World!') # From string. response = app.make_response(request, 'Hello, World!') self.assertEqual(isinstance(response, app.response_class), True) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, 'Hello, World!') # From tuple. response = app.make_response(request, 'Hello, World!', 404) self.assertEqual(isinstance(response, app.response_class), True) self.assertEqual(response.status_code, 404) self.assertEqual(response.data, 'Hello, World!') # From None. self.assertRaises(ValueError, app.make_response, request, None)
def test_render_json_response(self): local.current_handler = HomeHandler(Tipfy(), Request.from_values()) response = render_json_response({'foo': 'bar'}) self.assertEqual(isinstance(response, Response), True) self.assertEqual(response.mimetype, 'application/json') self.assertEqual(response.data, '{"foo":"bar"}')
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)
def test_get_cookie_invalid_signature(self): store = SecureCookieStore('secret') request = Request.from_values('/', headers=[ ('Cookie', 'session="foo|bar|baz"; Path=/') ]) self.assertEqual(store.get_cookie(request, 'session'), None)
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!')
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!')
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
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)
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)
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)
def test_get_cookie_badly_encoded(self): store = SecureCookieStore('secret') timestamp = str(int(time.time())) value = 'foo' signature = store._get_signature('session', value, timestamp) cookie_value = '|'.join([value, timestamp, signature]) request = Request.from_values('/', headers=[('Cookie', 'session="%s"; Path=/' % cookie_value)]) self.assertEqual(store.get_cookie(request, 'session'), None)
def test_login_with_form_invalid(self): app = self.get_app() request = Request.from_values('/') local.current_handler = RequestHandler(app, request) store = MultiAuthStore(local.current_handler) res = store.login_with_form('foo', 'bar', remember=True) self.assertEqual(res, False)
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')
def test_get_cookie_valid(self): store = SecureCookieStore('secret') request = Request.from_values( '/', headers= [('Cookie', 'session="eyJmb28iOiJiYXIifQ==|1284849476|847b472f2fabbf1efef55748a394b6f182acd8be"; Path=/' )]) self.assertEqual(store.get_cookie(request, 'session'), {'foo': 'bar'})
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)
def test_signup_url(self): app = get_app() app.router.add(Rule('/', name='home', handler=HomeHandler)) request = Request.from_values('/') local.current_handler = RequestHandler(app, request) app.router.match(request) store = AuthStore(local.current_handler) self.assertEqual(store.signup_url(), local.current_handler.url_for('auth/signup', redirect='/'))
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()
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!')
def test_login_with_auth_id(self): app = self.get_app() request = Request.from_values('/') local.current_handler = RequestHandler(app, request) store = MultiAuthStore(local.current_handler) store.login_with_auth_id('foo_id', remember=False) user = User.create('foo', 'foo_id', password='******') app = self.get_app() store.login_with_auth_id('foo_id', remember=True)
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!')
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'})
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')
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!')
def test_dispatch(self): class HomeHandler(RequestHandler): def get(self, **kwargs): return Response('Home sweet home!') app = Tipfy(rules=[ Rule('/', endpoint='home', handler=HomeHandler), ], debug=True) handler = HomeHandler(app, Request.from_values('/')) response = handler.dispatch('get') self.assertEqual(response.data, 'Home sweet home!')
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('/'))
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)
def test_login_with_form(self): user = User.create('foo', 'foo_id', password='******') app = self.get_app() request = Request.from_values('/') local.current_handler = RequestHandler(app, request) store = MultiAuthStore(local.current_handler) res = store.login_with_form('foo', 'bar', remember=True) self.assertEqual(res, True) res = store.login_with_form('foo', 'bar', remember=False) self.assertEqual(res, True)
def test_jinja2_mixin_render_template(self): class MyHandler(RequestHandler, Jinja2Mixin): pass app = Tipfy(config={'tipfyext.jinja2': {'templates_dir': templates_dir}}) local.request = Request.from_values() local.request.app = app handler = MyHandler(local.request) jinja2 = Jinja2(app) message = 'Hello, World!' response = handler.render_template('template1.html', message=message) self.assertEqual(response, message)
def setUp(self): # Clean up datastore. super(TestAcl, self).setUp() self.app = Tipfy() self.app.config['tipfy']['dev'] = False local.request = Request.from_values() local.request.app = self.app Acl.roles_map = {} Acl.roles_lock = CURRENT_VERSION_ID _rules_map.clear() test_utils.BaseTestCase.setUp(self)
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!')
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')
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)
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!')
def test_mako_mixin_render_template(self): class MyHandler(RequestHandler, MakoMixin): def __init__(self, app, request): self.app = app self.request = request self.context = {} app = Tipfy(config={'tipfyext.mako': {'templates_dir': templates_dir}}) request = Request.from_values() local.current_handler = handler = MyHandler(app, request) mako = Mako(app) message = 'Hello, World!' response = handler.render_template('template1.html', message=message) self.assertEqual(response, message + '\n')
def test_jinja2_mixin_render_template(self): class MyHandler(RequestHandler, Jinja2Mixin): pass app = Tipfy( config={'tipfyext.jinja2': { 'templates_dir': templates_dir }}) local.request = Request.from_values() local.request.app = app handler = MyHandler(local.request) jinja2 = Jinja2(app) message = 'Hello, World!' response = handler.render_template('template1.html', message=message) self.assertEqual(response, message)
def test_mako_mixin_render_response(self): class MyHandler(RequestHandler, MakoMixin): def __init__(self, app, request): self.app = app self.request = request self.context = {} app = Tipfy(config={'tipfyext.mako': {'templates_dir': templates_dir}}) request = Request.from_values() handler = MyHandler(app, request) mako = Mako(app) message = 'Hello, World!' response = handler.render_response('template1.html', message=message) self.assertEqual(isinstance(response, Response), True) self.assertEqual(response.mimetype, 'text/html') self.assertEqual(response.data, message + '\n')
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)
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, })