0

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 ?

1
  • 1
    You are not creating any user outside test. Did you tried my answer? Im kind of sure that is the problem. Commented Sep 20, 2015 at 5:38

1 Answer 1

2

I think what is going on is that you are not setting password properly.

As you want to know, django hashes the password of model User. Instead of calling User.objects.create(username="john", password="xxx") all you should do is: User.objects.create_user('john', password='xxx').

Apart from the answer I would like to tell you that you should not call your test class TestCase. Just to keep your code cleaner and probably avoid some future troubles.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.