也许我要问的问题非常笼统,没有具体的答案。情况是我花了几天时间试图找到答案,我很遗憾。我将不胜感激任何形式的回应或对任何可以帮助我的书籍或手册的参考。
我正在尝试使用测试重新创建Django身份验证过程,我将检查用户的用户和密码。
测试功能代码如下。
import sys, os
import unittest, mock, kiss
from sqlite3 import connect, PARSE_DECLTYPES
from datetime import date
from twisted.python import log
from twisted.cred import credentials
from twisted.cred.error import UnauthorizedLogin
from ampauth.testing import DjangoAuthChecker
"""
Some irrelevant code for this question.
"""
def test_GoodCredentials(self):
creds = credentials.UsernamePassword(self.username, self.password)
checker = DjangoAuthChecker()
d = checker.requestAvatarId(creds)
其中self.username
是用户名,self.password
是密码。
当我尝试使用DjangoAuthChecker类创建用户的头像时出现问题。
import os
from zope.interface import implements
from twisted.python import failure, log
from twisted.cred import portal, checkers, error, credentials
from twisted.internet import defer
from django.contrib.auth.models import User
class DjangoAuthChecker():
implements(checkers.ICredentialsChecker)
credentialInterfaces = (credentials.IUsernamePassword,
credentials.IUsernameHashedPassword)
def _passwordMatch(self, matched, user):
if matched:
return user
else:
return failure.Failure(error.UnauthorizedLogin())
def requestAvatarId(self, credentials, connection):
try:
user = User.objects.get(username=credentials.username)
return defer.maybeDeferred(
check_password,
credentials.password,
user.password).addCallback(self._passwordMatch, user)
except User.DoesNotExist:
return defer.fail(error.UnauthorizedLogin())
之前的代码在屏幕上显示以下错误。
(.venv_test)sgongar@debian8-64bits-dev:~/Dev/client/protocol/tests$ python test_creds.py
Traceback (most recent call last):
File "test_creds.py", line 48, in <module>
from ampauth.testing import DjangoAuthChecker
File "/home/sgongar/Dev/client/protocol/ampauth/testing.py", line 33, in <module>
from django.contrib.auth.models import User
File "/home/sgongar/Dev/client/protocol/.venv_test/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 7, in <module>
from django.middleware.csrf import rotate_token
File "/home/sgongar/Dev/client/protocol/.venv_test/local/lib/python2.7/site-packages/django/middleware/csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "/home/sgongar/Dev/client/protocol/.venv_test/local/lib/python2.7/site-packages/django/utils/cache.py", line 26, in <module>
from django.core.cache import caches
File "/home/sgongar/Dev/client/protocol/.venv_test/local/lib/python2.7/site-packages/django/core/cache/__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/home/sgongar/Dev/client/protocol/.venv_test/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/sgongar/Dev/client/protocol/.venv_test/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
所以问题是,我可能需要一个设置文件才能使用单个django类吗? 我以为我可以在不需要设置文件的情况下导入一个类。
如何用模拟对象替换设置文件?
好吧,我添加了几行来配置设置,我设法避免了错误。现在我有另一个问题,即为用户创建表格,但会将问题发布在另一页面上。
from django.conf import settings
settings.configure(DEBUG=True,
DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'test.db'),
'TEST_NAME': os.path.join(BASE_DIR, 'test.db'),}},
INSTALLED_APPS = ('django.contrib.auth',))
from django.contrib.auth.models import User, check_password
from django.db import models