1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
22 import six
.moves
.urllib
.parse
as urlparse
24 pytest
.importorskip("requests")
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
33 # App with plugin enabled
35 def persona_plugin_app(request
):
38 mgoblin_config
=pkg_resources
.resource_filename(
39 'mediagoblin.tests.auth_configs',
40 'persona_appconfig.ini'))
43 class TestPersonaPlugin(object):
44 def test_authentication_views(self
, persona_plugin_app
):
45 res
= persona_plugin_app
.get('/auth/login/')
47 assert urlparse
.urlsplit(res
.location
)[2] == '/'
49 res
= persona_plugin_app
.get('/auth/register/')
51 assert urlparse
.urlsplit(res
.location
)[2] == '/'
53 res
= persona_plugin_app
.get('/auth/persona/login/')
55 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
57 res
= persona_plugin_app
.get('/auth/persona/register/')
59 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
61 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'test@example.com'))
62 def _test_registration():
64 template
.clear_test_template_context()
65 res
= persona_plugin_app
.post(
66 '/auth/persona/login/', {})
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']
72 assert register_form
.email
.data
== u
'test@example.com'
73 assert register_form
.persona_email
.data
== u
'test@example.com'
75 template
.clear_test_template_context()
76 res
= persona_plugin_app
.post(
77 '/auth/persona/register/', {})
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']
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.']
88 template
.clear_test_template_context()
89 res
= persona_plugin_app
.post(
90 '/auth/persona/register/',
92 'email': 'chris@example.com',
93 'persona_email': 'test@example.com'})
96 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
97 assert 'mediagoblin/user_pages/user_nonactive.html' in template
.TEMPLATE_TEST_CONTEXT
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'})
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']
111 assert register_form
.persona_email
.errors
== [u
'Sorry, an account is already registered to that Persona email.']
114 persona_plugin_app
.get('/auth/logout/')
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
)
123 test_user
= mg_globals
.database
.User
.query
.filter_by(
124 username
=u
'chris').first()
125 Session
.expunge(test_user
)
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'})
135 template
.clear_test_template_context()
136 res
= persona_plugin_app
.post(
137 '/auth/persona/login/')
140 assert urlparse
.urlsplit(res
.location
)[2] == '/'
141 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
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)
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(
156 {'email': 'test@example.com'})
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']
162 assert form
.email
.errors
== [u
"You can't delete your only Persona email address unless you have a password set."]
164 template
.clear_test_template_context()
165 res
= persona_plugin_app
.post(
166 '/edit/persona/', {})
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']
172 assert form
.email
.errors
== [u
'This field is required.']
174 # Try and delete Persona not owned by the user
175 template
.clear_test_template_context()
176 res
= persona_plugin_app
.post(
178 {'email': 'test1@example.com'})
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']
184 assert form
.email
.errors
== [u
'That Persona email address is not registered to this account.']
186 res
= persona_plugin_app
.get('/edit/persona/add/')
188 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'
190 # Add Persona email address
191 template
.clear_test_template_context()
192 res
= persona_plugin_app
.post(
193 '/edit/persona/add/')
196 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
199 res
= persona_plugin_app
.post(
201 {'email': 'test@example.com'})
204 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
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/')
215 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'