Fix #5382 - Add migration and fix so tombstones are removed from collections
[larjonas-mediagoblin.git] / mediagoblin / tests / test_persona.py
blob437cb7a19e53f964928b42a08011735555e9169a
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
17 import pkg_resources
18 import pytest
19 import six
20 try:
21 import mock
22 except ImportError:
23 import unittest.mock as mock
25 import six.moves.urllib.parse as urlparse
27 pytest.importorskip("requests")
29 from mediagoblin import mg_globals
30 from mediagoblin.db.base import Session
31 from mediagoblin.db.models import Privilege, LocalUser
32 from mediagoblin.tests.tools import get_app
33 from mediagoblin.tools import template
36 # App with plugin enabled
37 @pytest.fixture()
38 def persona_plugin_app(request):
39 return get_app(
40 request,
41 mgoblin_config=pkg_resources.resource_filename(
42 'mediagoblin.tests.auth_configs',
43 'persona_appconfig.ini'))
46 class TestPersonaPlugin(object):
47 def test_authentication_views(self, persona_plugin_app):
48 res = persona_plugin_app.get('/auth/login/')
50 assert urlparse.urlsplit(res.location)[2] == '/'
52 res = persona_plugin_app.get('/auth/register/')
54 assert urlparse.urlsplit(res.location)[2] == '/'
56 res = persona_plugin_app.get('/auth/persona/login/')
58 assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
60 res = persona_plugin_app.get('/auth/persona/register/')
62 assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
64 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test@example.com'))
65 def _test_registration():
66 # No register users
67 template.clear_test_template_context()
68 res = persona_plugin_app.post(
69 '/auth/persona/login/', {})
71 assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
72 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
73 register_form = context['register_form']
75 assert register_form.email.data == u'test@example.com'
76 assert register_form.persona_email.data == u'test@example.com'
78 template.clear_test_template_context()
79 res = persona_plugin_app.post(
80 '/auth/persona/register/', {})
82 assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
83 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
84 register_form = context['register_form']
86 assert register_form.username.errors == [u'This field is required.']
87 assert register_form.email.errors == [u'This field is required.']
88 assert register_form.persona_email.errors == [u'This field is required.']
90 # Successful register
91 template.clear_test_template_context()
92 res = persona_plugin_app.post(
93 '/auth/persona/register/',
94 {'username': 'chris',
95 'email': 'chris@example.com',
96 'persona_email': 'test@example.com'})
97 res.follow()
99 assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
100 assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT
102 # Try to register same Persona email address
103 template.clear_test_template_context()
104 res = persona_plugin_app.post(
105 '/auth/persona/register/',
106 {'username': 'chris1',
107 'email': 'chris1@example.com',
108 'persona_email': 'test@example.com'})
110 assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
111 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
112 register_form = context['register_form']
114 assert register_form.persona_email.errors == [u'Sorry, an account is already registered to that Persona email.']
116 # Logout
117 persona_plugin_app.get('/auth/logout/')
119 # Get user and detach from session
120 test_user = mg_globals.database.LocalUser.query.filter(
121 LocalUser.username==u'chris'
122 ).first()
123 active_privilege = Privilege.query.filter(
124 Privilege.privilege_name==u'active').first()
125 test_user.all_privileges.append(active_privilege)
126 test_user.save()
127 test_user = mg_globals.database.LocalUser.query.filter(
128 LocalUser.username==u'chris'
129 ).first()
130 Session.expunge(test_user)
132 # Add another user for _test_edit_persona
133 persona_plugin_app.post(
134 '/auth/persona/register/',
135 {'username': 'chris1',
136 'email': 'chris1@example.com',
137 'persona_email': 'test1@example.com'})
139 # Log back in
140 template.clear_test_template_context()
141 res = persona_plugin_app.post(
142 '/auth/persona/login/')
143 res.follow()
145 assert urlparse.urlsplit(res.location)[2] == '/'
146 assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
148 # Make sure user is in the session
149 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
150 session = context['request'].session
151 assert session['user_id'] == six.text_type(test_user.id)
153 _test_registration()
155 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'new@example.com'))
156 def _test_edit_persona():
157 # Try and delete only Persona email address
158 template.clear_test_template_context()
159 res = persona_plugin_app.post(
160 '/edit/persona/',
161 {'email': 'test@example.com'})
163 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
164 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
165 form = context['form']
167 assert form.email.errors == [u"You can't delete your only Persona email address unless you have a password set."]
169 template.clear_test_template_context()
170 res = persona_plugin_app.post(
171 '/edit/persona/', {})
173 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
174 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
175 form = context['form']
177 assert form.email.errors == [u'This field is required.']
179 # Try and delete Persona not owned by the user
180 template.clear_test_template_context()
181 res = persona_plugin_app.post(
182 '/edit/persona/',
183 {'email': 'test1@example.com'})
185 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
186 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
187 form = context['form']
189 assert form.email.errors == [u'That Persona email address is not registered to this account.']
191 res = persona_plugin_app.get('/edit/persona/add/')
193 assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
195 # Add Persona email address
196 template.clear_test_template_context()
197 res = persona_plugin_app.post(
198 '/edit/persona/add/')
199 res.follow()
201 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
203 # Delete a Persona
204 res = persona_plugin_app.post(
205 '/edit/persona/',
206 {'email': 'test@example.com'})
207 res.follow()
209 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
211 _test_edit_persona()
213 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test1@example.com'))
214 def _test_add_existing():
215 template.clear_test_template_context()
216 res = persona_plugin_app.post(
217 '/edit/persona/add/')
218 res.follow()
220 assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
222 _test_add_existing()