【问题标题】:Custom user model breaks auth tests自定义用户模型破坏了身份验证测试
【发布时间】:2011-03-27 12:17:36
【问题描述】:

嘿嘿,

我正在使用我选择称为 Member 的自定义用户模型,它扩展了默认用户模型(与 here 描述的差不多)。

问题在于,这会破坏 auth 应用程序的几个测试,因为测试夹具只创建标准 users,而不是 members。当我将缺少的 member 定义/行添加到 django/contrib/auth/fixtures/authtestdata.json 时,它们会再次运行,但这当然不是解决方案。

这是因为我做错了什么,我应该如何最好地解决它?

作为最后的办法,我只想在 Buildout 中为 authtestdata.json 添加一个补丁,但也许有更优雅的解决方案。

非常感谢,
特洛菲

【问题讨论】:

    标签: django testing fixtures authentication


    【解决方案1】:

    根据http://docs.djangoproject.com/en/1.2/topics/auth/#storing-additional-information-about-users,在 settings.py 中设置 AUTH_USER_PROFILE 参数可能会让您感到高兴。这是以 Django 可以使用的方式扩展标准用户模型的好方法。

    【讨论】:

    • 谢谢,但我在某个时候决定不这样做。我们存储的用户信息与他们的姓名同等(也许甚至更多)基本信息;将这些字段分成一个配置文件似乎非常不直观。子类化模型自 2008-04-26 以来一直有效,这似乎是一个教科书式的继承用例。如果出现更多问题,我会重新考虑,但到目前为止,我更喜欢我的 hacky 补丁。
    【解决方案2】:

    更新:请忽略我在下面的帖子。使用自定义身份验证后端的the original version,并在settings.pyAUTHENTICATION_BACKENDS 中的自定义后端下添加django.contrib.auth.backends.ModelBackend 作为第二个(备用)后端。效果一样,而且更简单。


    我现在摆脱了补丁,而是扩展了我在问题中链接到的custom auth backend,如果在成员表中找不到用户(我的自定义用户)。如果发生这种情况,则会发出警告(在运行测试时可能会有点烦人)。此外,消息测试会导致很多异常,但它们似乎对测试没有任何不利影响。我希望他们是故意的。 ^^

    # -*- encoding: utf-8 -*-
    import logging
    from django.conf import settings
    from django.contrib.auth.backends import ModelBackend
    from django.core.exceptions import ImproperlyConfigured
    from django.db.models import get_model
    
    logger = logging.getLogger(__name__)
    
    # http://scottbarnham.com/blog/2008/08/21/extending-the-django-user-model-with-inheritance/
    class CustomUserModelBackend(ModelBackend):
        def authenticate(self, username=None, password=None):
            try:
                user = self.user_class.objects.get(username=username)
                if user.check_password(password):
                    return user
            except self.user_class.DoesNotExist:
                user = super(CustomUserModelBackend, self).authenticate(username, password)
                if user:
                    logger.warn("Using User, not Member: %s" % user)
                return user
    
        def get_user(self, user_id):
            try:
                return self.user_class.objects.get(pk=user_id)
            except self.user_class.DoesNotExist:
                user = super(CustomUserModelBackend, self).get_user(user_id)
                if user:
                    logger.warn("Using User, not Member: %s" % user)
                return user
    
        @property
        def user_class(self):
            if not hasattr(self, '_user_class'):
                self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
                if not self._user_class:
                    raise ImproperlyConfigured('Could not get custom user model')
            return self._user_class
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      • 2019-07-01
      • 2021-11-17
      • 2018-01-26
      • 2021-01-09
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多