# HG changeset patch # User Filip de Waard # Date 1271984678 -7200 # Node ID 99c5e32ea2ed263ef8d91fd9ff49393ba14cbd3f # Parent 9942b083eefdd85a689d5e7da18c43bd0e9df73d implemented model.Session diff -r 9942b083eefdd85a689d5e7da18c43bd0e9df73d -r 99c5e32ea2ed263ef8d91fd9ff49393ba14cbd3f vix/model/__init__.py --- a/vix/model/__init__.py Mon Apr 19 22:59:47 2010 +0200 +++ b/vix/model/__init__.py Fri Apr 23 03:04:38 2010 +0200 @@ -25,7 +25,7 @@ import hashlib from datetime import datetime -from time import time +from time import time, mktime from couchdb import schema @@ -208,8 +208,6 @@ :type username: TextField :param created: date and time when the Session was created. :type created: DateTimeField - :param updated: date and time when the Session was last updated (if ever). - :type updated: DateTimeField :param expires: date and time the Session will expire. :type expires: DateTimeField @@ -221,26 +219,23 @@ username = schema.TextField() created = schema.DateTimeField(default=datetime.utcnow) - updated = schema.DateTimeField() expires = schema.DateTimeField() - def __init__(self, token=None, **values): + def __init__(self, token=None, duration=60, **values): """ Creates the Session. If no token is supplied one is generated - using the current time, a semi-random number and a memory addres - as the input of a MD5 digest. + using the current time and a semi-random number as the input of + the MD5 digest. """ - from pylons import config - if token is None: - token = hashlib.md5("%f%i%i" % ( - time(), random.randint(1, sys.maxint), id([]))).hexdigest() + token = hashlib.md5("%f%i" % ( + time(), random.randint(1, sys.maxint))).hexdigest() super(Session, self).__init__(token, **values) + + self.created = datetime.utcnow() - self.updated = datetime.utcnow() - - print config['couchdb_uri'] - print config['auth_session_duration'] + expires_stamp = mktime(self.created.timetuple()) + duration + self.expires = datetime.fromtimestamp(expires_stamp) diff -r 9942b083eefdd85a689d5e7da18c43bd0e9df73d -r 99c5e32ea2ed263ef8d91fd9ff49393ba14cbd3f vix/tests/__init__.py --- a/vix/tests/__init__.py Mon Apr 19 22:59:47 2010 +0200 +++ b/vix/tests/__init__.py Fri Apr 23 03:04:38 2010 +0200 @@ -35,12 +35,12 @@ def __init__(self, *args, **kwargs): wsgiapp = pylons.test.pylonsapp - config = pylons.config = wsgiapp.config + config = wsgiapp.config self.app = TestApp(wsgiapp) url._push_object(URLGenerator(config['routes.map'], environ)) TestCase.__init__(self, *args, **kwargs) -class DatabasePoweredTestCase(TestController): +class DatabasePoweredTestCase(TestCase): def setUp(self): """Create new test database for every test.""" diff -r 9942b083eefdd85a689d5e7da18c43bd0e9df73d -r 99c5e32ea2ed263ef8d91fd9ff49393ba14cbd3f vix/tests/test_models.py --- a/vix/tests/test_models.py Mon Apr 19 22:59:47 2010 +0200 +++ b/vix/tests/test_models.py Fri Apr 23 03:04:38 2010 +0200 @@ -239,6 +239,9 @@ user = user.load(model.db, u'fmw') self.assertEquals(user.get_permissions('vix_tests', 'news'), None) + + #TODO: use mocks in other tests here + #TODO: clean up docstrings of model.User def test_Session(self): """Test session model object.""" @@ -249,20 +252,28 @@ def _time(): return 1271706172.864481 + class _datetime(object): + def utcnow(self): + return datetime(2010, 4, 19, 11, 12, 5) + + def fromtimestamp(self, stamp): + return datetime.fromtimestamp(stamp) + with Replacer() as r: - r.replace('vix.model.datetime', - test_datetime(2010, 4, 19, 11, 12, 5)) - r.replace('vix.model.datetime.utcnow', - vix.model.datetime.now) + r.replace('vix.model.datetime', _datetime()) r.replace('vix.model.time', _time) r.replace('vix.model.random.randint', _random) - + session = model.Session() - - self.assertEquals(session.id, hashlib.md5("%f%i%i" % ( - _time(), _random(0,0), id([]))).hexdigest()) - self.assertEquals(session.updated, + + self.assertEquals(session.id, hashlib.md5("%f%i" % ( + _time(), _random(0,0))).hexdigest()) + self.assertEquals(session.created, datetime(2010, 4, 19, 11, 12, 5)) - #TODO: implement this + + self.assertEquals(session.expires, + datetime(2010, 4, 19, 11, 13, 5)) + + session = model.Session(duration=300) self.assertEquals(session.expires, datetime(2010, 4, 19, 11, 17, 5))