github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/config_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"encoding/json"
     5  	"github.com/jfrog/jfrog-cli-go/utils/config"
     6  	"github.com/jfrog/jfrog-cli-go/utils/log"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func init() {
    12  	log.SetDefaultLogger()
    13  }
    14  
    15  func TestBasicAuth(t *testing.T) {
    16  	inputDetails := config.ArtifactoryDetails{
    17  		Url:             "http://localhost:8080/artifactory",
    18  		DistributionUrl: "http://localhost:8080/distribution",
    19  		User:            "admin", Password: "password",
    20  		ApiKey: "", SshKeyPath: "", AccessToken: "",
    21  		ServerId:  "test",
    22  		IsDefault: false}
    23  	configAndTest(t, &inputDetails)
    24  }
    25  
    26  func TestApiKey(t *testing.T) {
    27  	// API key is no longer allowed to be configured without providing a username.
    28  	// This test is here to make sure that old configurations (with API key and no username) are still accepted.
    29  	inputDetails := config.ArtifactoryDetails{
    30  		Url:             "http://localhost:8080/artifactory",
    31  		DistributionUrl: "http://localhost:8080/distribution",
    32  		User:            "", Password: "",
    33  		ApiKey: "apiKey", SshKeyPath: "", AccessToken: "",
    34  		ServerId:  "test",
    35  		IsDefault: false}
    36  	configAndTest(t, &inputDetails)
    37  
    38  	inputDetails = config.ArtifactoryDetails{
    39  		Url:             "http://localhost:8080/artifactory",
    40  		DistributionUrl: "http://localhost:8080/distribution",
    41  		User:            "admin", Password: "",
    42  		ApiKey: "apiKey", SshKeyPath: "", AccessToken: "",
    43  		ServerId:  "test",
    44  		IsDefault: false}
    45  	configAndTest(t, &inputDetails)
    46  }
    47  
    48  func TestSshKey(t *testing.T) {
    49  	inputDetails := config.ArtifactoryDetails{
    50  		Url:             "ssh://localhost:1339/",
    51  		DistributionUrl: "http://localhost:1339/distribution",
    52  		User:            "", Password: "",
    53  		ApiKey: "", SshKeyPath: "/tmp/sshKey", AccessToken: "",
    54  		ServerId:  "test",
    55  		IsDefault: false}
    56  	configAndTest(t, &inputDetails)
    57  }
    58  
    59  func TestAccessToken(t *testing.T) {
    60  	inputDetails := config.ArtifactoryDetails{
    61  		Url:             "http://localhost:8080/artifactory",
    62  		DistributionUrl: "http://localhost:8080/distribution",
    63  		User:            "", Password: "",
    64  		ApiKey: "", SshKeyPath: "", AccessToken: "accessToken",
    65  		ServerId:  "test",
    66  		IsDefault: false}
    67  	configAndTest(t, &inputDetails)
    68  }
    69  
    70  func TestRefreshToken(t *testing.T) {
    71  	inputDetails := config.ArtifactoryDetails{
    72  		Url:             "http://localhost:8080/artifactory",
    73  		DistributionUrl: "http://localhost:8080/distribution",
    74  		User:            "", Password: "",
    75  		ApiKey: "", SshKeyPath: "", AccessToken: "accessToken", RefreshToken: "refreshToken",
    76  		ServerId:  "test",
    77  		IsDefault: false}
    78  	configAndTest(t, &inputDetails)
    79  
    80  	inputDetails = config.ArtifactoryDetails{
    81  		Url:             "http://localhost:8080/artifactory",
    82  		DistributionUrl: "http://localhost:8080/distribution",
    83  		User:            "user", Password: "pass",
    84  		ApiKey: "", SshKeyPath: "", AccessToken: "", RefreshToken: "", TokenRefreshInterval: 10,
    85  		ServerId:  "test",
    86  		IsDefault: false}
    87  	configAndTest(t, &inputDetails)
    88  }
    89  
    90  func TestEmpty(t *testing.T) {
    91  	inputDetails := config.ArtifactoryDetails{
    92  		Url:             "http://localhost:8080/artifactory",
    93  		DistributionUrl: "http://localhost:8080/distribution",
    94  		User:            "", Password: "",
    95  		ApiKey: "", SshKeyPath: "", AccessToken: "",
    96  		ServerId:  "test",
    97  		IsDefault: false}
    98  	configAndTest(t, &inputDetails)
    99  }
   100  
   101  func configAndTest(t *testing.T, inputDetails *config.ArtifactoryDetails) {
   102  	configCmd := NewConfigCommand().SetDetails(inputDetails).SetServerId("test")
   103  	err := configCmd.Config()
   104  	if err != nil {
   105  		t.Error(err.Error())
   106  	}
   107  	outputConfig, err := GetConfig("test")
   108  	if err != nil {
   109  		t.Error(err.Error())
   110  	}
   111  	if configStructToString(inputDetails) != configStructToString(outputConfig) {
   112  		t.Error("Unexpected configuration was saved to file. Expected: " + configStructToString(inputDetails) + " Got " + configStructToString(outputConfig))
   113  	}
   114  	err = DeleteConfig("test")
   115  	if err != nil {
   116  		t.Error(err.Error())
   117  	}
   118  	testExportImport(t, inputDetails)
   119  }
   120  
   121  func configStructToString(artConfig *config.ArtifactoryDetails) string {
   122  	artConfig.IsDefault = false
   123  	marshaledStruct, _ := json.Marshal(*artConfig)
   124  	return string(marshaledStruct)
   125  }
   126  
   127  func TestGetConfigurationFromUser(t *testing.T) {
   128  	inputDetails := config.ArtifactoryDetails{
   129  		Url:             "http://localhost:8080/artifactory",
   130  		DistributionUrl: "http://localhost:8080/distribution",
   131  		User:            "admin", Password: "password",
   132  		ApiKey: "", SshKeyPath: "", AccessToken: "",
   133  		ServerId:  "test",
   134  		IsDefault: false}
   135  
   136  	configCmd := NewConfigCommand().SetDetails(&inputDetails).SetDefaultDetails(&inputDetails)
   137  	err := configCmd.getConfigurationFromUser()
   138  	if err != nil {
   139  		t.Error(err)
   140  	}
   141  
   142  	if !strings.HasSuffix(inputDetails.GetUrl(), "/") {
   143  		t.Error("Expected url to end with /")
   144  	}
   145  }
   146  
   147  func testExportImport(t *testing.T, inputDetails *config.ArtifactoryDetails) {
   148  	serverToken, err := config.Export(inputDetails)
   149  	if err != nil {
   150  		t.Error(err.Error())
   151  	}
   152  	outputDetails, err := config.Import(serverToken)
   153  	if err != nil {
   154  		t.Error(err.Error())
   155  	}
   156  	if configStructToString(inputDetails) != configStructToString(outputDetails) {
   157  		t.Error("Unexpected configuration was saved to file. Expected: " + configStructToString(inputDetails) + " Got " + configStructToString(outputDetails))
   158  	}
   159  }