github.com/greenpau/go-authcrunch@v1.1.4/pkg/idp/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 idp
    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 TestNewIdentityProvider(t *testing.T) {
    28  	testcases := []struct {
    29  		name          string
    30  		config        *IdentityProviderConfig
    31  		disableLogger bool
    32  		want          string
    33  		shouldErr     bool
    34  		err           error
    35  	}{
    36  		{
    37  			name: "test oauth identity provider",
    38  			config: &IdentityProviderConfig{
    39  				Name: "contoso",
    40  				Kind: "oauth",
    41  				Params: map[string]interface{}{
    42  					"driver":        "generic",
    43  					"realm":         "contoso",
    44  					"client_id":     "foo",
    45  					"client_secret": "bar",
    46  					"base_auth_url": "https://localhost/oauth",
    47  					"metadata_url":  "https://localhost/oauth/.well-known/openid-configuration",
    48  				},
    49  			},
    50  			want: `{
    51  			  "name": "contoso",
    52  			  "kind": "oauth",
    53  			  "realm": "contoso"
    54              }`,
    55  		},
    56  		{
    57  			name: "test saml identity provider",
    58  			config: &IdentityProviderConfig{
    59  				Name: "jumpcloud",
    60  				Kind: "saml",
    61  				Params: map[string]interface{}{
    62  					"realm":                  "jumpcloud",
    63  					"driver":                 "generic",
    64  					"idp_metadata_location":  "JumpCloud-saml2-metadata.xml",
    65  					"idp_sign_cert_location": "certificate.pem",
    66  					"idp_login_url":          "https://sso.jumpcloud.com/saml2/authp",
    67  					"application_name":       "Auth Portal",
    68  					"entity_id":              "urn:authp",
    69  					"acs_urls": []string{
    70  						"https://localhost/saml/jumpcloud",
    71  					},
    72  				},
    73  			},
    74  			want: `{
    75  			  "name": "jumpcloud",
    76                "kind": "saml",
    77                "realm": "jumpcloud"
    78  			}`,
    79  		},
    80  		{
    81  			name: "test logger nil error",
    82  			config: &IdentityProviderConfig{
    83  				Name: "default",
    84  				Kind: "local",
    85  				Params: map[string]interface{}{
    86  					"path":  "foo",
    87  					"realm": "local",
    88  				},
    89  			},
    90  			disableLogger: true,
    91  			shouldErr:     true,
    92  			err:           errors.ErrIdentityProviderConfigureLoggerNotFound,
    93  		},
    94  		{
    95  			name: "test config validation error",
    96  			config: &IdentityProviderConfig{
    97  				Kind: "local",
    98  				Params: map[string]interface{}{
    99  					"path":  "foo",
   100  					"realm": "local",
   101  				},
   102  			},
   103  			shouldErr: true,
   104  			err: errors.ErrIdentityProviderConfigInvalid.WithArgs(
   105  				"empty identity provider name",
   106  			),
   107  		},
   108  	}
   109  	for _, tc := range testcases {
   110  		t.Run(tc.name, func(t *testing.T) {
   111  			var logger *zap.Logger
   112  			msgs := []string{fmt.Sprintf("test name: %s", tc.name)}
   113  			msgs = append(msgs, fmt.Sprintf("config:\n%v", tc.config))
   114  			if !tc.disableLogger {
   115  				logger = logutil.NewLogger()
   116  			}
   117  
   118  			st, err := NewIdentityProvider(tc.config, logger)
   119  			if err != nil {
   120  				if !tc.shouldErr {
   121  					t.Fatalf("expected success, got: %v", err)
   122  				}
   123  				if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" {
   124  					t.Fatalf("unexpected error: %v, want: %v", err, tc.err)
   125  				}
   126  				return
   127  			}
   128  			if tc.shouldErr {
   129  				t.Fatalf("unexpected success, want: %v", tc.err)
   130  			}
   131  			got := map[string]interface{}{
   132  				"name":  st.GetName(),
   133  				"realm": st.GetRealm(),
   134  				"kind":  st.GetKind(),
   135  			}
   136  
   137  			want := tests.Unpack(t, tc.want)
   138  
   139  			if diff := cmp.Diff(want, got); diff != "" {
   140  				t.Logf("JSON: %v", tests.UnpackJSON(t, got))
   141  				t.Errorf("NewIdentityProvider() mismatch (-want +got):\n%s", diff)
   142  			}
   143  		})
   144  	}
   145  }