Skip to content

Commit

Permalink
Added test for GetConfigurationProfileByUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 11, 2024
1 parent a04d806 commit c53fa7e
Showing 1 changed file with 61 additions and 6 deletions.
67 changes: 61 additions & 6 deletions tests/xscconfigprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,32 @@ package tests

import (
"encoding/json"
"fmt"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/xsc/services"
xscutils "github.com/jfrog/jfrog-client-go/xsc/services/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)

const configProfileWithoutRepo = "default-test-profile"

func TestGetConfigurationProfileByName(t *testing.T) {
initXscTest(t, services.ConfigProfileMinXscVersion)
initXscTest(t, services.ConfigProfileMinXscVersion, "")

xrayVersion, err := GetXrayDetails().GetVersion()
require.NoError(t, err)

mockServer, configProfileService := createXscMockServerForConfigProfile(t)
mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion)
defer mockServer.Close()

configProfile, err := configProfileService.GetConfigurationProfileByName("default-test-profile")
configProfile, err := configProfileService.GetConfigurationProfileByName(configProfileWithoutRepo)
assert.NoError(t, err)

profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileExample.json")
Expand All @@ -28,15 +38,55 @@ func TestGetConfigurationProfileByName(t *testing.T) {
assert.Equal(t, &configProfileForComparison, configProfile)
}

func createXscMockServerForConfigProfile(t *testing.T) (mockServer *httptest.Server, configProfileService *services.ConfigurationProfileService) {
func TestGetConfigurationProfileByUrl(t *testing.T) {
initXscTest(t, "", services.ConfigProfileByUrlMinXrayVersion)

// Verifying minimal xray version required for feature and test
xrayVersion, err := GetXrayDetails().GetVersion()
require.NoError(t, err)
err = utils.ValidateMinimumVersion(utils.Xray, xrayVersion, services.ConfigProfileByUrlMinXrayVersion)
if err != nil {
t.Skip(fmt.Sprintf("Skipping GetConfigurationProfileByName test since current Xray version is %s while minimal required version for the feature is %s", xrayVersion, services.ConfigProfileByUrlMinXrayVersion))

Check failure on line 49 in tests/xscconfigprofile_test.go

View workflow job for this annotation

GitHub Actions / Static-Check

S1038: should use t.Skipf(...) instead of t.Skip(fmt.Sprintf(...)) (gosimple)
}

mockServer, configProfileService := createXscMockServerForConfigProfile(t, xrayVersion)
defer mockServer.Close()

configProfile, err := configProfileService.GetConfigurationProfileByUrl(mockServer.URL)
assert.NoError(t, err)

profileFileContent, err := os.ReadFile("testdata/configprofile/configProfileWithRepoExample.json")
assert.NoError(t, err)
var configProfileForComparison services.ConfigProfile
err = json.Unmarshal(profileFileContent, &configProfileForComparison)
assert.NoError(t, err)
assert.Equal(t, &configProfileForComparison, configProfile)

}

func createXscMockServerForConfigProfile(t *testing.T, xrayVersion string) (mockServer *httptest.Server, configProfileService *services.ConfigurationProfileService) {
mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.RequestURI == "/xsc/api/v1/profile/default-test-profile" && r.Method == http.MethodGet {
apiUrlPart := "api/v1/"
var isXrayAfterXscMigration bool
if isXrayAfterXscMigration = xscutils.IsXscXrayInnerService(xrayVersion); isXrayAfterXscMigration {
apiUrlPart = ""
}

switch {
case strings.Contains(r.RequestURI, "/xsc/"+apiUrlPart+"profile/"+configProfileWithoutRepo):
w.WriteHeader(http.StatusOK)
content, err := os.ReadFile("testdata/configprofile/configProfileExample.json")
assert.NoError(t, err)
_, err = w.Write(content)
assert.NoError(t, err)
} else {

case strings.Contains(r.RequestURI, "xray/api/v1/xsc/profile_repos") && isXrayAfterXscMigration:
w.WriteHeader(http.StatusOK)
content, err := os.ReadFile("testdata/configprofile/configProfileWithRepoExample.json")
assert.NoError(t, err)
_, err = w.Write(content)
assert.NoError(t, err)
default:
assert.Fail(t, "received an unexpected request")
}
}))
Expand All @@ -45,10 +95,15 @@ func createXscMockServerForConfigProfile(t *testing.T) (mockServer *httptest.Ser
xscDetails.SetUrl(mockServer.URL + "/xsc")
xscDetails.SetAccessToken("")

xrayDetails := GetXrayDetails()
xrayDetails.SetUrl(mockServer.URL + "/xray")
xrayDetails.SetAccessToken("")

client, err := jfroghttpclient.JfrogClientBuilder().Build()
assert.NoError(t, err)

configProfileService = services.NewConfigurationProfileService(client)
configProfileService.XscDetails = xscDetails
configProfileService.XrayDetails = xrayDetails
return
}

0 comments on commit c53fa7e

Please sign in to comment.