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

     1  package nuget
     2  
     3  import (
     4  	"encoding/xml"
     5  	"github.com/jfrog/gofrog/io"
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/utils/nuget"
     7  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
     8  	"github.com/jfrog/jfrog-cli-go/utils/config"
     9  	"github.com/jfrog/jfrog-cli-go/utils/log"
    10  	"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
    11  	"io/ioutil"
    12  	"os"
    13  	"reflect"
    14  	"testing"
    15  )
    16  
    17  func TestGetFlagValueExists(t *testing.T) {
    18  	tests := []struct {
    19  		name              string
    20  		currentConfigPath string
    21  		createConfig      bool
    22  		expectErr         bool
    23  		cmdFlags          []string
    24  		expectedCmdFlags  []string
    25  	}{
    26  		{"simple", "file.config", true, false,
    27  			[]string{"-configFile", "file.config"}, []string{"-configFile", "file.config"}},
    28  
    29  		{"simple2", "file.config", true, false,
    30  			[]string{"-before", "-configFile", "file.config", "after"}, []string{"-before", "-configFile", "file.config", "after"}},
    31  
    32  		{"err", "file.config", false, true,
    33  			[]string{"-before", "-configFile"}, []string{"-before", "-configFile"}},
    34  
    35  		{"err2", "file.config", false, true,
    36  			[]string{"-configFile"}, []string{"-configFile"}},
    37  	}
    38  	for _, test := range tests {
    39  		t.Run(test.name, func(t *testing.T) {
    40  			if test.createConfig {
    41  				_, err := io.CreateRandFile(test.currentConfigPath, 0)
    42  				if err != nil {
    43  					t.Error(err)
    44  				}
    45  				defer os.Remove(test.currentConfigPath)
    46  			}
    47  			c := &nuget.Cmd{CommandFlags: test.cmdFlags}
    48  			_, err := getFlagValueIfExists("-configfile", c)
    49  			if err != nil && !test.expectErr {
    50  				t.Error(err)
    51  			}
    52  			if err == nil && test.expectErr {
    53  				t.Errorf("Expecting: error, Got: nil")
    54  			}
    55  			if !reflect.DeepEqual(c.CommandFlags, test.expectedCmdFlags) {
    56  				t.Errorf("Expecting: %s, Got: %s", test.expectedCmdFlags, c.CommandFlags)
    57  			}
    58  		})
    59  	}
    60  }
    61  
    62  func TestInitNewConfig(t *testing.T) {
    63  	log.SetDefaultLogger()
    64  	if !cliutils.IsWindows() {
    65  		t.Skip("Skipping nuget tests, since this is not a Windows machine.")
    66  	}
    67  
    68  	tempDirPath, err := fileutils.CreateTempDir()
    69  	if err != nil {
    70  		t.Error(err)
    71  	}
    72  	defer fileutils.RemoveTempDir(tempDirPath)
    73  
    74  	c := &nuget.Cmd{}
    75  	params := &NugetCommandArgs{rtDetails: &config.ArtifactoryDetails{Url: "http://some/url", User: "user", Password: "password"}}
    76  	configFile, err := writeToTempConfigFile(c, tempDirPath)
    77  	if err != nil {
    78  		t.Error(err)
    79  	}
    80  
    81  	// Prepare the config file with NuGet authentication
    82  	err = params.addNugetAuthenticationToNewConfig(configFile)
    83  	if err != nil {
    84  		t.Error(err)
    85  	}
    86  
    87  	content, err := ioutil.ReadFile(configFile.Name())
    88  	if err != nil {
    89  		t.Error(err)
    90  	}
    91  
    92  	nugetConfig := NugetConfig{}
    93  	err = xml.Unmarshal(content, &nugetConfig)
    94  	if err != nil {
    95  		t.Error("Unmarshalling failed with an error:", err.Error())
    96  	}
    97  
    98  	if len(nugetConfig.PackageSources) != 1 {
    99  		t.Error("Expected one package sources, got", len(nugetConfig.PackageSources))
   100  	}
   101  
   102  	source := "http://some/url/api/nuget"
   103  
   104  	for _, packageSource := range nugetConfig.PackageSources {
   105  		if packageSource.Key != sourceName {
   106  			t.Error("Expected", sourceName, ",got", packageSource.Key)
   107  		}
   108  
   109  		if packageSource.Value != source {
   110  			t.Error("Expected", source, ", got", packageSource.Value)
   111  		}
   112  	}
   113  
   114  	if len(nugetConfig.Apikeys) != 1 {
   115  		t.Error("Expected one api key, got", len(nugetConfig.Apikeys))
   116  	}
   117  
   118  	apiKey := nugetConfig.Apikeys[0]
   119  	if apiKey.Key != source {
   120  		t.Error("Expected", source, ", got", apiKey.Key)
   121  	}
   122  	if apiKey.Value == "" {
   123  		t.Error("Expected apiKey with value, got", apiKey.Value)
   124  	}
   125  
   126  	if len(nugetConfig.PackageSourceCredentials) != 1 {
   127  		t.Error("Expected one packageSourceCredentials, got", len(nugetConfig.PackageSourceCredentials))
   128  	}
   129  
   130  	if len(nugetConfig.PackageSourceCredentials[0].JFrogCli) != 2 {
   131  		t.Error("Expected two fields in the JFrogCli credentials, got", len(nugetConfig.PackageSourceCredentials[0].JFrogCli))
   132  	}
   133  }
   134  
   135  type NugetConfig struct {
   136  	XMLName                  xml.Name                   `xml:"configuration"`
   137  	PackageSources           []PackageSources           `xml:"packageSources>add"`
   138  	PackageSourceCredentials []PackageSourceCredentials `xml:"packageSourceCredentials"`
   139  	Apikeys                  []PackageSources           `xml:"apikeys>add"`
   140  }
   141  
   142  type PackageSources struct {
   143  	Key   string `xml:"key,attr"`
   144  	Value string `xml:"value,attr"`
   145  }
   146  
   147  type PackageSourceCredentials struct {
   148  	JFrogCli []PackageSources `xml:">add"`
   149  }