@@ -611,3 +611,80 @@ func TestValidateMasterAuthConfig(t *testing.T) {
611
611
}
612
612
}
613
613
}
614
+
615
+ var testMetadataContent = []byte (`{
616
+ "issuer": "https://127.0.0.1/",
617
+ "authorization_endpoint": "https://127.0.0.1/",
618
+ "token_endpoint": "https://127.0.0.1/",
619
+ "scopes_supported": ["openid", "profile", "email", "address", "phone", "offline_access"],
620
+ "response_types_supported": ["code", "code token"],
621
+ "grant_types_supported": ["authorization_code", "implicit"],
622
+ "code_challenge_methods_supported": ["plain", "S256"]}` )
623
+
624
+ func TestValidateExternalOAuthConfig (t * testing.T ) {
625
+ metadataFile , err := ioutil .TempFile ("" , "oauth.metadata" )
626
+ if err != nil {
627
+ t .Fatalf ("unexpected error: %v" , err )
628
+ }
629
+ defer os .Remove (metadataFile .Name ())
630
+ ioutil .WriteFile (metadataFile .Name (), testMetadataContent , os .FileMode (0644 ))
631
+ badMetadataFile , err := ioutil .TempFile ("" , "badoauth.metadata" )
632
+ if err != nil {
633
+ t .Fatalf ("unexpected error: %v" , err )
634
+ }
635
+ defer os .Remove (badMetadataFile .Name ())
636
+ ioutil .WriteFile (badMetadataFile .Name (), []byte ("bad file" ), os .FileMode (0644 ))
637
+ testCases := []struct {
638
+ name string
639
+ config * configapi.ExternalOAuthConfig
640
+ errors []string
641
+ }{
642
+ {
643
+ name : "No Metadata file" ,
644
+ config : & configapi.ExternalOAuthConfig {
645
+ MetadataFile : "NoFile" ,
646
+ MasterPublicURL : "https://127.0.0.1/" ,
647
+ AssetPublicURL : "https://127.0.0.1/" ,
648
+ },
649
+ errors : []string {"Metadata validation failed: Unable to read External OAuth Metadata file: open NoFile: no such file or directory" },
650
+ },
651
+ {
652
+ name : "Bad Metadata file" ,
653
+ config : & configapi.ExternalOAuthConfig {
654
+ MetadataFile : badMetadataFile .Name (),
655
+ MasterPublicURL : "https://127.0.0.1/" ,
656
+ AssetPublicURL : "https://127.0.0.1/" ,
657
+ },
658
+ errors : []string {"Metadata validation failed: Unable to decode External OAuth Metadata file: invalid character 'b' looking for beginning of value" },
659
+ },
660
+ {
661
+ name : "Bad Master Public URL" ,
662
+ config : & configapi.ExternalOAuthConfig {
663
+ MetadataFile : metadataFile .Name (),
664
+ MasterPublicURL : "bad" ,
665
+ AssetPublicURL : "https://127.0.0.1/" ,
666
+ },
667
+ errors : []string {"must contain a scheme (e.g. https://)" , "must contain a host" },
668
+ },
669
+ {
670
+ name : "Bad Asset Public URL" ,
671
+ config : & configapi.ExternalOAuthConfig {
672
+ MetadataFile : metadataFile .Name (),
673
+ MasterPublicURL : "https://127.0.0.1/" ,
674
+ AssetPublicURL : "bad" ,
675
+ },
676
+ errors : []string {"must contain a scheme (e.g. https://)" , "must contain a host" },
677
+ },
678
+ }
679
+ for _ , test := range testCases {
680
+ results := ValidateExternalOAuthConfig (test .config , nil )
681
+ actualErrors := sets .NewString ()
682
+ expectedErrors := sets .NewString (test .errors ... )
683
+ for i := range results .Errors {
684
+ actualErrors .Insert (results .Errors [i ].Detail )
685
+ }
686
+ if ! expectedErrors .Equal (actualErrors ) {
687
+ t .Errorf ("Expected errors: %v, actual errors: %v" , expectedErrors , actualErrors )
688
+ }
689
+ }
690
+ }
0 commit comments