Skip to content

Commit

Permalink
Cr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
orz25 committed Feb 14, 2024
1 parent 7607ecd commit 508373d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 44 deletions.
67 changes: 29 additions & 38 deletions artifactory/services/utils/tests/xray/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"os"
"strconv"
"strings"
"testing"

"github.com/buger/jsonparser"
"github.com/jfrog/jfrog-client-go/utils/log"
clienttests "github.com/jfrog/jfrog-client-go/utils/tests"
"github.com/jfrog/jfrog-client-go/xray/services"
"github.com/stretchr/testify/assert"
)

const (
Expand Down Expand Up @@ -184,58 +186,47 @@ func buildScanHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Invalid reports request", http.StatusBadRequest)
}

func xscGetVersionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, err := fmt.Fprint(w, xscVersionResponse)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
func xscGetVersionHandlerFunc(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, err := fmt.Fprint(w, xscVersionResponse)
assert.NoError(t, err)
return
}
return
http.Error(w, "Invalid xsc request", http.StatusBadRequest)
}
http.Error(w, "Invalid xsc request", http.StatusBadRequest)
}

func xscGitInfoHandler(w http.ResponseWriter, r *http.Request) {
req, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.Method == http.MethodPost {
var reqBody services.XscGitInfoContext
err = json.Unmarshal(req, &reqBody)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if reqBody.GitRepoUrl == "" || reqBody.BranchName == "" || reqBody.CommitHash == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := fmt.Fprint(w, XscGitInfoBadResponse)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
func xscGitInfoHandlerFunc(t *testing.T) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
req, err := io.ReadAll(r.Body)
assert.NoError(t, err)
if r.Method == http.MethodPost {
var reqBody services.XscGitInfoContext
err = json.Unmarshal(req, &reqBody)
assert.NoError(t, err)
if reqBody.GitRepoUrl == "" || reqBody.BranchName == "" || reqBody.CommitHash == "" {
w.WriteHeader(http.StatusBadRequest)
_, err := fmt.Fprint(w, XscGitInfoBadResponse)
assert.NoError(t, err)
return
}
w.WriteHeader(http.StatusCreated)
_, err = fmt.Fprint(w, XscGitInfoResponse)
assert.NoError(t, err)
return
}
w.WriteHeader(http.StatusCreated)
_, err = fmt.Fprint(w, XscGitInfoResponse)
if err != nil {
log.Error(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
http.Error(w, "Invalid xsc request", http.StatusBadRequest)
}
http.Error(w, "Invalid xsc request", http.StatusBadRequest)
}

func StartXrayMockServer() int {
func StartXrayMockServer(t *testing.T) int {
handlers := clienttests.HttpServerHandlers{}
handlers["/api/xray/scanBuild"] = scanBuildHandler
handlers["/api/v2/summary/artifact"] = artifactSummaryHandler
handlers["/api/v1/entitlements/feature/"] = entitlementsHandler
handlers["/xsc/api/v1/system/version"] = xscGetVersionHandler
handlers["/xsc/api/v1/gitinfo"] = xscGitInfoHandler
handlers["/xsc/api/v1/system/version"] = xscGetVersionHandlerFunc(t)
handlers["/xsc/api/v1/gitinfo"] = xscGitInfoHandlerFunc(t)
handlers[fmt.Sprintf("/%s/", services.ReportsAPI)] = reportHandler
handlers[fmt.Sprintf("/%s/", services.BuildScanAPI)] = buildScanHandler
handlers["/"] = http.NotFound
Expand Down
4 changes: 2 additions & 2 deletions tests/xray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestXrayVersion(t *testing.T) {

func TestXrayEntitlementsService(t *testing.T) {
initXrayTest(t)
xrayServerPort := xray.StartXrayMockServer()
xrayServerPort := xray.StartXrayMockServer(t)
xrayDetails := GetXrayDetails()
client, err := jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
Expand Down Expand Up @@ -69,7 +69,7 @@ func testEntitlements(t *testing.T, featureId string, expected bool) {

func TestScanBuild(t *testing.T) {
initXrayTest(t)
xrayServerPort := xray.StartXrayMockServer()
xrayServerPort := xray.StartXrayMockServer(t)
xrayDetails := newTestXrayDetails(GetXrayDetails())
client, err := jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
Expand Down
2 changes: 1 addition & 1 deletion tests/xrayreport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var testXrayReportService *services.ReportService

func TestXrayReport(t *testing.T) {
initXrayTest(t)
xrayServerPort := xray.StartXrayMockServer()
xrayServerPort := xray.StartXrayMockServer(t)
xrayDetails := GetXrayDetails()
client, err := jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
Expand Down
4 changes: 2 additions & 2 deletions tests/xrayscan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var testsScanService *xrayServices.ScanService

func TestNewXrayScanService(t *testing.T) {
initXrayTest(t)
xrayServerPort := xray.StartXrayMockServer()
xrayServerPort := xray.StartXrayMockServer(t)
artDetails := GetRtDetails()
client, err := jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(artDetails.GetClientCertPath()).
Expand Down Expand Up @@ -118,7 +118,7 @@ func sendGitInfoContext(t *testing.T, gitInfoContext *xrayServices.XscGitInfoCon
func initXrayScanTest(t *testing.T) (xrayServerPort int, xrayDetails auth.ServiceDetails, client *jfroghttpclient.JfrogHttpClient) {
var err error
initXrayTest(t)
xrayServerPort = xray.StartXrayMockServer()
xrayServerPort = xray.StartXrayMockServer(t)
xrayDetails = GetXrayDetails()
client, err = jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
Expand Down
2 changes: 1 addition & 1 deletion tests/xraysummary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var testsXraySummaryService *services.SummaryService

func TestNewXraySummaryService(t *testing.T) {
initXrayTest(t)
xrayServerPort := xray.StartXrayMockServer()
xrayServerPort := xray.StartXrayMockServer(t)
xrayDetails := GetXrayDetails()
client, err := jfroghttpclient.JfrogClientBuilder().
SetClientCertPath(xrayDetails.GetClientCertPath()).
Expand Down

0 comments on commit 508373d

Please sign in to comment.