简体   繁体   中英

Django - Testing Login View - AttributeError: 'HttpRequest' object has no attribute 'user'

I'm trying to test Django Login View with TestCase but I'm getting the following error -

ERROR: test_login_form (accounts.tests.LoginPageTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/accounts/tests.py", line 53, in test_login_form
    logged_in = self.client.force_login(new_user)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/test/client.py", line 619, in force_login
    self._login(user, backend)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/test/client.py", line 631, in _login
    login(request, user, backend)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/contrib/auth/__init__.py", line 135, in login
    user_logged_in.send(sender=user.__class__, request=request, user=user)
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 180, in send
    return [
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/venv/lib/python3.8/site-packages/django/dispatch/dispatcher.py", line 181, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/home/farhan/Documents/_playground_new/django_playground/cbv-dfb/accounts/views.py", line 29, in on_user_logged_out
    f"{request.user.username} successfully logged in!",
AttributeError: 'HttpRequest' object has no attribute 'user'

I have a user_logged_in signal that adds a message by Django messages framework upon logging in. Here is the code -


@receiver(user_logged_in)
def on_user_logged_in(sender, request, **kwargs):
    messages.add_message(
        request,
        messages.INFO,
        f"{request.user.username} successfully logged in!",
    )

And here is the code for the unit test -

class LoginPageTests(TestCase):
    username = "newuser"
    email = "newuser@email.com"
    password = "averydeifficultpasswordtobreak"

    def test_login_page_status_code(self):
        response = self.client.get("/accounts/login/")
        self.assertEqual(response.status_code, 200)

    def test_view_url_by_name(self):
        response = self.client.get(reverse("login"))
        self.assertEqual(response.status_code, 200)

    def test_view_uses_correct_template(self):
        response = self.client.get(reverse("login"))
        self.assertTemplateUsed(response, "registration/login.html")

    def test_login_form(self):
        new_user = get_user_model().objects.create_user(self.username, self.email)
        new_user.set_password(self.password)
        new_user.save()

        logged_in = self.client.login(username=self.username, password=self.password)
        self.assertEqual(logged_in, True)

If I just simply comment out the signal code, the test runs fine. How can I solve this?

The signal sends user as an argument, so use that instead of request.user .

@receiver(user_logged_in)
def on_user_logged_in(sender, request, user, **kwargs):
    messages.add_message(
        request,
        messages.INFO,
        f"{user.username} successfully logged in!",
    )

Note that self.client.login() is using the Django test client, it isn't testing the login form like the test name suggests. To test your login form, you could either instantate the login form and assert it behaves as expected, or you might be happy with an integration test that posts login details and asserts that the view logs in the user when the details are correct.

I am working on tests and have encountered a similar problem. You keep getting the MessageFailure error because you are still using the HttpRequest in the signal . The culprit is a Client() that handles the backend but has trouble with the message argument ( HttpRequest ).

You can take a look at my topic where I describe such experiences.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM