github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/config/config_test.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package config 7 8 import ( 9 "fmt" 10 "path/filepath" 11 "testing" 12 13 "github.com/golang/mock/gomock" 14 "github.com/stretchr/testify/require" 15 16 "github.com/iotexproject/iotex-core/ioctl/config" 17 "github.com/iotexproject/iotex-core/ioctl/util" 18 "github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient" 19 ) 20 21 func TestNewConfigCmd(t *testing.T) { 22 require := require.New(t) 23 ctrl := gomock.NewController(t) 24 defer ctrl.Finish() 25 client := mock_ioctlclient.NewMockClient(ctrl) 26 client.EXPECT().SelectTranslation(gomock.Any()).Return("config usage", config.English).Times(8) 27 client.EXPECT().SetInsecureWithFlag(gomock.Any()) 28 cmd := NewConfigCmd(client) 29 result, err := util.ExecuteCmd(cmd) 30 require.NoError(err) 31 require.Contains(result, "Available Commands") 32 } 33 34 func TestInitConfig(t *testing.T) { 35 require := require.New(t) 36 testPath := t.TempDir() 37 _configDir = testPath 38 cfg, cfgFilePath, err := InitConfig() 39 require.NoError(err) 40 require.Equal(testPath, cfg.Wallet) 41 require.Equal(_validExpl[0], cfg.Explorer) 42 require.Equal(_supportedLanguage[0], cfg.Language) 43 require.Equal(filepath.Join(testPath, _defaultConfigFileName), cfgFilePath) 44 require.Equal(_defaultWsEndpoint, cfg.WsEndpoint) 45 require.Equal(_defaultIPFSEndpoint, cfg.IPFSEndpoint) 46 require.Equal(_defaultIPFSGateway, cfg.IPFSGateway) 47 } 48 49 func TestConfigGet(t *testing.T) { 50 require := require.New(t) 51 testPath := t.TempDir() 52 info := newInfo(config.Config{ 53 Wallet: testPath, 54 SecureConnect: true, 55 Aliases: make(map[string]string), 56 DefaultAccount: config.Context{AddressOrAlias: "test"}, 57 Explorer: "iotexscan", 58 Language: "English", 59 AnalyserEndpoint: "testAnalyser", 60 WsEndpoint: "testWsEndpoint", 61 IPFSEndpoint: "testIPFSEndpoint", 62 IPFSGateway: "testIPFSGateway", 63 WsRegisterContract: "testWsRegisterContract", 64 }, testPath) 65 66 tcs := []struct { 67 arg string 68 expected string 69 }{ 70 { 71 "endpoint", 72 "no endpoint has been set", 73 }, 74 { 75 "wallet", 76 testPath, 77 }, 78 { 79 "defaultacc", 80 "{\n \"addressOrAlias\": \"test\"\n}", 81 }, 82 { 83 "explorer", 84 "iotexscan", 85 }, 86 { 87 "language", 88 "English", 89 }, 90 { 91 "nsv2height", 92 "0", 93 }, 94 { 95 "analyserEndpoint", 96 "testAnalyser", 97 }, 98 { 99 "wsEndpoint", 100 "testWsEndpoint", 101 }, 102 { 103 "ipfsEndpoint", 104 "testIPFSEndpoint", 105 }, 106 { 107 "ipfsGateway", 108 "testIPFSGateway", 109 }, 110 { 111 "wsRegisterContract", 112 "testWsRegisterContract", 113 }, 114 { 115 "all", 116 "\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\",\n \"ipfsEndpoint\": \"testIPFSEndpoint\",\n \"ipfsGateway\": \"testIPFSGateway\",\n \"wsRegisterContract\": \"testWsRegisterContract\"\n}", 117 }, 118 } 119 120 for _, tc := range tcs { 121 cfgItem, err := info.get(tc.arg) 122 if err != nil { 123 require.Contains(err.Error(), tc.expected) 124 } else { 125 require.Contains(cfgItem, tc.expected) 126 } 127 } 128 } 129 130 func TestConfigReset(t *testing.T) { 131 require := require.New(t) 132 cfgDir := t.TempDir() 133 cfgFile := fmt.Sprintf("%s/%s", cfgDir, "config.test") 134 135 info := newInfo(config.Config{ 136 Wallet: "wallet", 137 Endpoint: "testEndpoint", 138 SecureConnect: false, 139 DefaultAccount: config.Context{AddressOrAlias: ""}, 140 Explorer: "explorer", 141 Language: "Croatian", 142 AnalyserEndpoint: "testAnalyser", 143 WsEndpoint: "testWsEndpoint", 144 IPFSEndpoint: "testIPFSEndpoint", 145 IPFSGateway: "testIPFSGateway", 146 WsRegisterContract: "testWsRegisterContract", 147 }, cfgFile) 148 149 // write the config to the temp dir and then reset 150 require.NoError(info.writeConfig()) 151 require.NoError(info.loadConfig()) 152 cfg := info.readConfig 153 154 require.Equal("wallet", cfg.Wallet) 155 require.Equal("testEndpoint", cfg.Endpoint) 156 require.Equal(false, cfg.SecureConnect) 157 require.Equal("Croatian", cfg.Language) 158 require.Equal("testAnalyser", cfg.AnalyserEndpoint) 159 require.Equal("explorer", cfg.Explorer) 160 require.Equal(config.Context{AddressOrAlias: ""}, cfg.DefaultAccount) 161 require.Equal("testWsEndpoint", cfg.WsEndpoint) 162 require.Equal("testIPFSEndpoint", cfg.IPFSEndpoint) 163 require.Equal("testIPFSGateway", cfg.IPFSGateway) 164 require.Equal("testWsRegisterContract", cfg.WsRegisterContract) 165 166 require.NoError(info.reset()) 167 require.NoError(info.loadConfig()) 168 resetCfg := info.readConfig 169 170 // ensure config has been reset 171 require.Equal(cfgDir, resetCfg.Wallet) 172 require.Equal("", resetCfg.Endpoint) 173 require.Equal(true, resetCfg.SecureConnect) 174 require.Equal("English", resetCfg.Language) 175 require.Equal(_defaultAnalyserEndpoint, resetCfg.AnalyserEndpoint) 176 require.Equal(_defaultWsEndpoint, resetCfg.WsEndpoint) 177 require.Equal(_defaultIPFSEndpoint, resetCfg.IPFSEndpoint) 178 require.Equal(_defaultIPFSGateway, resetCfg.IPFSGateway) 179 require.Equal(_defaultWsRegisterContract, resetCfg.WsRegisterContract) 180 require.Equal("iotexscan", resetCfg.Explorer) 181 require.Equal(*new(config.Context), resetCfg.DefaultAccount) 182 } 183 184 func TestConfigSet(t *testing.T) { 185 require := require.New(t) 186 testPath := t.TempDir() 187 cfgFile := fmt.Sprintf("%s/%s", testPath, "config.test") 188 189 info := newInfo(config.Config{ 190 Wallet: testPath, 191 SecureConnect: true, 192 Aliases: make(map[string]string), 193 DefaultAccount: config.Context{AddressOrAlias: "test"}, 194 Explorer: "iotexscan", 195 Language: "English", 196 AnalyserEndpoint: "testAnalyser", 197 WsEndpoint: "testWsEndpoint", 198 IPFSEndpoint: "testIPFSEndpoint", 199 IPFSGateway: "testIPFSGateway", 200 }, cfgFile) 201 202 tcs := []struct { 203 args []string 204 expected string 205 }{ 206 { 207 []string{"endpoint", "invalid endpoint"}, 208 "endpoint invalid endpoint is not valid", 209 }, 210 { 211 []string{"wallet", testPath}, 212 testPath, 213 }, 214 { 215 []string{"defaultacc", "io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he"}, 216 "Defaultacc is set to io10a298zmzvrt4guq79a9f4x7qedj59y7ery84he", 217 }, 218 { 219 []string{"defaultacc", "suzxctxgbidciovisbrecerurkbjkmyqrftxtnjyp"}, 220 "failed to validate alias or address suzxctxgbidciovisbrecerurkbjkmyqrftxtnjyp", 221 }, 222 { 223 []string{"explorer", "iotxplorer"}, 224 "Explorer is set to iotxplorer", 225 }, 226 { 227 []string{"explorer", "invalid"}, 228 "explorer invalid is not valid\nValid explorers: [iotexscan iotxplorer custom]", 229 }, 230 { 231 []string{"language", "中文"}, 232 "Language is set to 中文", 233 }, 234 { 235 []string{"language", "unknown language"}, 236 "language unknown language is not supported\nSupported languages: [English 中文]", 237 }, 238 { 239 []string{"nsv2height", "20"}, 240 "Nsv2height is set to 20", 241 }, 242 { 243 []string{"nsv2height", "invalid height"}, 244 "invalid height", 245 }, 246 { 247 []string{"unknownField", ""}, 248 "no matching config", 249 }, 250 { 251 []string{"wsEndpoint", "testWsEndpoint"}, 252 "testWsEndpoint", 253 }, 254 { 255 []string{"ipfsEndpoint", "testIPFSEndpoint"}, 256 "testIPFSEndpoint", 257 }, 258 { 259 []string{"ipfsGateway", "testIPFSGateway"}, 260 "testIPFSGateway", 261 }, 262 { 263 []string{"wsRegisterContract", "testWsRegisterContract"}, 264 "testWsRegisterContract", 265 }, 266 } 267 268 for _, tc := range tcs { 269 setResult, err := info.set(tc.args, false, nil) 270 if err != nil { 271 require.Contains(err.Error(), tc.expected) 272 } else { 273 require.Contains(setResult, tc.expected) 274 } 275 } 276 }