github.com/greenpau/go-authcrunch@v1.1.4/pkg/sso/config_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  	"testing"
    23  )
    24  
    25  func TestNewSingleSignOnProviderConfig(t *testing.T) {
    26  	testcases := []struct {
    27  		name      string
    28  		input     map[string]interface{}
    29  		want      string
    30  		shouldErr bool
    31  		err       error
    32  	}{
    33  		{
    34  			name: "test aws sso provider",
    35  			input: map[string]interface{}{
    36  				"name":             "aws",
    37  				"driver":           "aws",
    38  				"entity_id":        "caddy-authp-idp",
    39  				"private_key_path": "../../testdata/sso/authp_saml.key",
    40  				"cert_path":        "../../testdata/sso/authp_saml.crt",
    41  				"locations": []string{
    42  					"https://localhost/sso/aws",
    43  					"https://127.0.0.1/sso/aws",
    44  				},
    45  			},
    46  			want: `{
    47  				"name":             "aws",
    48                  "driver":           "aws",
    49                  "entity_id":        "caddy-authp-idp",
    50  				"private_key_path": "../../testdata/sso/authp_saml.key",
    51                  "cert_path":        "../../testdata/sso/authp_saml.crt",
    52                  "locations": [
    53                      "https://localhost/sso/aws",
    54                      "https://127.0.0.1/sso/aws"
    55                  ]
    56              }`,
    57  		},
    58  		{
    59  			name:      "test empty sso provider parameters",
    60  			input:     nil,
    61  			shouldErr: true,
    62  			err: errors.ErrSingleSignOnProviderConfigInvalid.WithArgs(
    63  				"input data error",
    64  				fmt.Errorf("empty identity provider parameters"),
    65  			),
    66  		},
    67  		{
    68  			name: "test json Unmarshal error",
    69  			input: map[string]interface{}{
    70  				"name":             "aws",
    71  				"driver":           []string{"aws"},
    72  				"entity_id":        "caddy-authp-idp",
    73  				"private_key_path": "../../testdata/sso/authp_saml.key",
    74  				"cert_path":        "../../testdata/sso/authp_saml.crt",
    75  				"locations": []string{
    76  					"https://localhost/sso/aws",
    77  					"https://127.0.0.1/sso/aws",
    78  				},
    79  			},
    80  			shouldErr: true,
    81  			err: errors.ErrSingleSignOnProviderConfigInvalid.WithArgs(
    82  				"json.Unmarshal error",
    83  				fmt.Errorf("json: cannot unmarshal array into Go struct field SingleSignOnProviderConfig.driver of type string"),
    84  			),
    85  		},
    86  		{
    87  			name: "test config validation error",
    88  			input: map[string]interface{}{
    89  				"name":             "aws",
    90  				"driver":           "foo",
    91  				"entity_id":        "caddy-authp-idp",
    92  				"private_key_path": "../../testdata/sso/authp_saml.key",
    93  				"cert_path":        "../../testdata/sso/authp_saml.crt",
    94  				"locations": []string{
    95  					"https://localhost/sso/aws",
    96  					"https://127.0.0.1/sso/aws",
    97  				},
    98  			},
    99  			shouldErr: true,
   100  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "unsupported driver name"),
   101  		},
   102  		{
   103  			name: "test empty provider name error",
   104  			input: map[string]interface{}{
   105  				"name":             "",
   106  				"driver":           "aws",
   107  				"entity_id":        "caddy-authp-idp",
   108  				"private_key_path": "../../testdata/sso/authp_saml.key",
   109  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   110  				"locations": []string{
   111  					"https://localhost/sso/aws",
   112  					"https://127.0.0.1/sso/aws",
   113  				},
   114  			},
   115  			shouldErr: true,
   116  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "empty provider name"),
   117  		},
   118  		{
   119  			name: "test empty entity id error",
   120  			input: map[string]interface{}{
   121  				"name":             "aws",
   122  				"driver":           "aws",
   123  				"entity_id":        "",
   124  				"private_key_path": "../../testdata/sso/authp_saml.key",
   125  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   126  				"locations": []string{
   127  					"https://localhost/sso/aws",
   128  					"https://127.0.0.1/sso/aws",
   129  				},
   130  			},
   131  			shouldErr: true,
   132  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "empty entity id"),
   133  		},
   134  		{
   135  			name: "test empty private key path error",
   136  			input: map[string]interface{}{
   137  				"name":             "aws",
   138  				"driver":           "aws",
   139  				"entity_id":        "caddy-authp-idp",
   140  				"private_key_path": "",
   141  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   142  				"locations": []string{
   143  					"https://localhost/sso/aws",
   144  					"https://127.0.0.1/sso/aws",
   145  				},
   146  			},
   147  			shouldErr: true,
   148  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "empty private key path"),
   149  		},
   150  		{
   151  			name: "test empty cert path error",
   152  			input: map[string]interface{}{
   153  				"name":             "aws",
   154  				"driver":           "aws",
   155  				"entity_id":        "caddy-authp-idp",
   156  				"private_key_path": "../../testdata/sso/authp_saml.key",
   157  				"cert_path":        "",
   158  				"locations": []string{
   159  					"https://localhost/sso/aws",
   160  					"https://127.0.0.1/sso/aws",
   161  				},
   162  			},
   163  			shouldErr: true,
   164  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "empty cert path"),
   165  		},
   166  		{
   167  			name: "test empty locations error",
   168  			input: map[string]interface{}{
   169  				"name":             "aws",
   170  				"driver":           "aws",
   171  				"entity_id":        "caddy-authp-idp",
   172  				"private_key_path": "../../testdata/sso/authp_saml.key",
   173  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   174  				"locations":        []string{},
   175  			},
   176  			shouldErr: true,
   177  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "empty locations"),
   178  		},
   179  		{
   180  			name: "test empty driver name error",
   181  			input: map[string]interface{}{
   182  				"name":             "aws",
   183  				"driver":           "",
   184  				"entity_id":        "caddy-authp-idp",
   185  				"private_key_path": "../../testdata/sso/authp_saml.key",
   186  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   187  				"locations": []string{
   188  					"https://localhost/sso/aws",
   189  					"https://127.0.0.1/sso/aws",
   190  				},
   191  			},
   192  			shouldErr: true,
   193  			err:       errors.ErrSingleSignOnProviderConfigInvalid.WithArgs("misconfiguration", "empty driver name"),
   194  		},
   195  		{
   196  			name: "test required field not found error",
   197  			input: map[string]interface{}{
   198  				"name":   "aws",
   199  				"driver": "aws",
   200  				// "entity_id":        "caddy-authp-idp",
   201  				"private_key_path": "../../testdata/sso/authp_saml.key",
   202  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   203  				"locations": []string{
   204  					"https://localhost/sso/aws",
   205  					"https://127.0.0.1/sso/aws",
   206  				},
   207  			},
   208  			shouldErr: true,
   209  			err: errors.ErrSingleSignOnProviderConfigInvalid.WithArgs(
   210  				"input data error",
   211  				fmt.Errorf("required field %q not found", "entity_id"),
   212  			),
   213  		},
   214  		{
   215  			name: "test unsupported field found error",
   216  			input: map[string]interface{}{
   217  				"name":             "aws",
   218  				"driver":           "aws",
   219  				"entity_id":        "caddy-authp-idp",
   220  				"private_key_path": "../../testdata/sso/authp_saml.key",
   221  				"cert_path":        "../../testdata/sso/authp_saml.crt",
   222  				"locations": []string{
   223  					"https://localhost/sso/aws",
   224  					"https://127.0.0.1/sso/aws",
   225  				},
   226  				"foo": "bar",
   227  			},
   228  			shouldErr: true,
   229  			err: errors.ErrSingleSignOnProviderConfigInvalid.WithArgs(
   230  				"input data error",
   231  				fmt.Errorf("found unsupported %q field", "foo"),
   232  			),
   233  		},
   234  	}
   235  	for _, tc := range testcases {
   236  		t.Run(tc.name, func(t *testing.T) {
   237  			cfg, err := NewSingleSignOnProviderConfig(tc.input)
   238  			if err != nil {
   239  				if !tc.shouldErr {
   240  					t.Fatalf("expected success, got: %v", err)
   241  				}
   242  				if diff := cmp.Diff(err.Error(), tc.err.Error()); diff != "" {
   243  					t.Fatalf("unexpected error: %v, want: %v", err, tc.err)
   244  				}
   245  				return
   246  			}
   247  			if tc.shouldErr {
   248  				t.Fatalf("unexpected success, want: %v", tc.err)
   249  			}
   250  			got := tests.Unpack(t, cfg)
   251  			want := tests.Unpack(t, tc.want)
   252  
   253  			if diff := cmp.Diff(want, got); diff != "" {
   254  				t.Logf("JSON: %v", tests.UnpackJSON(t, got))
   255  				t.Errorf("NewSingleSignOnProviderConfig() mismatch (-want +got):\n%s", diff)
   256  			}
   257  		})
   258  	}
   259  }