Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test client redirects logged users to login page #12676

Open
danilociaffi opened this issue Oct 22, 2024 · 2 comments
Open

Test client redirects logged users to login page #12676

danilociaffi opened this issue Oct 22, 2024 · 2 comments

Comments

@danilociaffi
Copy link

Expected Behavior

When I log in through the test client, the following requests should be authenticated.

Actual Behavior

Whenever I try to use the client login in Django tests, it gets ignored and I get a redirect to the login page.
After a few test I noticed that it logs me in correctly, but kicks me out when I use it (see below).

Steps to Reproduce the Problem

Here is a stripped down version of my code that still reproduces this.

from django.auth import get_user
from django.test import TestCase
from django.urls import reverse

class MapListViewTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        # Create a user
        cls.user = get_user_model().objects.create(username="testuser")
        cls.user.set_password("password")
        cls.user.save()

    def setUp(self):
        self.client.login(
            username=self.user.get_username(), password=self.user.password
        )

    def test_map_list_view_renders(self):
        """Test that the MapListView renders successfully and includes a list of maps."""
        logger.info(get_user(self.client)) # <-- <django.contrib.auth.models.AnonymousUser object at 0x7377d5589540>
        
        self.client.login(
            username=self.user.get_username(), password=self.user.password
        )
        logger.info(get_user(self.client)) # <-- <Profile: testuser>

        response = self.client.get(reverse("map_list"), follow=True)
        logger.info(get_user(self.client)) # <-- <django.contrib.auth.models.AnonymousUser object at 0x7377d5589540>

        self.assertEqual(response.status_code, 200) # <-- OK
        self.assertTemplateUsed(response, "my_maps/map_list.html") # <-- lands on 'account/login.html'

I don't know if this is of any relevance, but the redirection changes the address from http://localhost/... to http://testserver/....
Also, this works correctly on vanilla django with the built-in user model.

Specifications

  • GeoNode version: 4.3.x
  • Installation type (vanilla, geonode-project): geonode-project
  • Installation method (manual, docker): docker
  • Platform: Ubuntu
  • Additional details:
@mattiagiupponi
Copy link
Contributor

inherit the your MapListViewTest from GeoNodeBaseTestSupport instead of TestCase.
The support class provided will take care of preparing the environment to run tests with GeoNode.
If still does not works, you can always use the self.client.force_login(self.user).

@danilociaffi
Copy link
Author

It worked, thank you. Is there any documentation I missed about this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants