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/>.
21 from mediagoblin
import mg_globals
22 from mediagoblin
.db
.base
import Session
23 from mediagoblin
.tests
.tools
import get_app
24 from mediagoblin
.tools
import template
27 # App with plugin enabled
29 def persona_plugin_app(request
):
32 mgoblin_config
=pkg_resources
.resource_filename(
33 'mediagoblin.tests.auth_configs',
34 'persona_appconfig.ini'))
37 class TestPersonaPlugin(object):
38 def test_authentication_views(self
, persona_plugin_app
):
39 res
= persona_plugin_app
.get('/auth/login/')
41 assert urlparse
.urlsplit(res
.location
)[2] == '/'
43 res
= persona_plugin_app
.get('/auth/register/')
45 assert urlparse
.urlsplit(res
.location
)[2] == '/'
47 res
= persona_plugin_app
.get('/auth/persona/login/')
49 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
51 res
= persona_plugin_app
.get('/auth/persona/register/')
53 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
55 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'test@example.com'))
56 def _test_registration():
58 template
.clear_test_template_context()
59 res
= persona_plugin_app
.post(
60 '/auth/persona/login/', {})
62 assert 'mediagoblin/auth/register.html' in template
.TEMPLATE_TEST_CONTEXT
63 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
64 register_form
= context
['register_form']
66 assert register_form
.email
.data
== u
'test@example.com'
67 assert register_form
.persona_email
.data
== u
'test@example.com'
69 template
.clear_test_template_context()
70 res
= persona_plugin_app
.post(
71 '/auth/persona/register/', {})
73 assert 'mediagoblin/auth/register.html' in template
.TEMPLATE_TEST_CONTEXT
74 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
75 register_form
= context
['register_form']
77 assert register_form
.username
.errors
== [u
'This field is required.']
78 assert register_form
.email
.errors
== [u
'This field is required.']
79 assert register_form
.persona_email
.errors
== [u
'This field is required.']
82 template
.clear_test_template_context()
83 res
= persona_plugin_app
.post(
84 '/auth/persona/register/',
86 'email': 'chris@example.com',
87 'persona_email': 'test@example.com'})
90 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
91 assert 'mediagoblin/user_pages/user.html' in template
.TEMPLATE_TEST_CONTEXT
93 # Try to register same Persona email address
94 template
.clear_test_template_context()
95 res
= persona_plugin_app
.post(
96 '/auth/persona/register/',
97 {'username': 'chris1',
98 'email': 'chris1@example.com',
99 'persona_email': 'test@example.com'})
101 assert 'mediagoblin/auth/register.html' in template
.TEMPLATE_TEST_CONTEXT
102 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/auth/register.html']
103 register_form
= context
['register_form']
105 assert register_form
.persona_email
.errors
== [u
'Sorry, an account is already registered to that Persona email.']
108 persona_plugin_app
.get('/auth/logout/')
110 # Get user and detach from session
111 test_user
= mg_globals
.database
.User
.query
.filter_by(
112 username
=u
'chris').first()
113 test_user
.email_verified
= True
114 test_user
.status
= u
'active'
116 test_user
= mg_globals
.database
.User
.query
.filter_by(
117 username
=u
'chris').first()
118 Session
.expunge(test_user
)
120 # Add another user for _test_edit_persona
121 persona_plugin_app
.post(
122 '/auth/persona/register/',
123 {'username': 'chris1',
124 'email': 'chris1@example.com',
125 'persona_email': 'test1@example.com'})
128 template
.clear_test_template_context()
129 res
= persona_plugin_app
.post(
130 '/auth/persona/login/')
133 assert urlparse
.urlsplit(res
.location
)[2] == '/'
134 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
136 # Make sure user is in the session
137 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/root.html']
138 session
= context
['request'].session
139 assert session
['user_id'] == unicode(test_user
.id)
143 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'new@example.com'))
144 def _test_edit_persona():
145 # Try and delete only Persona email address
146 template
.clear_test_template_context()
147 res
= persona_plugin_app
.post(
149 {'email': 'test@example.com'})
151 assert 'mediagoblin/plugins/persona/edit.html' in template
.TEMPLATE_TEST_CONTEXT
152 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/plugins/persona/edit.html']
153 form
= context
['form']
155 assert form
.email
.errors
== [u
"You can't delete your only Persona email address unless you have a password set."]
157 template
.clear_test_template_context()
158 res
= persona_plugin_app
.post(
159 '/edit/persona/', {})
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']
165 assert form
.email
.errors
== [u
'This field is required.']
167 # Try and delete Persona not owned by the user
168 template
.clear_test_template_context()
169 res
= persona_plugin_app
.post(
171 {'email': 'test1@example.com'})
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']
177 assert form
.email
.errors
== [u
'That Persona email address is not registered to this account.']
179 res
= persona_plugin_app
.get('/edit/persona/add/')
181 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'
183 # Add Persona email address
184 template
.clear_test_template_context()
185 res
= persona_plugin_app
.post(
186 '/edit/persona/add/')
189 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
192 res
= persona_plugin_app
.post(
194 {'email': 'test@example.com'})
197 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
201 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'test1@example.com'))
202 def _test_add_existing():
203 template
.clear_test_template_context()
204 res
= persona_plugin_app
.post(
205 '/edit/persona/add/')
208 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'