Monkey修补了django auth的登录,现在它的测试失败了

我的应用程序试图用一些基本的审计/日志记录功能包装django.contrib.auth.views登录和注销视图.我遵循django-axes项目中描述的处方,并且在运行服务器和其他一些测试时,它按预期工作,透明地没有问题.

代码如下:

from django.contrib.auth import views as auth_views
from myapp.watchers import watch_login

class WatcherMiddleware(object):
    def __init__(self):
        auth_views.login = watch_login(auth_views.login)

def watch_login(func):
    def decorated_login(request, *args, **kwargs):
        #do some stuff
        response = func(request, *args, **kwargs)
        #more stuff
        return response
    return decorated_login

网址:

#Edit: Added project's urls - just using vanilla django's auth login
(r'^accounts/login/$', 'django.contrib.auth.views.login',{"template_name":settings.LOGIN_TEMPLATE }),

但是,在我们的构建工作流程中,我们遇到了django.contrib.auth.tests.views中的一些问题.

具体来说,这些是django.contrib.auth中失败的测试:

ERROR: test_current_site_in_context_after_login (django.contrib.auth.tests.views.LoginTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\django\contrib\auth\tests\views.py", line 192, in test_current_site_in_context_after_login
    response = self.client.get(reverse('django.contrib.auth.views.login'))
  File "C:\Python26\lib\site-packages\django\core\urlresolvers.py", line 351, in reverse
    *args, **kwargs)))
  File "C:\Python26\lib\site-packages\django\core\urlresolvers.py", line 297, in reverse
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'myapp.watchers.decorated_login' with arguments '()' and keyword arguments '{}' not found.

======================================================================
ERROR: test_security_check (django.contrib.auth.tests.views.LoginTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Python26\lib\site-packages\django\contrib\auth\tests\views.py", line 204, in test_security_check
    login_url = reverse('django.contrib.auth.views.login')
  File "C:\Python26\lib\site-packages\django\core\urlresolvers.py", line 351, in reverse
    *args, **kwargs)))
  File "C:\Python26\lib\site-packages\django\core\urlresolvers.py", line 297, in reverse
    "arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'myapp.watchers.decorated_login' with arguments '()' and keyword arguments '{}' not found.

只有这两个测试失败,包含了包装的登录猴子补丁.
似乎django auth测试中的reverse()调用的行为与未调试的函数的行为方式不同.

我们之所以采用这种方式来包装日志记录而不是使用django 1.3的新认证信号是因为那里提供的日志记录方法只告诉你是否发生了错误的尝试 – 它不会让你访问请求对象到记录有关该不正确请求的其他信息.在这种情况下修补身份验证表单不会有帮助,因此我们需要包装登录功能.

我的登录功能包装有问题吗?尽管整体功能没有变化,但由于其他副作用导致测试失败,这是否可以预期?

编辑:我正在运行python 2.6.4,django 1.2.5

最佳答案
难道你不能简单地将它包装在另一个视图中?

网址:

url(
    r'^accounts/login/$',
    'accounts.views.login',
    {"template_name":settings.LOGIN_TEMPLATE }
),

accounts.views:

from django.contrib.auth import views as auth_views   

def login(request, *args, **kwars):
    # do some stuff    
    response = auth_views.login(request, *args, **kwars)
    # more stuff
    return response

像这样,django.contrib.auth.tests将测试它们所编写的视图,你可以为你需要的“更多东西”编写自己的测试.

转载注明原文:Monkey修补了django auth的登录,现在它的测试失败了 - 代码日志