Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_session_in_db(self):
request.app_name = "myapp"
db = DAL("sqlite:memory")
session = Session(secret="a", expiration=10, storage=DBStore(db))
request.cookies.clear()
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
session = Session(expiration=10, storage=DBStore(db))
session.on_request()
self.assertEqual(session.get("key"), "value")
def test_session(self):
request.app_name = "myapp"
session = Session(secret="a", expiration=10)
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
session = Session(secret="b", expiration=10)
session.on_request()
self.assertEqual(session.get("key"), None)
session = Session(secret="a", expiration=10)
import time
import unittest
import uuid
import mechanize
from py4web import action, DAL, Field, Session, Cache
from py4web.core import bottle, request, error404
os.environ["PY4WEB_APPS_FOLDER"] = os.path.sep.join(
os.path.normpath(__file__).split(os.path.sep)[:-2]
)
db = DAL("sqlite://storage_%s" % uuid.uuid4(), folder="/tmp/")
db.define_table("thing", Field("name"))
session = Session(secret="my secret")
cache = Cache()
action.app_name = "tests"
@action("index")
@cache.memoize(expiration=1)
@action.uses(db, session)
@action.requires(lambda: True)
def index():
db.thing.insert(name="test")
session["number"] = session.get("number", 0) + 1
return "ok %s %s" % (session["number"], db(db.thing).count())
def run_server():
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = (
str(response._cookies)[len("Set-Cookie: ") :]
.split(";")[0]
.split("=", 1)
)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
conn = memcache.Client(["127.0.0.1:11211"], debug=0)
session = Session(expiration=10, storage=conn)
session.on_request()
self.assertEqual(session.get("key"), "value")
request.cookies[a] = "wrong_cookie"
conn = memcache.Client(["127.0.0.1:11211"], debug=0)
session = Session(expiration=10, storage=conn)
session.on_request()
self.assertEqual(session.get("key"), None)
finally:
if memcache_process:
memcache_process.kill()
def test_session_in_db(self):
request.app_name = "myapp"
db = DAL("sqlite:memory")
session = Session(secret="a", expiration=10, storage=DBStore(db))
request.cookies.clear()
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
session = Session(expiration=10, storage=DBStore(db))
session.on_request()
self.assertEqual(session.get("key"), "value")
request.cookies[a] = "wrong_cookie"
def test_session_in_memcache(self):
try:
memcache_process = subprocess.Popen(["memcached", "-p", "11211"])
time.sleep(1)
request.app_name = "myapp"
conn = memcache.Client(["127.0.0.1:11211"], debug=0)
session = Session(secret="a", expiration=10, storage=conn)
request.cookies.clear()
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = (
str(response._cookies)[len("Set-Cookie: ") :]
.split(";")[0]
.split("=", 1)
)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
conn = memcache.Client(["127.0.0.1:11211"], debug=0)
def test_session(self):
request.app_name = "myapp"
session = Session(secret="a", expiration=10)
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
session = Session(secret="b", expiration=10)
session.on_request()
self.assertEqual(session.get("key"), None)
session = Session(secret="a", expiration=10)
session.on_request()
session = Session(secret="a", expiration=10)
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
session = Session(secret="b", expiration=10)
session.on_request()
self.assertEqual(session.get("key"), None)
session = Session(secret="a", expiration=10)
session.on_request()
self.assertEqual(session.get("key"), "value")
def test_session_in_db(self):
request.app_name = "myapp"
db = DAL("sqlite:memory")
session = Session(secret="a", expiration=10, storage=DBStore(db))
request.cookies.clear()
session.on_request()
session["key"] = "value"
session.on_success()
cookie_name = session.local.session_cookie_name
a, b = str(response._cookies)[len("Set-Cookie: ") :].split(";")[0].split("=", 1)
request.cookies[a] = b
request.cookies = response._cookies
session.local.data.clear()
session = Session(expiration=10, storage=DBStore(db))
session.on_request()
self.assertEqual(session.get("key"), "value")
request.cookies[a] = "wrong_cookie"
session = Session(expiration=10, storage=DBStore(db))
session.on_request()
self.assertEqual(session.get("key"), None)
import unittest
import uuid
import mechanize
from py4web import action, DAL, Field, Session, Cache
from py4web.core import bottle, request, error404
os.environ["PY4WEB_APPS_FOLDER"] = os.path.sep.join(
os.path.normpath(__file__).split(os.path.sep)[:-2]
)
db = DAL("sqlite://storage_%s" % uuid.uuid4(), folder="/tmp/")
db.define_table("thing", Field("name"))
session = Session(secret="my secret")
cache = Cache()
action.app_name = "tests"
@action("index")
@cache.memoize(expiration=1)
@action.uses(db, session)
@action.requires(lambda: True)
def index():
db.thing.insert(name="test")
session["number"] = session.get("number", 0) + 1
return "ok %s %s" % (session["number"], db(db.thing).count())
def run_server():
bottle.run(host="localhost", port=8001)