@@ -9,6 +9,7 @@ use axum::{
9
9
use http_body_util:: BodyExt ;
10
10
use policy_evaluator:: admission_response:: AdmissionResponseStatus ;
11
11
use policy_server:: api:: admission_review:: AdmissionReviewResponse ;
12
+ use regex:: Regex ;
12
13
use tower:: ServiceExt ;
13
14
14
15
#[ tokio:: test]
@@ -252,3 +253,63 @@ async fn test_timeout_protection_reject() {
252
253
)
253
254
) ;
254
255
}
256
+
257
+ #[ tokio:: test]
258
+ async fn test_policy_with_invalid_settings ( ) {
259
+ let app = app ( ) . await ;
260
+
261
+ let request = Request :: builder ( )
262
+ . method ( http:: Method :: POST )
263
+ . header ( header:: CONTENT_TYPE , "application/json" )
264
+ . uri ( "/validate/invalid_settings" )
265
+ . body ( Body :: from ( include_str ! ( "data/pod_sleep_100ms.json" ) ) )
266
+ . unwrap ( ) ;
267
+
268
+ let response = app. oneshot ( request) . await . unwrap ( ) ;
269
+
270
+ assert_eq ! ( response. status( ) , 200 ) ;
271
+
272
+ let admission_review_response: AdmissionReviewResponse =
273
+ serde_json:: from_slice ( & response. into_body ( ) . collect ( ) . await . unwrap ( ) . to_bytes ( ) ) . unwrap ( ) ;
274
+
275
+ assert ! ( !admission_review_response. response. allowed) ;
276
+
277
+ let pattern =
278
+ Regex :: new ( r"Policy settings are invalid:.*Error decoding validation payload.*invalid type: string.*expected u64.*" )
279
+ . unwrap ( ) ;
280
+
281
+ let status = admission_review_response. response . status . unwrap ( ) ;
282
+
283
+ assert_eq ! ( status. code, Some ( 500 ) ) ;
284
+ assert ! ( pattern. is_match( & status. message. unwrap( ) ) ) ;
285
+ }
286
+
287
+ #[ tokio:: test]
288
+ async fn test_policy_with_wrong_url ( ) {
289
+ let app = app ( ) . await ;
290
+
291
+ let request = Request :: builder ( )
292
+ . method ( http:: Method :: POST )
293
+ . header ( header:: CONTENT_TYPE , "application/json" )
294
+ . uri ( "/audit/wrong_url" )
295
+ . body ( Body :: from ( include_str ! ( "data/pod_sleep_100ms.json" ) ) )
296
+ . unwrap ( ) ;
297
+
298
+ let response = app. oneshot ( request) . await . unwrap ( ) ;
299
+
300
+ assert_eq ! ( response. status( ) , 200 ) ;
301
+
302
+ let admission_review_response: AdmissionReviewResponse =
303
+ serde_json:: from_slice ( & response. into_body ( ) . collect ( ) . await . unwrap ( ) . to_bytes ( ) ) . unwrap ( ) ;
304
+
305
+ assert ! ( !admission_review_response. response. allowed) ;
306
+
307
+ let pattern =
308
+ Regex :: new ( r"Error while downloading policy 'wrong_url' from ghcr.io/kubewarden/tests/not_existing:v0.1.0.*" )
309
+ . unwrap ( ) ;
310
+
311
+ let status = admission_review_response. response . status . unwrap ( ) ;
312
+
313
+ assert_eq ! ( status. code, Some ( 500 ) ) ;
314
+ assert ! ( pattern. is_match( & status. message. unwrap( ) ) ) ;
315
+ }
0 commit comments