def test_get_user_id_without_sessionid(self): sessionid = '321' user_id = 123 request = mock.MagicMock( COOKIES={} ) user_db.create_session_data(sessionid, {'user_id': user_id}) auth_user_id = auth.get_user_id(request) self.assertEqual(auth_user_id, None)
def test_get_user_id_wrong_sessionid(self): sessionid = '321' sessionid_in_db = '322' user_id = 123 request = mock.MagicMock( COOKIES={auth.SESSION_KEY: sessionid} ) user_db.create_session_data(sessionid_in_db, {'user_id': user_id}) auth_user_id = auth.get_user_id(request) self.assertEqual(auth_user_id, None)
def test_set_session_data(self): username = '******' password = '******' user_id = user_db.create_user(username, password) session_id = str(utils.get_random_string(50)) data = {'user_id' : user_id} user_db.create_session_data(session_id, data) session = user_db.get_session(session_id) self.assertEqual(session.data, data)
def test_logout_user(self): sessionid = '321' user_id = 123 request = mock.MagicMock(COOKIES={auth.SESSION_KEY : sessionid}) user_db.create_session_data(sessionid, {'user_id': user_id}) response = mock.MagicMock() auth.logout_user(request, response) session_data = user_db.get_session(sessionid) self.assertEqual(session_data, None) response.delete_cookie.assert_called_with( key=auth.SESSION_KEY )
def test_del_session(self): user_id1 = factories.create_user() user_id2 = factories.create_user() session_id = str(utils.get_random_string(50)) session_id2 = str(utils.get_random_string(50)) data = {'user_id': user_id1} data2 = {'user_id': user_id2} user_db.create_session_data(session_id, data) user_db.create_session_data(session_id2, data2) user_db.del_session(session_id) deleted_row = user_db.get_session(session_id) session2 = user_db.get_session(session_id2) self.assertEqual(deleted_row, None) self.assertEqual(session2.data, data2)
def authorize_user(response, user_id): sessionid = _generate_sessionid() user_db.create_session_data(sessionid, {'user_id': user_id}) response.set_cookie(key=SESSION_KEY, value=sessionid, max_age=None) return response