github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/apiProviderUpload_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"os"
     8  	"testing"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/apim"
    11  	apimhttp "github.com/SAP/jenkins-library/pkg/apim"
    12  	"github.com/SAP/jenkins-library/pkg/mock"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestRunApiProviderUpload(t *testing.T) {
    17  	t.Parallel()
    18  
    19  	t.Run("API Provider upload succesfull test", func(t *testing.T) {
    20  		file, tmpErr := os.CreateTemp("", "test.json")
    21  		if tmpErr != nil {
    22  			t.FailNow()
    23  		}
    24  		defer os.RemoveAll(file.Name()) // clean up
    25  		filesMock := mock.FilesMock{}
    26  		filesMock.AddFile(file.Name(), []byte(apimhttp.GetServiceKey()))
    27  		config := getDefaultOptionsForApiProvider()
    28  		config.FilePath = file.Name()
    29  		httpClientMock := &apimhttp.HttpMockAPIM{StatusCode: 201, ResponseBody: ``}
    30  		apim := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClientMock}
    31  		// test
    32  		err := createApiProvider(&config, apim, filesMock.FileRead)
    33  
    34  		// assert
    35  		if assert.NoError(t, err) {
    36  			t.Run("check url", func(t *testing.T) {
    37  				assert.Equal(t, "/apiportal/api/1.0/Management.svc/APIProviders", httpClientMock.URL)
    38  			})
    39  			t.Run("check method", func(t *testing.T) {
    40  				assert.Equal(t, "POST", httpClientMock.Method)
    41  			})
    42  		}
    43  	})
    44  
    45  	t.Run("API Provider upload failed test", func(t *testing.T) {
    46  		file, tmpErr := os.CreateTemp("", "test.json")
    47  		if tmpErr != nil {
    48  			t.FailNow()
    49  		}
    50  		defer os.RemoveAll(file.Name()) // clean up
    51  		filesMock := mock.FilesMock{}
    52  		filesMock.AddFile(file.Name(), []byte(apimhttp.GetServiceKey()))
    53  		config := getDefaultOptionsForApiProvider()
    54  		config.FilePath = file.Name()
    55  		httpClientMock := &apimhttp.HttpMockAPIM{StatusCode: 400}
    56  		apim := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClientMock}
    57  		// test
    58  		err := createApiProvider(&config, apim, filesMock.FileRead)
    59  		// assert
    60  		assert.EqualError(t, err, "HTTP POST request to /apiportal/api/1.0/Management.svc/APIProviders failed with error: : Bad Request")
    61  	})
    62  
    63  	t.Run("valid api provider payload test", func(t *testing.T) {
    64  		apiProviderPayload := `{
    65  			"oauth": {
    66  				"url": "https://demo",
    67  				"clientid": "demouser",
    68  				"clientsecret": "******",
    69  				"tokenurl": "https://demo/oauth/token"
    70  				}
    71  			}`
    72  		apimData := apim.Bundle{Payload: apiProviderPayload}
    73  		assert.Equal(t, apimData.IsPayloadJSON(), true)
    74  	})
    75  
    76  	t.Run("invalid api provider payload test", func(t *testing.T) {
    77  		apiProviderPayload := `this is not json`
    78  		apimData := apim.Bundle{Payload: apiProviderPayload}
    79  		assert.Equal(t, apimData.IsPayloadJSON(), false)
    80  	})
    81  
    82  }
    83  
    84  func getDefaultOptionsForApiProvider() apiProviderUploadOptions {
    85  	return apiProviderUploadOptions{
    86  		APIServiceKey: apimhttp.GetServiceKey(),
    87  		FilePath:      "test.json",
    88  	}
    89  }