Beispiel #1
0
class TestSessionMiddleware(TestCase):

    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = ReadTheDocsSessionMiddleware()

        self.user = create_user(username='******', password='******')

    def test_fallback_cookie(self):
        request = self.factory.get('/')
        response = HttpResponse()
        self.middleware.process_request(request)
        request.session['test'] = 'value'
        response = self.middleware.process_response(request, response)

        self.assertTrue(settings.SESSION_COOKIE_NAME in response.cookies)
        self.assertTrue(self.middleware.cookie_name_fallback in response.cookies)

    def test_main_cookie_samesite_none(self):
        request = self.factory.get('/')
        response = HttpResponse()
        self.middleware.process_request(request)
        request.session['test'] = 'value'
        response = self.middleware.process_response(request, response)

        self.assertEqual(response.cookies[settings.SESSION_COOKIE_NAME]['samesite'], 'None')
        self.assertEqual(response.cookies[self.middleware.cookie_name_fallback]['samesite'], '')
    def test_no_session_logged_out(self):
        mid = ReadTheDocsSessionMiddleware()

        # Null session here
        request = self.factory.get('/api/v2/footer_html/')
        mid.process_request(request)
        self.assertIsInstance(request.session, SessionBase)
        self.assertEqual(list(request.session.keys()), [])

        # Proper session here
        home_request = self.factory.get('/')
        mid.process_request(home_request)
        self.assertEqual(home_request.session.TEST_COOKIE_NAME, 'testcookie')
Beispiel #3
0
    def setUp(self):
        self.factory = RequestFactory()
        self.middleware = ReadTheDocsSessionMiddleware()

        self.user = create_user(username='******', password='******')