def test_flashes(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank('/') req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, []) session.add_flash('foo') rsp = webapp2.Response() store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, [[u'foo', None]]) flashes = session.get_flashes() self.assertEqual(flashes, []) session.add_flash('bar') session.add_flash('baz', 'important') rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, [[u'bar', None], [u'baz', 'important']]) flashes = session.get_flashes() self.assertEqual(flashes, [])
def test_get_save_session(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) rsp = webapp2.Response() # Nothing changed, we want to test anyway. store.save_sessions(rsp) session["a"] = "b" session["c"] = "d" session["e"] = "f" store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) self.assertEqual(session["a"], "b") self.assertEqual(session["c"], "d") self.assertEqual(session["e"], "f") session["g"] = "h" rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) self.assertEqual(session["a"], "b") self.assertEqual(session["c"], "d") self.assertEqual(session["e"], "f") self.assertEqual(session["g"], "h") self.assertRaises(KeyError, session.pop, "foo")
def test_get_save_session(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank('/') req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) rsp = webapp2.Response() # Nothing changed, we want to test anyway. store.save_sessions(rsp) session['a'] = 'b' session['c'] = 'd' session['e'] = 'f' store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') session['g'] = 'h' rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') self.assertEqual(session['g'], 'h') self.assertRaises(KeyError, session.pop, 'foo')
def test_get_save_session(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank('/') req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) session['a'] = 'b' session['c'] = 'd' session['e'] = 'f' rsp = webapp2.Response() store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') session['g'] = 'h' rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') self.assertEqual(session['g'], 'h')
def test_not_implemented(self): req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req) f = sessions.BaseSessionFactory("foo", store) self.assertRaises(NotImplementedError, f.get_session) self.assertRaises(NotImplementedError, f.save_session, None) f = sessions.CustomBackendSessionFactory("foo", store) self.assertRaises(NotImplementedError, f._get_by_sid, None)
def test_set_secure_cookie(self): rsp = webapp2.Response() # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req) store.set_secure_cookie("foo", {"bar": "baz"}) store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) res = store.get_secure_cookie("foo") self.assertEqual(res, {"bar": "baz"})
def test_set_secure_cookie(self): rsp = webapp2.Response() # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank('/') req.app = app store = sessions.SessionStore(req) store.set_secure_cookie('foo', {'bar': 'baz'}) store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) res = store.get_secure_cookie('foo') self.assertEqual(res, {'bar': 'baz'})
def __init__(self, handler, session=None, secret=None, cookie_name='webapp2authomatic', backend='memcache', config=None): """ .. warning:: Do not use the ``'securecookie'`` backend with :class:`.providers.OpenID` provider. The `python-openid`_ library saves **non json serializable** objects to session which the ``'securecookie'`` backend cannot cope with. :param handler: A :class:`webapp2.RequestHandler` instance. :param session: A :class:`webapp2_extras.session.SessionDict` instance. :param str secret: The session secret. :param str cookie_name: The name of the session cookie. :param backend: The session backend. One of ``'memcache'`` or ``'datastore'``. :param config: The session config. """ self.handler = handler if session is None: if not secret: raise GAEError('Either session or secret must be specified!') else: # Create new session. cfg = config or dict(secret_key=secret, cookie_name=cookie_name) session_store = sessions.SessionStore(handler.request, cfg) self.session_dict = session_store.get_session(backend=backend) else: # Use supplied session. self.session_dict = session
def test_set_session_store(self): app = webapp2.WSGIApplication(config={ "webapp2_extras.sessions": { "secret_key": "my-super-secret", } }) req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req) self.assertEqual(len(req.registry), 0) sessions.set_store(store, request=req) self.assertEqual(len(req.registry), 1) s = sessions.get_store(request=req) self.assertTrue(isinstance(s, sessions.SessionStore))
def test_config(self): app = webapp2.WSGIApplication() req = webapp2.Request.blank('/') req.app = app self.assertRaises(Exception, sessions.SessionStore, req) # Just to set a special config. app = webapp2.WSGIApplication() req = webapp2.Request.blank('/') req.app = app store = sessions.SessionStore(req, config={ 'secret_key': 'my-super-secret', 'cookie_name': 'foo' }) session = store.get_session(factory=self.factory) session['bar'] = 'bar' rsp = webapp2.Response() store.save_sessions(rsp) self.assertTrue(rsp.headers['Set-Cookie'].startswith('foo='))
def test_config(self): app = webapp2.WSGIApplication() req = webapp2.Request.blank("/") req.app = app self.assertRaises(Exception, sessions.SessionStore, req) # Just to set a special config. app = webapp2.WSGIApplication() req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req, config={ "secret_key": "my-super-secret", "cookie_name": "foo" }) session = store.get_session(factory=self.factory) session["bar"] = "bar" rsp = webapp2.Response() store.save_sessions(rsp) self.assertTrue(rsp.headers["Set-Cookie"].startswith("foo="))
def test_flashes(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, []) session.add_flash("foo") rsp = webapp2.Response() store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, [["foo", None]]) flashes = session.get_flashes() self.assertEqual(flashes, []) session.add_flash("bar") session.add_flash("baz", "important") rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, [["bar", None], ["baz", "important"]]) flashes = session.get_flashes() self.assertEqual(flashes, []) rsp = webapp2.Response() store.save_sessions(rsp) # Round 4 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(factory=self.factory) flashes = session.get_flashes() self.assertEqual(flashes, [])
def test_get_save_session(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank('/') req.app = app store = sessions.SessionStore(req) session = store.get_session(backend='datastore') rsp = webapp2.Response() # Nothing changed, we want to test anyway. store.save_sessions(rsp) session['a'] = 'b' session['c'] = 'd' session['e'] = 'f' store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(backend='datastore') self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') session['g'] = 'h' rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(backend='datastore') self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') self.assertEqual(session['g'], 'h') # Round 4 ------------------------------------------------------------- # For this attempt we don't want the memcache backup. sid = session.container.sid memcache.delete(sid) cookies = rsp.headers.get('Set-Cookie') req = webapp2.Request.blank('/', headers=[('Cookie', cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(backend='datastore') self.assertEqual(session['a'], 'b') self.assertEqual(session['c'], 'd') self.assertEqual(session['e'], 'f') self.assertEqual(session['g'], 'h')
def test_get_save_session(self): # Round 1 ------------------------------------------------------------- req = webapp2.Request.blank("/") req.app = app store = sessions.SessionStore(req) session = store.get_session(backend="datastore") rsp = webapp2.Response() # Nothing changed, we want to test anyway. store.save_sessions(rsp) session["a"] = "b" session["c"] = "d" session["e"] = "f" store.save_sessions(rsp) # Round 2 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(backend="datastore") self.assertEqual(session["a"], "b") self.assertEqual(session["c"], "d") self.assertEqual(session["e"], "f") session["g"] = "h" rsp = webapp2.Response() store.save_sessions(rsp) # Round 3 ------------------------------------------------------------- cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(backend="datastore") self.assertEqual(session["a"], "b") self.assertEqual(session["c"], "d") self.assertEqual(session["e"], "f") self.assertEqual(session["g"], "h") # Round 4 ------------------------------------------------------------- # For this attempt we don't want the memcache backup. sid = session.container.sid memcache.delete(sid) cookies = rsp.headers.get("Set-Cookie") req = webapp2.Request.blank("/", headers=[("Cookie", cookies)]) req.app = app store = sessions.SessionStore(req) session = store.get_session(backend="datastore") self.assertEqual(session["a"], "b") self.assertEqual(session["c"], "d") self.assertEqual(session["e"], "f") self.assertEqual(session["g"], "h")