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/>.
23 import unittest
.mock
as mock
25 import six
.moves
.urllib
.parse
as urlparse
27 pytest
.importorskip("requests")
29 from mediagoblin
import mg_globals
30 from mediagoblin
.db
.base
import Session
31 from mediagoblin
.db
.models
import Privilege
32 from mediagoblin
.tests
.tools
import get_app
33 from mediagoblin
.tools
import template
36 # App with plugin enabled
38 def persona_plugin_app(request
):
41 mgoblin_config
=pkg_resources
.resource_filename(
42 'mediagoblin.tests.auth_configs',
43 'persona_appconfig.ini'))
46 class TestPersonaPlugin(object):
47 def test_authentication_views(self
, persona_plugin_app
):
48 res
= persona_plugin_app
.get('/auth/login/')
50 assert urlparse
.urlsplit(res
.location
)[2] == '/'
52 res
= persona_plugin_app
.get('/auth/register/')
54 assert urlparse
.urlsplit(res
.location
)[2] == '/'
56 res
= persona_plugin_app
.get('/auth/persona/login/')
58 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
60 res
= persona_plugin_app
.get('/auth/persona/register/')
62 assert urlparse
.urlsplit(res
.location
)[2] == '/auth/login/'
64 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'test@example.com'))
65 def _test_registration():
67 template
.clear_test_template_context()
68 res
= persona_plugin_app
.post(
69 '/auth/persona/login/', {})
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']
75 assert register_form
.email
.data
== u
'test@example.com'
76 assert register_form
.persona_email
.data
== u
'test@example.com'
78 template
.clear_test_template_context()
79 res
= persona_plugin_app
.post(
80 '/auth/persona/register/', {})
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']
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.']
91 template
.clear_test_template_context()
92 res
= persona_plugin_app
.post(
93 '/auth/persona/register/',
95 'email': 'chris@example.com',
96 'persona_email': 'test@example.com'})
99 assert urlparse
.urlsplit(res
.location
)[2] == '/u/chris/'
100 assert 'mediagoblin/user_pages/user_nonactive.html' in template
.TEMPLATE_TEST_CONTEXT
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'})
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']
114 assert register_form
.persona_email
.errors
== [u
'Sorry, an account is already registered to that Persona email.']
117 persona_plugin_app
.get('/auth/logout/')
119 # Get user and detach from session
120 test_user
= mg_globals
.database
.User
.query
.filter_by(
121 username
=u
'chris').first()
122 active_privilege
= Privilege
.query
.filter(
123 Privilege
.privilege_name
==u
'active').first()
124 test_user
.all_privileges
.append(active_privilege
)
126 test_user
= mg_globals
.database
.User
.query
.filter_by(
127 username
=u
'chris').first()
128 Session
.expunge(test_user
)
130 # Add another user for _test_edit_persona
131 persona_plugin_app
.post(
132 '/auth/persona/register/',
133 {'username': 'chris1',
134 'email': 'chris1@example.com',
135 'persona_email': 'test1@example.com'})
138 template
.clear_test_template_context()
139 res
= persona_plugin_app
.post(
140 '/auth/persona/login/')
143 assert urlparse
.urlsplit(res
.location
)[2] == '/'
144 assert 'mediagoblin/root.html' in template
.TEMPLATE_TEST_CONTEXT
146 # Make sure user is in the session
147 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/root.html']
148 session
= context
['request'].session
149 assert session
['user_id'] == six
.text_type(test_user
.id)
153 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'new@example.com'))
154 def _test_edit_persona():
155 # Try and delete only Persona email address
156 template
.clear_test_template_context()
157 res
= persona_plugin_app
.post(
159 {'email': 'test@example.com'})
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
"You can't delete your only Persona email address unless you have a password set."]
167 template
.clear_test_template_context()
168 res
= persona_plugin_app
.post(
169 '/edit/persona/', {})
171 assert 'mediagoblin/plugins/persona/edit.html' in template
.TEMPLATE_TEST_CONTEXT
172 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/plugins/persona/edit.html']
173 form
= context
['form']
175 assert form
.email
.errors
== [u
'This field is required.']
177 # Try and delete Persona not owned by the user
178 template
.clear_test_template_context()
179 res
= persona_plugin_app
.post(
181 {'email': 'test1@example.com'})
183 assert 'mediagoblin/plugins/persona/edit.html' in template
.TEMPLATE_TEST_CONTEXT
184 context
= template
.TEMPLATE_TEST_CONTEXT
['mediagoblin/plugins/persona/edit.html']
185 form
= context
['form']
187 assert form
.email
.errors
== [u
'That Persona email address is not registered to this account.']
189 res
= persona_plugin_app
.get('/edit/persona/add/')
191 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'
193 # Add Persona email address
194 template
.clear_test_template_context()
195 res
= persona_plugin_app
.post(
196 '/edit/persona/add/')
199 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
202 res
= persona_plugin_app
.post(
204 {'email': 'test@example.com'})
207 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/account/'
211 @mock.patch('mediagoblin.plugins.persona.views._get_response', mock
.Mock(return_value
=u
'test1@example.com'))
212 def _test_add_existing():
213 template
.clear_test_template_context()
214 res
= persona_plugin_app
.post(
215 '/edit/persona/add/')
218 assert urlparse
.urlsplit(res
.location
)[2] == '/edit/persona/'