11 dbm "github.com/tendermint/tmlibs/db"
13 "github.com/bytom/crypto/ed25519/chainkd"
14 "github.com/bytom/database/leveldb"
15 "github.com/bytom/errors"
16 "github.com/bytom/protocol"
17 "github.com/bytom/testutil"
20 func TestCreateAccountWithUppercase(t *testing.T) {
21 m := mockAccountManager(t)
23 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias, nil)
29 if account.Alias != strings.ToLower(alias) {
30 t.Fatal("created account alias should be lowercase")
34 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
35 m := mockAccountManager(t)
36 alias := " with space "
37 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias, nil)
43 if account.Alias != strings.TrimSpace(alias) {
44 t.Fatal("created account alias should be lowercase")
48 func TestCreateAccount(t *testing.T) {
49 m := mockAccountManager(t)
50 ctx := context.Background()
52 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
54 testutil.FatalErr(t, err)
57 found, err := m.findByID(ctx, account.ID)
59 t.Errorf("unexpected error %v", err)
61 if !testutil.DeepEqual(account, found) {
62 t.Errorf("expected account %v to be recorded as %v", account, found)
66 func TestCreateAccountReusedAlias(t *testing.T) {
67 m := mockAccountManager(t)
68 ctx := context.Background()
69 m.createTestAccount(ctx, t, "test-alias", nil)
71 _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
72 if errors.Root(err) != ErrDuplicateAlias {
73 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
77 func TestDeleteAccount(t *testing.T) {
78 m := mockAccountManager(t)
79 ctx := context.Background()
81 account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", nil)
83 testutil.FatalErr(t, err)
86 account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", nil)
88 testutil.FatalErr(t, err)
92 AccountInfo string `json:"account_info"`
94 {AccountInfo: account1.Alias},
95 {AccountInfo: account2.ID},
98 if err = m.DeleteAccount(cases[0]); err != nil {
99 testutil.FatalErr(t, err)
102 found, err := m.findByID(ctx, account1.ID)
104 t.Errorf("expected account %v should be deleted", found)
107 if err = m.DeleteAccount(cases[1]); err != nil {
108 testutil.FatalErr(t, err)
111 found, err = m.findByID(ctx, account2.ID)
113 t.Errorf("expected account %v should be deleted", found)
117 func TestUpdateAccountTags(t *testing.T) {
118 dirPath, err := ioutil.TempDir(".", "")
122 defer os.RemoveAll(dirPath)
124 testDB := dbm.NewDB("testdb", "leveldb", "temp")
125 defer os.RemoveAll("temp")
127 store := leveldb.NewStore(testDB)
128 txPool := protocol.NewTxPool()
129 chain, err := protocol.NewChain(store, txPool)
134 m := NewManager(testDB, chain)
135 ctx := context.Background()
137 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "account-alias",
138 map[string]interface{}{
142 testutil.FatalErr(t, err)
146 wantTags := map[string]interface{}{
150 if m.UpdateTags(ctx, account.ID, wantTags) != nil {
151 testutil.FatalErr(t, err)
154 account1, err := m.FindByAlias(ctx, account.Alias)
156 testutil.FatalErr(t, err)
159 gotTags := account1.Tags
160 if !reflect.DeepEqual(gotTags, wantTags) {
161 t.Fatalf("tags:\ngot: %v\nwant: %v", gotTags, wantTags)
165 wantTags = map[string]interface{}{
169 if m.UpdateTags(ctx, account.Alias, wantTags) != nil {
170 testutil.FatalErr(t, err)
173 account2, err := m.FindByAlias(ctx, account.Alias)
175 testutil.FatalErr(t, err)
178 gotTags = account2.Tags
179 if !reflect.DeepEqual(gotTags, wantTags) {
180 t.Fatalf("tags:\ngot: %v\nwant: %v", gotTags, wantTags)
184 func TestFindByID(t *testing.T) {
185 m := mockAccountManager(t)
186 ctx := context.Background()
187 account := m.createTestAccount(ctx, t, "", nil)
189 found, err := m.findByID(ctx, account.ID)
191 testutil.FatalErr(t, err)
194 if !testutil.DeepEqual(account, found) {
195 t.Errorf("expected found account to be %v, instead found %v", account, found)
199 func TestFindByAlias(t *testing.T) {
200 m := mockAccountManager(t)
201 ctx := context.Background()
202 account := m.createTestAccount(ctx, t, "some-alias", nil)
204 found, err := m.FindByAlias(ctx, "some-alias")
206 testutil.FatalErr(t, err)
209 if !testutil.DeepEqual(account, found) {
210 t.Errorf("expected found account to be %v, instead found %v", account, found)
214 func mockAccountManager(t *testing.T) *Manager {
215 dirPath, err := ioutil.TempDir(".", "")
219 defer os.RemoveAll(dirPath)
221 testDB := dbm.NewDB("testdb", "leveldb", "temp")
222 defer os.RemoveAll("temp")
224 store := leveldb.NewStore(testDB)
225 txPool := protocol.NewTxPool()
226 chain, err := protocol.NewChain(store, txPool)
231 return NewManager(testDB, chain)
234 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
235 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags)
237 testutil.FatalErr(t, err)