Example #1
0
    def test_set_django_http_request(self):
        audit_log = DjangoAuditLogger()
        request = self.request_factory.get("/",
                                           SERVER_NAME="localhost",
                                           HTTP_USER_AGENT='test_agent')
        audit_log.set_django_http_request(request)

        self.assertEqual(audit_log.http_request['method'], 'GET')
        self.assertEqual(audit_log.http_request['url'], 'http://localhost/')
        self.assertEqual(audit_log.http_request['user_agent'], 'test_agent')
Example #2
0
    def test_set_django_http_response(self):
        audit_log = DjangoAuditLogger()
        request = self.request_factory.get('/')
        response = View.as_view()(request)
        audit_log.set_django_http_response(response)

        self.assertEqual(audit_log.http_response['status_code'], 405)
        self.assertEqual(audit_log.http_response['reason'],
                         'Method Not Allowed')
        self.assertTrue('Allow' in audit_log.http_response['headers'])
        self.assertTrue('Content-Type' in audit_log.http_response['headers'])
Example #3
0
    def test_set_user_from_request(self):
        user = User.objects.create_user(username='******',
                                        email='*****@*****.**')
        group, _ = Group.objects.get_or_create(name='testgroup')
        group.user_set.add(user)

        request = self.request_factory.get("/")
        request.user = user

        audit_log = DjangoAuditLogger()
        audit_log.set_user_from_request(request, realm='testrealm')

        self.assertEqual(audit_log.user['authenticated'], True)
        self.assertEqual(audit_log.user['provider']['name'], '')
        self.assertEqual(audit_log.user['provider']['realm'], 'testrealm')
        self.assertEqual(audit_log.user['email'], '*****@*****.**')
        self.assertEqual(audit_log.user['roles'], ['testgroup'])
        self.assertEqual(audit_log.user['ip'], '127.0.0.1')
Example #4
0
 def process_request(self, request: HttpRequest) -> None:
     if not hasattr(request,
                    'audit_log') and not self.exempt_request(request):
         audit_log = DjangoAuditLogger()
         audit_log.set_django_http_request(request)
         audit_log.set_user_from_request(request)
         request.audit_log = audit_log
Example #5
0
    def test_get_headers_from_response(self):
        expected_headers = {
            'Header1': 'value1',
            'Header2': 'value2',
            'Header3': 'value3'
        }
        response = HttpResponse()
        for header, value in expected_headers.items():
            response.__setitem__(header, value)

        headers = DjangoAuditLogger()._get_headers_from_response(response)

        # Assert that the header we put in, will come out.
        # Note that the HttpResponse class will add a default header
        # 'content-type'.
        for header, expected_value in expected_headers.items():
            self.assertEqual(headers[header], expected_value)
 def test_get_log_formatter_default(self, mocked_get_log_formatter):
     DjangoAuditLogger().get_log_formatter()
     mocked_get_log_formatter.assert_called_with()
 def test_get_log_formatter(self):
     self.assertEqual(DjangoAuditLogger().get_log_formatter(),
                      get_log_formatter())
 def test_get_log_handler(self, mocked_init_logger):
     self.assertEqual(DjangoAuditLogger().get_log_handler(),
                      get_log_handler())
 def test_get_logger_name_default(self):
     self.assertEqual(DjangoAuditLogger().get_logger_name(), 'audit_log')
Example #10
0
 def test_get_logger_name(self):
     self.assertEqual(DjangoAuditLogger().get_logger_name(),
                      'test_logger_name')