def test_match_rules(self): # rule1 - to match all users r1 = RuleSet() self.assertTrue(r1.match_user(self.anon)) request = self.factory.get('/') request.user = self.anon self.assertTrue(r1.match_uri, request.path) middleware = ProfilingMiddleware() self.assertEqual(middleware.match_rules(request, [r1]), [r1]) # now change the uri_regex so we no longer get a match r1.uri_regex = "^xyz$" self.assertEqual(middleware.match_rules(request, [r1]), []) # now change the user_groups so we no longer get a match request.user = self.bob r1.uri_regex = "" r1.user_filter_type = RuleSet.USER_FILTER_GROUP r1.user_group_filter = "test" self.assertEqual(middleware.match_rules(request, [r1]), []) # add bob to the group self.bob.groups.add(self.test_group) self.assertEqual(middleware.match_rules(request, [r1]), [r1])
def test_global_exclude_function(self): # set the func to ignore everything RuleSet().save() request = self.factory.get('/') request.profiler = ProfilingRecord().start() middleware = ProfilingMiddleware() # process normally, record is saved. middleware.process_response(request, MockResponse(200)) self.assertIsNotNone(request.profiler.id) # NB for some reason (prb. due to imports, the standard # 'override_settings' decorator doesn't work here.) settings.GLOBAL_EXCLUDE_FUNC = lambda x: False request.profiler = ProfilingRecord().start() # process now, and profiler is cancelled middleware.process_response(request, MockResponse(200)) self.assertFalse(hasattr(request, 'profiler')) settings.GLOBAL_EXCLUDE_FUNC = lambda x: True
def test_process_response(self): request = self.factory.get('/') middleware = ProfilingMiddleware() with self.assertRaises(AssertionError): middleware.process_response(request, None) # try no matching rules request.profiler = ProfilingRecord().start() response = middleware.process_response(request, MockResponse(200)) self.assertEqual(response.status_code, 200) self.assertFalse(hasattr(request, 'profiler')) # try matching a rule, and checking response values r1 = RuleSet() r1.save() request.profiler = ProfilingRecord().start() response = middleware.process_response(request, MockResponse(200)) self.assertIsNotNone(response) self.assertTrue(request.profiler.response_status_code, response.status_code) self.assertTrue(response['X-Profiler-Duration'], request.profiler.duration)
def test_process_response_signal_cancellation(self): request = self.factory.get('/') request.profiler = ProfilingRecord().start() middleware = ProfilingMiddleware() # try matching a rule, anc checking response values r1 = RuleSet() r1.save() self.signal_received = False def on_request_profile_complete(sender, **kwargs): self.signal_received = True kwargs.get('instance').cancel() request_profile_complete.connect(on_request_profile_complete) middleware.process_response(request, MockResponse(200)) # because we returned False from the signal receiver, # we should have stopped profiling. self.assertTrue(self.signal_received) # because we called cancel(), the record is not saved. self.assertIsNone(request.profiler.id)
def test_process_view(self): request = self.factory.get('/') request.profiler = ProfilingRecord() ProfilingMiddleware().process_view(request, dummy_view_func, [], {}) self.assertEqual(request.profiler.view_func_name, "dummy_view_func")
def test_process_request(self): request = self.factory.get('/') ProfilingMiddleware().process_request(request) # this implicitly checks that the profile is attached, # and that start() has been called. self.assertIsNotNone(request.profiler.elapsed)