github.com/greenpau/go-authcrunch@v1.1.4/pkg/sso/provider_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 "fmt" 19 "github.com/google/go-cmp/cmp" 20 "github.com/greenpau/go-authcrunch/internal/tests" 21 "github.com/greenpau/go-authcrunch/pkg/errors" 22 logutil "github.com/greenpau/go-authcrunch/pkg/util/log" 23 "go.uber.org/zap" 24 "testing" 25 ) 26 27 func TestNewSingleSignOnProvider(t *testing.T) { 28 testcases := []struct { 29 name string 30 config *SingleSignOnProviderConfig 31 disableLogger bool 32 want string 33 shouldErr bool 34 err error 35 }{ 36 { 37 name: "test valid sso provider", 38 config: &SingleSignOnProviderConfig{ 39 Name: "aws", 40 Driver: "aws", 41 EntityID: "caddy-authp-idp", 42 PrivateKeyPath: "../../testdata/sso/authp_saml.key", 43 CertPath: "../../testdata/sso/authp_saml.crt", 44 Locations: []string{ 45 "https://localhost/sso/aws", 46 "https://127.0.0.1/sso/aws", 47 }, 48 }, 49 want: `{ 50 "name": "aws", 51 "driver": "aws", 52 "config": { 53 "name": "aws", 54 "driver": "aws", 55 "entity_id": "caddy-authp-idp", 56 "private_key_path": "../../testdata/sso/authp_saml.key", 57 "cert_path": "../../testdata/sso/authp_saml.crt", 58 "locations": [ 59 "https://localhost/sso/aws", 60 "https://127.0.0.1/sso/aws" 61 ] 62 } 63 }`, 64 }, 65 /* 66 { 67 name: "test saml identity provider", 68 config: &SingleSignOnProviderConfig{ 69 Name: "jumpcloud", 70 Kind: "saml", 71 Params: map[string]interface{}{ 72 "realm": "jumpcloud", 73 "driver": "generic", 74 "idp_metadata_location": "JumpCloud-saml2-metadata.xml", 75 "idp_sign_cert_location": "certificate.pem", 76 "idp_login_url": "https://sso.jumpcloud.com/saml2/authp", 77 "application_name": "Auth Portal", 78 "entity_id": "urn:authp", 79 "acs_urls": []string{ 80 "https://localhost/saml/jumpcloud", 81 }, 82 }, 83 }, 84 want: `{ 85 "name": "jumpcloud", 86 "kind": "saml", 87 "realm": "jumpcloud" 88 }`, 89 }, 90 */ 91 { 92 name: "test logger nil error", 93 config: &SingleSignOnProviderConfig{ 94 Name: "aws", 95 Driver: "aws", 96 EntityID: "caddy-authp-idp", 97 PrivateKeyPath: "/tmp/ssoprivatekey.pem", 98 Locations: []string{ 99 "https://localhost/sso/aws", 100 "https://127.0.0.1/sso/aws", 101 }, 102 }, 103 disableLogger: true, 104 shouldErr: true, 105 err: errors.ErrSingleSignOnProviderConfigureLoggerNotFound, 106 }, 107 108 /* 109 { 110 name: "test config validation error", 111 config: &SingleSignOnProviderConfig{ 112 Kind: "local", 113 Params: map[string]interface{}{ 114 "path": "foo", 115 "realm": "local", 116 }, 117 }, 118 shouldErr: true, 119 err: errors.ErrSingleSignOnProviderConfigInvalid.WithArgs( 120 "empty identity provider name", 121 ), 122 }, 123 */ 124 } 125 for _, tc := range testcases { 126 t.Run(tc.name, func(t *testing.T) { 127 var logger *zap.Logger 128 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 129 msgs = append(msgs, fmt.Sprintf("config:\n%v", tc.config)) 130 if !tc.disableLogger { 131 logger = logutil.NewLogger() 132 } 133 134 provider, err := NewSingleSignOnProvider(tc.config, logger) 135 if err != nil { 136 if !tc.shouldErr { 137 t.Fatalf("expected success, got: %v", err) 138 } 139 if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" { 140 t.Fatalf("unexpected error: %v, want: %v", err, tc.err) 141 } 142 return 143 } 144 if tc.shouldErr { 145 t.Fatalf("unexpected success, want: %v", tc.err) 146 } 147 got := map[string]interface{}{ 148 "name": provider.GetName(), 149 "driver": provider.GetDriver(), 150 "config": provider.GetConfig(), 151 } 152 153 want := tests.Unpack(t, tc.want) 154 155 if diff := cmp.Diff(want, got); diff != "" { 156 t.Logf("JSON: %v", tests.UnpackJSON(t, got)) 157 t.Errorf("NewSingleSignOnProvider() mismatch (-want +got):\n%s", diff) 158 } 159 }) 160 } 161 }