Why is user.last_login
None
in the following test:
from django.test import TestCase, Client
class TestModels(TestCase):
@classmethod
def setUpTestData(self):
self.user = User.objects.create(
username="testUser", password="testPassword"
)
self.auth_client = Client()
self.auth_client.login(username="testUser", password="testPassword")
def test_user_last_login(self):
user = User.objects.get(id=self.user.id) # pull the latest data
print(user.last_login). # <-- this is None, why?!
self.assertIsNotNone(user.last_login)
I am clearly logging in, then querying the user from the database to refresh the data.
CodePudding user response:
last_login
is not updated because you are not logging in. All User passwords are encrypted, so when you are creating user like that, his password will be in plain text and you won't be able to authenticate by it. To create User with enrypted password, change
self.user = User.objects.create(
username="testUser", password="testPassword"
)
to
self.user = User.objects.create_user(
username="testUser", password="testPassword"
)
and your tests will pass