8
8
9
9
_X_INTEGRATION = "x-amazon-apigateway-integration"
10
10
_X_ANY_METHOD = 'x-amazon-apigateway-any-method'
11
+ _ALLOW_CREDENTALS_TRUE = "'true'"
11
12
12
13
class TestSwaggerEditor_init (TestCase ):
13
14
@@ -308,18 +309,21 @@ def test_must_add_options_to_new_path(self):
308
309
allowed_headers = ["headers" , "2" ]
309
310
allowed_methods = {"key" : "methods" }
310
311
max_age = 60
312
+ allow_credentials = True
313
+ options_method_response_allow_credentials = True
311
314
path = "/foo"
312
315
expected = {"some cors" : "return value" }
313
316
314
317
self .editor ._options_method_response_for_cors = Mock ()
315
318
self .editor ._options_method_response_for_cors .return_value = expected
316
319
317
- self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age )
320
+ self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age , allow_credentials )
318
321
self .assertEquals (expected , self .editor .swagger ["paths" ][path ]["options" ])
319
322
self .editor ._options_method_response_for_cors .assert_called_with (allowed_origins ,
320
323
allowed_headers ,
321
324
allowed_methods ,
322
- max_age )
325
+ max_age ,
326
+ options_method_response_allow_credentials )
323
327
324
328
def test_must_skip_existing_path (self ):
325
329
path = "/withoptions"
@@ -346,28 +350,33 @@ def test_must_work_for_optional_allowed_headers(self):
346
350
allowed_headers = None # No Value
347
351
allowed_methods = "methods"
348
352
max_age = 60
353
+ allow_credentials = True
354
+ options_method_response_allow_credentials = True
349
355
350
356
expected = {"some cors" : "return value" }
351
357
path = "/foo"
352
358
353
359
self .editor ._options_method_response_for_cors = Mock ()
354
360
self .editor ._options_method_response_for_cors .return_value = expected
355
361
356
- self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age )
362
+ self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age , allow_credentials )
357
363
358
364
self .assertEquals (expected , self .editor .swagger ["paths" ][path ]["options" ])
359
365
360
366
self .editor ._options_method_response_for_cors .assert_called_with (allowed_origins ,
361
367
allowed_headers ,
362
368
allowed_methods ,
363
- max_age )
369
+ max_age ,
370
+ options_method_response_allow_credentials )
364
371
365
372
def test_must_make_default_value_with_optional_allowed_methods (self ):
366
373
367
374
allowed_origins = "origins"
368
375
allowed_headers = "headers"
369
376
allowed_methods = None # No Value
370
377
max_age = 60
378
+ allow_credentials = True
379
+ options_method_response_allow_credentials = True
371
380
372
381
default_allow_methods_value = "some default value"
373
382
default_allow_methods_value_with_quotes = "'{}'" .format (default_allow_methods_value )
@@ -380,7 +389,7 @@ def test_must_make_default_value_with_optional_allowed_methods(self):
380
389
self .editor ._options_method_response_for_cors = Mock ()
381
390
self .editor ._options_method_response_for_cors .return_value = expected
382
391
383
- self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age )
392
+ self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age , allow_credentials )
384
393
385
394
self .assertEquals (expected , self .editor .swagger ["paths" ][path ]["options" ])
386
395
@@ -389,7 +398,29 @@ def test_must_make_default_value_with_optional_allowed_methods(self):
389
398
# Must be called with default value.
390
399
# And value must be quoted
391
400
default_allow_methods_value_with_quotes ,
392
- max_age )
401
+ max_age ,
402
+ options_method_response_allow_credentials )
403
+
404
+ def test_must_accept_none_allow_credentials (self ):
405
+ allowed_origins = "origins"
406
+ allowed_headers = ["headers" , "2" ]
407
+ allowed_methods = {"key" : "methods" }
408
+ max_age = 60
409
+ allow_credentials = None
410
+ options_method_response_allow_credentials = False
411
+ path = "/foo"
412
+ expected = {"some cors" : "return value" }
413
+
414
+ self .editor ._options_method_response_for_cors = Mock ()
415
+ self .editor ._options_method_response_for_cors .return_value = expected
416
+
417
+ self .editor .add_cors (path , allowed_origins , allowed_headers , allowed_methods , max_age , allow_credentials )
418
+ self .assertEquals (expected , self .editor .swagger ["paths" ][path ]["options" ])
419
+ self .editor ._options_method_response_for_cors .assert_called_with (allowed_origins ,
420
+ allowed_headers ,
421
+ allowed_methods ,
422
+ max_age ,
423
+ options_method_response_allow_credentials )
393
424
394
425
395
426
class TestSwaggerEditor_options_method_response_for_cors (TestCase ):
@@ -400,6 +431,7 @@ def test_correct_value_is_returned(self):
400
431
methods = {"a" : "b" }
401
432
origins = [1 ,2 ,3 ]
402
433
max_age = 60
434
+ allow_credentials = True
403
435
404
436
expected = {
405
437
"summary" : "CORS support" ,
@@ -414,10 +446,11 @@ def test_correct_value_is_returned(self):
414
446
"default" : {
415
447
"statusCode" : "200" ,
416
448
"responseParameters" : {
449
+ "method.response.header.Access-Control-Allow-Credentials" : _ALLOW_CREDENTALS_TRUE ,
417
450
"method.response.header.Access-Control-Allow-Headers" : headers ,
418
451
"method.response.header.Access-Control-Allow-Methods" : methods ,
419
452
"method.response.header.Access-Control-Allow-Origin" : origins ,
420
- "method.response.header.Access-Control-Max-Age" : max_age
453
+ "method.response.header.Access-Control-Max-Age" : max_age ,
421
454
},
422
455
"responseTemplates" : {
423
456
"application/json" : "{}\n "
@@ -429,6 +462,9 @@ def test_correct_value_is_returned(self):
429
462
"200" : {
430
463
"description" : "Default response for CORS method" ,
431
464
"headers" : {
465
+ "Access-Control-Allow-Credentials" : {
466
+ "type" : "string"
467
+ },
432
468
"Access-Control-Allow-Headers" : {
433
469
"type" : "string"
434
470
},
@@ -446,20 +482,27 @@ def test_correct_value_is_returned(self):
446
482
}
447
483
}
448
484
449
- actual = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (origins , headers , methods , max_age )
485
+ actual = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (origins , headers ,
486
+ methods , max_age ,
487
+ allow_credentials )
450
488
self .assertEquals (expected , actual )
451
489
452
490
def test_allow_headers_is_skipped_with_no_value (self ):
453
491
headers = None # No value
454
492
methods = "methods"
455
493
origins = "origins"
494
+ allow_credentials = True
456
495
457
496
expected = {
497
+ "method.response.header.Access-Control-Allow-Credentials" : _ALLOW_CREDENTALS_TRUE ,
458
498
"method.response.header.Access-Control-Allow-Methods" : methods ,
459
- "method.response.header.Access-Control-Allow-Origin" : origins
499
+ "method.response.header.Access-Control-Allow-Origin" : origins ,
460
500
}
461
501
462
502
expected_headers = {
503
+ "Access-Control-Allow-Credentials" : {
504
+ "type" : "string"
505
+ },
463
506
"Access-Control-Allow-Methods" : {
464
507
"type" : "string"
465
508
},
@@ -469,7 +512,7 @@ def test_allow_headers_is_skipped_with_no_value(self):
469
512
}
470
513
471
514
options_config = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (
472
- origins , headers , methods )
515
+ origins , headers , methods , allow_credentials = allow_credentials )
473
516
474
517
actual = options_config [_X_INTEGRATION ]["responses" ]["default" ]["responseParameters" ]
475
518
self .assertEquals (expected , actual )
@@ -479,14 +522,16 @@ def test_allow_methods_is_skipped_with_no_value(self):
479
522
headers = "headers"
480
523
methods = None # No value
481
524
origins = "origins"
525
+ allow_credentials = True
482
526
483
527
expected = {
528
+ "method.response.header.Access-Control-Allow-Credentials" : _ALLOW_CREDENTALS_TRUE ,
484
529
"method.response.header.Access-Control-Allow-Headers" : headers ,
485
- "method.response.header.Access-Control-Allow-Origin" : origins
530
+ "method.response.header.Access-Control-Allow-Origin" : origins ,
486
531
}
487
532
488
533
options_config = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (
489
- origins , headers , methods )
534
+ origins , headers , methods , allow_credentials = allow_credentials )
490
535
491
536
actual = options_config [_X_INTEGRATION ]["responses" ]["default" ]["responseParameters" ]
492
537
self .assertEquals (expected , actual )
@@ -495,14 +540,15 @@ def test_allow_origins_is_not_skipped_with_no_value(self):
495
540
headers = None
496
541
methods = None
497
542
origins = None
543
+ allow_credentials = False
498
544
499
545
expected = {
500
546
# We will ALWAYS set AllowOrigin. This is a minimum requirement for CORS
501
547
"method.response.header.Access-Control-Allow-Origin" : origins
502
548
}
503
549
504
550
options_config = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (
505
- origins , headers , methods )
551
+ origins , headers , methods , allow_credentials = allow_credentials )
506
552
507
553
actual = options_config [_X_INTEGRATION ]["responses" ]["default" ]["responseParameters" ]
508
554
self .assertEquals (expected , actual )
@@ -512,19 +558,38 @@ def test_max_age_can_be_set_to_zero(self):
512
558
methods = "methods"
513
559
origins = "origins"
514
560
max_age = 0
561
+ allow_credentials = True
515
562
516
563
expected = {
564
+ "method.response.header.Access-Control-Allow-Credentials" : _ALLOW_CREDENTALS_TRUE ,
517
565
"method.response.header.Access-Control-Allow-Methods" : methods ,
518
566
"method.response.header.Access-Control-Allow-Origin" : origins ,
519
- "method.response.header.Access-Control-Max-Age" : max_age
567
+ "method.response.header.Access-Control-Max-Age" : max_age ,
520
568
}
521
569
522
570
options_config = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (
523
- origins , headers , methods , max_age )
571
+ origins , headers , methods , max_age , allow_credentials )
524
572
525
573
actual = options_config [_X_INTEGRATION ]["responses" ]["default" ]["responseParameters" ]
526
574
self .assertEquals (expected , actual )
527
575
576
+ def test_allow_credentials_is_skipped_with_false_value (self ):
577
+ headers = "headers"
578
+ methods = "methods"
579
+ origins = "origins"
580
+ allow_credentials = False
581
+
582
+ expected = {
583
+ "method.response.header.Access-Control-Allow-Headers" : headers ,
584
+ "method.response.header.Access-Control-Allow-Methods" : methods ,
585
+ "method.response.header.Access-Control-Allow-Origin" : origins ,
586
+ }
587
+
588
+ options_config = SwaggerEditor (SwaggerEditor .gen_skeleton ())._options_method_response_for_cors (
589
+ origins , headers , methods , allow_credentials = allow_credentials )
590
+
591
+ actual = options_config [_X_INTEGRATION ]["responses" ]["default" ]["responseParameters" ]
592
+ self .assertEquals (expected , actual )
528
593
529
594
class TestSwaggerEditor_make_cors_allowed_methods_for_path (TestCase ):
530
595
0 commit comments