github.com/jfrog/jfrog-client-go@v1.40.2/tests/xray_test.go (about) 1 package tests 2 3 import ( 4 "github.com/jfrog/jfrog-client-go/auth" 5 "github.com/stretchr/testify/assert" 6 "strconv" 7 "testing" 8 9 "github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests/xray" 10 "github.com/jfrog/jfrog-client-go/http/jfroghttpclient" 11 "github.com/jfrog/jfrog-client-go/xray/services" 12 ) 13 14 var testsXrayEntitlementsService *services.EntitlementsService 15 16 func TestXrayVersion(t *testing.T) { 17 initXrayTest(t) 18 version, err := GetXrayDetails().GetVersion() 19 if err != nil { 20 t.Error(err) 21 } 22 23 if version == "" { 24 t.Error("Expected a version, got empty string") 25 } 26 } 27 28 func TestXrayEntitlementsService(t *testing.T) { 29 initXrayTest(t) 30 xrayServerPort := xray.StartXrayMockServer(t) 31 xrayDetails := GetXrayDetails() 32 client, err := jfroghttpclient.JfrogClientBuilder(). 33 SetClientCertPath(xrayDetails.GetClientCertPath()). 34 SetClientCertKeyPath(xrayDetails.GetClientCertKeyPath()). 35 AppendPreRequestInterceptor(xrayDetails.RunPreRequestFunctions). 36 Build() 37 if err != nil { 38 t.Error(err) 39 } 40 testsXrayEntitlementsService = services.NewEntitlementsService(client) 41 testsXrayEntitlementsService.XrayDetails = xrayDetails 42 testsXrayEntitlementsService.XrayDetails.SetUrl("http://localhost:" + strconv.Itoa(xrayServerPort) + "/") 43 44 // Run tests 45 tests := []struct { 46 name string 47 featureId string 48 expected bool 49 }{ 50 {name: "userEntitled", featureId: xray.ContextualAnalysisFeatureId, expected: true}, 51 {name: "userNotEntitled", featureId: xray.BadFeatureId, expected: false}, 52 } 53 for _, test := range tests { 54 t.Run(test.name, func(t *testing.T) { 55 testEntitlements(t, test.featureId, test.expected) 56 }) 57 } 58 } 59 60 func testEntitlements(t *testing.T, featureId string, expected bool) { 61 result, err := testsXrayEntitlementsService.IsEntitled(featureId) 62 if err != nil { 63 t.Error(err) 64 } 65 if result != expected { 66 t.Error("Expected:", expected, "Got: ", result) 67 } 68 } 69 70 func TestScanBuild(t *testing.T) { 71 initXrayTest(t) 72 xrayServerPort := xray.StartXrayMockServer(t) 73 xrayDetails := newTestXrayDetails(GetXrayDetails()) 74 client, err := jfroghttpclient.JfrogClientBuilder(). 75 SetClientCertPath(xrayDetails.GetClientCertPath()). 76 SetClientCertKeyPath(xrayDetails.GetClientCertKeyPath()). 77 AppendPreRequestInterceptor(xrayDetails.RunPreRequestFunctions). 78 Build() 79 if err != nil { 80 t.Error(err) 81 } 82 testsBuildScanService := services.NewBuildScanService(client) 83 xrayDetails.SetUrl("http://localhost:" + strconv.Itoa(xrayServerPort) + "/") 84 tests := []struct { 85 name string 86 buildName string 87 buildNumber string 88 xrayVersion string 89 }{ 90 {name: "get-api", buildName: "test-get", buildNumber: "3", xrayVersion: "3.75.12"}, 91 {name: "post-api", buildName: "test-post", buildNumber: "3", xrayVersion: "3.77.0"}, 92 } 93 for _, test := range tests { 94 t.Run(test.name, func(t *testing.T) { 95 xrayDetails.version = test.xrayVersion 96 testsBuildScanService.XrayDetails = xrayDetails 97 scanResponse, noFailBuildPolicy, err := testsBuildScanService.ScanBuild(services.XrayBuildParams{BuildName: test.buildName, BuildNumber: test.buildNumber}, true) 98 assert.NoError(t, err) 99 assert.True(t, noFailBuildPolicy) 100 assert.NotNil(t, scanResponse) 101 }) 102 } 103 } 104 105 func initXrayTest(t *testing.T) { 106 if !*TestXray { 107 t.Skip("Skipping xray test. To run xray test add the '-test.xray=true' option.") 108 } 109 } 110 111 type testXrayDetails struct { 112 auth.ServiceDetails 113 version string 114 } 115 116 func newTestXrayDetails(serviceDetails auth.ServiceDetails) testXrayDetails { 117 return testXrayDetails{ServiceDetails: serviceDetails} 118 } 119 120 func (txd testXrayDetails) GetVersion() (string, error) { 121 return txd.version, nil 122 }