github.com/xgoffin/jenkins-library@v1.154.0/cmd/apiKeyValueMapUpload_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestRunApiKeyValueMapUpload(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	t.Run("Successfull Api Key Value Map Create Test", func(t *testing.T) {
    15  		// init
    16  		config := getDefaultOptions()
    17  		httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "PositiveCase"}
    18  		// test
    19  		err := runApiKeyValueMapUpload(&config, nil, &httpClient)
    20  		// assert
    21  		if assert.NoError(t, err) {
    22  			t.Run("check url", func(t *testing.T) {
    23  				assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries", httpClient.URL)
    24  			})
    25  
    26  			t.Run("check method", func(t *testing.T) {
    27  				assert.Equal(t, "POST", httpClient.Method)
    28  			})
    29  		}
    30  	})
    31  
    32  	t.Run("Failed case of API Key Value Map Create Test", func(t *testing.T) {
    33  		// init
    34  		config := getDefaultOptions()
    35  
    36  		httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "Negative"}
    37  
    38  		// test
    39  		err := runApiKeyValueMapUpload(&config, nil, &httpClient)
    40  
    41  		// assert
    42  		assert.EqualError(t, err, "HTTP \"POST\" request to \"https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries\" failed with error: 401 Unauthorized")
    43  	})
    44  
    45  	t.Run("Test API Key Value Map payload", func(t *testing.T) {
    46  		// init
    47  		config := getDefaultOptions()
    48  		testPayload := bytes.NewBuffer([]byte("{\"encrypted\":true,\"keyMapEntryValues\":[{\"map_name\":\"demoMap\",\"name\":\"demo\",\"value\":\"name\"}],\"name\":\"demoMap\",\"scope\":\"ENV\"}"))
    49  		// test
    50  		payload, err := createJSONPayload(&config)
    51  		// assert
    52  		require.NoError(t, err)
    53  		assert.Equal(t, testPayload, payload)
    54  	})
    55  
    56  	t.Run("Http Response not accepted Test case", func(t *testing.T) {
    57  		// init
    58  		config := getDefaultOptions()
    59  
    60  		httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "HttpResponseNotAccepted"}
    61  
    62  		// test
    63  		err := runApiKeyValueMapUpload(&config, nil, &httpClient)
    64  
    65  		// assert
    66  		assert.EqualError(t, err, "Failed to upload API key value map artefact, Response Status code: 202")
    67  	})
    68  
    69  	t.Run("Http Response not accepted Test case", func(t *testing.T) {
    70  		// init
    71  		config := getDefaultOptions()
    72  
    73  		httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "NilHttpResponse"}
    74  
    75  		// test
    76  		err := runApiKeyValueMapUpload(&config, nil, &httpClient)
    77  
    78  		// assert
    79  		assert.EqualError(t, err, "HTTP \"POST\" request to \"https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries\" failed with error: invalid payalod")
    80  	})
    81  
    82  }
    83  
    84  func getDefaultOptions() apiKeyValueMapUploadOptions {
    85  	return apiKeyValueMapUploadOptions{
    86  		APIServiceKey: `{
    87  			"oauth": {
    88  				"url": "https://demo",
    89  				"clientid": "demouser",
    90  				"clientsecret": "******",
    91  				"tokenurl": "https://demo/oauth/token"
    92  			}
    93  		}`,
    94  		Key:             "demo",
    95  		Value:           "name",
    96  		KeyValueMapName: "demoMap",
    97  	}
    98  }