last day (15 days later) » 

10:02 PM
0
Q: getting a 401 when trying to log in my users

rocky raccoonI'm doing testing and I'm not being authenticated. class BaseTestCase(TestCase): def setUp(self): self.user_ben = User.objects.get(username='Ben312') self.client = Client() self.client.login(email=self.user_ben.email, password=self.user_ben.password) Here I have t...

 
I found this answer, which indicates the middleware needs to have django.contrib.sessions enabled when testing.
 
I got a new error from this, but I feel the is a step in the right direction. I already having django.contrib.sessions inside my INSTALLED_APPS.
@Tony I do want to mention that I have token authentication.
 
The setting needs to go in the MIDDLEWARE = ( ... section of the settings file, not the INSTALLED_APPS. Before Django 1.10 the setting name was MIDDLEWARE_CLASSES
 
I can't do that. I get an error for doing that.
the post you referred me to says to add django.contrib.sessions inside INSTALLED_APPS not middle ware
 
Apologies, I read the comment on that answer regarding a middleware and confused the situation.
Have you checked the result of the Client.login(..) call?
 
10:12 PM
Oh wow, there's a chat for this lol? I keep getting false.
here's the assert error:
"login with username=%r, password=%r failed" % (user, password)
AssertionError: False is not true : login with username=<User: Ben312>, password='pbkdf2_sha256$30000$XvudFaXUyN$muwPouhiuDKd8xTnGKuCnD1Sn+LBd5pOz73z7L1/I=' failed
I'm not sure if I should use the plain text password of the user or the hashed password (e.g user_ben.password)
Wait a minute. I think I just found a type on my part: when I called client.login, I only put the User object as the username and not the user's username. self.client.login(self.ben_user.username, password=self.ben_user.password. all I get now is `self.client.login(self.user_jessica.username, self.user_jessica.password)
TypeError: login() takes 1 positional argument but 3 were given`
whoops, I was using a different account. it should be ben
 
Sorry, had to step away for a moment... have you fixed your problem?
Regarding the login, since you are getting False for the login that would explain the 401 response
But now the question is why does the login fail ;)
I think you are fine passing the password in clear (docs.djangoproject.com/en/1.10/topics/testing/tools/…)
Although I've just realised the example I just posted is passing the variables to a URL, not a direct call to Client.login()
 
I'm still getting the error, despite inputting the username:/ I do have a @login_required and @api_view decorators above my original view that I'm testing, I'm not sure if that's messing it up.
 
You should not need to change your code to make a test pass... the @login_required is there to protect your application.
Which username and password works when you run the app in the browser? Not when you run a test.
Just reading the docs on the Client.login() it says "...the test runner is executed using a test database, which contains no users by default."
Have you created a user as part of your test?
 
10:36 PM
Oh, you might be right. Let me create a user and see. Originally I tried that but I didn't input that user's username into login, only the instance of User.
 

  last day (15 days later) »