Skip to content

Commit 3df5496

Browse files
committed
Merge remote-tracking branch 'benma/test-config'
2 parents 50a7e8d + e226513 commit 3df5496

File tree

3 files changed

+143
-22
lines changed

3 files changed

+143
-22
lines changed

backend/config/accounts.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@ func (cfg AccountsConfig) Lookup(code accountsTypes.Code) *Account {
8484
}
8585
return nil
8686
}
87+
88+
// migrateActiveTokens removes tokens from AccountsConfig.
89+
func migrateActiveTokens(accountsConf *AccountsConfig) error {
90+
for _, account := range accountsConf.Accounts {
91+
if account.CoinCode != coin.CodeETH {
92+
continue
93+
}
94+
95+
err := account.SetTokenActive("eth-erc20-sai0x89d", false)
96+
if err != nil {
97+
return err
98+
}
99+
100+
}
101+
return nil
102+
}

backend/config/config.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,6 @@ func NewConfig(appConfigFilename string, accountsConfigFilename string) (*Config
269269
return config, nil
270270
}
271271

272-
// SetBtcOnly sets non-bitcoin accounts in the config to false.
273-
func (config *Config) SetBtcOnly() {
274-
config.appConfig.Backend.DeprecatedLitecoinActive = false
275-
config.appConfig.Backend.DeprecatedEthereumActive = false
276-
}
277-
278272
// SetBTCElectrumServers sets the BTC configuration to the provided electrumIP and electrumCert.
279273
func (config *Config) SetBTCElectrumServers(electrumAddress, electrumCert string) {
280274
config.appConfig.Backend.BTC = btcCoinConfig{
@@ -441,19 +435,3 @@ func migrateUserLanguage(appconf *AppConfig) {
441435
delete(frontconf, "userLanguage")
442436
}
443437
}
444-
445-
// migrateActiveTokens removes tokens from AccountsConfig.
446-
func migrateActiveTokens(accountsConf *AccountsConfig) error {
447-
for _, account := range accountsConf.Accounts {
448-
if account.CoinCode != coin.CodeETH {
449-
continue
450-
}
451-
452-
err := account.SetTokenActive("eth-erc20-sai0x89d", false)
453-
if err != nil {
454-
return err
455-
}
456-
457-
}
458-
return nil
459-
}

backend/config/config_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2023 Shift Crypto AG
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package config
16+
17+
import (
18+
"encoding/json"
19+
"errors"
20+
"os"
21+
"testing"
22+
23+
"github.com/digitalbitbox/bitbox-wallet-app/backend/coins/coin"
24+
"github.com/digitalbitbox/bitbox-wallet-app/util/test"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestNewConfig(t *testing.T) {
29+
appConfigFilename := test.TstTempFile("appConfig")
30+
accountsConfigFilename := test.TstTempFile("accountsConfig")
31+
32+
cfg, err := NewConfig(appConfigFilename, accountsConfigFilename)
33+
require.NoError(t, err)
34+
35+
appJsonBytes, err := os.ReadFile(appConfigFilename)
36+
require.NoError(t, err)
37+
expectedAppJsonBytes, err := json.Marshal(NewDefaultAppConfig())
38+
require.NoError(t, err)
39+
require.JSONEq(t, string(expectedAppJsonBytes), string(appJsonBytes))
40+
41+
accountsJsonBytes, err := os.ReadFile(accountsConfigFilename)
42+
require.NoError(t, err)
43+
expectedAccountsJsonBytes, err := json.Marshal(newDefaultAccountsonfig())
44+
require.NoError(t, err)
45+
require.JSONEq(t, string(expectedAccountsJsonBytes), string(accountsJsonBytes))
46+
47+
// Load existing config.
48+
cfg2, err := NewConfig(appConfigFilename, accountsConfigFilename)
49+
require.NoError(t, err)
50+
require.Equal(t, cfg, cfg2)
51+
}
52+
53+
func TestSetAppConfig(t *testing.T) {
54+
appConfigFilename := test.TstTempFile("appConfig")
55+
accountsConfigFilename := test.TstTempFile("accountsConfig")
56+
57+
cfg, err := NewConfig(appConfigFilename, accountsConfigFilename)
58+
require.NoError(t, err)
59+
60+
appCfg := cfg.AppConfig()
61+
require.Equal(t, coin.BtcUnitDefault, appCfg.Backend.BtcUnit)
62+
appCfg.Backend.BtcUnit = coin.BtcUnitSats
63+
appCfg.Frontend = map[string]interface{}{"foo": "bar"}
64+
require.NoError(t, cfg.SetAppConfig(appCfg))
65+
66+
cfg2, err := NewConfig(appConfigFilename, accountsConfigFilename)
67+
require.NoError(t, err)
68+
require.Equal(t, cfg, cfg2)
69+
require.Equal(t, coin.BtcUnitSats, cfg2.AppConfig().Backend.BtcUnit)
70+
require.Equal(t, map[string]interface{}{"foo": "bar"}, cfg2.AppConfig().Frontend)
71+
}
72+
73+
func TestModifyAccountsConfig(t *testing.T) {
74+
appConfigFilename := test.TstTempFile("appConfig")
75+
accountsConfigFilename := test.TstTempFile("accountsConfig")
76+
77+
cfg, err := NewConfig(appConfigFilename, accountsConfigFilename)
78+
require.NoError(t, err)
79+
80+
require.NoError(t, cfg.ModifyAccountsConfig(func(accountsCfg *AccountsConfig) error {
81+
accountsCfg.Accounts = append(accountsCfg.Accounts, &Account{Used: true})
82+
return nil
83+
}))
84+
85+
cfg2, err := NewConfig(appConfigFilename, accountsConfigFilename)
86+
require.NoError(t, err)
87+
require.Equal(t, cfg, cfg2)
88+
require.Equal(t, []*Account{{Used: true}}, cfg2.AccountsConfig().Accounts)
89+
90+
require.Error(t, cfg.ModifyAccountsConfig(func(accountsCfg *AccountsConfig) error {
91+
return errors.New("error")
92+
}))
93+
}
94+
95+
// TestMigrationSaved tests that migrations are applied when a config is loaded, and that the
96+
// migrations are persisted.
97+
func TestMigrationsAtLoad(t *testing.T) {
98+
appConfigFilename := test.TstTempFile("appConfig")
99+
accountsConfigFilename := test.TstTempFile("accountsConfig")
100+
101+
// Persist a config that includes data that will be migrated.
102+
cfg, err := NewConfig(appConfigFilename, accountsConfigFilename)
103+
require.NoError(t, err)
104+
appCfg := cfg.AppConfig()
105+
appCfg.Frontend = map[string]interface{}{
106+
"userLanguage": "de",
107+
}
108+
require.NoError(t, cfg.SetAppConfig(appCfg))
109+
require.NoError(t, cfg.ModifyAccountsConfig(func(accountsCfg *AccountsConfig) error {
110+
accountsCfg.Accounts = append(accountsCfg.Accounts,
111+
&Account{CoinCode: coin.CodeETH, ActiveTokens: []string{"eth-erc20-sai0x89d"}})
112+
return nil
113+
}))
114+
115+
// Loading the conf applies the migrations.
116+
cfg2, err := NewConfig(appConfigFilename, accountsConfigFilename)
117+
require.NoError(t, err)
118+
require.Equal(t, "de", cfg2.AppConfig().Backend.UserLanguage)
119+
require.Equal(t,
120+
[]*Account{{CoinCode: coin.CodeETH, ActiveTokens: nil}},
121+
cfg2.AccountsConfig().Accounts)
122+
123+
// The migrations were persisted.
124+
cfg3, err := NewConfig(appConfigFilename, accountsConfigFilename)
125+
require.NoError(t, err)
126+
require.Equal(t, cfg2, cfg3)
127+
}

0 commit comments

Comments
 (0)