~roadmr/canonical-identity-provider/u2f-db-fields

« back to all changes in this revision

Viewing changes to src/identityprovider/tests/test_utils.py

[r=nataliabidart,james-w] Implement a password blacklist.
Passwords which should not be used can be added to a file and users will either be disallowed from setting them, or asked to change them if their existing password was newly added to the blacklist.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
import os
3
4
import urllib2
4
5
from datetime import date, datetime, time, timedelta
5
6
from uuid import uuid4
12
13
from django.test import TestCase
13
14
from django.test.utils import override_settings
14
15
 
 
16
from identityprovider import utils
15
17
from identityprovider.models.person import Person
16
18
from identityprovider.utils import (
17
19
    canonical_url,
266
268
        request = self.factory.make_request(**kwargs)
267
269
        with self.assertRaises(SuspiciousOperation):
268
270
            get_provider_url(request)
 
271
 
 
272
 
 
273
class BlacklistedPasswordTestCase(TestCase):
 
274
 
 
275
    @override_settings(PASSWORD_BLACKLIST_FILE="bl_test_filename")
 
276
    def test_populate_password_blacklist(self):
 
277
        raw_pass_data = ("cthulhu50\nr'lyeh25\ndunwich600\nDunWich600"
 
278
                         "\n\r        \r\n   e   \nCthulhu50")
 
279
        with open(settings.PASSWORD_BLACKLIST_FILE, "w") as bl_file:
 
280
            self.addCleanup(os.unlink, settings.PASSWORD_BLACKLIST_FILE)
 
281
            bl_file.write(raw_pass_data)
 
282
        expected_passwords = {
 
283
            'cthulhu50', "r'lyeh25", 'dunwich600', '        ', '   e   '}
 
284
        utils.populate_password_blacklist()
 
285
        self.assertEqual(utils.get_password_blacklist(),
 
286
                         expected_passwords)
 
287
 
 
288
    def test_password_is_not_blacklisted(self):
 
289
        utils.blackted_password_set = set(['bogus', 'blacklist'])
 
290
        self.assertFalse(utils.password_is_blacklisted('goodpass'))
 
291
 
 
292
    def test_password_is_blacklisted(self):
 
293
        utils.blacklisted_password_set = set(['bogus', 'blacklist'])
 
294
        self.assertTrue(utils.password_is_blacklisted('blacklist'))
 
295
 
 
296
    def test_password_is_blacklisted_variable_case(self):
 
297
        utils.blacklisted_password_set = set(['bogus', 'blacklist'])
 
298
        self.assertTrue(utils.password_is_blacklisted('BlackList'))