github.com/Axway/agent-sdk@v1.1.101/pkg/config/externalidpconfig_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/Axway/agent-sdk/pkg/cmd/properties"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestExternalIDPConfig(t *testing.T) {
    13  	testCases := []struct {
    14  		name     string
    15  		envNames map[string]string
    16  		hasError bool
    17  	}{
    18  		{
    19  			name:     "no external IDP config",
    20  			envNames: map[string]string{},
    21  			hasError: false,
    22  		},
    23  		{
    24  			name: "no name in IDP config",
    25  			envNames: map[string]string{
    26  				"AGENTFEATURES_IDP_METADATAURL_1": "test",
    27  			},
    28  			hasError: true,
    29  		},
    30  		{
    31  			name: "no metadata URL in IDP config",
    32  			envNames: map[string]string{
    33  				"AGENTFEATURES_IDP_NAME_1": "test",
    34  			},
    35  			hasError: true,
    36  		},
    37  		{
    38  			name: "no auth config in IDP config",
    39  			envNames: map[string]string{
    40  				"AGENTFEATURES_IDP_NAME_1":        "test",
    41  				"AGENTFEATURES_IDP_METADATAURL_1": "test",
    42  			},
    43  			hasError: true,
    44  		},
    45  		{
    46  			name: "invalid IDP auth type config in IDP config",
    47  			envNames: map[string]string{
    48  				"AGENTFEATURES_IDP_NAME_1":        "test",
    49  				"AGENTFEATURES_IDP_METADATAURL_1": "test",
    50  				"AGENTFEATURES_IDP_AUTH_TYPE_1":   "invalid",
    51  			},
    52  			hasError: true,
    53  		},
    54  		{
    55  			name: "accessToken auth config with no token in IDP config",
    56  			envNames: map[string]string{
    57  				"AGENTFEATURES_IDP_NAME_1":        "test",
    58  				"AGENTFEATURES_IDP_METADATAURL_1": "test",
    59  				"AGENTFEATURES_IDP_AUTH_TYPE_1":   "accessToken",
    60  			},
    61  			hasError: true,
    62  		},
    63  		{
    64  			name: "accessToken auth config with valid token in IDP config",
    65  			envNames: map[string]string{
    66  				"AGENTFEATURES_IDP_NAME_1":             "test",
    67  				"AGENTFEATURES_IDP_METADATAURL_1":      "test",
    68  				"AGENTFEATURES_IDP_AUTH_TYPE_1":        "accessToken",
    69  				"AGENTFEATURES_IDP_AUTH_ACCESSTOKEN_1": "accessToken",
    70  			},
    71  			hasError: false,
    72  		},
    73  		{
    74  			name: "client auth config with no clientid/secret in IDP config",
    75  			envNames: map[string]string{
    76  				"AGENTFEATURES_IDP_NAME_1":        "test",
    77  				"AGENTFEATURES_IDP_METADATAURL_1": "test",
    78  				"AGENTFEATURES_IDP_AUTH_TYPE_1":   "client",
    79  			},
    80  			hasError: true,
    81  		},
    82  		{
    83  			name: "client auth config with no client secret in IDP config",
    84  			envNames: map[string]string{
    85  				"AGENTFEATURES_IDP_NAME_1":          "test",
    86  				"AGENTFEATURES_IDP_METADATAURL_1":   "test",
    87  				"AGENTFEATURES_IDP_AUTH_TYPE_1":     "client",
    88  				"AGENTFEATURES_IDP_AUTH_CLIENTID_1": "client-id",
    89  			},
    90  			hasError: true,
    91  		},
    92  		{
    93  			name: "client auth config with valid client config in IDP config",
    94  			envNames: map[string]string{
    95  				"AGENTFEATURES_IDP_NAME_1":                "test",
    96  				"AGENTFEATURES_IDP_METADATAURL_1":         "test",
    97  				"AGENTFEATURES_IDP_REQUESTHEADERS_1":      "{\"hdr\":\"value\"}",
    98  				"AGENTFEATURES_IDP_QUERYPARAMS_1":         "{\"param\":\"value\"}",
    99  				"AGENTFEATURES_IDP_AUTH_TYPE_1":           "client",
   100  				"AGENTFEATURES_IDP_AUTH_CLIENTID_1":       "client-id",
   101  				"AGENTFEATURES_IDP_AUTH_CLIENTSECRET_1":   "client-secret",
   102  				"AGENTFEATURES_IDP_AUTH_REQUESTHEADERS_1": "{\"authhdr\":\"value\"}",
   103  				"AGENTFEATURES_IDP_AUTH_QUERYPARAMS_1":    "{\"authparam\":\"value\"}",
   104  			},
   105  			hasError: false,
   106  		},
   107  	}
   108  	for _, test := range testCases {
   109  		t.Run(test.name, func(t *testing.T) {
   110  			for key, val := range test.envNames {
   111  				os.Setenv(key, val)
   112  			}
   113  			defer func() {
   114  				for key := range test.envNames {
   115  					os.Setenv(key, "")
   116  				}
   117  			}()
   118  			prop := properties.NewProperties(nil)
   119  			AddAgentFeaturesConfigProperties(prop)
   120  			cfg, err := ParseAgentFeaturesConfig(prop)
   121  			assert.Nil(t, err)
   122  			assert.NotNil(t, cfg)
   123  			err = cfg.(*AgentFeaturesConfiguration).ValidateCfg()
   124  			if test.hasError {
   125  				assert.NotNil(t, err)
   126  			} else {
   127  				assert.Nil(t, err)
   128  				idpCfgs := cfg.GetExternalIDPConfig()
   129  				for _, idp := range idpCfgs.GetIDPList() {
   130  					buf, err := json.Marshal(idp)
   131  					assert.Nil(t, err)
   132  					assert.NotNil(t, buf)
   133  					parsedIdP := &IDPConfiguration{}
   134  					err = json.Unmarshal(buf, &parsedIdP)
   135  					assert.Nil(t, err)
   136  					assert.Equal(t, idp.GetIDPName(), parsedIdP.GetIDPName())
   137  					assert.Equal(t, idp.GetIDPType(), parsedIdP.GetIDPType())
   138  					assert.Equal(t, idp.GetMetadataURL(), parsedIdP.GetMetadataURL())
   139  					assert.Equal(t, len(idp.GetRequestHeaders()), len(parsedIdP.GetRequestHeaders()))
   140  					assert.Equal(t, len(idp.GetQueryParams()), len(parsedIdP.GetQueryParams()))
   141  					assert.Equal(t, idp.GetAuthConfig().GetType(), parsedIdP.GetAuthConfig().GetType())
   142  					assert.Equal(t, idp.GetAuthConfig().GetAccessToken(), parsedIdP.GetAuthConfig().GetAccessToken())
   143  					assert.Equal(t, idp.GetAuthConfig().GetClientID(), parsedIdP.GetAuthConfig().GetClientID())
   144  					assert.Equal(t, idp.GetAuthConfig().GetClientSecret(), parsedIdP.GetAuthConfig().GetClientSecret())
   145  					assert.Equal(t, len(idp.GetAuthConfig().GetRequestHeaders()), len(parsedIdP.GetAuthConfig().GetRequestHeaders()))
   146  					assert.Equal(t, len(idp.GetAuthConfig().GetQueryParams()), len(parsedIdP.GetAuthConfig().GetQueryParams()))
   147  
   148  				}
   149  
   150  			}
   151  		})
   152  	}
   153  }