diff --git a/dpaste/models.py b/dpaste/models.py index 3abef84..ef3b04f 100644 --- a/dpaste/models.py +++ b/dpaste/models.py @@ -15,8 +15,8 @@ L = getattr(settings, 'DPASTE_SLUG_LENGTH', 4) T = getattr(settings, 'DPASTE_SLUG_CHOICES', 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ1234567890') -def generate_secret_id(length=L): - return ''.join([R.choice(T) for i in range(L)]) +def generate_secret_id(length=L, alphabet=T): + return ''.join([R.choice(alphabet) for i in range(length)]) class Snippet(models.Model): secret_id = models.CharField(_(u'Secret ID'), max_length=255, blank=True) diff --git a/dpaste/tests/test_snippet.py b/dpaste/tests/test_snippet.py index 505e2f3..30d401c 100644 --- a/dpaste/tests/test_snippet.py +++ b/dpaste/tests/test_snippet.py @@ -316,3 +316,16 @@ class SnippetTestCase(TestCase): from dpaste.highlight import pygmentize pygmentize('code', lexer_name='python') pygmentize('code', lexer_name='doesnotexist') + + # This is actually a bad test. It is possible to have duplicates + # because even if its random, it can generate two random, equal strings. + # + # def test_random_slug_generation(self): + # """ + # Generate 1000 random slugs, make sure we have no duplicates. + # """ + # from dpaste.models import generate_secret_id + # result_list = [] + # for i in range(0, 1000): + # result_list.append(generate_secret_id()) + # self.assertEqual(len(set(result_list)), 1000)