-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing MetaLocal and support for session.key = value
- Loading branch information
Showing
8 changed files
with
271 additions
and
228 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import io | ||
import os | ||
import unittest | ||
import uuid | ||
|
||
from py4web.core import (DAL, HTTP, Field, Session, _before_request, bottle, | ||
request) | ||
from py4web.core import DAL, HTTP, Field, MetaLocal, Session, bottle, request, safely | ||
from py4web.utils.auth import Auth, AuthAPI | ||
|
||
SECRET = str(uuid.uuid4()) | ||
|
@@ -12,10 +12,8 @@ | |
class TestAuth(unittest.TestCase): | ||
def setUp(self): | ||
os.environ["PY4WEB_APPS_FOLDER"] = "apps" | ||
_before_request() # mimic before_request bottle-hook | ||
self.db = DAL("sqlite:memory") | ||
self.session = Session(secret=SECRET, expiration=10) | ||
self.session.initialize() | ||
self.auth = Auth( | ||
self.session, self.db, define_tables=True, password_complexity=None | ||
) | ||
|
@@ -24,30 +22,36 @@ def setUp(self): | |
request.app_name = "_scaffold" | ||
|
||
def tearDown(self): | ||
# this is normally done by @action | ||
safely(lambda: MetaLocal.local_delete(self.session)) | ||
bottle.app.router.remove("/*") | ||
|
||
def action(self, name, method, query, data): | ||
request.environ["REQUEST_METHOD"] = method | ||
request.environ["ombott.request.query"] = query | ||
request.environ["ombott.request.json"] = data | ||
request.environ["wsgi.input"] = io.BytesIO() | ||
# we break a symmetry below. should fix in auth.py | ||
if name.startswith("api/"): | ||
return getattr(AuthAPI, name[4:])(self.auth) | ||
else: | ||
return getattr(self.auth.form_source, name)() | ||
|
||
def on_request(self, context={}, keep_session=False): | ||
storage = self.session._safe_local | ||
|
||
# mimic before_request bottle-hook | ||
_before_request() | ||
|
||
# mimic action.uses() | ||
self.session.initialize() | ||
# store the current session | ||
try: | ||
storage = self.session.local.__dict__ | ||
except RuntimeError: | ||
storage = None | ||
# reinitialize everything | ||
safely(lambda: MetaLocal.local_delete(self.session)) | ||
safely(lambda: MetaLocal.local_delete(self.auth.flash)) | ||
self.session.on_request(context) | ||
self.auth.flash.on_request(context) | ||
self.auth.on_request(context) | ||
if keep_session: | ||
self.session._safe_local = storage | ||
# restore the previous session | ||
if keep_session and storage: | ||
self.session.local.__dict__.update(storage) | ||
|
||
def test_extra_fields(self): | ||
db = DAL("sqlite:memory") | ||
|
@@ -101,7 +105,6 @@ def test_register(self): | |
{"status": "error", "message": "Invalid Credentials", "code": 400}, | ||
) | ||
|
||
self.on_request() | ||
self.on_request() | ||
body = {"email": "[email protected]", "password": "123456789"} | ||
self.assertEqual( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.