github.com/osievert/jfrog-cli-core@v1.2.7/artifactory/commands/config_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/jfrog/jfrog-cli-core/utils/config"
     9  	"github.com/jfrog/jfrog-cli-core/utils/coreutils"
    10  	"github.com/jfrog/jfrog-cli-core/utils/log"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func init() {
    15  	log.SetDefaultLogger()
    16  }
    17  
    18  func TestBasicAuth(t *testing.T) {
    19  	inputDetails := config.ArtifactoryDetails{
    20  		Url:             "http://localhost:8080/artifactory",
    21  		DistributionUrl: "http://localhost:8080/distribution",
    22  		User:            "admin", Password: "password",
    23  		ApiKey: "", SshKeyPath: "", AccessToken: "",
    24  		ServerId:  "test",
    25  		IsDefault: false}
    26  	configAndTest(t, &inputDetails)
    27  }
    28  
    29  func TestUsernameSavedLowercase(t *testing.T) {
    30  	inputDetails := config.ArtifactoryDetails{
    31  		Url:             "http://localhost:8080/artifactory",
    32  		DistributionUrl: "http://localhost:8080/distribution",
    33  		User:            "ADMIN", Password: "password",
    34  		ApiKey: "", SshKeyPath: "", AccessToken: "",
    35  		ServerId:  "test",
    36  		IsDefault: false}
    37  
    38  	outputConfig, err := configAndGetTestServer(t, &inputDetails, false)
    39  	assert.NoError(t, err)
    40  	assert.Equal(t, outputConfig.User, "admin", "The config command is supposed to save username as lowercase")
    41  }
    42  
    43  func TestApiKey(t *testing.T) {
    44  	// API key is no longer allowed to be configured without providing a username.
    45  	// This test is here to make sure that old configurations (with API key and no username) are still accepted.
    46  	inputDetails := config.ArtifactoryDetails{
    47  		Url:             "http://localhost:8080/artifactory",
    48  		DistributionUrl: "http://localhost:8080/distribution",
    49  		User:            "", Password: "",
    50  		ApiKey: "apiKey", SshKeyPath: "", AccessToken: "",
    51  		ServerId:  "test",
    52  		IsDefault: false}
    53  	configAndTest(t, &inputDetails)
    54  
    55  	inputDetails = config.ArtifactoryDetails{
    56  		Url:             "http://localhost:8080/artifactory",
    57  		DistributionUrl: "http://localhost:8080/distribution",
    58  		User:            "admin", Password: "",
    59  		ApiKey: "apiKey", SshKeyPath: "", AccessToken: "",
    60  		ServerId:  "test",
    61  		IsDefault: false}
    62  	configAndTest(t, &inputDetails)
    63  }
    64  
    65  func TestSshKey(t *testing.T) {
    66  	inputDetails := config.ArtifactoryDetails{
    67  		Url:             "ssh://localhost:1339/",
    68  		DistributionUrl: "http://localhost:1339/distribution",
    69  		User:            "", Password: "",
    70  		ApiKey: "", SshKeyPath: "/tmp/sshKey", AccessToken: "",
    71  		ServerId:  "test",
    72  		IsDefault: false}
    73  	configAndTest(t, &inputDetails)
    74  }
    75  
    76  func TestAccessToken(t *testing.T) {
    77  	inputDetails := config.ArtifactoryDetails{
    78  		Url:             "http://localhost:8080/artifactory",
    79  		DistributionUrl: "http://localhost:8080/distribution",
    80  		User:            "", Password: "",
    81  		ApiKey: "", SshKeyPath: "", AccessToken: "accessToken",
    82  		ServerId:  "test",
    83  		IsDefault: false}
    84  	configAndTest(t, &inputDetails)
    85  }
    86  
    87  func TestRefreshToken(t *testing.T) {
    88  	// Import after tokens were generated.
    89  	inputDetails := config.ArtifactoryDetails{
    90  		Url:             "http://localhost:8080/artifactory",
    91  		DistributionUrl: "http://localhost:8080/distribution",
    92  		User:            "user", Password: "pass",
    93  		ApiKey: "", SshKeyPath: "", AccessToken: "accessToken", RefreshToken: "refreshToken",
    94  		ServerId:  "test",
    95  		IsDefault: false}
    96  	configAndTest(t, &inputDetails)
    97  
    98  	// Import before tokens were generated.
    99  	inputDetails = config.ArtifactoryDetails{
   100  		Url:             "http://localhost:8080/artifactory",
   101  		DistributionUrl: "http://localhost:8080/distribution",
   102  		User:            "user", Password: "pass",
   103  		ApiKey: "", SshKeyPath: "", AccessToken: "", RefreshToken: "", TokenRefreshInterval: 10,
   104  		ServerId:  "test",
   105  		IsDefault: false}
   106  	configAndTest(t, &inputDetails)
   107  }
   108  
   109  func TestEmpty(t *testing.T) {
   110  	inputDetails := config.ArtifactoryDetails{
   111  		Url:             "http://localhost:8080/artifactory",
   112  		DistributionUrl: "http://localhost:8080/distribution",
   113  		User:            "", Password: "",
   114  		ApiKey: "", SshKeyPath: "", AccessToken: "",
   115  		ServerId:  "test",
   116  		IsDefault: false}
   117  	configAndTest(t, &inputDetails)
   118  }
   119  
   120  func configAndTest(t *testing.T, inputDetails *config.ArtifactoryDetails) {
   121  	outputConfig, err := configAndGetTestServer(t, inputDetails, false)
   122  	assert.NoError(t, err)
   123  	assert.Equal(t, configStructToString(inputDetails), configStructToString(outputConfig), "unexpected configuration was saved to file")
   124  	assert.NoError(t, DeleteConfig("test"))
   125  	testExportImport(t, inputDetails)
   126  }
   127  
   128  func configAndGetTestServer(t *testing.T, inputDetails *config.ArtifactoryDetails, basicAuthOnly bool) (*config.ArtifactoryDetails, error) {
   129  	configCmd := NewConfigCommand().SetDetails(inputDetails).SetServerId("test").SetUseBasicAuthOnly(basicAuthOnly)
   130  	assert.NoError(t, configCmd.Config())
   131  	return GetConfig("test", false)
   132  }
   133  
   134  func configStructToString(artConfig *config.ArtifactoryDetails) string {
   135  	artConfig.IsDefault = false
   136  	marshaledStruct, _ := json.Marshal(*artConfig)
   137  	return string(marshaledStruct)
   138  }
   139  
   140  func TestEscapingUrlInConfigurationFromUser(t *testing.T) {
   141  	inputDetails := config.ArtifactoryDetails{
   142  		Url:             "http://localhost:8080/artifactory",
   143  		DistributionUrl: "http://localhost:8080/distribution",
   144  		User:            "admin", Password: "password",
   145  		ApiKey: "", SshKeyPath: "", AccessToken: "",
   146  		ServerId: "test", ClientCertPath: "test/cert/path", ClientCertKeyPath: "test/cert/key/path",
   147  		IsDefault: false}
   148  
   149  	configCmd := NewConfigCommand().SetDetails(&inputDetails).SetDefaultDetails(&inputDetails).SetUseBasicAuthOnly(true)
   150  	assert.NoError(t, configCmd.getConfigurationFromUser())
   151  	assert.True(t, strings.HasSuffix(inputDetails.GetUrl(), "/"), "expected url to end with /")
   152  }
   153  
   154  func TestBasicAuthOnlyOption(t *testing.T) {
   155  	inputDetails := config.ArtifactoryDetails{
   156  		Url:  "http://localhost:8080/artifactory",
   157  		User: "admin", Password: "password",
   158  		ServerId: "test", IsDefault: false}
   159  
   160  	// Verify setting the option disables refreshable tokens.
   161  	outputConfig, err := configAndGetTestServer(t, &inputDetails, true)
   162  	assert.NoError(t, err)
   163  	assert.Equal(t, coreutils.TokenRefreshDisabled, outputConfig.TokenRefreshInterval, "expected refreshable token to be disabled")
   164  	assert.NoError(t, DeleteConfig("test"))
   165  
   166  	// Verify setting the option enables refreshable tokens.
   167  	outputConfig, err = configAndGetTestServer(t, &inputDetails, false)
   168  	assert.NoError(t, err)
   169  	assert.Equal(t, coreutils.TokenRefreshDefaultInterval, outputConfig.TokenRefreshInterval, "expected refreshable token to be enabled")
   170  	assert.NoError(t, DeleteConfig("test"))
   171  }
   172  
   173  func testExportImport(t *testing.T, inputDetails *config.ArtifactoryDetails) {
   174  	serverToken, err := config.Export(inputDetails)
   175  	assert.NoError(t, err)
   176  	outputDetails, err := config.Import(serverToken)
   177  	assert.NoError(t, err)
   178  	assert.Equal(t, configStructToString(inputDetails), configStructToString(outputDetails), "unexpected configuration was saved to file")
   179  }