def delete(self, *args, **kwargs): """ Delete session from both DB and cache (first DB, then cache) """ SessionStore = import_module(settings.SESSION_ENGINE).SessionStore session = SessionStore(session_key=self.session_key) r = super(Session, self).delete(*args, **kwargs) session.delete() return r
def test_load_modified(self): self.store[auth.SESSION_KEY] = 1 self.store.save() store2 = SessionStore(session_key=self.store.session_key, user_agent='TestUA/1.1', ip='8.8.8.8') store2.load() self.assertEqual(store2.user_agent, 'TestUA/1.1') self.assertEqual(store2.ip, '8.8.8.8') self.assertEqual(store2.user_id, 1) self.assertEqual(store2.modified, True)
def test_load_modified(store): store[auth.SESSION_KEY] = 1 store.save() store2 = SessionStore(session_key=store.session_key, user_agent='TestUA/1.1', ip='8.8.8.8') store2.load() assert store2.user_agent == 'TestUA/1.1' assert store2.ip == '8.8.8.8' assert store2.get(auth.SESSION_KEY) == 1 assert store2.modified == True
def test_very_long_ua(self): ua = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; ELT; ' \ 'BTRS29395; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; ' \ 'Embedded Web Browser from: http://bsalsa.com/; .NET CLR 2.0.50727; ' \ '.NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; ' \ '.NET CLR 1.1.4322; ELT; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; FDM; ' \ '.NET4.0C; .NET4.0E; ELT)' store = SessionStore(user_agent=ua, ip='127.0.0.1') store.save() session = Session.objects.get(pk=store.session_key) self.assertEqual(session.user_agent, ua[:300])
def test_delete(): """ Session.delete should delete session from both DB and cache """ store = SessionStore(user_agent='TestUA/1.1', ip='127.0.0.1') store.create() session_key = store.session_key session = Session.objects.get(pk=session_key) session.delete() assert not store.exists(session_key)
def test_get_decoded(): store = SessionStore(user_agent='TestUA/1.1', ip='127.0.0.1') store[auth.SESSION_KEY] = 1 store['foo'] = 'bar' store.save() session = Session.objects.get(pk=store.session_key) assert session.get_decoded() == { 'foo': 'bar', auth.SESSION_KEY: 1, IP_SESSION_KEY: '127.0.0.1', USER_AGENT_SESSION_KEY: 'TestUA/1.1' }
def delete(self): SessionStore = import_module(settings.SESSION_ENGINE).SessionStore sessions = [SessionStore(session_key=obj.session_key) for obj in self] r = super(SessionQuerySet, self).delete() for session in sessions: session.delete() return r
def setUp(self): self.store = SessionStore(user_agent='TestUA/1.1', ip='127.0.0.1')
class SessionStoreTest(TestCase): def setUp(self): self.store = SessionStore(user_agent='TestUA/1.1', ip='127.0.0.1') def test_untouched_init(self): self.assertFalse(self.store.modified) self.assertFalse(self.store.accessed) def test_auth_session_key(self): self.assertFalse(auth.SESSION_KEY in self.store) self.assertFalse(self.store.modified) self.assertTrue(self.store.accessed) self.store.get(auth.SESSION_KEY) self.assertFalse(self.store.modified) self.store[auth.SESSION_KEY] = 1 self.assertTrue(self.store.modified) def test_save(self): self.store[auth.SESSION_KEY] = 1 self.store.save() session = Session.objects.get(pk=self.store.session_key) self.assertEqual(session.user_agent, 'TestUA/1.1') self.assertEqual(session.ip, '127.0.0.1') self.assertEqual(session.user_id, 1) self.assertAlmostEqual(now(), session.updated_at, delta=timedelta(seconds=5)) def test_load_unmodified(self): self.store[auth.SESSION_KEY] = 1 self.store.save() store2 = SessionStore(session_key=self.store.session_key, user_agent='TestUA/1.1', ip='127.0.0.1') store2.load() self.assertEqual(store2.user_agent, 'TestUA/1.1') self.assertEqual(store2.ip, '127.0.0.1') self.assertEqual(store2.user_id, 1) self.assertEqual(store2.modified, False) def test_load_modified(self): self.store[auth.SESSION_KEY] = 1 self.store.save() store2 = SessionStore(session_key=self.store.session_key, user_agent='TestUA/1.1', ip='8.8.8.8') store2.load() self.assertEqual(store2.user_agent, 'TestUA/1.1') self.assertEqual(store2.ip, '8.8.8.8') self.assertEqual(store2.user_id, 1) self.assertEqual(store2.modified, True) def test_duplicate_create(self): s1 = SessionStore(session_key='DUPLICATE', user_agent='TestUA/1.1', ip='127.0.0.1') s1.create() s2 = SessionStore(session_key='DUPLICATE', user_agent='TestUA/1.1', ip='127.0.0.1') s2.create() self.assertNotEqual(s1.session_key, s2.session_key) s3 = SessionStore(session_key=s1.session_key, user_agent='TestUA/1.1', ip='127.0.0.1') with self.assertRaises(CreateError): s3.save(must_create=True) def test_delete(self): # not persisted, should just return self.store.delete() # create, then delete self.store.create() session_key = self.store.session_key self.store.delete() # non-existing sessions, should not raise self.store.delete() self.store.delete(session_key) def test_clear(self): """ Clearing the session should clear all non-browser information """ self.store[auth.SESSION_KEY] = 1 self.store.clear() self.store.save() session = Session.objects.get(pk=self.store.session_key) self.assertEqual(session.user_id, None)
def test_bulk_delete_from_both_cache_and_db(self): s1 = SessionStore(session_key='test1', user_agent='Python/2.7', ip='127.0.0.1') s1.create() s2 = SessionStore(session_key='test2', user_agent='Python/2.7', ip='127.0.0.1') s2.create() s3 = SessionStore(session_key='test3', user_agent='TestUA/1.1', ip='127.0.0.1') s3.create() self.assertIsNotNone( cache.get(SessionStore.cache_key_prefix + s1.session_key)) self.assertIsNotNone( cache.get(SessionStore.cache_key_prefix + s2.session_key)) self.assertIsNotNone( cache.get(SessionStore.cache_key_prefix + s3.session_key)) Session.objects.filter(user_agent='Python/2.7').delete() self.assertIsNone( cache.get(SessionStore.cache_key_prefix + s1.session_key)) self.assertIsNone( cache.get(SessionStore.cache_key_prefix + s2.session_key)) self.assertIsNotNone( cache.get(SessionStore.cache_key_prefix + s3.session_key))
def test_duplicate_create(self): s1 = SessionStore(session_key='DUPLICATE', user_agent='TestUA/1.1', ip='127.0.0.1') s1.create() s2 = SessionStore(session_key='DUPLICATE', user_agent='TestUA/1.1', ip='127.0.0.1') s2.create() self.assertNotEqual(s1.session_key, s2.session_key) s3 = SessionStore(session_key=s1.session_key, user_agent='TestUA/1.1', ip='127.0.0.1') with self.assertRaises(CreateError): s3.save(must_create=True)
def test_bulk_delete_from_both_cache_and_db(): s1 = SessionStore(user_agent='Python/2.7', ip='127.0.0.1') s1.create() s2 = SessionStore(user_agent='Python/2.7', ip='127.0.0.1') s2.create() s3 = SessionStore(user_agent='TestUA/1.1', ip='127.0.0.1') s3.create() assert s1.exists(s1.session_key) assert s2.exists(s2.session_key) assert s3.exists(s3.session_key) Session.objects.filter(user_agent='Python/2.7').delete() assert not s1.exists(s1.session_key) assert not s2.exists(s2.session_key) assert s3.exists(s3.session_key)
def test_duplicate_create(): s1 = SessionStore(session_key='DUPLICATE', user_agent='TestUA/1.1', ip='127.0.0.1') s1.create() s2 = SessionStore(session_key='DUPLICATE', user_agent='TestUA/1.1', ip='127.0.0.1') s2.create() assert s1.session_key != s2.session_key s3 = SessionStore(session_key=s1.session_key, user_agent='TestUA/1.1', ip='127.0.0.1') with pytest.raises(CreateError): s3.save(must_create=True)
def setup_store(): return SessionStore(user_agent='TestUA/1.1', ip='127.0.0.1')