示例#1
0
 def test_middleware(self):
     for middleware, vehicle in [(PinningSessionMiddleware(), 'session'),
                                 (PinningCookieMiddleware(), 'cookie')]:
         # The first request shouldn't pin the database
         middleware.process_request(self.mock_request)
         self.assertFalse(pinning.thread_is_pinned())
         
         # A simulated write also shouldn't, if the request isn't a POST
         pinning.set_db_write()
         middleware.process_request(self.mock_request)
         self.assertFalse(pinning.thread_is_pinned())
         
         # This response should set the session variable and clear the pin
         pinning.set_db_write()
         self.mock_request.method = 'POST'
         response = middleware.process_response(self.mock_request,
                                                self.mock_response)
         self.assertFalse(pinning.thread_is_pinned())
         self.assertFalse(pinning.db_was_written())
         if vehicle == 'session':
             self.assertTrue(
                 self.mock_request.session.get(PINNING_KEY, False)
             )
         else:
             self.assertEqual(response.cookie, PINNING_KEY)
             self.mock_request.COOKIES = [response.cookie]
         
         # The subsequent request should then pin the database
         middleware.process_request(self.mock_request)
         self.assertTrue(pinning.thread_is_pinned())
         
         pinning.unpin_thread()
         
         if vehicle == 'session':
             # After the pinning period has expired, the request should no
             # longer pin the thread
             exp = timedelta(seconds=PINNING_SECONDS - 5)
             self.mock_request.session[PINNING_KEY] = datetime.now() - exp
             middleware.process_request(self.mock_request)
             self.assertFalse(pinning.thread_is_pinned())
             
             pinning.unpin_thread()
示例#2
0
    def test_middleware(self):
        for middleware, vehicle in [(PinningSessionMiddleware(), 'session'),
                                    (PinningCookieMiddleware(), 'cookie')]:
            # The first request shouldn't pin the database
            middleware.process_request(self.mock_request)
            self.assertFalse(pinning.thread_is_pinned())

            # A simulated write also shouldn't, if the request isn't a POST
            pinning.set_db_write()
            middleware.process_request(self.mock_request)
            self.assertFalse(pinning.thread_is_pinned())

            # This response should set the session variable and clear the pin
            pinning.set_db_write()
            self.mock_request.method = 'POST'
            response = middleware.process_response(self.mock_request,
                                                   self.mock_response)
            self.assertFalse(pinning.thread_is_pinned())
            self.assertFalse(pinning.db_was_written())
            if vehicle == 'session':
                self.assertTrue(
                    self.mock_request.session.get(PINNING_KEY, False)
                )
            else:
                self.assertEqual(response.cookie, PINNING_KEY)
                self.mock_request.COOKIES = [response.cookie]

            # The subsequent request should then pin the database
            middleware.process_request(self.mock_request)
            self.assertTrue(pinning.thread_is_pinned())

            pinning.unpin_thread()

            if vehicle == 'session':
                # After the pinning period has expired, the request should no
                # longer pin the thread
                exp = timedelta(seconds=PINNING_SECONDS - 5)
                self.mock_request.session[PINNING_KEY] = datetime.now() - exp
                middleware.process_request(self.mock_request)
                self.assertFalse(pinning.thread_is_pinned())

                pinning.unpin_thread()
示例#3
0
 def db_for_write(self, model, **hints):
     pinning.set_db_write()
     pinning.pin_thread()
     return super(PinningMixin, self).db_for_write(model, **hints)
示例#4
0
 def db_for_write(self, model, **hints):
     pinning.set_db_write()
     pinning.pin_thread()
     return super(PinningMixin, self).db_for_write(model, **hints)