github.com/greenpau/go-authcrunch@v1.1.4/pkg/idp/saml/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 saml 16 17 import ( 18 "fmt" 19 "github.com/greenpau/go-authcrunch/internal/tests" 20 "github.com/greenpau/go-authcrunch/pkg/errors" 21 logutil "github.com/greenpau/go-authcrunch/pkg/util/log" 22 "go.uber.org/zap" 23 "testing" 24 ) 25 26 func TestNewIdentityProvider(t *testing.T) { 27 testcases := []struct { 28 name string 29 config *Config 30 logger *zap.Logger 31 want map[string]interface{} 32 shouldErr bool 33 errPhase string 34 err error 35 }{ 36 { 37 name: "jumpcloud saml provider", 38 config: &Config{ 39 Name: "jumpcloud", 40 Realm: "jumpcloud", 41 Driver: "generic", 42 IdpMetadataLocation: "../../../testdata/saml/jumpcloud/JumpCloud-saml2-metadata.xml", 43 IdpSignCertLocation: "../../../testdata/saml/jumpcloud/certificate.pem", 44 IdpLoginURL: "https://sso.jumpcloud.com/saml2/authp", 45 ApplicationName: "Auth Portal", 46 EntityID: "urn:authp", 47 AssertionConsumerServiceURLs: []string{ 48 "https://localhost/saml/jumpcloud", 49 }, 50 }, 51 logger: logutil.NewLogger(), 52 want: map[string]interface{}{ 53 "name": "jumpcloud", 54 "kind": "saml", 55 "realm": "jumpcloud", 56 "config": map[string]interface{}{ 57 "acs_urls": []interface{}{ 58 "https://localhost/saml/jumpcloud", 59 }, 60 "application_name": "Auth Portal", 61 "entity_id": "urn:authp", 62 "idp_login_url": "https://sso.jumpcloud.com/saml2/authp", 63 "idp_metadata_location": "../../../testdata/saml/jumpcloud/JumpCloud-saml2-metadata.xml", 64 "idp_sign_cert_location": "../../../testdata/saml/jumpcloud/certificate.pem", 65 "name": "jumpcloud", 66 "driver": "generic", 67 "realm": "jumpcloud", 68 "login_icon": map[string]interface{}{ 69 "class_name": "lab la-codepen la-2x", 70 "color": "white", 71 "background_color": "#324960", 72 "text_color": "#37474f", 73 }, 74 }, 75 }, 76 }, 77 { 78 name: "azure saml provider", 79 config: &Config{ 80 Name: "azure", 81 Realm: "azure", 82 Driver: "azure", 83 IdpMetadataLocation: "../../../testdata/saml/azure/metadata.xml", 84 IdpSignCertLocation: "../../../testdata/saml/azure/certificate.pem", 85 TenantID: "1b9e886b-8ff2-4378-b6c8-6771259a5f51", 86 ApplicationID: "623cae7c-e6b2-43c5-853c-2059c9b2cb58", 87 ApplicationName: "My Gatekeeper", 88 EntityID: "urn:caddy:mygatekeeper", 89 AssertionConsumerServiceURLs: []string{ 90 "https://localhost/auth/saml/azure", 91 }, 92 }, 93 logger: logutil.NewLogger(), 94 want: map[string]interface{}{ 95 "name": "azure", 96 "kind": "saml", 97 "realm": "azure", 98 "config": map[string]interface{}{ 99 "acs_urls": []interface{}{ 100 "https://localhost/auth/saml/azure", 101 }, 102 103 "application_id": "623cae7c-e6b2-43c5-853c-2059c9b2cb58", 104 "application_name": "My Gatekeeper", 105 "entity_id": "urn:caddy:mygatekeeper", 106 "idp_login_url": "https://account.activedirectory.windowsazure.com/applications/signin/My Gatekeeper/623cae7c-e6b2-43c5-853c-2059c9b2cb58?tenantId=1b9e886b-8ff2-4378-b6c8-6771259a5f51", 107 "idp_metadata_location": "../../../testdata/saml/azure/metadata.xml", 108 "idp_sign_cert_location": "../../../testdata/saml/azure/certificate.pem", 109 "name": "azure", 110 "driver": "azure", 111 "realm": "azure", 112 "tenant_id": "1b9e886b-8ff2-4378-b6c8-6771259a5f51", 113 "login_icon": map[string]interface{}{ 114 "class_name": "lab la-windows la-2x", 115 "color": "white", 116 "background_color": "#03a9f4", 117 "text": "Azure", 118 "text_color": "#37474f", 119 }, 120 }, 121 }, 122 }, 123 { 124 name: "test nil logger", 125 config: &Config{ 126 Realm: "azure", 127 }, 128 shouldErr: true, 129 errPhase: "initialize", 130 err: errors.ErrIdentityProviderConfigureLoggerNotFound, 131 }, 132 { 133 name: "test invalid config", 134 config: &Config{ 135 Realm: "azure", 136 }, 137 logger: logutil.NewLogger(), 138 shouldErr: true, 139 errPhase: "initialize", 140 err: errors.ErrIdentityProviderConfigureNameEmpty, 141 }, 142 } 143 for _, tc := range testcases { 144 t.Run(tc.name, func(t *testing.T) { 145 got := make(map[string]interface{}) 146 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 147 msgs = append(msgs, fmt.Sprintf("config:\n%v", tc.config)) 148 149 prv, err := NewIdentityProvider(tc.config, tc.logger) 150 if tc.errPhase == "initialize" { 151 if tests.EvalErrWithLog(t, err, "NewIdentityProvider", tc.shouldErr, tc.err, msgs) { 152 return 153 } 154 } else { 155 if tests.EvalErrWithLog(t, err, "NewIdentityProvider", false, nil, msgs) { 156 return 157 } 158 } 159 160 err = prv.Configure() 161 if tc.errPhase == "configure" { 162 if tests.EvalErrWithLog(t, err, "IdentityProvider.Configure", tc.shouldErr, tc.err, msgs) { 163 return 164 } 165 } else { 166 if tests.EvalErrWithLog(t, err, "IdentityProvider.Configure", false, nil, msgs) { 167 return 168 } 169 } 170 171 got["name"] = prv.GetName() 172 got["realm"] = prv.GetRealm() 173 got["kind"] = prv.GetKind() 174 got["config"] = prv.GetConfig() 175 176 tests.EvalObjectsWithLog(t, "IdentityProvider", tc.want, got, msgs) 177 }) 178 } 179 }