This file is indexed.

/usr/share/pyshared/passlib/tests/test_ext_django.py is in python-passlib 1.5.3-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
"""test passlib.ext.django"""
#=========================================================
#imports
#=========================================================
from __future__ import with_statement
#core
import logging; log = logging.getLogger(__name__)
import sys
import warnings
#site
#pkg
from passlib.context import CryptContext, CryptPolicy
from passlib.apps import django_context
from passlib.ext.django import utils
from passlib.hash import sha256_crypt
from passlib.tests.utils import TestCase, unittest, ut_version, catch_warnings
import passlib.tests.test_drivers as td
from passlib.utils import Undef
from passlib.registry import get_crypt_handler
#module

#=========================================================
# import & configure django settings,
#=========================================================

try:
    from django.conf import settings, LazySettings
    has_django = True
except ImportError:
    settings = None
    has_django = False

has_django0 = False #are we using django 0.9 release?
has_django1 = False #inverse - are we using django >= 1.0

if has_django:
    from django import VERSION
    has_django0 = (VERSION < (1,0))
    has_django1 = (VERSION >= (1,0))

    if not isinstance(settings, LazySettings):
        #this could mean django has been configured somehow,
        #which we don't want, since test cases reset and manipulate settings.
        raise RuntimeError("expected django.conf.settings to be LazySettings: %r" % (settings,))

    #else configure a blank settings instance for our unittests
    if has_django0:
        if settings._target is None:
            from django.conf import UserSettingsHolder, global_settings
            settings._target = UserSettingsHolder(global_settings)
    else:
        if not settings.configured:
            settings.configure()

def update_settings(**kwds):
    for k,v in kwds.iteritems():
        if v is Undef:
            if hasattr(settings, k):
                if has_django0:
                    delattr(settings._target, k)
                else:
                    delattr(settings, k)
        else:
            setattr(settings, k, v)

#=========================================================
# and prepare helper to skip all relevant tests
# if django isn't installed.
#=========================================================
def skipUnlessDjango(cls):
    "helper to skip class if django not present"
    if has_django:
        return cls
    if ut_version < 2:
        return None
    return unittest.skip("Django not installed")(cls)

#=========================================================
# mock user object
#=========================================================
if has_django:
    import django.contrib.auth.models as dam

    class FakeUser(dam.User):
        "stub user object for testing"
        #this mainly just overrides .save() to test commit behavior.

        saved_password = None

        def save(self):
            self.saved_password = self.password

#=========================================================
# helper contexts
#=========================================================

# simple context which looks NOTHING like django,
# so we can tell if patching worked.
simple_context = CryptContext(
    schemes = [ "md5_crypt", "des_crypt" ],
    default = "md5_crypt",
    deprecated = [ "des_crypt" ],
)

# some sample hashes
sample1 = 'password'
sample1_md5 = '$1$kAd49ifN$biuRAv1Tv0zGHyCv0uIqW.'
sample1_des = 'PPPTDkiCeu/jM'
sample1_sha1 = 'sha1$b215d$9ee0a66f84ef1ad99096355e788135f7e949bd41'

# context for testing category funcs
category_context = CryptContext(
    schemes = [ "sha256_crypt" ],
    sha256_crypt__rounds = 1000,
    staff__sha256_crypt__rounds = 2000,
    superuser__sha256_crypt__rounds = 3000,
)

def get_cc_rounds(**kwds):
    "helper for testing category funcs"
    user = FakeUser(**kwds)
    user.set_password("placeholder")
    return sha256_crypt.from_string(user.password).rounds

