aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests/test_persona.py
blob: 25f0ca1ef98264a0275c9ab95256064bf566f5f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pkg_resources
import pytest
import six
try:
    from unittest import mock
except ImportError:
    import unittest.mock as mock

import six.moves.urllib.parse as urlparse

pytest.importorskip("requests")

from mediagoblin import mg_globals
from mediagoblin.db.base import Session
from mediagoblin.db.models import Privilege, LocalUser
from mediagoblin.tests.tools import get_app
from mediagoblin.tools import template


# App with plugin enabled
@pytest.fixture()
def persona_plugin_app(request):
    return get_app(
        request,
        mgoblin_config=pkg_resources.resource_filename(
            'mediagoblin.tests.auth_configs',
            'persona_appconfig.ini'))


class TestPersonaPlugin:
    def test_authentication_views(self, persona_plugin_app):
        res = persona_plugin_app.get('/auth/login/')

        assert urlparse.urlsplit(res.location)[2] == '/'

        res = persona_plugin_app.get('/auth/register/')

        assert urlparse.urlsplit(res.location)[2] == '/'

        res = persona_plugin_app.get('/auth/persona/login/')

        assert urlparse.urlsplit(res.location)[2] == '/auth/login/'

        res = persona_plugin_app.get('/auth/persona/register/')

        assert urlparse.urlsplit(res.location)[2] == '/auth/login/'

        @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value='test@example.com'))
        def _test_registration():
            # No register users
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/auth/persona/login/', {})

            assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
            register_form = context['register_form']

            assert register_form.email.data == 'test@example.com'
            assert register_form.persona_email.data == 'test@example.com'

            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/auth/persona/register/', {})

            assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
            register_form = context['register_form']

            assert register_form.username.errors == ['This field is required.']
            assert register_form.email.errors == ['This field is required.']
            assert register_form.persona_email.errors == ['This field is required.']

            # Successful register
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/auth/persona/register/',
                {'username': 'chris',
                 'email': 'chris@example.com',
                 'persona_email': 'test@example.com'})
            res.follow()

            assert urlparse.urlsplit(res.location)[2] == '/u/chris/'
            assert 'mediagoblin/user_pages/user_nonactive.html' in template.TEMPLATE_TEST_CONTEXT

            # Try to register same Persona email address
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/auth/persona/register/',
                {'username': 'chris1',
                 'email': 'chris1@example.com',
                 'persona_email': 'test@example.com'})

            assert 'mediagoblin/auth/register.html' in template.TEMPLATE_TEST_CONTEXT
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/auth/register.html']
            register_form = context['register_form']

            assert register_form.persona_email.errors == ['Sorry, an account is already registered to that Persona email.']

            # Logout
            persona_plugin_app.get('/auth/logout/')

            # Get user and detach from session
            test_user = mg_globals.database.LocalUser.query.filter(
                LocalUser.username=='chris'
            ).first()
            active_privilege = Privilege.query.filter(
                Privilege.privilege_name=='active').first()
            test_user.all_privileges.append(active_privilege)
            test_user.save()
            test_user = mg_globals.database.LocalUser.query.filter(
                LocalUser.username=='chris'
            ).first()
            Session.expunge(test_user)

            # Add another user for _test_edit_persona
            persona_plugin_app.post(
                '/auth/persona/register/',
                {'username': 'chris1',
                 'email': 'chris1@example.com',
                 'persona_email': 'test1@example.com'})

            # Log back in
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/auth/persona/login/')
            res.follow()

            assert urlparse.urlsplit(res.location)[2] == '/'
            assert 'mediagoblin/root.html' in template.TEMPLATE_TEST_CONTEXT

            # Make sure user is in the session
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/root.html']
            session = context['request'].session
            assert session['user_id'] == str(test_user.id)

        _test_registration()

        @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value='new@example.com'))
        def _test_edit_persona():
            # Try and delete only Persona email address
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/edit/persona/',
                {'email': 'test@example.com'})

            assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
            form = context['form']

            assert form.email.errors == ["You can't delete your only Persona email address unless you have a password set."]

            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/edit/persona/', {})

            assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
            form = context['form']

            assert form.email.errors == ['This field is required.']

            # Try and delete Persona not owned by the user
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/edit/persona/',
                {'email': 'test1@example.com'})

            assert 'mediagoblin/plugins/persona/edit.html' in template.TEMPLATE_TEST_CONTEXT
            context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/plugins/persona/edit.html']
            form = context['form']

            assert form.email.errors == ['That Persona email address is not registered to this account.']

            res = persona_plugin_app.get('/edit/persona/add/')

            assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'

            # Add Persona email address
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/edit/persona/add/')
            res.follow()

            assert urlparse.urlsplit(res.location)[2] == '/edit/account/'

            # Delete a Persona
            res = persona_plugin_app.post(
                '/edit/persona/',
                {'email': 'test@example.com'})
            res.follow()

            assert urlparse.urlsplit(res.location)[2] == '/edit/account/'

        _test_edit_persona()

        @mock.patch('mediagoblin.plugins.persona.views._get_response', mock.Mock(return_value='test1@example.com'))
        def _test_add_existing():
            template.clear_test_template_context()
            res = persona_plugin_app.post(
                '/edit/persona/add/')
            res.follow()

            assert urlparse.urlsplit(res.location)[2] == '/edit/persona/'

        _test_add_existing()