def test_csrf_cookie_with_nevercache(self): """ Test that the CSRF cookie is properly set when using nevercache. """ # Clear the cached value for cache_installed and initialize nevercache cache_installed.cache_clear() initialize_nevercache() # Test uses an authenticated user as the middleware behavior differs self.client.login(username=self._username, password=self._password) response = self.client.get("/nevercache_view/") # CSRF token is expected to be rendered self.assertEqual(response.status_code, 200) self.assertContains(response, "csrfmiddlewaretoken") # The CSRF cookie should be present csrf_cookie = response.cookies.get(settings.CSRF_COOKIE_NAME, False) self.assertNotEqual(csrf_cookie, False)
def tearDownClass(cls): # Initialize nevercache again now that @override_settings is finished cache_installed.cache_clear() initialize_nevercache()