def test_api_request(self): middleware = audit.AuditMiddleware(FakeApp()) self.ENV_HEADERS['REQUEST_METHOD'] = 'GET' req = webob.Request.blank('/foo/bar', environ=self.ENV_HEADERS) with mock.patch('openstack.common.notifier.api.notify') as notify: middleware(req) # Check first notification with only 'request' call_args = notify.call_args_list[0][0] self.assertEqual(call_args[2], 'http.request') self.assertEqual(call_args[3], api.INFO) self.assertEqual(set(call_args[4].keys()), set(['request'])) request = call_args[4]['request'] self.assertEqual(request['PATH_INFO'], '/foo/bar') self.assertEqual(request['REQUEST_METHOD'], 'GET') self.assertIn('CADF_EVENT', request) self.assertEqual(request['CADF_EVENT']['outcome'], 'pending') # Check second notification with request + response call_args = notify.call_args_list[1][0] self.assertEqual(call_args[2], 'http.response') self.assertEqual(call_args[3], api.INFO) self.assertEqual(set(call_args[4].keys()), set(['request', 'response'])) request = call_args[4]['request'] self.assertEqual(request['PATH_INFO'], '/foo/bar') self.assertEqual(request['REQUEST_METHOD'], 'GET') self.assertIn('CADF_EVENT', request) self.assertEqual(request['CADF_EVENT']['outcome'], 'success')
def test_process_response_fail(self): def func_error(self, req, res): raise Exception('error') self.stubs.Set(cadf_api.OpenStackAuditApi, 'mod_audit_event', func_error) middleware = audit.AuditMiddleware(FakeApp()) req = webob.Request.blank('/foo/bar', environ={'REQUEST_METHOD': 'GET'}) middleware.process_response(req, webob.response.Response())