def test_login(self): from qa.security import Session session = {} user = StubUser() Session.login(session, user) self.assertTrue('logged_in' in session) self.assertTrue(session['user'] == user.username) self.assertTrue(session['user_db_id'] == user.id)
def test_requires_logged_in(self): from qa.security import Session, requires_logged_in request = testing.DummyRequest() decorated_view = requires_logged_in(self.dummy_view) self.assertEqual( decorated_view(None, request).location, "http://example.com/login", "Should redirect to login") Session.login(request.session, StubUser()) self.assertTrue(decorated_view(None, request), "Should return self.dummy_view value of True")
def test_requires_question_set_contributor(self): from qa.security import Session, requires_question_set_contributor from pyramid.httpexceptions import HTTPForbidden request = testing.DummyRequest() request.db = self.db Session.login(request.session, self.user) request.matchdict = {'question_set_id': self.question_set.id} decorated_view = requires_question_set_contributor(self.dummy_view) self.assertTrue(decorated_view(None, request), "Should return self.dummy_view value of True") Session.login(request.session, self.user2) self.assertRaises(HTTPForbidden, decorated_view, None, request)
def test_user_id(self): from qa.security import Session session = {'user_db_id': 0} self.assertEqual(Session.user_id(session), 0)
def test_logout(self): from qa.security import Session session = {'logged_in': True} Session.logout(session) self.assertFalse(Session.logged_in(session))
def test_logged_in(self): from qa.security import Session session = {'logged_in': True} self.assertTrue(Session.logged_in(session))