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/>.
21 pytest.importorskip("requests")
23 from mediagoblin import mg_globals
24 from mediagoblin.db.base import Session
25 from mediagoblin.db.models import Privilege
26 from mediagoblin.tests.tools import get_app
27 from mediagoblin.tools import template
30 # App with plugin enabled
32 def persona_plugin_app(request):
35 mgoblin_config=pkg_resources.resource_filename(
36 'mediagoblin.tests.auth_configs',
37 'persona_appconfig.ini'))
40 class TestPersonaPlugin(object):
41 def test_authentication_views(self, persona_plugin_app):
42 res = persona_plugin_app.get('/auth/login/')
44 assert urlparse.urlsplit(res.location)[2] == '/'
46 res = persona_plugin_app.get('/auth/register/')
48 assert urlparse.urlsplit(res.location)[2] == '/'
50 res = persona_plugin_app.get('/auth/persona/login/')
52 assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
54 res = persona_plugin_app.get('/auth/persona/register/')
56 assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
58 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test@example.com'))
59 def _test_registration():
61 template.clear_test_template_context()
62 res = persona_plugin_app.post(
63 '/auth/persona/login/', {})
65 assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
66 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
67 register_form = context['register_form']
69 assert register_form.email.data == u'test@example.com'
70 assert register_form.persona_email.data == u'test@example.com'
72 template.clear_test_template_context()
73 res = persona_plugin_app.post(
74 '/auth/persona/register/', {})
76 assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
77 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
78 register_form = context['register_form']
80 assert register_form.username.errors == [u'This field is required.']
81 assert register_form.email.errors == [u'This field is required.']
82 assert register_form.persona_email.errors == [u'This field is required.']
85 template.clear_test_template_context()
86 res = persona_plugin_app.post(
87 '/auth/persona/register/',
89 'email': 'chris@example.com',
90 'persona_email': 'test@example.com'})
93 assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
94 assert 'mediagoblin/user_pages/user.html' in template.TEMPLATE_TEST_CONTEXT
96 # Try to register same Persona email address
97 template.clear_test_template_context()
98 res = persona_plugin_app.post(
99 '/auth/persona/register/',
100 {'username': 'chris1',
101 'email': 'chris1@example.com',
102 'persona_email': 'test@example.com'})
104 assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
105 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
106 register_form = context['register_form']
108 assert register_form.persona_email.errors == [u'Sorry, an account is already registered to that Persona email.']
111 persona_plugin_app.get('/auth/logout/')
113 # Get user and detach from session
114 test_user = mg_globals.database.User.query.filter_by(
115 username=u'chris').first()
116 active_privilege = Privilege.query.filter(
117 Privilege.privilege_name==u'active').first()
118 test_user.all_privileges.append(active_privilege)
120 test_user = mg_globals.database.User.query.filter_by(
121 username=u'chris').first()
122 Session.expunge(test_user)
124 # Add another user for _test_edit_persona
125 persona_plugin_app.post(
126 '/auth/persona/register/',
127 {'username': 'chris1',
128 'email': 'chris1@example.com',
129 'persona_email': 'test1@example.com'})
132 template.clear_test_template_context()
133 res = persona_plugin_app.post(
134 '/auth/persona/login/')
137 assert urlparse.urlsplit(res.location)[2] == '/'
138 assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
140 # Make sure user is in the session
141 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
142 session = context['request'].session
143 assert session['user_id'] == unicode(test_user.id)
147 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'new@example.com'))
148 def _test_edit_persona():
149 # Try and delete only Persona email address
150 template.clear_test_template_context()
151 res = persona_plugin_app.post(
153 {'email': 'test@example.com'})
155 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
156 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
157 form = context['form']
159 assert form.email.errors == [u"You can't delete your only Persona email address unless you have a password set."]
161 template.clear_test_template_context()
162 res = persona_plugin_app.post(
163 '/edit/persona/', {})
165 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
166 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
167 form = context['form']
169 assert form.email.errors == [u'This field is required.']
171 # Try and delete Persona not owned by the user
172 template.clear_test_template_context()
173 res = persona_plugin_app.post(
175 {'email': 'test1@example.com'})
177 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
178 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
179 form = context['form']
181 assert form.email.errors == [u'That Persona email address is not registered to this account.']
183 res = persona_plugin_app.get('/edit/persona/add/')
185 assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
187 # Add Persona email address
188 template.clear_test_template_context()
189 res = persona_plugin_app.post(
190 '/edit/persona/add/')
193 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
196 res = persona_plugin_app.post(
198 {'email': 'test@example.com'})
201 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
205 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test1@example.com'))
206 def _test_add_existing():
207 template.clear_test_template_context()
208 res = persona_plugin_app.post(
209 '/edit/persona/add/')
212 assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'