Wednesday, March 4, 2020

Django LiveServerTestCase - Unable to force_login in multiple tests

Versions

  • Django version: 1.9
  • Python: 3.5
  • Django Rest Framework: 3.5.4

Error Observed:

Error  Traceback (most recent call last):    File "/Users/ds/git/some_repo/integration/test_multiple_login.py", line 32, in test_two      self.client.force_login(self.user)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/test/client.py", line 608, in force_login      self._login(user)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/test/client.py", line 621, in _login      login(request, user)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/__init__.py", line 117, in login      user_logged_in.send(sender=user.__class__, request=request, user=user)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 192, in send      response = receiver(signal=self, sender=sender, **named)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/models.py", line 23, in update_last_login      user.save(update_fields=['last_login'])    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 74, in save      super(AbstractBaseUser, self).save(*args, **kwargs)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py", line 708, in save      force_update=force_update, update_fields=update_fields)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py", line 736, in save_base      updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)    File "/Users/ds/.pyenv/versions/3.5.7/envs/work_env/lib/python3.5/site-packages/django/db/models/base.py", line 805, in _save_table      raise DatabaseError("Save with update_fields did not affect any rows.")  django.db.utils.DatabaseError: Save with update_fields did not affect any rows.

Sample Code to reproduce this:

from django.test import TestCase, LiveServerTestCase    from paths.factories import UserFactory      class ATestClass(LiveServerTestCase):        @classmethod      def setUpClass(cls):          """          Test Data common across all tests          """            super().setUpClass()            cls.user = UserFactory(is_staff=False, is_superuser=False)        def test_one(self):          """          Login and then do some actions          """          self.client.force_login(self.user)            # do something          self.assertTrue(True)        def test_two(self):          """          Login and do some actions          """            self.client.force_login(self.user)            # do something          self.assertFalse(False)


What worked?

  • If I replace LiveServerTestCase with TestCase it works as expected. However, since I need access to live_server_url I need to inherit from LiveServerTestCase for my use case

Why LiveServerTestCase?

  • My Test class needs access to live_server_url.

What have I tried?

  • I tried to move force_login statement to setUp method instead of doing the login within the test method - doesn't help
  • I tried disconnecting the update_last_login signal user_logged_in.disconnect(update_last_login) - While the signal disconnected, and this allowed the test to progress (i.e. force_login statement in the second test went through without any runtime errors, but actual login failed and the API I was testing, returned 403 forbidden
  • I tried setting last_login attribute on self.user object to None before the force_login statement - doesn't help.

But Django version is old

  • I know, but for reasons outside of my control, I am not in a position to upgrade Django version. I am looking for some workable patch in my own tests that I can use to get my use case working.
So any steps to help me patch this from within my tests would be greatly helpful.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1d005cef-af43-46a5-89d3-a9b7f285a16d%40googlegroups.com.

No comments:

Post a Comment