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