コード例 #1
0
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)
コード例 #2
0
    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)
コード例 #3
0
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)
コード例 #4
0
    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))
コード例 #5
0
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)
コード例 #6
0
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)