Skip to content

Commit

Permalink
authentication cache survives session expiry
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjanss committed Dec 5, 2012
1 parent b2eee12 commit 6f3268d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions camelot/model/authentication.py
Expand Up @@ -45,9 +45,11 @@ def end_of_times():
def get_current_authentication( _obj = None ): def get_current_authentication( _obj = None ):
"""Get the currently logged in :class:'AuthenticationMechanism'""" """Get the currently logged in :class:'AuthenticationMechanism'"""
global _current_authentication_ global _current_authentication_
if not hasattr( _current_authentication_, 'mechanism' ) or not _current_authentication_.mechanism: if not hasattr( _current_authentication_, 'mechanism' ) \
import getpass or not _current_authentication_.mechanism \
_current_authentication_.mechanism = AuthenticationMechanism.get_or_create( unicode( getpass.getuser(), encoding='utf-8', errors='ignore' ) ) or not orm.object_session( _current_authentication_.mechanism ):
import getpass
_current_authentication_.mechanism = AuthenticationMechanism.get_or_create( unicode( getpass.getuser(), encoding='utf-8', errors='ignore' ) )
return _current_authentication_.mechanism return _current_authentication_.mechanism


def clear_current_authentication(): def clear_current_authentication():
Expand Down
12 changes: 12 additions & 0 deletions test/test_model.py
@@ -1,5 +1,7 @@
import os import os


from sqlalchemy import orm

from camelot.test import ModelThreadTestCase from camelot.test import ModelThreadTestCase


class ModelCase( ModelThreadTestCase ): class ModelCase( ModelThreadTestCase ):
Expand All @@ -13,6 +15,16 @@ def setUp(self):
self.app_admin = ApplicationAdmin() self.app_admin = ApplicationAdmin()
self.person_admin = self.app_admin.get_related_admin( Person ) self.person_admin = self.app_admin.get_related_admin( Person )


def test_current_authentication( self ):
from camelot.model.authentication import get_current_authentication
authentication = get_current_authentication()
# current authentication cache should survive
# a session expire + expunge
orm.object_session( authentication ).expire_all()
orm.object_session( authentication ).expunge_all()
authentication = get_current_authentication()
self.assertTrue( authentication.username )

def test_person_contact_mechanism( self ): def test_person_contact_mechanism( self ):
from camelot.model.party import Person from camelot.model.party import Person
person = Person( first_name = u'Robin', person = Person( first_name = u'Robin',
Expand Down

0 comments on commit 6f3268d

Please sign in to comment.