From 33ad44bc97f08df71f227f6f2a006e770a75c353 Mon Sep 17 00:00:00 2001 From: Barry Warsaw Date: Fri, 1 Apr 2011 18:51:29 -0400 Subject: * Re-organize the interface between buildout.cfg and the zope.testing layer initialization. buildout.cfg is now really simple; it calls one method. That method does all the relevant layer initializations. This better localizes what has to be set up before testing can even begin. * IUsers now have a created_on property which contains the datetime at which the user record was created. * Rework the date and uid factories so that they consult the MockAndMonkeyLayer for the current testing flag. Also, those factories register themselves with the layer so that they'll get automatically reset between tests, without the layer actually having to know about them. * Move the User model object initialization into User.__init__() from the user manager. The User now also adds itself to the store. * Add a 'uid factory' for unique id creation, which is test suite aware. --- src/mailman/utilities/uid.py | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/mailman/utilities/uid.py (limited to 'src/mailman/utilities/uid.py') diff --git a/src/mailman/utilities/uid.py b/src/mailman/utilities/uid.py new file mode 100644 index 000000000..cc2cf3b12 --- /dev/null +++ b/src/mailman/utilities/uid.py @@ -0,0 +1,67 @@ +# Copyright (C) 2011 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman is free software: you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman 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 General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# GNU Mailman. If not, see . + +"""Unique ID generation. + +Use these functions to create unique ids rather than inlining calls to hashlib +and whatnot. These are better instrumented for testing purposes. +""" + +from __future__ import absolute_import, unicode_literals + +__metaclass__ = type +__all__ = [ + 'UniqueIDFactory', + 'factory', + ] + + +import os +import time +import hashlib + +from mailman.testing.layers import MockAndMonkeyLayer +from mailman.utilities.passwords import SALT_LENGTH + + + +class UniqueIDFactory: + """A factory for unique ids.""" + + # The predictable id. + predictable_id = None + + def new_uid(self, bytes=None): + if MockAndMonkeyLayer.testing_mode: + uid = self.predictable_id + self.predictable_id += 1 + return unicode(uid) + salt = os.urandom(SALT_LENGTH) + h = hashlib.sha1(repr(time.time())) + h.update(salt) + if bytes is not None: + h.update(bytes) + return unicode(h.hexdigest(), 'us-ascii') + + def reset(self): + self.predictable_id = 1 + + + +factory = UniqueIDFactory() +factory.reset() +MockAndMonkeyLayer.register_reset(factory.reset) -- cgit v1.2.3-70-g09d2