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