Skip to content

Commit

Permalink
initial test for npm- works
Browse files Browse the repository at this point in the history
  • Loading branch information
eranturgeman committed Dec 7, 2023
1 parent d82831f commit e243e44
Showing 1 changed file with 83 additions and 4 deletions.
87 changes: 83 additions & 4 deletions xray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
biutils "github.com/jfrog/build-info-go/utils"
"github.com/jfrog/jfrog-cli-core/v2/xray/commands/audit/sca/npm"
"github.com/jfrog/jfrog-cli-core/v2/xray/scangraph"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -56,6 +57,10 @@ var (

func InitXrayTests() {
initXrayCli()
initArtifactoryCli()
cleanUpOldRepositories()
tests.AddTimestampToGlobalVars()
createRequiredRepos()
}

func authenticateXray() string {
Expand Down Expand Up @@ -836,24 +841,27 @@ func TestCurationAudit(t *testing.T) {
}
serverMock, config := curationServer(t, expectedRequest, requestToFail)

cleanUpJfrogHome, err := coretests.SetJfrogHome()
cleanUpJfrogHome, err := coretests.SetJfrogHome() //TODO dont forget to create jfrog home in the test
assert.NoError(t, err)
defer cleanUpJfrogHome()

config.User = "admin"
config.Password = "password"
config.ServerId = "test"
//TODO this creates the server, but a mock server
configCmd := coreCmd.NewConfigCommand(coreCmd.AddOrEdit, "test").SetDetails(config).SetUseBasicAuthOnly(true).SetInteractive(false)
assert.NoError(t, configCmd.Run())

// todo make sure that the artifactory repo that is created for the test is also deleted after the test
//todo remember that in GO we must download from virtualExample
defer serverMock.Close()
// Create build config
resolutionServerId := "server-id-resolve"
resolutionServerId := "server-id-resolve" //todo no need
deploymentServerId := "server-id-deploy"
resolutionRepo := "repo-resolve"
deploymentRepo := "repo-deploy"
deploymentRepo := "repo-deploy" //todo no need
context := createContext(t, resolutionServerId+"="+config.ServerId, resolutionRepo+"=npms", deploymentServerId+"="+config.ServerId, deploymentRepo+"=npm-local", "global=false")
err = artCmdUtils.CreateBuildConfig(context, artUtils.Npm)
err = artCmdUtils.CreateBuildConfig(context, artUtils.Npm) // TODO look here for artifactory tests
assert.NoError(t, err)

localXrayCli := xrayCli.WithoutCredentials()
Expand Down Expand Up @@ -950,3 +958,74 @@ func setStringFlags(flagSet *flag.FlagSet, flags ...string) []string {
}
return cmdFlags
}

// We test resolution from an Artifactory server using BuildDependencyTree that resolves dependencies before building the tree
func TestDependencyResolutionFromArtifactory(t *testing.T) {
/*
testCases := []struct {
testProjectPath []string
repoName string
projectType artUtils.ProjectType
}{
{
testProjectPath: []string{"npm", "npmproject"},
repoName: tests.NpmRemoteRepo,
projectType: artUtils.Npm,
},
}
*/

initXrayTest(t, "") // checks for the -test.xray flag and for minVersion if needed

tempDirPath, createTempDirCallback := coretests.CreateTempDirWithCallbackAndAssert(t)
defer createTempDirCallback()
testProjectPath := filepath.Join(filepath.FromSlash(tests.GetTestResourcesPath()), "npm", "npmproject") //TODO here
assert.NoError(t, biutils.CopyDir(testProjectPath, tempDirPath, true, nil))
rootDir, err := os.Getwd()
require.NoError(t, err)
require.NoError(t, os.Chdir(filepath.Join(tempDirPath)))
defer func() {
assert.NoError(t, os.Chdir(rootDir))
}()

// use SetJfrogHome and then revert it with defer - this is needed so the tests will not change the home path for jfrog
cleanUpJfrogHome, err := coretests.SetJfrogHome()
assert.NoError(t, err)
defer cleanUpJfrogHome()

// Create config command using NewConfigCommand. this replaces running the command of jf config that should run with a prompt
server := &config.ServerDetails{
Url: *tests.JfrogUrl,
ArtifactoryUrl: *tests.JfrogUrl + tests.ArtifactoryEndpoint,
AccessToken: *tests.JfrogAccessToken,
ServerId: "test",
}

configCmd := coreCmd.NewConfigCommand(coreCmd.AddOrEdit, "test").SetDetails(serverDetails).SetUseBasicAuthOnly(true).SetInteractive(false)
assert.NoError(t, configCmd.Run())

// Create build config using createContext. this replaces jf npm-config that should run with a prompt
context := createContext(t, "repo-resolve="+tests.NpmRemoteRepo, "server-id-resolve="+serverDetails.ServerId, "global=false") //TODO here
err = artCmdUtils.CreateBuildConfig(context, artUtils.Npm) //TODO here
assert.NoError(t, err)

// make a search command on the repo's cache and check that it is empty. use --fail-no-op flag
artifactoryPathToSearch := tests.NpmRemoteRepo + "-cache/*" //TODO here
output := artifactoryCli.RunCliCmdWithOutput(t, "s", artifactoryPathToSearch)
// We check the repo's cache is empty before resolving from artifactory
assert.Equal(t, "[]\n", output)

// run the install command that should fill the cache (see about flag that should ignore cache).

// We run BuildDependencyTree on an un-installed project. Since we set an Artifactory server and repository we expect the dependencies will be resolved from there
auditParams := (&utils.AuditBasicParams{}).SetServerDetails(server).SetDepsRepo(tests.NpmRemoteRepo) //TODO here
_, _, err = npm.BuildDependencyTree(auditParams) //TODO here
assert.NoError(t, err)

// make another search to make sure the cache if filled with the resolved dependencies. use --fail-no-op flag
// After resolving from Artifactory we expect the repo's cache to be non-empty
output = artifactoryCli.RunCliCmdWithOutput(t, "s", artifactoryPathToSearch)
// We check the repo's cache is empty before resolving from artifactory
assert.NotEqual(t, "[]\n", output)
}

0 comments on commit e243e44

Please sign in to comment.