github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/cpi/commonUtils_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cpi
     5  
     6  import (
     7  	"github.com/stretchr/testify/assert"
     8  	"testing"
     9  )
    10  
    11  func TestReadCpiServiceKeyFile(t *testing.T) {
    12  	properServiceKey := `{
    13  			"oauth": {
    14  				"url": "https://demo",
    15  				"clientid": "demouser",
    16  				"clientsecret": "******",
    17  				"tokenurl": "https://demo/oauth/token"
    18  				}
    19  			}`
    20  	faultyServiceKey := `this is not json`
    21  
    22  	tests := []struct {
    23  		name              string
    24  		serviceKey        string
    25  		wantCpiServiceKey ServiceKey
    26  		wantedErrorMsg    string
    27  	}{
    28  		{
    29  			"happy path",
    30  			properServiceKey,
    31  			ServiceKey{
    32  				OAuth: OAuth{
    33  					Host:                  "https://demo",
    34  					OAuthTokenProviderURL: "https://demo/oauth/token",
    35  					ClientID:              "demouser",
    36  					ClientSecret:          "******",
    37  				},
    38  			},
    39  			"",
    40  		},
    41  		{
    42  			"faulty json",
    43  			faultyServiceKey,
    44  			ServiceKey{},
    45  			"error unmarshalling serviceKey: invalid character 'h' in literal true (expecting 'r')",
    46  		},
    47  	}
    48  	for _, tt := range tests {
    49  		t.Run(tt.name, func(t *testing.T) {
    50  			gotCpiServiceKey, err := ReadCpiServiceKey(tt.serviceKey)
    51  			if tt.wantedErrorMsg != "" {
    52  				assert.EqualError(t, err, tt.wantedErrorMsg)
    53  			}
    54  			assert.Equal(t, tt.wantCpiServiceKey, gotCpiServiceKey)
    55  		})
    56  	}
    57  }