~ricardokirkner/django-openid-auth/custom-user-model

« back to all changes in this revision

Viewing changes to django_openid_auth/tests/test_auth.py

Revert the UserOpenID model change which added the account_verified flag. The intention was to allow for the support of multiple UserOpenIDs for a given User, but in reality the solution wouldn't have provided this, because we have no idea which OP provided the e-mail address associated with the account. In the meantime, allow for the simple case of handling account_verified for a single UserOpenID, while not ruling out future support for the complex case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import unittest
30
30
 
31
31
from django.conf import settings
32
 
from django.contrib.auth.models import Group, User
 
32
from django.contrib.auth.models import (
 
33
    Group,
 
34
    Permission,
 
35
    User,
 
36
)
33
37
from django.test import TestCase
34
38
 
35
39
from django_openid_auth.auth import OpenIDBackend
181
185
        user_openid, created = UserOpenID.objects.get_or_create(
182
186
            user=user,
183
187
            claimed_id='http://example.com/existing_identity',
184
 
            display_id='http://example.com/existing_identity',
185
 
            account_verified=False)
 
188
            display_id='http://example.com/existing_identity')
186
189
        data = dict(first_name=u"Some56789012345678901234567890123",
187
190
            last_name=u"User56789012345678901234567890123",
188
191
            email=u"someotheruser@example.com", account_verified=False)
189
192
 
190
 
        self.backend.update_user_details(user_openid, data, response)
 
193
        self.backend.update_user_details(user, data, response)
191
194
 
192
195
        self.assertEqual("Some56789012345678901234567890",  user.first_name)
193
196
        self.assertEqual("User56789012345678901234567890",  user.last_name)
206
209
            user=user, claimed_id=claimed_id, display_id=display_id)
207
210
        return user_openid
208
211
 
209
 
    def _test_account_verified(self, user_openid, verified, expected):
 
212
    def _test_account_verified(self, user, initially_verified, expected):
210
213
        # set user's verification status
211
 
        user_openid.account_verified = verified
 
214
        permission = Permission.objects.get(codename='account_verified')
 
215
        if initially_verified:
 
216
            user.user_permissions.add(permission)
 
217
        else:
 
218
            user.user_permissions.remove(permission)
 
219
 
 
220
        if hasattr(user, '_perm_cache'):
 
221
            del user._perm_cache
212
222
 
213
223
        # get a response including verification status
214
224
        response = self.make_response_ax()
215
225
        data = dict(first_name=u"Some56789012345678901234567890123",
216
 
            last_name=u"User56789012345678901234567890123",
217
 
            email=u"someotheruser@example.com", account_verified=expected)
218
 
        self.backend.update_user_details(user_openid, data, response)
 
226
                    last_name=u"User56789012345678901234567890123",
 
227
                    email=u"someotheruser@example.com",
 
228
                    account_verified=expected)
 
229
        self.backend.update_user_details(user, data, response)
219
230
 
220
231
        # refresh object from the database
221
 
        user_openid = UserOpenID.objects.get(pk=user_openid.pk)
 
232
        user = User.objects.get(pk=user.pk)
222
233
        # check the verification status
223
 
        self.assertEqual(user_openid.account_verified, expected)
224
 
        self.assertEqual(user_openid.user.has_perm(
225
 
            'django_openid_auth.account_verified'), expected)
226
 
 
227
 
    def test_update_user_openid_unverified(self):
228
 
        user_openid = self.make_user_openid()
229
 
 
230
 
        for verified in (False, True):
231
 
            self._test_account_verified(user_openid, verified, expected=False)
232
 
 
233
 
    def test_update_user_openid_verified(self):
234
 
        user_openid = self.make_user_openid()
235
 
 
236
 
        for verified in (False, True):
237
 
            self._test_account_verified(user_openid, verified, expected=True)
 
234
        self.assertEqual(user.has_perm('django_openid_auth.account_verified'),
 
235
                         expected)
 
236
 
 
237
    def test_update_user_perms_unverified(self):
 
238
        user_openid = self.make_user_openid()
 
239
 
 
240
        for initially_verified in (False, True):
 
241
            self._test_account_verified(
 
242
                user_openid.user, initially_verified, expected=False)
 
243
 
 
244
    def test_update_user_perms_verified(self):
 
245
        user_openid = self.make_user_openid()
 
246
 
 
247
        for initially_verified in (False, True):
 
248
            self._test_account_verified(
 
249
                user_openid.user, initially_verified, expected=True)
238
250
 
239
251
    def test_extract_user_details_name_with_trailing_space(self):
240
252
        response = self.make_response_ax(fullname="SomeUser ")