Skip to content

Instantly share code, notes, and snippets.

@GabiThume
Last active December 16, 2015 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GabiThume/5479889 to your computer and use it in GitHub Desktop.
Save GabiThume/5479889 to your computer and use it in GitHub Desktop.
Trying a solution for bug: http://issues.mediagoblin.org/ticket/146
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mediagoblin.tools import common
DEBUG = 'debug'
INFO = 'info'
SUCCESS = 'success'
WARNING = 'warning'
ERROR = 'error'
ADD_MESSAGE_TEST = []
def add_message(request, level, text):
messages = request.session.setdefault('messages', [])
messages.append({'level': level, 'text': text})
if common.TESTS_ENABLED:
ADD_MESSAGE_TEST.append(messages)
request.session.save()
def fetch_messages(request, clear_from_session=True):
messages = request.session.get('messages')
if messages and clear_from_session:
# Save that we removed the messages from the session
request.session['messages'] = []
request.session.save()
return messages
def clear_add_message():
global ADD_MESSAGE_TEST
ADD_MESSAGE_TEST = []
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mediagoblin.messages import fetch_messages, add_message
from mediagoblin.tools import template
def test_messages(test_app):
"""
Added messages should show up in the request.session,
fetched messages should be the same as the added ones,
and fetching should clear the message list.
"""
# Aquire a request object
test_app.get('/')
context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
request = context['request']
# The message queue should be empty
assert request.session.get('messages', []) == []
# First of all, we should clear the messages queue
messages.clear_add_message()
# Adding a message should modify the session accordingly
add_message(request, 'herp_derp', 'First!')
test_msg_queue = [{'text': 'First!', 'level': 'herp_derp'}]
# Alternative tests to the following, test divided in two steps:
# assert request.session['messages'] == test_msg_queue
# I'm considering there is no concurrence for ADD_MESSAGE_TEST (because
# we are just running tests, so I imagine anyone is trying to call
# add_message() at the same time... in this way I'm getting the last
# message added into the list.
# 1. Tests if add_message worked
assert messages.ADD_MESSAGE_TEST[-1] == test_msg_queue
# 2. Tests if add_message updated session information
assert messages.ADD_MESSAGE_TEST[-1] == request.session['messages']
# fetch_messages should return and empty the queue
assert fetch_messages(request) == test_msg_queue
assert request.session.get('messages') == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment