1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
# -*- coding: utf-8 -*-
# test/test_session.py
# Part of Gracie, an OpenID provider.
#
# Copyright © 2007–2010 Ben Finney <ben+python@benfinney.id.au>
# This is free software; you may copy, modify and/or distribute this work
# under the terms of the GNU General Public License, version 2 or later.
# No warranty expressed or implied. See the file ‘LICENSE.GPL-2’ for details.
""" Unit test for session module.
"""
import sys
import scaffold
from gracie import session
class SessionManager_TestCase(scaffold.TestCase):
""" Test cases for SessionManager class. """
def setUp(self):
""" Set up test fixtures """
self.manager_class = session.SessionManager
def test_create_session_should_return_session_id(self):
""" Creating a session should return session ID """
instance = self.manager_class()
session_id = instance.create_session()
self.failIfIs(None, session_id)
def test_get_session_unknown_id_raises_keyerror(self):
""" Getting an unknown session ID should raise KeyError """
instance = self.manager_class()
session_id = "DECAFBAD"
self.failUnlessRaises(
KeyError,
instance.get_session, session_id
)
def test_get_session_returns_same_session(self):
""" Getting a session by ID should return same username """
instance = self.manager_class()
session = dict(
username = "fred",
foo = "spam",
)
session_id = instance.create_session(session)
session['session_id'] = session_id
got_session = instance.get_session(session_id)
self.failUnlessEqual(session, got_session)
def test_create_session_should_create_unique_id(self):
""" Creating a session should create unique ID each time """
instance = self.manager_class()
usernames = ["larry", "curly", "moe"]
sessions = dict()
for username in usernames:
session_id = instance.create_session()
self.failIfIn(sessions, session_id)
sessions[session_id] = dict(
session_id = session_id,
username = username,
)
def test_create_multiple_session_for_same_username(self):
""" Creating multiple sessions for same username should succeed """
instance = self.manager_class()
usernames = ["larry", "curly", "moe"]
sessions = dict()
for username in usernames:
for _ in range(10):
session = dict(username=username)
session_id = instance.create_session(session)
session.update(dict(
session_id = session_id
))
sessions[session_id] = session
for session_id, session in sessions.items():
got_session = instance.get_session(session_id)
self.failUnlessEqual(session, got_session)
def test_remove_session_unknown_should_raise_keyerror(self):
""" Removing an unknown session ID should raise KeyError """
instance = self.manager_class()
session_id = "DECAFBAD"
self.failUnlessRaises(
KeyError,
instance.remove_session, session_id
)
def test_remove_session_should_cause_get_session_failure(self):
""" Removing a session should result in failure to get session """
instance = self.manager_class()
identity_name = "fred"
session_id = instance.create_session()
instance.remove_session(session_id)
self.failUnlessRaises(
KeyError,
instance.get_session, session_id
)
|