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 pytest
.importorskip("requests")
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
29 # App with plugin enabled
31 def persona_plugin_app(request
):
34 mgoblin_config
=pkg_resources
.resource_filename(
35 'mediagoblin.tests.auth_configs',
36 'persona_appconfig.ini'))
39 class TestPersonaPlugin(object):
40 def test_authentication_views(self
, persona_plugin_app
):
41 res
= persona_plugin_app
.get('/auth/login/')
43 assert urlparse
.urlsplit(res
.location
)[2] == '/'
45 res
= persona_plugin_app
.get('/auth/register/')
47 assert urlparse
.urlsplit(res
.location
)[2] == '/'
49 res
= persona_plugin_app
.get('/auth/persona/login/')
51 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
53 res
= persona_plugin_app
.get('/auth/persona/register/')
55 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
57 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'test@example.com'))
58 def _test_registration():
60 template
.clear_test_template_context()
61 res
= persona_plugin_app
.post(
62 '/auth/persona/login/', {})
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']
68 assert register_form
.email
.data
== u
'test@example.com'
69 assert register_form
.persona_email
.data
== u
'test@example.com'
71 template
.clear_test_template_context()
72 res
= persona_plugin_app
.post(
73 '/auth/persona/register/', {})
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']
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.']
84 template
.clear_test_template_context()
85 res
= persona_plugin_app
.post(
86 '/auth/persona/register/',
88 'email': 'chris@example.com',
89 'persona_email': 'test@example.com'})
92 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
93 assert 'mediagoblin/user_pages/user_nonactive.html' in template
.TEMPLATE_TEST_CONTEXT
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'})
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']
107 assert register_form
.persona_email
.errors
== [u
'Sorry, an account is already registered to that Persona email.']
110 persona_plugin_app
.get('/auth/logout/')
112 # Get user and detach from session
113 test_user
= mg_globals
.database
.User
.query
.filter_by(
114 username
=u
'chris').first()
115 test_user
.email_verified
= True
116 test_user
.status
= u
'active'
118 test_user
= mg_globals
.database
.User
.query
.filter_by(
119 username
=u
'chris').first()
120 Session
.expunge(test_user
)
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'})
130 template
.clear_test_template_context()
131 res
= persona_plugin_app
.post(
132 '/auth/persona/login/')
135 assert urlparse
.urlsplit(res
.location
)[2] == '/'
136 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
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)
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(
151 {'email': 'test@example.com'})
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']
157 assert form
.email
.errors
== [u
"You can't delete your only Persona email address unless you have a password set."]
159 template
.clear_test_template_context()
160 res
= persona_plugin_app
.post(
161 '/edit/persona/', {})
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']
167 assert form
.email
.errors
== [u
'This field is required.']
169 # Try and delete Persona not owned by the user
170 template
.clear_test_template_context()
171 res
= persona_plugin_app
.post(
173 {'email': 'test1@example.com'})
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']
179 assert form
.email
.errors
== [u
'That Persona email address is not registered to this account.']
181 res
= persona_plugin_app
.get('/edit/persona/add/')
183 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'
185 # Add Persona email address
186 template
.clear_test_template_context()
187 res
= persona_plugin_app
.post(
188 '/edit/persona/add/')
191 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
194 res
= persona_plugin_app
.post(
196 {'email': 'test@example.com'})
199 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
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/')
210 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'