test_persona.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. import pkg_resources
  17. import pytest
  18. import six
  19. try:
  20. import mock
  21. except ImportError:
  22. import unittest.mock as mock
  23. import six.moves.urllib.parse as urlparse
  24. pytest.importorskip("requests")
  25. from mediagoblin import mg_globals
  26. from mediagoblin.db.base import Session
  27. from mediagoblin.db.models import Privilege
  28. from mediagoblin.tests.tools import get_app
  29. from mediagoblin.tools import template
  30. # App with plugin enabled
  31. @pytest.fixture()
  32. def persona_plugin_app(request):
  33. return get_app(
  34. request,
  35. mgoblin_config=pkg_resources.resource_filename(
  36. 'mediagoblin.tests.auth_configs',
  37. 'persona_appconfig.ini'))
  38. class TestPersonaPlugin(object):
  39. def test_authentication_views(self, persona_plugin_app):
  40. res = persona_plugin_app.get('/auth/login/')
  41. assert urlparse.urlsplit(res.location)[2] == '/'
  42. res = persona_plugin_app.get('/auth/register/')
  43. assert urlparse.urlsplit(res.location)[2] == '/'
  44. res = persona_plugin_app.get('/auth/persona/login/')
  45. assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
  46. res = persona_plugin_app.get('/auth/persona/register/')
  47. assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
  48. @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test@example.com'))
  49. def _test_registration():
  50. # No register users
  51. template.clear_test_template_context()
  52. res = persona_plugin_app.post(
  53. '/auth/persona/login/', {})
  54. assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
  55. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
  56. register_form = context['register_form']
  57. assert register_form.email.data == u'test@example.com'
  58. assert register_form.persona_email.data == u'test@example.com'
  59. template.clear_test_template_context()
  60. res = persona_plugin_app.post(
  61. '/auth/persona/register/', {})
  62. assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
  63. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
  64. register_form = context['register_form']
  65. assert register_form.username.errors == [u'This field is required.']
  66. assert register_form.email.errors == [u'This field is required.']
  67. assert register_form.persona_email.errors == [u'This field is required.']
  68. # Successful register
  69. template.clear_test_template_context()
  70. res = persona_plugin_app.post(
  71. '/auth/persona/register/',
  72. {'username': 'chris',
  73. 'email': 'chris@example.com',
  74. 'persona_email': 'test@example.com'})
  75. res.follow()
  76. assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
  77. assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT
  78. # Try to register same Persona email address
  79. template.clear_test_template_context()
  80. res = persona_plugin_app.post(
  81. '/auth/persona/register/',
  82. {'username': 'chris1',
  83. 'email': 'chris1@example.com',
  84. 'persona_email': 'test@example.com'})
  85. assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
  86. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
  87. register_form = context['register_form']
  88. assert register_form.persona_email.errors == [u'Sorry, an account is already registered to that Persona email.']
  89. # Logout
  90. persona_plugin_app.get('/auth/logout/')
  91. # Get user and detach from session
  92. test_user = mg_globals.database.User.query.filter_by(
  93. username=u'chris').first()
  94. active_privilege = Privilege.query.filter(
  95. Privilege.privilege_name==u'active').first()
  96. test_user.all_privileges.append(active_privilege)
  97. test_user.save()
  98. test_user = mg_globals.database.User.query.filter_by(
  99. username=u'chris').first()
  100. Session.expunge(test_user)
  101. # Add another user for _test_edit_persona
  102. persona_plugin_app.post(
  103. '/auth/persona/register/',
  104. {'username': 'chris1',
  105. 'email': 'chris1@example.com',
  106. 'persona_email': 'test1@example.com'})
  107. # Log back in
  108. template.clear_test_template_context()
  109. res = persona_plugin_app.post(
  110. '/auth/persona/login/')
  111. res.follow()
  112. assert urlparse.urlsplit(res.location)[2] == '/'
  113. assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
  114. # Make sure user is in the session
  115. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
  116. session = context['request'].session
  117. assert session['user_id'] == six.text_type(test_user.id)
  118. _test_registration()
  119. @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'new@example.com'))
  120. def _test_edit_persona():
  121. # Try and delete only Persona email address
  122. template.clear_test_template_context()
  123. res = persona_plugin_app.post(
  124. '/edit/persona/',
  125. {'email': 'test@example.com'})
  126. assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
  127. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
  128. form = context['form']
  129. assert form.email.errors == [u"You can't delete your only Persona email address unless you have a password set."]
  130. template.clear_test_template_context()
  131. res = persona_plugin_app.post(
  132. '/edit/persona/', {})
  133. assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
  134. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
  135. form = context['form']
  136. assert form.email.errors == [u'This field is required.']
  137. # Try and delete Persona not owned by the user
  138. template.clear_test_template_context()
  139. res = persona_plugin_app.post(
  140. '/edit/persona/',
  141. {'email': 'test1@example.com'})
  142. assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
  143. context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
  144. form = context['form']
  145. assert form.email.errors == [u'That Persona email address is not registered to this account.']
  146. res = persona_plugin_app.get('/edit/persona/add/')
  147. assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
  148. # Add Persona email address
  149. template.clear_test_template_context()
  150. res = persona_plugin_app.post(
  151. '/edit/persona/add/')
  152. res.follow()
  153. assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
  154. # Delete a Persona
  155. res = persona_plugin_app.post(
  156. '/edit/persona/',
  157. {'email': 'test@example.com'})
  158. res.follow()
  159. assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
  160. _test_edit_persona()
  161. @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test1@example.com'))
  162. def _test_add_existing():
  163. template.clear_test_template_context()
  164. res = persona_plugin_app.post(
  165. '/edit/persona/add/')
  166. res.follow()
  167. assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
  168. _test_add_existing()