def test_update_access_custom(self): access = MagicMock() attr = MagicMock() update_data = {'custom': attr} result = update_access(access, **update_data) self.assertEqual(result, access) self.assertEqual(access.custom, attr)
def test_update_access_process(self): access = MagicMock() attr = MagicMock() update_data = {'process': attr} result = update_access(access, **update_data) self.assertEqual(result, access) self.assertEqual(access.process, attr)
def test_update_access_response(self): access = MagicMock() attr = MagicMock() update_data = {'response': {}} with patch('audit_tools.audit.models.access.AccessResponse', return_value=attr) as attr_mock: result = update_access(access, **update_data) self.assertEqual(attr_mock.call_count, 1) self.assertEqual(result, access) self.assertEqual(access.response, attr)
def process_response(self, request, response): """Postprocess response. :param request: Http request. :type request: django.http.HttpRequest :param response: Response. :type response: django.http.HttpResponse :return: None """ try: if self._blacklisted: logger.debug("<Process Response> View:%s %s", str(self._view), 'BlackList') elif self._disabled: logger.debug("<Process Response> View:%s %s", str(self._view), 'Disabled') else: logger.debug("<Process Response> View:%s", str(self._view)) if not self._blacklisted and not self._disabled: # Response self._response = self._extract_response_data(response) # Time self._time['response'] = datetime.datetime.now() # Providers custom = {app: f(request) for app, f in self._providers.iteritems()} custom = {k: v for k, v in custom.iteritems() if v is not None and len(v) > 0} # Save Access and Process self._access = update_access(self._access, response=self._response, time=self._time, custom=custom) if not settings.RUN_ASYNC: save_access(self._access) else: save_access.apply_async((self._access, )) logger.info("<Process Response> View:%s", self._view['full_name']) except Exception: logger.exception("<Process Response>") return response