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/>. | |
fd19da34 | 16 | |
4f8f0a4e RE |
17 | import pkg_resources |
18 | import pytest | |
e49b7e02 | 19 | import six |
9459fa3c BP |
20 | try: |
21 | import mock | |
22 | except ImportError: | |
23 | import unittest.mock as mock | |
4f8f0a4e | 24 | |
fd19da34 BP |
25 | import six.moves.urllib.parse as urlparse |
26 | ||
bd0b5daa RE |
27 | pytest.importorskip("requests") |
28 | ||
4f8f0a4e | 29 | from mediagoblin import mg_globals |
b4997540 JT |
30 | from mediagoblin.db.base import Session |
31 | from mediagoblin.db.models import Privilege, LocalUser | |
4f8f0a4e RE |
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/' | |
515e3bd9 | 100 | assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT |
4f8f0a4e RE |
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 | |
b4997540 | 120 | test_user = mg_globals.database.LocalUser.query.filter( |
d88fcb03 JT |
121 | LocalUser.username==u'chris' |
122 | ).first() | |
6483b370 | 123 | active_privilege = Privilege.query.filter( |
124 | Privilege.privilege_name==u'active').first() | |
125 | test_user.all_privileges.append(active_privilege) | |
4f8f0a4e | 126 | test_user.save() |
b4997540 | 127 | test_user = mg_globals.database.LocalUser.query.filter( |
d88fcb03 JT |
128 | LocalUser.username==u'chris' |
129 | ).first() | |
4f8f0a4e RE |
130 | Session.expunge(test_user) |
131 | ||
132 | # Add another user for _test_edit_persona | |
133 | persona_plugin_app.post( | |
134 | '/auth/persona/register/', | |
135 | {'username': 'chris1', | |
136 | 'email': 'chris1@example.com', | |
137 | 'persona_email': 'test1@example.com'}) | |
138 | ||
139 | # Log back in | |
140 | template.clear_test_template_context() | |
141 | res = persona_plugin_app.post( | |
142 | '/auth/persona/login/') | |
143 | res.follow() | |
144 | ||
145 | assert urlparse.urlsplit(res.location)[2] == '/' | |
146 | assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT | |
147 | ||
148 | # Make sure user is in the session | |
149 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html'] | |
150 | session = context['request'].session | |
e49b7e02 | 151 | assert session['user_id'] == six.text_type(test_user.id) |
4f8f0a4e RE |
152 | |
153 | _test_registration() | |
154 | ||
155 | @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'new@example.com')) | |
156 | def _test_edit_persona(): | |
157 | # Try and delete only Persona email address | |
158 | template.clear_test_template_context() | |
159 | res = persona_plugin_app.post( | |
160 | '/edit/persona/', | |
161 | {'email': 'test@example.com'}) | |
162 | ||
163 | assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT | |
164 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html'] | |
165 | form = context['form'] | |
166 | ||
167 | assert form.email.errors == [u"You can't delete your only Persona email address unless you have a password set."] | |
168 | ||
169 | template.clear_test_template_context() | |
170 | res = persona_plugin_app.post( | |
171 | '/edit/persona/', {}) | |
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'This field is required.'] | |
178 | ||
179 | # Try and delete Persona not owned by the user | |
180 | template.clear_test_template_context() | |
181 | res = persona_plugin_app.post( | |
182 | '/edit/persona/', | |
183 | {'email': 'test1@example.com'}) | |
184 | ||
185 | assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT | |
186 | context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html'] | |
187 | form = context['form'] | |
188 | ||
189 | assert form.email.errors == [u'That Persona email address is not registered to this account.'] | |
190 | ||
191 | res = persona_plugin_app.get('/edit/persona/add/') | |
192 | ||
193 | assert urlparse.urlsplit(res.location)[2] == '/edit/persona/' | |
194 | ||
195 | # Add Persona email address | |
196 | template.clear_test_template_context() | |
197 | res = persona_plugin_app.post( | |
198 | '/edit/persona/add/') | |
199 | res.follow() | |
200 | ||
201 | assert urlparse.urlsplit(res.location)[2] == '/edit/account/' | |
202 | ||
203 | # Delete a Persona | |
204 | res = persona_plugin_app.post( | |
205 | '/edit/persona/', | |
206 | {'email': 'test@example.com'}) | |
207 | res.follow() | |
208 | ||
209 | assert urlparse.urlsplit(res.location)[2] == '/edit/account/' | |
210 | ||
211 | _test_edit_persona() | |
212 | ||
213 | @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value=u'test1@example.com')) | |
214 | def _test_add_existing(): | |
215 | template.clear_test_template_context() | |
216 | res = persona_plugin_app.post( | |
217 | '/edit/persona/add/') | |
218 | res.follow() | |
219 | ||
220 | assert urlparse.urlsplit(res.location)[2] == '/edit/persona/' | |
221 | ||
222 | _test_add_existing() |