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