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

     1  package npm
     2  
     3  import (
     4  	"io/ioutil"
     5  	"strings"
     6  	"testing"
     7  )
     8  
     9  func TestPrepareConfigData(t *testing.T) {
    10  	configJson, err := ioutil.ReadFile("../testdata/config.json")
    11  	if err != nil {
    12  		t.Error(err)
    13  	}
    14  
    15  	expectedConfig :=
    16  		[]string{
    17  			"json = true",
    18  			"allow-same-version = false",
    19  			"user-agent = npm/5.5.1 node/v8.9.1 darwin x64",
    20  			"@jfrog:registry = http://goodRegistry",
    21  			"email = ddd@dd.dd",
    22  			"cache-lock-retries = 10",
    23  			"registry = http://goodRegistry",
    24  			"_auth = YWRtaW46QVBCN1ZkZFMzN3NCakJiaHRGZThVb0JlZzFl"}
    25  
    26  	npmi := NpmCommandArgs{registry: "http://goodRegistry", jsonOutput: true, npmAuth: "_auth = YWRtaW46QVBCN1ZkZFMzN3NCakJiaHRGZThVb0JlZzFl"}
    27  	actualConfig, err := npmi.prepareConfigData([]byte(configJson))
    28  	if err != nil {
    29  		t.Error(err)
    30  	}
    31  	actualConfigArray := strings.Split(string(actualConfig), "\n")
    32  	if len(actualConfigArray) != len(expectedConfig) {
    33  		t.Errorf("expeted:\n%s\n\ngot:\n%s", expectedConfig, actualConfigArray)
    34  	}
    35  	for _, eConfig := range expectedConfig {
    36  		found := false
    37  		for _, aConfig := range actualConfigArray {
    38  			if aConfig == eConfig {
    39  				found = true
    40  				break
    41  			}
    42  		}
    43  		if !found {
    44  			t.Error("The expected config:", eConfig, "is missing from the actual configuration list:\n", actualConfigArray)
    45  			t.Errorf("The expected config: %s is missing from the actual configuration list:\n %s", eConfig, actualConfigArray)
    46  		}
    47  	}
    48  }
    49  
    50  func TestPrepareConfigDataTypeRestriction(t *testing.T) {
    51  	var typeRestrictions = map[string]string{
    52  		`{"production": true}`:    "production",
    53  		`{"only": "prod"}`:        "production",
    54  		`{"only": "production"}`:  "production",
    55  		`{"only": "development"}`: "development",
    56  		`{"only": "dev"}`:         "development",
    57  		`{"only": null}`:          "",
    58  		`{"only": ""}`:            "",
    59  		`{"kuku": true}`:          ""}
    60  
    61  	for json, typeRestriction := range typeRestrictions {
    62  		npmi := NpmCommandArgs{}
    63  		npmi.prepareConfigData([]byte(json))
    64  		if npmi.typeRestriction != typeRestriction {
    65  			t.Errorf("Type restriction was supposed to be %s but set to: %s when using the json:\n%s", typeRestriction, npmi.typeRestriction, json)
    66  		}
    67  	}
    68  }
    69  
    70  func TestParseDependencies(t *testing.T) {
    71  	dependenciesJsonList, err := ioutil.ReadFile("../testdata/dependenciesList.json")
    72  	if err != nil {
    73  		t.Error(err)
    74  	}
    75  
    76  	expectedDependenciesList := []string{
    77  		"underscore-1.4.4",
    78  		"@jfrog/npm_scoped-1.0.0",
    79  		"xml-1.0.1",
    80  		"xpm-0.1.1",
    81  		"binary-search-tree-0.2.4",
    82  		"nedb-1.0.2",
    83  		"@ilg/es6-promisifier-0.1.9",
    84  		"wscript-avoider-3.0.2",
    85  		"yaml-0.2.3",
    86  		"@ilg/cli-start-options-0.1.19",
    87  		"async-0.2.10",
    88  		"find-0.2.7",
    89  		"jquery-3.2.0",
    90  		"nub-1.0.0",
    91  		"shopify-liquid-1.d7.9",
    92  	}
    93  	npmi := NpmCommandArgs{}
    94  	npmi.dependencies = make(map[string]*dependency)
    95  	err = npmi.parseDependencies([]byte(dependenciesJsonList), "myScope")
    96  	if err != nil {
    97  		t.Error(err)
    98  	}
    99  	if len(expectedDependenciesList) != len(npmi.dependencies) {
   100  		t.Error("The expected dependencies list length is", len(expectedDependenciesList), "and should be:\n", expectedDependenciesList,
   101  			"\nthe actual dependencies list length is", len(npmi.dependencies), "and the list is:\n", npmi.dependencies)
   102  		t.Error("The expected dependencies list length is", len(expectedDependenciesList), "and should be:\n", expectedDependenciesList,
   103  			"\nthe actual dependencies list length is", len(npmi.dependencies), "and the list is:\n", npmi.dependencies)
   104  	}
   105  	for _, eDependency := range expectedDependenciesList {
   106  		found := false
   107  		for aDependency := range npmi.dependencies {
   108  			if aDependency == eDependency {
   109  				found = true
   110  				break
   111  			}
   112  		}
   113  		if !found {
   114  			t.Error("The expected dependency:", eDependency, "is missing from the actual dependencies list:\n", npmi.dependencies)
   115  		}
   116  	}
   117  }
   118  
   119  func TestGetRegistry(t *testing.T) {
   120  	var getRegistryTest = []struct {
   121  		repo     string
   122  		url      string
   123  		expected string
   124  	}{
   125  		{"repo", "http://url/art", "http://url/art/api/npm/repo"},
   126  		{"repo", "http://url/art/", "http://url/art/api/npm/repo"},
   127  		{"repo", "", "/api/npm/repo"},
   128  		{"", "http://url/art", "http://url/art/api/npm/"},
   129  	}
   130  
   131  	for _, testCase := range getRegistryTest {
   132  		if getNpmRepositoryUrl(testCase.repo, testCase.url) != testCase.expected {
   133  			t.Errorf("The expected output of getRegistry(\"%s\", \"%s\") is %s. But the actual result is:%s", testCase.repo, testCase.url, testCase.expected, getNpmRepositoryUrl(testCase.repo, testCase.url))
   134  		}
   135  	}
   136  }