#=========================================================
# test utils
#=========================================================
class PatchTest(TestCase):
    "test passlib.ext.django.utils:set_django_password_context"

    case_prefix = "passlib.ext.django utils"

    def assert_unpatched(self):
        "helper to ensure django hasn't been patched"
        state = utils._django_patch_state

        #make sure we aren't currently patched
        self.assertIs(state, None)

        #make sure nothing else patches django
        for func in [
            dam.check_password,
            dam.User.check_password,
            dam.User.set_password,
            ]:
            self.assertEquals(func.__module__, "django.contrib.auth.models")
        self.assertFalse(hasattr(dam.User, "password_context"))

    def assert_patched(self, context=Undef):
        "helper to ensure django HAS been patched"
        state = utils._django_patch_state

        #make sure we're patched
        self.assertIsNot(state, None)

        #make sure our methods are exposed
        for func in [
            dam.check_password,
            dam.User.check_password,
            dam.User.set_password,
            ]:
            self.assertEquals(func.__module__, "passlib.ext.django.utils")

        #make sure methods match
        self.assertIs(dam.check_password, state['models_check_password'])
        self.assertIs(dam.User.check_password.im_func, state['user_check_password'])
        self.assertIs(dam.User.set_password.im_func, state['user_set_password'])

        #make sure context matches
        obj = dam.User.password_context
        self.assertIs(obj, state['context'])
        if context is not Undef:
            self.assertIs(obj, context)

        #make sure old methods were stored
        for key in [
                "orig_models_check_password",
                "orig_user_check_password",
                "orig_user_set_password",
            ]:
            value = state[key]
            self.assertEquals(value.__module__, "django.contrib.auth.models")

    def setUp(self):
        #reset to baseline, and verify
        utils.set_django_password_context(None)
        self.assert_unpatched()

    def tearDown(self):
        #reset to baseline, and verify
        utils.set_django_password_context(None)
        self.assert_unpatched()

    def test_00_patch_control(self):
        "test set_django_password_context patch/unpatch"

        #check context=None has no effect
        utils.set_django_password_context(None)
        self.assert_unpatched()

        #patch to use stock django context
        utils.set_django_password_context(django_context)
        self.assert_patched(context=django_context)

        #try to remove patch
        utils.set_django_password_context(None)
        self.assert_unpatched()

        #patch to use stock django context again
        utils.set_django_password_context(django_context)
        self.assert_patched(context=django_context)

        #try to remove patch again
        utils.set_django_password_context(None)
        self.assert_unpatched()

    def test_01_patch_control_detection(self):
        "test set_django_password_context detection of foreign monkeypatches"
        def dummy():
            pass

        with catch_warnings(record=True) as wlog:
            warnings.simplefilter("always")

            #patch to use stock django context
            utils.set_django_password_context(django_context)
            self.assert_patched(context=django_context)
            self.assertEquals(len(wlog), 0)

            #mess with User.set_password, make sure it's detected
            dam.User.set_password = dummy
            utils.set_django_password_context(django_context)
            self.assert_patched(context=django_context)
            self.assertEquals(len(wlog), 1)
            self.assertWarningMatches(wlog.pop(),
                message_re="^another library has patched.*User\.set_password$")

            #mess with user.check_password, make sure it's detected
            dam.User.check_password = dummy
            utils.set_django_password_context(django_context)
            self.assert_patched(context=django_context)
            self.assertEquals(len(wlog), 1)
            self.assertWarningMatches(wlog.pop(),
                message_re="^another library has patched.*User\.check_password$")

            #mess with user.check_password, make sure it's detected
            dam.check_password = dummy
            utils.set_django_password_context(django_context)
            self.assert_patched(context=django_context)
            self.assertEquals(len(wlog), 1)
            self.assertWarningMatches(wlog.pop(),
                message_re="^another library has patched.*models:check_password$")

    def test_01_patch_bad_types(self):
        "test set_django_password_context bad inputs"
        set = utils.set_django_password_context
        self.assertRaises(TypeError, set, CryptPolicy())
        self.assertRaises(TypeError, set, "")

    def test_02_models_check_password(self):
        "test monkeypatched models.check_password()"

        # patch to use simple context
        utils.set_django_password_context(simple_context)
        self.assert_patched(context=simple_context)

        # check correct hashes pass
        self.assertTrue(dam.check_password(sample1, sample1_des))
        self.assertTrue(dam.check_password(sample1, sample1_md5))

        # check bad password fail w/ false
        self.assertFalse(dam.check_password('x', sample1_des))
        self.assertFalse(dam.check_password('x', sample1_md5))

        # and other hashes fail w/ error
        self.assertRaises(ValueError, dam.check_password, sample1, sample1_sha1)
        self.assertRaises(ValueError, dam.check_password, sample1, None)

    def test_03_check_password(self):
        "test monkeypatched User.check_password()"
        # NOTE: using FakeUser so we can test .save()
        user = FakeUser()

        # patch to use simple context
        utils.set_django_password_context(simple_context)
        self.assert_patched(context=simple_context)

        # test that blank hash is never accepted
        self.assertEqual(user.password, '')
        self.assertIs(user.saved_password, None)
        self.assertFalse(user.check_password('x'))

        # check correct secrets pass, and wrong ones fail
        user.password = sample1_md5
        self.assertTrue(user.check_password(sample1))
        self.assertFalse(user.check_password('x'))
        self.assertFalse(user.check_password(None))

        # none of that should have triggered update of password
        self.assertEqual(user.password, sample1_md5)
        self.assertIs(user.saved_password, None)

        #check unusable password
        if has_django1:
            user.set_unusable_password()
            self.assertFalse(user.has_usable_password())
            self.assertFalse(user.check_password(None))
            self.assertFalse(user.check_password(''))
            self.assertFalse(user.check_password(sample1))

    def test_04_check_password_migration(self):
        "test User.check_password() hash migration"
        # NOTE: using FakeUser so we can test .save()
        user = FakeUser()

        # patch to use simple context
        utils.set_django_password_context(simple_context)
        self.assert_patched(context=simple_context)

        # set things up with a password that needs migration
        user.password = sample1_des
        self.assertEqual(user.password, sample1_des)
        self.assertIs(user.saved_password, None)

        # run check with bad password...
        # shouldn't have migrated
        self.assertFalse(user.check_password('x'))
        self.assertFalse(user.check_password(None))

        self.assertEqual(user.password, sample1_des)
        self.assertIs(user.saved_password, None)

        # run check with correct password...
        # should have migrated to md5 and called save()
        self.assertTrue(user.check_password(sample1))

        self.assertTrue(user.password.startswith("$1$"))
        self.assertEqual(user.saved_password, user.password)

        # check resave doesn't happen
        user.saved_password = None
        self.assertTrue(user.check_password(sample1))
        self.assertIs(user.saved_password, None)

    def test_05_set_password(self):
        "test monkeypatched User.set_password()"
        user = FakeUser()

        # patch to use simple context
        utils.set_django_password_context(simple_context)
        self.assert_patched(context=simple_context)

        # sanity check
        self.assertEqual(user.password, '')
        self.assertIs(user.saved_password, None)
        if has_django1:
            self.assertTrue(user.has_usable_password())

        # set password
        user.set_password(sample1)
        self.assertTrue(user.check_password(sample1))
        self.assertEquals(simple_context.identify(user.password), "md5_crypt")
        self.assertIs(user.saved_password, None)

        #check unusable password
        user.set_password(None)
        if has_django1:
            self.assertFalse(user.has_usable_password())
        self.assertIs(user.saved_password, None)

    def test_06_get_category(self):
        "test default get_category function"
        func = utils.get_category
        self.assertIs(func(FakeUser()), None)
        self.assertEquals(func(FakeUser(is_staff=True)), "staff")
        self.assertEquals(func(FakeUser(is_superuser=True)), "superuser")
        self.assertEquals(func(FakeUser(is_staff=True,
                                        is_superuser=True)), "superuser")

    def test_07_get_category(self):
        "test set_django_password_context's get_category parameter"
        # test patch uses default get_category
        utils.set_django_password_context(category_context)
        self.assertEquals(get_cc_rounds(), 1000)
        self.assertEquals(get_cc_rounds(is_staff=True), 2000)
        self.assertEquals(get_cc_rounds(is_superuser=True), 3000)

        # test patch uses explicit get_category
        def get_category(user):
            return user.first_name or None
        utils.set_django_password_context(category_context, get_category)
        self.assertEquals(get_cc_rounds(), 1000)
        self.assertEquals(get_cc_rounds(first_name='other'), 1000)
        self.assertEquals(get_cc_rounds(first_name='staff'), 2000)
        self.assertEquals(get_cc_rounds(first_name='superuser'), 3000)

        # test patch can disable get_category
        utils.set_django_password_context(category_context, None)
        self.assertEquals(get_cc_rounds(), 1000)
        self.assertEquals(get_cc_rounds(first_name='other'), 1000)
        self.assertEquals(get_cc_rounds(first_name='staff', is_staff=True), 1000)
        self.assertEquals(get_cc_rounds(first_name='superuser', is_superuser=True), 1000)

