Skip to content

Commit

Permalink
Improve repositores code
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sverdlov <[email protected]>
  • Loading branch information
sverdlov93 committed Nov 6, 2024
1 parent 3dff2bb commit 0f0856e
Showing 1 changed file with 0 additions and 169 deletions.
169 changes: 0 additions & 169 deletions artifactory/commands/utils/npmcmdutils_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package utils

import (
"fmt"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
commonTests "github.com/jfrog/jfrog-cli-core/v2/common/tests"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
)

var testArtifactoryUrl = "https://acme.jfrog.io/artifactory"

func TestGetRegistry(t *testing.T) {
var getRegistryTest = []struct {
repo string
Expand Down Expand Up @@ -80,163 +71,3 @@ func TestGetNpmAuth(t *testing.T) {
})
}
}

// Helper function to set up the BuildToolLogin.
func setupNpmrcManager(buildTool project.ProjectType) *BuildToolLogin {
serverDetails := &config.ServerDetails{
ArtifactoryUrl: testArtifactoryUrl,
}
return NewNpmrcYarnrcManager(buildTool, "my-repo-virtual", serverDetails)
}

// Helper function to create a temporary .npmrc file for isolated tests.
func createTempNpmrc(t *testing.T) string {
// Create a temporary directory for npmrc
tempDir := t.TempDir()

// Set the NPM_CONFIG_USERCONFIG environment variable
tempNpmrcPath := filepath.Join(tempDir, ".npmrc")
t.Setenv("NPM_CONFIG_USERCONFIG", tempNpmrcPath)

return tempNpmrcPath
}

// Helper function to create a temporary .npmrc file for isolated tests.
func createTempYarnrc(t *testing.T) (string, func() error) {
homeDir, err := os.UserHomeDir()
assert.NoError(t, err)
yarnrcPath := filepath.Join(homeDir, ".yarnrc")
restoreYarnrcFunc, err := ioutils.BackupFile(yarnrcPath, ".yarnrc.backup")
assert.NoError(t, err)
return yarnrcPath, restoreYarnrcFunc
}

// Test for configuring registry in npm.
func TestConfigureRegistry_Npm(t *testing.T) {
tempNpmrcPath := createTempNpmrc(t)

nm := setupNpmrcManager(project.Npm)

err := nm.ConfigureRegistry()
assert.NoError(t, err)

// Verify that the correct .npmrc entry was added
fileContent, err := os.ReadFile(tempNpmrcPath)
assert.NoError(t, err)
assert.Contains(t, string(fileContent), "registry="+testArtifactoryUrl)
}

// Test for configuring registry in yarn.
func TestConfigureRegistry_Yarn(t *testing.T) {
yarnrcPath, restoreYarnrcFunc := createTempYarnrc(t)
defer func() {
assert.NoError(t, restoreYarnrcFunc())
}()
nm := setupNpmrcManager(project.Yarn)

err := nm.ConfigureRegistry()
assert.NoError(t, err)

// Verify that the correct .yarnrc entry was added
fileContent, err := os.ReadFile(yarnrcPath)
assert.NoError(t, err)
expectedRegistryLine := fmt.Sprintf("registry \"%s/api/npm/my-repo-virtual\"", testArtifactoryUrl)
assert.Contains(t, string(fileContent), expectedRegistryLine)
}

// Test for setting token auth in npm.
func TestConfigureAuth_Token_Npm(t *testing.T) {
tempNpmrcPath := createTempNpmrc(t)

nm := setupNpmrcManager(project.Npm)

err := nm.handleNpmrcTokenAuth("my-access-token")
assert.NoError(t, err)

// Verify that the correct auth token entry was added to .npmrc
fileContent, err := os.ReadFile(tempNpmrcPath)
assert.NoError(t, err)
expectedAuthLine := fmt.Sprintf("//%s/api/npm/my-repo-virtual:_authToken=my-access-token", strings.TrimPrefix(testArtifactoryUrl, "https://"))
assert.Contains(t, string(fileContent), expectedAuthLine)
}

// Test for setting token auth in yarn.
func TestConfigureAuth_Token_Yarn(t *testing.T) {
yarnrcPath, restoreYarnrcFunc := createTempYarnrc(t)
defer func() {
assert.NoError(t, restoreYarnrcFunc())
}()

nm := setupNpmrcManager(project.Yarn)

err := nm.handleNpmrcTokenAuth("my-access-token")
assert.NoError(t, err)

// Verify that the correct auth token entry was added to .yarnrc
fileContent, err := os.ReadFile(yarnrcPath)
assert.NoError(t, err)
expectedAuthLine := fmt.Sprintf("\"//%s/api/npm/my-repo-virtual:_authToken\" my-access-token", strings.TrimPrefix(testArtifactoryUrl, "https://"))
assert.Contains(t, string(fileContent), expectedAuthLine)
}

func TestHandleNpmrcBasicAuth(t *testing.T) {
// Set up a temporary .npmrc configuration.
tempDir := t.TempDir()
t.Setenv("NPM_CONFIG_USERCONFIG", filepath.Join(tempDir, ".npmrc"))

// Set up the BuildToolLogin for npm build tool.
nm := setupNpmrcManager(project.Npm)

// Actual username and password for testing.
username := "myUser"
password := "myPassword"

// Expected base64 encoded value. (Base64 encoded "myUser:myPassword")
expectedAuthValue := "bXlVc2VyOm15UGFzc3dvcmQ="

// Run the method to handle Basic Auth.
err := nm.handleNpmrcBasicAuth(username, password)
assert.NoError(t, err)

// Read the resulting .npmrc file and verify the auth value.
npmrcContent, err := os.ReadFile(filepath.Join(tempDir, ".npmrc"))
assert.NoError(t, err)

// Verify that the auth key and value are correctly set.
expectedAuthLine := fmt.Sprintf("//%s/api/npm/my-repo-virtual:_auth=\"%s\"", strings.TrimPrefix(testArtifactoryUrl, "https://"), expectedAuthValue)
assert.Contains(t, string(npmrcContent), expectedAuthLine)
}

// Test for handling anonymous access in npm.
func TestHandleAnonymousAccess_Npm(t *testing.T) {
tempNpmrcPath := createTempNpmrc(t)

nm := setupNpmrcManager(project.Npm)
// Set basic auth credentials to be removed on the by the anonymous access method.
assert.NoError(t, nm.handleNpmrcBasicAuth("user", "password"))
assert.FileExists(t, tempNpmrcPath)

err := nm.handleNpmAnonymousAccess()
assert.NoError(t, err)
// Verify that .npmrc was deleted because it should be empty.
assert.NoFileExists(t, tempNpmrcPath)
}

// Test for handling anonymous access in yarn.
func TestHandleAnonymousAccess_Yarn(t *testing.T) {
yarnrcPath, restoreYarnrcFunc := createTempYarnrc(t)
defer func() {
assert.NoError(t, restoreYarnrcFunc())
}()

nm := setupNpmrcManager(project.Yarn)

err := nm.handleNpmAnonymousAccess()
assert.NoError(t, err)

// Verify that the auth entries were removed from .yarnrc
fileContent, err := os.ReadFile(yarnrcPath)
assert.NoError(t, err)
assert.NotContains(t, string(fileContent), "_auth")
assert.NotContains(t, string(fileContent), "_authToken")
}

0 comments on commit 0f0856e

Please sign in to comment.