|
| 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