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

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