Edit2: I got Why I am getting error, password miss-match. It is due to test database does not contain username and password itself.
Edit1:
Following I am tried to write test cases for login api, when I write test out-side any class then it does not gives any error, But when I created class class TestCase(TestCase): and define method def test_login(self):. It gives password miss-match, but outside same code running successfully.
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} Here we can see message are different format for successful
{"message": "", "result": {"username": "john", "session_key": "xyz"}, "error": 0}
For Unsuccessful
{u'message': u'username=john and password=xxx Username and Password mismatch', u'result': {}, u'error': 1}.
login api is written like following, but when I define test inside class Test_login it is not going if part but go in else part. In else part I tried to print username and password with response. I am getting response like {u'message': u'username=john and password=xxx Username and Password mismatch', u'result': {}, u'error': 1}
Here we can see username and password are correct. Why it is not going if part. I research on net get similar problem. It had mentioned same problem. So from what I see, username and password are correct. What's wrong ?
Backend Login api
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' So from what I see, username and password are correct. What's wrong ?