Beispiel #1
0
    def test_set_request_with_custom_user(self):

        factory = RequestFactory()
        request = factory.get("/test")
        request.META['HTTP_USER_AGENT'] = "test-browser"
        request.META['HTTP_REFERER'] = "google.com"
        profile = ProfilingRecord()

        profile.set_request(request)
        self.assertEqual(profile.request, request)
        self.assertEqual(profile.http_method, request.method)
        self.assertEqual(profile.request_uri, request.path)
        # for some reason user-agent is a tuple - need to read specs!
        self.assertEqual(profile.http_user_agent, "test-browser")
        self.assertEqual(profile.http_referer, "google.com")
        self.assertEqual(profile.session_key, "")
        self.assertEqual(profile.user, None)

        # test that we can set the session
        request.session = MockSession("test-session-key")
        profile = ProfilingRecord().set_request(request)
        self.assertEqual(profile.session_key, "test-session-key")

        # test that we can set the custom user
        request.user = CustomUser.objects.create_user(
            mobile_number="+886-999888777", password="******")
        profile = ProfilingRecord().set_request(request)
        self.assertEqual(profile.user, request.user)

        # but we do not save anonymous users
        request.user = AnonymousUser()
        profile = ProfilingRecord().set_request(request)
        self.assertEqual(profile.user, None)
Beispiel #2
0
 def test_elapsed(self):
     profile = ProfilingRecord()
     with self.assertRaises(AssertionError):
         profile.elapsed
     profile.start()
     self.assertIsNotNone(profile.elapsed)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
Beispiel #3
0
 def test_elapsed(self):
     profile = ProfilingRecord()
     with self.assertRaises(AssertionError):
         profile.elapsed
     profile.start()
     self.assertIsNotNone(profile.elapsed)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
Beispiel #4
0
 def test_stop(self):
     profile = ProfilingRecord()
     self.assertRaises(AssertionError, profile.stop)
     profile.start().stop()
     self.assertIsNotNone(profile.start_ts)
     self.assertIsNotNone(profile.end_ts)
     self.assertIsNotNone(profile.duration)
     self.assertTrue(profile.duration > 0)
Beispiel #5
0
 def test_stop(self):
     profile = ProfilingRecord()
     self.assertRaises(AssertionError, profile.stop)
     profile.start().stop()
     self.assertIsNotNone(profile.start_ts)
     self.assertIsNotNone(profile.end_ts)
     self.assertIsNotNone(profile.duration)
     self.assertTrue(profile.duration > 0)
Beispiel #6
0
 def test_start(self):
     profile = ProfilingRecord().start()
     self.assertIsNotNone(profile.start_ts)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
     # now check again to see that end and duration are cleared
     profile.end_ts = datetime.datetime.utcnow()
     profile.duration = 1
     profile.start()
     self.assertIsNotNone(profile.start_ts)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
Beispiel #7
0
 def test_cancel(self):
     profile = ProfilingRecord().cancel()
     self.assertIsNone(profile.start_ts)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
     self.assertTrue(profile.is_cancelled)
     # same thing, but this time post-start
     profile = ProfilingRecord().start().cancel()
     self.assertIsNone(profile.start_ts)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
     self.assertTrue(profile.is_cancelled)
Beispiel #8
0
    def test_capture(self):
        # repeat, but this time cancel before capture
        profile = ProfilingRecord()
        response = MockResponse(200)
        profile.start().set_response(response).capture()
        self.assertIsNotNone(profile.start_ts)
        self.assertIsNotNone(profile.end_ts)
        self.assertIsNotNone(profile.duration)
        self.assertIsNotNone(profile.id)
        self.assertEqual(response['X-Profiler-Duration'], profile.duration)

        profile = ProfilingRecord().cancel().capture()
        self.assertIsNone(profile.start_ts)
        self.assertIsNone(profile.end_ts)
        self.assertIsNone(profile.duration)
        self.assertIsNone(profile.id)
Beispiel #9
0
    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)
Beispiel #11
0
    def test_set_request_with_custom_user(self):

        factory = RequestFactory()
        request = factory.get("/test")
        request.META['HTTP_USER_AGENT'] = "test-browser"
        request.META['HTTP_REFERER'] = "google.com"
        profile = ProfilingRecord()

        profile.set_request(request)
        self.assertEqual(profile.request, request)
        self.assertEqual(profile.http_method, request.method)
        self.assertEqual(profile.request_uri, request.path)
        # for some reason user-agent is a tuple - need to read specs!
        self.assertEqual(profile.http_user_agent, "test-browser")
        self.assertEqual(profile.http_referer, "google.com")
        self.assertEqual(profile.session_key, "")
        self.assertEqual(profile.user, None)

        # test that we can set the session
        request.session = MockSession("test-session-key")
        profile = ProfilingRecord().set_request(request)
        self.assertEqual(profile.session_key, "test-session-key")

        # test that we can set the custom user
        request.user = CustomUser.objects.create_user(
            mobile_number="+886-999888777",
            password="******"
        )
        profile = ProfilingRecord().set_request(request)
        self.assertEqual(profile.user, request.user)

        # but we do not save anonymous users
        request.user = AnonymousUser()
        profile = ProfilingRecord().set_request(request)
        self.assertEqual(profile.user, None)
Beispiel #12
0
 def test_start(self):
     profile = ProfilingRecord().start()
     self.assertIsNotNone(profile.start_ts)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
     # now check again to see that end and duration are cleared
     profile.end_ts = datetime.datetime.utcnow()
     profile.duration = 1
     profile.start()
     self.assertIsNotNone(profile.start_ts)
     self.assertIsNone(profile.end_ts)
     self.assertIsNone(profile.duration)
Beispiel #13
0
 def test_default_properties(self):
     profile = ProfilingRecord()
     props = [
         ('user', None),
         ('session_key', ""),
         ('start_ts', None),
         ('end_ts', None),
         ('duration', None),
         ('http_method', ""),
         ('request_uri', ""),
         ('remote_addr', ""),
         ('http_user_agent', ""),
         ('http_referer', ""),
         ('view_func_name', ""),
         ('response_status_code', None),
     ]
     for p in props:
         self.assertEqual(getattr(profile, p[0]), p[1])
     self.assertIsNotNone(unicode(profile))
     self.assertIsNotNone(str(profile))
     self.assertIsNotNone(repr(profile))
Beispiel #14
0
    def test_capture(self):
        # repeat, but this time cancel before capture
        profile = ProfilingRecord()
        response = MockResponse(200)
        profile.start().set_response(response).capture()
        self.assertIsNotNone(profile.start_ts)
        self.assertIsNotNone(profile.end_ts)
        self.assertIsNotNone(profile.duration)
        self.assertIsNotNone(profile.id)
        self.assertEqual(response['X-Profiler-Duration'], profile.duration)

        profile = ProfilingRecord().cancel().capture()
        self.assertIsNone(profile.start_ts)
        self.assertIsNone(profile.end_ts)
        self.assertIsNone(profile.duration)
        self.assertIsNone(profile.id)
Beispiel #15
0
    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)
Beispiel #16
0
 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")
Beispiel #17
0
 def test_set_response(self):
     response = MockResponse(200)
     profiler = ProfilingRecord().start().set_response(response)
     self.assertEqual(profiler.response, response)
     self.assertEqual(profiler.response_status_code, 200)
     self.assertEqual(profiler.response_content_length, 13)
 def process_request(self, request):
     """Start profiling."""
     request.profiler = ProfilingRecord().start()