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/protocol/bc"
18 "github.com/bytom/testutil"
21 func TestCreateAccountWithUppercase(t *testing.T) {
22 m := mockAccountManager(t)
24 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias, nil)
30 if account.Alias != strings.ToLower(alias) {
31 t.Fatal("created account alias should be lowercase")
35 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
36 m := mockAccountManager(t)
37 alias := " with space "
38 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias, nil)
44 if account.Alias != strings.TrimSpace(alias) {
45 t.Fatal("created account alias should be lowercase")
49 func TestCreateAccount(t *testing.T) {
50 m := mockAccountManager(t)
51 ctx := context.Background()
53 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
55 testutil.FatalErr(t, err)
58 found, err := m.findByID(ctx, account.ID)
60 t.Errorf("unexpected error %v", err)
62 if !testutil.DeepEqual(account, found) {
63 t.Errorf("expected account %v to be recorded as %v", account, found)
67 func TestCreateAccountReusedAlias(t *testing.T) {
68 m := mockAccountManager(t)
69 ctx := context.Background()
70 m.createTestAccount(ctx, t, "test-alias", nil)
72 _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
73 if errors.Root(err) != ErrDuplicateAlias {
74 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
78 func TestDeleteAccount(t *testing.T) {
79 m := mockAccountManager(t)
80 ctx := context.Background()
82 account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", nil)
84 testutil.FatalErr(t, err)
87 account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", nil)
89 testutil.FatalErr(t, err)
93 AccountInfo string `json:"account_info"`
95 {AccountInfo: account1.Alias},
96 {AccountInfo: account2.ID},
99 if err = m.DeleteAccount(cases[0]); err != nil {
100 testutil.FatalErr(t, err)
103 found, err := m.findByID(ctx, account1.ID)
105 t.Errorf("expected account %v should be deleted", found)
108 if err = m.DeleteAccount(cases[1]); err != nil {
109 testutil.FatalErr(t, err)
112 found, err = m.findByID(ctx, account2.ID)
114 t.Errorf("expected account %v should be deleted", found)
118 func TestUpdateAccountTags(t *testing.T) {
119 dirPath, err := ioutil.TempDir(".", "")
123 defer os.RemoveAll(dirPath)
125 testDB := dbm.NewDB("testdb", "leveldb", "temp")
126 defer os.RemoveAll("temp")
128 store := leveldb.NewStore(testDB)
129 txPool := protocol.NewTxPool()
130 chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
135 m := NewManager(testDB, chain)
136 ctx := context.Background()
138 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "account-alias",
139 map[string]interface{}{
143 testutil.FatalErr(t, err)
147 wantTags := map[string]interface{}{
151 if m.UpdateTags(ctx, account.ID, wantTags) != nil {
152 testutil.FatalErr(t, err)
155 account1, err := m.FindByAlias(ctx, account.Alias)
157 testutil.FatalErr(t, err)
160 gotTags := account1.Tags
161 if !reflect.DeepEqual(gotTags, wantTags) {
162 t.Fatalf("tags:\ngot: %v\nwant: %v", gotTags, wantTags)
166 wantTags = map[string]interface{}{
170 if m.UpdateTags(ctx, account.Alias, wantTags) != nil {
171 testutil.FatalErr(t, err)
174 account2, err := m.FindByAlias(ctx, account.Alias)
176 testutil.FatalErr(t, err)
179 gotTags = account2.Tags
180 if !reflect.DeepEqual(gotTags, wantTags) {
181 t.Fatalf("tags:\ngot: %v\nwant: %v", gotTags, wantTags)
185 func TestFindByID(t *testing.T) {
186 m := mockAccountManager(t)
187 ctx := context.Background()
188 account := m.createTestAccount(ctx, t, "", nil)
190 found, err := m.findByID(ctx, account.ID)
192 testutil.FatalErr(t, err)
195 if !testutil.DeepEqual(account, found) {
196 t.Errorf("expected found account to be %v, instead found %v", account, found)
200 func TestFindByAlias(t *testing.T) {
201 m := mockAccountManager(t)
202 ctx := context.Background()
203 account := m.createTestAccount(ctx, t, "some-alias", nil)
205 found, err := m.FindByAlias(ctx, "some-alias")
207 testutil.FatalErr(t, err)
210 if !testutil.DeepEqual(account, found) {
211 t.Errorf("expected found account to be %v, instead found %v", account, found)
215 func mockAccountManager(t *testing.T) *Manager {
216 dirPath, err := ioutil.TempDir(".", "")
220 defer os.RemoveAll(dirPath)
222 testDB := dbm.NewDB("testdb", "leveldb", "temp")
223 defer os.RemoveAll("temp")
225 store := leveldb.NewStore(testDB)
226 txPool := protocol.NewTxPool()
227 chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
232 return NewManager(testDB, chain)
235 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
236 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags)
238 testutil.FatalErr(t, err)