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