From fb4303d861769bbf517b1a33a252d8a220f12088 Mon Sep 17 00:00:00 2001 From: Hadar Shpivak Date: Mon, 30 Sep 2024 21:47:49 +0300 Subject: [PATCH] add xrayUrlOnly to audit_test --- audit_test.go | 36 +++++++++++++++++++++++++++++++++++- scans_test.go | 2 +- tests/utils/test_config.go | 14 +++++++++----- tests/utils/test_utils.go | 4 ++-- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/audit_test.go b/audit_test.go index cd8ed215..0a0648c2 100644 --- a/audit_test.go +++ b/audit_test.go @@ -444,7 +444,7 @@ func addDummyPackageDescriptor(t *testing.T, hasPackageJson bool) { // JAS func TestXrayAuditNotEntitledForJas(t *testing.T) { - cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, getNoJasAuditMockCommand) + cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, false, getNoJasAuditMockCommand) defer cleanUp() output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false) // Verify that scan results are printed @@ -590,3 +590,37 @@ func TestAuditOnEmptyProject(t *testing.T) { output := securityTests.PlatformCli.WithoutCredentials().RunCliCmdWithOutput(t, "audit", "--format="+string(format.SimpleJson)) securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0, 0, 0) } + +//xray-url only + +func TestXrayAuditNotEntitledForJasWithXrayUrl(t *testing.T) { + cliToRun, cleanUp := securityTestUtils.InitTestWithMockCommandOrParams(t, true, getNoJasAuditMockCommandWithXrayUrl) + defer cleanUp() + output := testXrayAuditJas(t, cliToRun, filepath.Join("jas", "jas"), "3", false) + // Verify that scan results are printed + securityTestUtils.VerifySimpleJsonScanResults(t, output, 0, 8, 0) + // Verify that JAS results are not printed + securityTestUtils.VerifySimpleJsonJasResults(t, output, 0, 0, 0, 0, 0, 0, 0, 0, 0) +} + +func getNoJasAuditMockCommandWithXrayUrl() components.Command { + return components.Command{ + Name: docs.Audit, + Flags: docs.GetCommandFlags(docs.Audit), + Action: func(c *components.Context) error { + auditCmd, err := cli.CreateAuditCmd(c) + if err != nil { + return err + } + // Disable Jas for this test + auditCmd.SetUseJas(false) + return progressbar.ExecWithProgress(auditCmd) + }, + } +} + +func TestXrayAuditJasSimpleJsonWithXrayUrl(t *testing.T) { + output := testXrayAuditJas(t, securityTests.PlatformCli, filepath.Join("jas", "jas"), "3", false) + securityTestUtils.VerifySimpleJsonScanResults(t, output, 0, 8, 0) + securityTestUtils.VerifySimpleJsonJasResults(t, output, 1, 9, 6, 3, 1, 1, 2, 0, 0) +} diff --git a/scans_test.go b/scans_test.go index 1cba58c1..8d7339ea 100644 --- a/scans_test.go +++ b/scans_test.go @@ -142,7 +142,7 @@ func initNativeDockerWithXrayTest(t *testing.T) (mockCli *coreTests.JfrogCli, cl if !*securityTests.TestDockerScan || !*securityTests.TestSecurity { t.Skip("Skipping Docker scan test. To run Xray Docker test add the '-test.dockerScan=true' and '-test.security=true' options.") } - return securityTestUtils.InitTestWithMockCommandOrParams(t, cli.DockerScanMockCommand) + return securityTestUtils.InitTestWithMockCommandOrParams(t, false, cli.DockerScanMockCommand) } func runDockerScan(t *testing.T, testCli *coreTests.JfrogCli, imageName, watchName string, minViolations, minVulnerabilities, minLicenses int, minInactives int, validateSecrets bool) { diff --git a/tests/utils/test_config.go b/tests/utils/test_config.go index fcf1e6c7..3ba75920 100644 --- a/tests/utils/test_config.go +++ b/tests/utils/test_config.go @@ -53,18 +53,22 @@ func InitTestCliDetails() { configTests.TestApplication = &testApplication if configTests.PlatformCli == nil { - configTests.PlatformCli = GetTestCli(testApplication) + configTests.PlatformCli = GetTestCli(testApplication, false) } } -func GetTestCli(testApplication components.App) (testCli *coreTests.JfrogCli) { - creds := authenticateXray() +func GetTestCli(testApplication components.App, xrayUrlOnly bool) (testCli *coreTests.JfrogCli) { + creds := authenticateXray(xrayUrlOnly) return coreTests.NewJfrogCli(func() error { return plugins.RunCliWithPlugin(testApplication)() }, "", creds) } -func authenticateXray() string { +func authenticateXray(xrayUrlOnly bool) string { *configTests.JfrogUrl = clientUtils.AddTrailingSlashIfNeeded(*configTests.JfrogUrl) - configTests.XrDetails = &config.ServerDetails{Url: *configTests.JfrogUrl, ArtifactoryUrl: *configTests.JfrogUrl + configTests.ArtifactoryEndpoint, XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint} + if xrayUrlOnly { + configTests.XrDetails = &config.ServerDetails{XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint} + } else { + configTests.XrDetails = &config.ServerDetails{Url: *configTests.JfrogUrl, ArtifactoryUrl: *configTests.JfrogUrl + configTests.ArtifactoryEndpoint, XrayUrl: *configTests.JfrogUrl + configTests.XrayEndpoint} + } cred := fmt.Sprintf("--url=%s", configTests.XrDetails.XrayUrl) if *configTests.JfrogAccessToken != "" { configTests.XrDetails.AccessToken = *configTests.JfrogAccessToken diff --git a/tests/utils/test_utils.go b/tests/utils/test_utils.go index 3aae46c0..3b95cf51 100644 --- a/tests/utils/test_utils.go +++ b/tests/utils/test_utils.go @@ -50,7 +50,7 @@ func InitSecurityTest(t *testing.T, xrayMinVersion string) { ValidateXrayVersion(t, xrayMinVersion) } -func InitTestWithMockCommandOrParams(t *testing.T, mockCommands ...func() components.Command) (mockCli *coreTests.JfrogCli, cleanUp func()) { +func InitTestWithMockCommandOrParams(t *testing.T, xrayUrlOnly bool, mockCommands ...func() components.Command) (mockCli *coreTests.JfrogCli, cleanUp func()) { oldHomeDir := os.Getenv(coreutils.HomeDir) // Create server config to use with the command. CreateJfrogHomeConfig(t, true) @@ -59,7 +59,7 @@ func InitTestWithMockCommandOrParams(t *testing.T, mockCommands ...func() compon for _, mockCommand := range mockCommands { commands = append(commands, mockCommand()) } - return GetTestCli(components.CreateEmbeddedApp("security", commands)), func() { + return GetTestCli(components.CreateEmbeddedApp("security", commands), xrayUrlOnly), func() { clientTests.SetEnvAndAssert(t, coreutils.HomeDir, oldHomeDir) } }