PatchTest = skipUnlessDjango(PatchTest)

#=========================================================
# test django plugin
#=========================================================

django_hash_tests = [
                    td.HexMd5Test,
                    td.DjangoDesCryptTest,
                    td.DjangoSaltedMd5Test,
                    td.DjangoSaltedSha1Test,
                     ]

default_hash_tests = django_hash_tests + [ td.Builtin_SHA512CryptTest ]

if has_django0:
    django_hash_tests.remove(td.DjangoDesCryptTest)

class PluginTest(TestCase):
    "test django plugin via settings"

    case_prefix = "passlib.ext.django plugin"

    def setUp(self):
        #remove django patch
        utils.set_django_password_context(None)

        #ensure django settings are empty
        update_settings(
            PASSLIB_CONTEXT=Undef,
            PASSLIB_GET_CATEGORY=Undef,
        )

        #unload module so it's re-run
        sys.modules.pop("passlib.ext.django.models", None)

    def tearDown(self):
        #remove django patch
        utils.set_django_password_context(None)

    def check_hashes(self, tests, new_hash=None, deprecated=None):
        u = FakeUser()
        deprecated = None

        # check new hash construction
        if new_hash:
            u.set_password("placeholder")
            handler = get_crypt_handler(new_hash)
            self.assertTrue(handler.identify(u.password))

        # run against hashes from tests...
        for test in tests:
            for secret, hash in test.all_correct_hashes:

                # check against valid password
                u.password = hash
                if has_django0 and isinstance(secret, unicode):
                    secret = secret.encode("utf-8")
                self.assertTrue(u.check_password(secret))
                if new_hash and deprecated and test.handler.name in deprecated:
                    self.assertFalse(handler.identify(hash))
                    self.assertTrue(handler.identify(u.password))

                # check against invalid password
                u.password = hash
                self.assertFalse(u.check_password('x'+secret))
                if new_hash and deprecated and test.handler.name in deprecated:
                    self.assertFalse(handler.identify(hash))
                    self.assertEquals(u.password, hash)

        # check disabled handling
        if has_django1:
            u.set_password(None)
            handler = get_crypt_handler("django_disabled")
            self.assertTrue(handler.identify(u.password))
            self.assertFalse(u.check_password('placeholder'))

    def test_00_actual_django(self):
        "test actual Django behavior has not changed"
        #NOTE: if this test fails,
        #      probably means newer version of Django,
        #      and passlib's policies should be updated.
        self.check_hashes(django_hash_tests,
                          "django_salted_sha1",
                          ["hex_md5"])

    def test_01_explicit_unset(self, value=None):
        "test PASSLIB_CONTEXT = None"
        update_settings(
            PASSLIB_CONTEXT=value,
        )
        import passlib.ext.django.models
        self.check_hashes(django_hash_tests,
                          "django_salted_sha1",
                          ["hex_md5"])

    def test_02_stock_ctx(self):
        "test PASSLIB_CONTEXT = utils.STOCK_CTX"
        self.test_01_explicit_unset(value=utils.STOCK_CTX)

    def test_03_implicit_default_ctx(self):
        "test PASSLIB_CONTEXT unset"
        import passlib.ext.django.models
        self.check_hashes(default_hash_tests,
                          "sha512_crypt",
                          ["hex_md5", "django_salted_sha1",
                           "django_salted_md5",
                           "django_des_crypt",
                           ])

    def test_04_explicit_default_ctx(self):
        "test PASSLIB_CONTEXT = utils.DEFAULT_CTX"
        update_settings(
            PASSLIB_CONTEXT=utils.DEFAULT_CTX,
        )
        self.test_03_implicit_default_ctx()

    def test_05_default_ctx_alias(self):
        "test PASSLIB_CONTEXT = 'passlib-default'"
        update_settings(
            PASSLIB_CONTEXT="passlib-default",
        )
        self.test_03_implicit_default_ctx()

    def test_06_categories(self):
        "test PASSLIB_GET_CATEGORY unset"
        update_settings(
            PASSLIB_CONTEXT=category_context.policy,
        )
        import passlib.ext.django.models

        self.assertEquals(get_cc_rounds(), 1000)
        self.assertEquals(get_cc_rounds(is_staff=True), 2000)
        self.assertEquals(get_cc_rounds(is_superuser=True), 3000)

    def test_07_categories_explicit(self):
        "test PASSLIB_GET_CATEGORY = function"
        def get_category(user):
            return user.first_name or None
        update_settings(
            PASSLIB_CONTEXT = category_context.policy,
            PASSLIB_GET_CATEGORY = get_category,
        )
        import passlib.ext.django.models

        self.assertEquals(get_cc_rounds(), 1000)
        self.assertEquals(get_cc_rounds(first_name='other'), 1000)
        self.assertEquals(get_cc_rounds(first_name='staff'), 2000)
        self.assertEquals(get_cc_rounds(first_name='superuser'), 3000)

    def test_08_categories_disabled(self):
        "test PASSLIB_GET_CATEGORY = None"
        update_settings(
            PASSLIB_CONTEXT = category_context.policy,
            PASSLIB_GET_CATEGORY = None,
        )
        import passlib.ext.django.models

        self.assertEquals(get_cc_rounds(), 1000)
        self.assertEquals(get_cc_rounds(first_name='other'), 1000)
        self.assertEquals(get_cc_rounds(first_name='staff', is_staff=True), 1000)
        self.assertEquals(get_cc_rounds(first_name='superuser', is_superuser=True), 1000)

PluginTest = skipUnlessDjango(PluginTest)

#=========================================================
#eof
#=========================================================