Skip to content

Commit

Permalink
add xrayUrlOnly to audit_test
Browse files Browse the repository at this point in the history
  • Loading branch information
hadarshjfrog committed Sep 30, 2024
1 parent 80e61ff commit fb4303d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
36 changes: 35 additions & 1 deletion audit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion scans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions tests/utils/test_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down

0 comments on commit fb4303d

Please sign in to comment.