github.com/greenpau/go-authcrunch@v1.1.4/pkg/sso/metadata_test.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sso 16 17 import ( 18 "bytes" 19 "fmt" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 fileutil "github.com/greenpau/go-authcrunch/pkg/util/file" 24 logutil "github.com/greenpau/go-authcrunch/pkg/util/log" 25 "go.uber.org/zap" 26 ) 27 28 func TestGetMetadata(t *testing.T) { 29 testcases := []struct { 30 name string 31 config *SingleSignOnProviderConfig 32 metadataFilePath string 33 disableLogger bool 34 want string 35 shouldErr bool 36 err error 37 }{ 38 { 39 name: "test valid sso provider metadata", 40 metadataFilePath: "../../testdata/sso/authp_saml_metadata.xml", 41 config: &SingleSignOnProviderConfig{ 42 Name: "aws", 43 Driver: "aws", 44 EntityID: "caddy-authp-idp", 45 PrivateKeyPath: "../../testdata/sso/authp_saml.key", 46 CertPath: "../../testdata/sso/authp_saml.crt", 47 Locations: []string{ 48 "https://localhost/apps/sso/aws", 49 "https://127.0.0.1/apps/sso/aws", 50 }, 51 }, 52 want: `{ 53 "name": "aws", 54 "driver": "aws", 55 "config": { 56 "name": "aws", 57 "driver": "aws", 58 "entity_id": "caddy-authp-idp", 59 "private_key_path": "../../testdata/sso/authp_saml.key", 60 "cert_path": "../../testdata/sso/authp_saml.crt", 61 "locations": [ 62 "https://localhost/apps/sso/aws", 63 "https://127.0.0.1/apps/sso/aws" 64 ] 65 } 66 }`, 67 }, 68 } 69 for _, tc := range testcases { 70 t.Run(tc.name, func(t *testing.T) { 71 var logger *zap.Logger 72 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 73 msgs = append(msgs, fmt.Sprintf("config:\n%v", tc.config)) 74 logger = logutil.NewLogger() 75 provider, err := NewSingleSignOnProvider(tc.config, logger) 76 if err != nil { 77 t.Fatalf("failed initializing sso provider: %v", err) 78 } 79 80 want, err := fileutil.ReadFileBytes(tc.metadataFilePath) 81 if err != nil { 82 t.Fatalf("failed reading %q file: %v", tc.metadataFilePath, err) 83 } 84 want = bytes.TrimSpace(want) 85 86 got, err := provider.GetMetadata() 87 88 if err != nil { 89 if !tc.shouldErr { 90 t.Fatalf("expected success, got: %v", err) 91 } 92 if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" { 93 t.Fatalf("unexpected error: %v, want: %v", err, tc.err) 94 } 95 return 96 } 97 if tc.shouldErr { 98 t.Fatalf("unexpected success, want: %v", tc.err) 99 } 100 101 if diff := cmp.Diff(want, got); diff != "" { 102 t.Errorf("provider.GetMetadata() mismatch (-want +got):\n%s", diff) 103 } 104 }) 105 } 106 }