Edit2: 我得到了为什么我收到错误,密码不匹配。这是由于测试数据库本身不包含用户名和密码。
编辑1:
下面我尝试为登录 api 编写测试用例,当我在任何类之外编写测试时,它不会给出任何错误,但是当我创建类 class TestCase(TestCase):并定义方法时def test_login(self):。它给出了密码不匹配,但在相同的代码之外成功运行。
from django.test import TestCase
from django.test import Client
import json
#Creating test out side class
credential=dict()
c =Client()
credential["username"]="john"
credential["password"]="xxx"
response =c.put('/api/login', data=json.dumps(credential))
print("content")
print(response.content)
"""
{"message": "", "result": {"username": "john", "session_key": "xyz"}, "error": 0}
"""
print("session_key")
content = json.loads(response.content)
key = content['result']['session_key']
print key
#Creating test inside class
class TestCase(TestCase):
def test_login(self):
User.objects.create(username="john", password="xxx")
credential=dict()
c =Client()
credential["username"]="john"
credential["password"]="xxx"
response =c.put('/api/login', data=json.dumps(credential))
content=json.loads(response.content)
print 'content'
print content
{u'message': u'Username and Password mismatch', u'result': {}, u'error': 1}
在这里我们可以看到消息是成功的不同格式
{"message": "", "result": {"username": "john", "session_key": "xyz"}, "error": 0}
对于不成功
{u'message': u'username=john and password=xxx Username and Password mismatch', u'result': {}, u'error': 1}。
login api 编写如下,但是当我在类 Test_login 中定义 test 时,它不是if part而是 go in else part。在其他部分,我尝试打印用户名和密码以及响应。我收到了类似的回应{u'message': u'username=john and password=xxx Username and Password mismatch', u'result': {}, u'error': 1}
在这里我们可以看到用户名和密码是正确的。为什么它不去if part。我在网上研究得到类似的问题。它提到了同样的问题。因此,据我所知,用户名和密码是正确的。怎么了 ?
后台登录接口
user = auth.authenticate(username=username, password=password)
if user is not None:
print 'if user is not None:'
msg=Some response
else:
msg = strtemp+' username='+username+' and password='+password+' Username and Password mismatch'
因此,据我所知,用户名和密码是正确的。怎么了 ?