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