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/protocol/bc"
17 "github.com/bytom/testutil"
20 func TestCreateAccount(t *testing.T) {
21 m := mockAccountManager(t)
22 ctx := context.Background()
24 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
26 testutil.FatalErr(t, err)
29 found, err := m.findByID(ctx, account.ID)
31 t.Errorf("unexpected error %v", err)
33 if !testutil.DeepEqual(account, found) {
34 t.Errorf("expected account %v to be recorded as %v", account, found)
38 func TestCreateAccountReusedAlias(t *testing.T) {
39 m := mockAccountManager(t)
40 ctx := context.Background()
41 m.createTestAccount(ctx, t, "test-alias", nil)
43 _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
44 if errors.Root(err) != ErrDuplicateAlias {
45 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
49 func TestDeleteAccount(t *testing.T) {
50 m := mockAccountManager(t)
51 ctx := context.Background()
53 account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", nil)
55 testutil.FatalErr(t, err)
58 account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", nil)
60 testutil.FatalErr(t, err)
64 AccountInfo string `json:"account_info"`
66 {AccountInfo: account1.Alias},
67 {AccountInfo: account2.ID},
70 if err = m.DeleteAccount(cases[0]); err != nil {
71 testutil.FatalErr(t, err)
74 found, err := m.findByID(ctx, account1.ID)
76 t.Errorf("expected account %v should be deleted", found)
79 if err = m.DeleteAccount(cases[1]); err != nil {
80 testutil.FatalErr(t, err)
83 found, err = m.findByID(ctx, account2.ID)
85 t.Errorf("expected account %v should be deleted", found)
89 func TestUpdateAccountTags(t *testing.T) {
90 dirPath, err := ioutil.TempDir(".", "")
94 defer os.RemoveAll(dirPath)
96 testDB := dbm.NewDB("testdb", "leveldb", "temp")
97 defer os.RemoveAll("temp")
99 store := leveldb.NewStore(testDB)
100 txPool := protocol.NewTxPool()
101 chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
106 m := NewManager(testDB, chain)
107 ctx := context.Background()
109 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "account-alias",
110 map[string]interface{}{
114 testutil.FatalErr(t, err)
118 wantTags := map[string]interface{}{
122 if m.UpdateTags(ctx, account.ID, wantTags) != nil {
123 testutil.FatalErr(t, err)
126 account1, err := m.FindByAlias(ctx, account.Alias)
128 testutil.FatalErr(t, err)
131 gotTags := account1.Tags
132 if !reflect.DeepEqual(gotTags, wantTags) {
133 t.Fatalf("tags:\ngot: %v\nwant: %v", gotTags, wantTags)
137 wantTags = map[string]interface{}{
141 if m.UpdateTags(ctx, account.Alias, wantTags) != nil {
142 testutil.FatalErr(t, err)
145 account2, err := m.FindByAlias(ctx, account.Alias)
147 testutil.FatalErr(t, err)
150 gotTags = account2.Tags
151 if !reflect.DeepEqual(gotTags, wantTags) {
152 t.Fatalf("tags:\ngot: %v\nwant: %v", gotTags, wantTags)
156 func TestFindByID(t *testing.T) {
157 m := mockAccountManager(t)
158 ctx := context.Background()
159 account := m.createTestAccount(ctx, t, "", nil)
161 found, err := m.findByID(ctx, account.ID)
163 testutil.FatalErr(t, err)
166 if !testutil.DeepEqual(account, found) {
167 t.Errorf("expected found account to be %v, instead found %v", account, found)
171 func TestFindByAlias(t *testing.T) {
172 m := mockAccountManager(t)
173 ctx := context.Background()
174 account := m.createTestAccount(ctx, t, "some-alias", nil)
176 found, err := m.FindByAlias(ctx, "some-alias")
178 testutil.FatalErr(t, err)
181 if !testutil.DeepEqual(account, found) {
182 t.Errorf("expected found account to be %v, instead found %v", account, found)
186 func mockAccountManager(t *testing.T) *Manager {
187 dirPath, err := ioutil.TempDir(".", "")
191 defer os.RemoveAll(dirPath)
193 testDB := dbm.NewDB("testdb", "leveldb", "temp")
194 defer os.RemoveAll("temp")
196 store := leveldb.NewStore(testDB)
197 txPool := protocol.NewTxPool()
198 chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
203 return NewManager(testDB, chain)
206 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
207 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags)
209 testutil.FatalErr(t, err)