Home > Net >  Unit testing an authenticated user in Django
Unit testing an authenticated user in Django

Time:12-23

I am trying to test access as a logged in user. I'm having trouble with the unit test below which won't work with me.

class ExampleTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.example_user = get_user_model().objects.create_user(
            username='exampleuser',
            email='[email protected]'
        )
        self.example_user.set_password('testing12345')
        self.example_user.save()

    def test_taking_too_much_time(self):
        my_client = self.client
        #my_client = Client()
        my_client.login(username='exampleuser', password='testing12345')
        #my_client.force_login(self.example_user)
        response = my_client.get('/example/link/')
        self.assertEqual(response.status_code, 200)

I have tried everything, but despite various solutions, I receive a message.

AttributeError: 'HttpRequest' object has no attribute 'user'

Can you provide the code of the login view and the view where the error is thrown? – jmangold

From what I can see the login is from the 'django.contrib.auth' module. Below the login url.

path('log/', auth_views.LoginView.as_view(template_name='login-template.html', form_class=login-form), name='log'),

Simplified error-throwing signal that you also asked me for.

from django.contrib.auth.signals import user_logged_in
from folder.models import Person
@receiver(user_logged_in, sender=Person)
def when_logging_in(request, sender, **kwargs):
    messages.info(request, f'{request.user.username} hello.')

Okay could you also provide the whole stacktrace?

I see, that friend has a similar problem. Django - Testing Login View - AttributeError: 'HttpRequest' object has no attribute 'user' After adding the 'user' instance in the argument and replacing with request.

@receiver (user_logged_in, sender = Person)
def when_logging_in (request, sender, user, ** kwargs):
     messages.info (request, f '{user.username} hello.')

The trouble persists and the same thing appears, which is 'MessageFailure'. Unfortunately, it still doesn't work. I am waiting for feedback.

CodePudding user response:

I am not familiar with the Client(), rather we are using the RequestFactory and passing the resultingrequest object to the views directly.

def test_taking_too_much_time(self):
    factory = RequestFactory()
    request= factory.get('/example/link/',)
    request.user = self.user
    response = ExampleLinkView.as_view()(request)
    self.assertEqual(response.status_code, 200)

The login is bypassed, because no middlewares will be applied. You can read more about that here Advanced Testing.

CodePudding user response:

I spent more time on it and checked all the signal code, related functions, dependencies, project settings. There is a lot of it. The culprit is the Client() which serves the backend but has trouble with the HttpRequest.

@receiver (user_logged_in, sender = Person)
def when_logging_in (request, sender, user, ** kwargs):
      messages.info (request, f '{user.username} hello.')

It's okay to use an instance instead of a request, but not for messages where one of the arguments is request. So, where could a message to the user be placed so as not to lose functionality, but to perform unit tests using Client().

  • Related