10 dbm "github.com/tendermint/tmlibs/db"
12 "github.com/bytom/crypto/ed25519/chainkd"
13 "github.com/bytom/database/leveldb"
14 "github.com/bytom/errors"
15 "github.com/bytom/protocol"
16 "github.com/bytom/testutil"
19 func TestCreateAccountWithUppercase(t *testing.T) {
20 m := mockAccountManager(t)
22 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias)
28 if account.Alias != strings.ToLower(alias) {
29 t.Fatal("created account alias should be lowercase")
33 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
34 m := mockAccountManager(t)
35 alias := " with space "
36 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias)
42 if account.Alias != strings.TrimSpace(alias) {
43 t.Fatal("created account alias should be lowercase")
47 func TestCreateAccount(t *testing.T) {
48 m := mockAccountManager(t)
49 ctx := context.Background()
51 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
53 testutil.FatalErr(t, err)
56 found, err := m.findByID(ctx, account.ID)
58 t.Errorf("unexpected error %v", err)
60 if !testutil.DeepEqual(account, found) {
61 t.Errorf("expected account %v to be recorded as %v", account, found)
65 func TestCreateAccountReusedAlias(t *testing.T) {
66 m := mockAccountManager(t)
67 ctx := context.Background()
68 m.createTestAccount(ctx, t, "test-alias", nil)
70 _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
71 if errors.Root(err) != ErrDuplicateAlias {
72 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
76 func TestDeleteAccount(t *testing.T) {
77 m := mockAccountManager(t)
78 ctx := context.Background()
80 account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1")
82 testutil.FatalErr(t, err)
85 account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2")
87 testutil.FatalErr(t, err)
91 AccountInfo string `json:"account_info"`
93 {AccountInfo: account1.Alias},
94 {AccountInfo: account2.ID},
97 if err = m.DeleteAccount(cases[0]); err != nil {
98 testutil.FatalErr(t, err)
101 found, err := m.findByID(ctx, account1.ID)
103 t.Errorf("expected account %v should be deleted", found)
106 if err = m.DeleteAccount(cases[1]); err != nil {
107 testutil.FatalErr(t, err)
110 found, err = m.findByID(ctx, account2.ID)
112 t.Errorf("expected account %v should be deleted", found)
116 func TestFindByID(t *testing.T) {
117 m := mockAccountManager(t)
118 ctx := context.Background()
119 account := m.createTestAccount(ctx, t, "", nil)
121 found, err := m.findByID(ctx, account.ID)
123 testutil.FatalErr(t, err)
126 if !testutil.DeepEqual(account, found) {
127 t.Errorf("expected found account to be %v, instead found %v", account, found)
131 func TestFindByAlias(t *testing.T) {
132 m := mockAccountManager(t)
133 ctx := context.Background()
134 account := m.createTestAccount(ctx, t, "some-alias", nil)
136 found, err := m.FindByAlias(ctx, "some-alias")
138 testutil.FatalErr(t, err)
141 if !testutil.DeepEqual(account, found) {
142 t.Errorf("expected found account to be %v, instead found %v", account, found)
146 func mockAccountManager(t *testing.T) *Manager {
147 dirPath, err := ioutil.TempDir(".", "")
151 defer os.RemoveAll(dirPath)
153 testDB := dbm.NewDB("testdb", "leveldb", "temp")
154 defer os.RemoveAll("temp")
156 store := leveldb.NewStore(testDB)
157 txPool := protocol.NewTxPool()
158 chain, err := protocol.NewChain(store, txPool)
163 return NewManager(testDB, chain)
166 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
167 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias)
169 testutil.FatalErr(t, err)