我一直在尝试测试尚未经过验证的用户。
class TestLoginApi(TestCase):
URL = '/rest-auth/login/'
EMAIL = 'testuser@test
PASSWORD = 'password'
DATA = {'email': EMAIL, 'password': PASSWORD}
@classmethod
def setUpTestData(cls):
cls.user = get_user_model().objects.create_user(username='testuser', email=cls.EMAIL,
password=cls.PASSWORD)
def test_login_api_without_verification(self):
response = self.client.post(self.URL, self.DATA, format='json')
"响应" line抛出以下错误
错误
追踪(最近一次呼叫最后一次):
文件" /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py",第59行,在testPartExecutor中 产量
文件" /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py" ;,第601行,在运行中 TestMethod的()
文件" /Users/docdocengineering3/GitHub/website/project/authorization/Tests/test_login.py",第34行,在test_login_api_without_verification中 response = self.client.post(self.URL,self.DATA,format =' json')
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/test/client.py" ;,第548行,在帖子中 安全=安全,**额外)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/test/client.py" ;,第350行,在帖子中 安全=安全,**额外)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/test/client.py" ;,第416行,通用 return self.request(** r)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/test/client.py" ;,第501行,请求中 six.reraise(* exc_info)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/utils/six.py" ;,第686行,重新加入 提高价值
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/core/handlers/exception.py" ;,第41行,内在 response = get_response(request)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/core/handlers/base.py" ;,第187行,在_get_response中 response = self.process_exception_by_middleware(e,request)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/core/handlers/base.py" ;,第185行,在_get_response中 response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/views/decorators/csrf.py",第58行,在wrapped_view中 return view_func(* args,** kwargs)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/views/generic/base.py" ;,第68行,在视图中 return self.dispatch(request,* args,** kwargs)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/utils/decorators.py" ;,第67行,在_wrapper中 return bound_func(* args,** kwargs)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/views/decorators/debug.py" ;,第76行,在sensitive_post_parameters_wrapper中 返回视图(请求,* args,** kwargs)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/utils/decorators.py" ;,第63行,在bound_func中 return func。 get (self,type(self))(* args2,** kwargs2)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_auth/views.py" ;,第49行,在发送中 return super(LoginView,self).dispatch(* args,** kwargs)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_framework/views.py" ;,第489行,在发送中 response = self.handle_exception(exc)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_framework/views.py",第449行,在handle_exception中 self.raise_uncaught_exception(EXC)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_framework/views.py" ;,第486行,在发送中 response = handler(request,* args,** kwargs)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_auth/views.py" ;,第92行,在帖子中 self.serializer.is_valid(raise_exception = TRUE)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_framework/serializers.py" ;,第237行,in is_valid self._validated_data = self.run_validation(self.initial_data)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_framework/serializers.py",第435行,在run_validation中 value = self.validate(value)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/rest_auth/serializers.py" ;,第105行,在验证中 email_address = user.emailaddress_set.get(email = user.email)
文件" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/db/models/manager.py",第85行,在manager_method中 return getattr(self.get_queryset(),name)(* args,** kwargs)
File" /Users/docdocengineering3/.virtualenvs/docdoc/lib/python3.6/site-packages/django/db/models/query.py" ;,第380行,获取 self.model._meta.object_name
例外:EmailAddress匹配查询不存在。
我不知道为什么会这样。这不是一个设置问题,因为我尝试过实际运行系统,创建用户(不验证)以及何时使用PostMan"相同的链接,我得到了正确的错误
{"non_field_errors": [ "E-mail is not verified." ] }
所以它适用于实际产品,但不适用于测试。谁知道为什么?非常感谢任何帮助。
这是唯一可以开始工作的测试,所有其他测试都能正确运行。
答案 0 :(得分:0)
您的设置EMAIL_VERIFICATION
可能设置为mandatory
。
因此,您要么在测试中将其更改为none
,要么手动创建EmailAddress
对象(当强制验证开启时,django-allauth需要它)