github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/commands/npm/npmcommand_test.go (about) 1 package npm 2 3 import ( 4 "fmt" 5 biutils "github.com/jfrog/build-info-go/utils" 6 "github.com/jfrog/gofrog/version" 7 commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests" 8 "github.com/jfrog/jfrog-cli-core/v2/utils/tests" 9 testsUtils "github.com/jfrog/jfrog-client-go/utils/tests" 10 "github.com/stretchr/testify/assert" 11 "net/http" 12 "os" 13 "path/filepath" 14 "strings" 15 "testing" 16 ) 17 18 // #nosec G101 -- Dummy token for tests. 19 const authToken = "YWRtaW46QVBCN1ZkZFMzN3NCakJiaHRGZThVb0JlZzFl" 20 21 func TestPrepareConfigData(t *testing.T) { 22 configBefore := []byte( 23 "json=true\n" + 24 "user-agent=npm/5.5.1 node/v8.9.1 darwin x64\n" + 25 "metrics-registry=http://somebadregistry\nscope=\n" + 26 "//reg=ddddd\n" + 27 "@jfrog:registry=http://somebadregistry\n" + 28 "registry=http://somebadregistry\n" + 29 "email=ddd@dd.dd\n" + 30 "allow-same-version=false\n" + 31 "cache-lock-retries=10") 32 33 expectedConfig := 34 []string{ 35 "json = true", 36 "allow-same-version=false", 37 "user-agent=npm/5.5.1 node/v8.9.1 darwin x64", 38 "@jfrog:registry = http://goodRegistry", 39 "email=ddd@dd.dd", 40 "cache-lock-retries=10", 41 "registry = http://goodRegistry", 42 } 43 44 npmi := NpmCommand{registry: "http://goodRegistry", jsonOutput: true, npmAuth: "_auth = " + authToken, npmVersion: version.NewVersion("9.5.0")} 45 configAfter, err := npmi.prepareConfigData(configBefore) 46 if err != nil { 47 t.Error(err) 48 } 49 actualConfigArray := strings.Split(string(configAfter), "\n") 50 for _, eConfig := range expectedConfig { 51 found := false 52 for _, aConfig := range actualConfigArray { 53 if aConfig == eConfig { 54 found = true 55 break 56 } 57 } 58 if !found { 59 t.Errorf("The expected config: %s is missing from the actual configuration list:\n %s", eConfig, actualConfigArray) 60 } 61 } 62 63 // Assert that NPM_CONFIG__AUTH environment variable was set 64 assert.Equal(t, authToken, os.Getenv(fmt.Sprintf(npmConfigAuthEnv, "//goodRegistry"))) 65 testsUtils.UnSetEnvAndAssert(t, fmt.Sprintf(npmConfigAuthEnv, "//goodRegistry")) 66 } 67 68 func TestSetNpmConfigAuthEnv(t *testing.T) { 69 testCases := []struct { 70 name string 71 npmCm *NpmCommand 72 value string 73 expectedEnv string 74 }{ 75 { 76 name: "set scoped registry auth env", 77 npmCm: &NpmCommand{ 78 npmVersion: version.NewVersion("9.3.1"), 79 registry: "https://registry.example.com", 80 }, 81 value: "some_auth_token", 82 expectedEnv: "npm_config_//registry.example.com:_auth", 83 }, 84 { 85 name: "set legacy auth env", 86 npmCm: &NpmCommand{ 87 npmVersion: version.NewVersion("8.19.3"), 88 registry: "https://registry.example.com", 89 }, 90 value: "some_auth_token", 91 expectedEnv: "npm_config__auth", 92 }, 93 } 94 95 for _, tc := range testCases { 96 t.Run(tc.name, func(t *testing.T) { 97 err := tc.npmCm.setNpmConfigAuthEnv(tc.value) 98 assert.NoError(t, err) 99 envValue := os.Getenv(tc.expectedEnv) 100 assert.Equal(t, tc.value, envValue) 101 assert.NoError(t, os.Unsetenv(tc.expectedEnv)) 102 }) 103 } 104 } 105 106 func TestSetArtifactoryAsResolutionServer(t *testing.T) { 107 tmpDir, createTempDirCallback := tests.CreateTempDirWithCallbackAndAssert(t) 108 defer createTempDirCallback() 109 110 npmProjectPath := filepath.Join("..", "..", "..", "tests", "testdata", "npm-project") 111 err := biutils.CopyDir(npmProjectPath, tmpDir, false, nil) 112 assert.NoError(t, err) 113 114 cwd, err := os.Getwd() 115 assert.NoError(t, err) 116 chdirCallback := testsUtils.ChangeDirWithCallback(t, cwd, tmpDir) 117 defer chdirCallback() 118 119 // Prepare mock server 120 testServer, serverDetails, _ := commonTests.CreateRtRestsMockServer(t, func(w http.ResponseWriter, r *http.Request) { 121 if r.RequestURI == "/api/system/version" { 122 w.WriteHeader(http.StatusOK) 123 _, err = w.Write([]byte("{\"version\" : \"7.75.4\"}")) 124 assert.NoError(t, err) 125 } 126 }) 127 defer testServer.Close() 128 129 depsRepo := "my-rt-resolution-repo" 130 131 clearResolutionServerFunc, err := SetArtifactoryAsResolutionServer(serverDetails, depsRepo) 132 assert.NoError(t, err) 133 assert.NotNil(t, clearResolutionServerFunc) 134 defer func() { 135 assert.NoError(t, clearResolutionServerFunc()) 136 }() 137 138 assert.FileExists(t, filepath.Join(tmpDir, ".npmrc")) 139 }