Merge branch '905-activities'
[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 six
20 try:
21 import mock
22 except ImportError:
23 import unittest.mock as mock
24
25 import six.moves.urllib.parse as urlparse
26
27 pytest.importorskip("requests")
28
29 from mediagoblin import mg_globals
30 from mediagoblin.db.base import Session
31 from mediagoblin.db.models import Privilege
32 from mediagoblin.tests.tools import get_app
33 from mediagoblin.tools import template
34
35
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'))
44
45
46 class TestPersonaPlugin(object):
47 def test_authentication_views(self, persona_plugin_app):
48 res = persona_plugin_app.get('/auth/login/')
49
50 assert urlparse.urlsplit(res.location)[2] == '/'
51
52 res = persona_plugin_app.get('/auth/register/')
53
54 assert urlparse.urlsplit(res.location)[2] == '/'
55
56 res = persona_plugin_app.get('/auth/persona/login/')
57
58 assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
59
60 res = persona_plugin_app.get('/auth/persona/register/')
61
62 assert urlparse.urlsplit(res.location)[2] == '/auth/login/'
63
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/', {})
70
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']
74
75 assert register_form.email.data == u'test@example.com'
76 assert register_form.persona_email.data == u'test@example.com'
77
78 template.clear_test_template_context()
79 res = persona_plugin_app.post(
80 '/auth/persona/register/', {})
81
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']
85
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.']
89
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()
98
99 assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
100 assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT
101
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'})
109
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']
113
114 assert register_form.persona_email.errors == [u'Sorry, an account is already registered to that Persona email.']
115
116 # Logout
117 persona_plugin_app.get('/auth/logout/')
118
119 # Get user and detach from session
120 test_user = mg_globals.database.User.query.filter_by(
121 username=u'chris').first()
122 active_privilege = Privilege.query.filter(
123 Privilege.privilege_name==u'active').first()
124 test_user.all_privileges.append(active_privilege)
125 test_user.save()
126 test_user = mg_globals.database.User.query.filter_by(
127 username=u'chris').first()
128 Session.expunge(test_user)
129
130 # Add another user for _test_edit_persona
131 persona_plugin_app.post(
132 '/auth/persona/register/',
133 {'username': 'chris1',
134 'email': 'chris1@example.com',
135 'persona_email': 'test1@example.com'})
136
137 # Log back in
138 template.clear_test_template_context()
139 res = persona_plugin_app.post(
140 '/auth/persona/login/')
141 res.follow()
142
143 assert urlparse.urlsplit(res.location)[2] == '/'
144 assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT
145
146 # Make sure user is in the session
147 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
148 session = context['request'].session
149 assert session['user_id'] == six.text_type(test_user.id)
150
151 _test_registration()
152
153 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'new@example.com'))
154 def _test_edit_persona():
155 # Try and delete only Persona email address
156 template.clear_test_template_context()
157 res = persona_plugin_app.post(
158 '/edit/persona/',
159 {'email': 'test@example.com'})
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"You can't delete your only Persona email address unless you have a password set."]
166
167 template.clear_test_template_context()
168 res = persona_plugin_app.post(
169 '/edit/persona/', {})
170
171 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
172 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
173 form = context['form']
174
175 assert form.email.errors == [u'This field is required.']
176
177 # Try and delete Persona not owned by the user
178 template.clear_test_template_context()
179 res = persona_plugin_app.post(
180 '/edit/persona/',
181 {'email': 'test1@example.com'})
182
183 assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
184 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
185 form = context['form']
186
187 assert form.email.errors == [u'That Persona email address is not registered to this account.']
188
189 res = persona_plugin_app.get('/edit/persona/add/')
190
191 assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
192
193 # Add Persona email address
194 template.clear_test_template_context()
195 res = persona_plugin_app.post(
196 '/edit/persona/add/')
197 res.follow()
198
199 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
200
201 # Delete a Persona
202 res = persona_plugin_app.post(
203 '/edit/persona/',
204 {'email': 'test@example.com'})
205 res.follow()
206
207 assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
208
209 _test_edit_persona()
210
211 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test1@example.com'))
212 def _test_add_existing():
213 template.clear_test_template_context()
214 res = persona_plugin_app.post(
215 '/edit/persona/add/')
216 res.follow()
217
218 assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'
219
220 _test_add_existing()