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

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/SAP/jenkins-library/pkg/mock"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type apiProxyDownloadMockUtils struct {
    15  	*mock.ExecMockRunner
    16  	*mock.FilesMock
    17  }
    18  
    19  func TestRunApiProxyDownload(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	t.Run("Successfull Download of API Proxy", func(t *testing.T) {
    23  		tempDir := t.TempDir()
    24  		apiServiceKey := `{
    25  			"oauth": {
    26  				"url": "https://demo",
    27  				"clientid": "demouser",
    28  				"clientsecret": "******",
    29  				"tokenurl": "https://demo/oauth/token"
    30  			}
    31  		}`
    32  		config := apiProxyDownloadOptions{
    33  			APIServiceKey: apiServiceKey,
    34  			APIProxyName:  "flow1",
    35  			DownloadPath:  tempDir,
    36  		}
    37  		httpClient := httpMockCpis{CPIFunction: "APIProxyDownload", ResponseBody: ``, TestType: "PositiveAndGetetIntegrationArtifactDownloadResBody"}
    38  		err := runApiProxyDownload(&config, nil, &httpClient)
    39  		absolutePath := filepath.Join(tempDir, "flow1.zip")
    40  		if assert.NoError(t, err) {
    41  			t.Run("check file", func(t *testing.T) {
    42  				assert.Equal(t, fileExists(absolutePath), true)
    43  			})
    44  			t.Run("check url", func(t *testing.T) {
    45  				assert.Equal(t, "https://demo/apiportal/api/1.0/Transport.svc/APIProxies?name=flow1", httpClient.URL)
    46  			})
    47  
    48  			t.Run("check method", func(t *testing.T) {
    49  				assert.Equal(t, "GET", httpClient.Method)
    50  			})
    51  		}
    52  	})
    53  
    54  	t.Run("Failed case of api proxy artifact Download", func(t *testing.T) {
    55  		apiServiceKey := `{
    56  			"oauth": {
    57  				"url": "https://demo",
    58  				"clientid": "demouser",
    59  				"clientsecret": "******",
    60  				"tokenurl": "https://demo/oauth/token"
    61  			}
    62  		}`
    63  		config := apiProxyDownloadOptions{
    64  			APIServiceKey: apiServiceKey,
    65  			APIProxyName:  "proxy1",
    66  			DownloadPath:  "tmp",
    67  		}
    68  		httpClient := httpMockCpis{CPIFunction: "APIProxyDownloadFailure", ResponseBody: ``, TestType: "Negative"}
    69  		err := runApiProxyDownload(&config, nil, &httpClient)
    70  		assert.EqualError(t, err, "HTTP GET request to https://demo/apiportal/api/1.0/Transport.svc/APIProxies?name=proxy1 failed with error: Service not Found")
    71  	})
    72  }