test_messages.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # GNU MediaGoblin -- federated, autonomous media hosting
  2. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from mediagoblin import messages
  17. from mediagoblin.tools import template
  18. def test_messages(test_app):
  19. """
  20. Added messages should show up in the request.session,
  21. fetched messages should be the same as the added ones,
  22. and fetching should clear the message list.
  23. """
  24. # Aquire a request object
  25. test_app.get('/')
  26. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
  27. request = context['request']
  28. # The message queue should be empty
  29. assert request.session.get('messages', []) == []
  30. # First of all, we should clear the messages queue
  31. messages.clear_add_message()
  32. # Adding a message should modify the session accordingly
  33. messages.add_message(request, 'herp_derp', 'First!')
  34. test_msg_queue = [{'text': 'First!', 'level': 'herp_derp'}]
  35. # Alternative tests to the following, test divided in two steps:
  36. # assert request.session['messages'] == test_msg_queue
  37. # 1. Tests if add_message worked
  38. assert messages.ADD_MESSAGE_TEST[-1] == test_msg_queue
  39. # 2. Tests if add_message updated session information
  40. assert messages.ADD_MESSAGE_TEST[-1] == request.session['messages']
  41. # fetch_messages should return and empty the queue
  42. assert messages.fetch_messages(request) == test_msg_queue
  43. assert request.session.get('messages') == []