1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/integrationArtifactUpload_test.go
Linda Siebert 78a29d782b
CPI - Introduce service key (#2901)
* Switch to service key for CPI GetMplStatus

Introduces read method for service key files, mock utils and tests.

* Use secret text instead of file

* Change serviceKey definition

* Update cpiUpload to use Service Key

retrieved the host and uaa information from service key

* Update cpiDeploy to use service key

retrieved the host and uaa information from service key

* Update cpiServiceEndpoint to use Service Key

retrieved the host and uaa information from service key

* Update cpiDownload to use Service Key

retrieved the host and uaa information from service key

* Update cpiUpdateConfig to use Service Key

retrieved the host and uaa information from service key

* Refactor serviceKey var name

* Fixed references to service key to follow the real format

they should be accessed through oauth instead of uaa because of the format of the json

* Rename ServiceKey to APIServiceKey

To support having a different service key(and for readability), we need to change the name to API.

* Add STAGES and STEPS yaml

add in to each yaml file of cpi integration

* Revert "Add STAGES and STEPS yaml"

This reverts commit aa2665d158.

* Change comments/formatting commonUtils

Make comments more understandable and follow code climate suggestions

* Change documentation files for steps

remove OAuth and host and change credentials to be servicekey

Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
2021-06-28 10:50:33 +02:00

197 lines
5.8 KiB
Go

package cmd
import (
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type integrationArtifactUploadMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newIntegrationArtifactUploadTestsUtils() integrationArtifactUploadMockUtils {
utils := integrationArtifactUploadMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunIntegrationArtifactUpload(t *testing.T) {
t.Parallel()
t.Run("Successfull Integration Flow Create Test", func(t *testing.T) {
filesMock := mock.FilesMock{}
path := filepath.Join("tempDir", "iflow4.zip")
filesMock.AddFile(path, []byte("dummy content"))
exists, err := filesMock.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactUploadOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowName: "flow4",
IntegrationFlowID: "flow4",
IntegrationFlowVersion: "1.0.4",
PackageID: "CICD",
FilePath: path,
}
httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndCreateIntegrationDesigntimeArtifactResBody"}
err = runIntegrationArtifactUpload(&config, nil, &filesMock, &httpClient)
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifactSaveAsVersion?Id='flow4'&SaveAsVersion='1.0.4'", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "POST", httpClient.Method)
})
}
})
t.Run("Successfull Integration Flow Update Test", func(t *testing.T) {
files := mock.FilesMock{}
path := filepath.Join("tempDir", "iflow4.zip")
files.AddFile(path, []byte("dummy content"))
exists, err := files.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactUploadOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowName: "flow4",
IntegrationFlowID: "flow4",
IntegrationFlowVersion: "1.0.4",
PackageID: "CICD",
FilePath: path,
}
httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndUpdateIntegrationDesigntimeArtifactResBody"}
err = runIntegrationArtifactUpload(&config, nil, &files, &httpClient)
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "https://demo/api/v1/IntegrationDesigntimeArtifacts", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "POST", httpClient.Method)
})
}
})
t.Run("Failed case of Integration Flow Get Test", func(t *testing.T) {
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactUploadOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowName: "flow4",
IntegrationFlowID: "flow4",
IntegrationFlowVersion: "1.0.4",
PackageID: "CICD",
FilePath: "path",
}
httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndGetIntegrationDesigntimeArtifactResBody"}
err := runIntegrationArtifactUpload(&config, nil, nil, &httpClient)
assert.Error(t, err)
})
t.Run("Failed case of Integration Flow Update Test", func(t *testing.T) {
files := mock.FilesMock{}
path := filepath.Join("tempDir", "iflow4.zip")
files.AddFile(path, []byte("dummy content"))
exists, err := files.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactUploadOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowName: "flow4",
IntegrationFlowID: "flow4",
IntegrationFlowVersion: "1.0.4",
PackageID: "CICD",
FilePath: path,
}
httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndCreateIntegrationDesigntimeArtifactResBody"}
err = runIntegrationArtifactUpload(&config, nil, &files, &httpClient)
assert.EqualError(t, err, "HTTP POST request to https://demo/api/v1/IntegrationDesigntimeArtifactSaveAsVersion?Id='flow4'&SaveAsVersion='1.0.4' failed with error: : 401 Unauthorized")
})
t.Run("Failed case of Integration Flow Create Test", func(t *testing.T) {
filesMock := mock.FilesMock{}
path := filepath.Join("tempDir", "iflow4.zip")
filesMock.AddFile(path, []byte("dummy content"))
exists, err := filesMock.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactUploadOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowName: "flow4",
IntegrationFlowID: "flow4",
IntegrationFlowVersion: "1.0.4",
PackageID: "CICD",
FilePath: path,
}
httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndUpdateIntegrationDesigntimeArtifactResBody"}
err = runIntegrationArtifactUpload(&config, nil, &filesMock, &httpClient)
assert.EqualError(t, err, "HTTP POST request to https://demo/api/v1/IntegrationDesigntimeArtifacts failed with error: : 401 Unauthorized")
})
}