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 get_user_id(request): sessionid = request.COOKIES.get(SESSION_KEY) if not sessionid: return session = user_db.get_session(sessionid) if session: return session.data.get('user_id')
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_success_authorize_user(self, mocked_generate_sessionid): sessioon_id = '123123' user_id = 123 response = mock.MagicMock() mocked_generate_sessionid.return_value = sessioon_id auth.authorize_user(response, user_id) session = user_db.get_session(sessioon_id) self.assertEqual(session.data, {'user_id': user_id}) response.set_cookie.assert_called_with( key=auth.SESSION_KEY, value=sessioon_id, max_age=None, )