9 dbm "github.com/tendermint/tmlibs/db"
11 "github.com/bytom/blockchain/txdb"
12 "github.com/bytom/crypto/ed25519/chainkd"
13 "github.com/bytom/errors"
14 "github.com/bytom/protocol"
15 "github.com/bytom/protocol/bc"
16 "github.com/bytom/testutil"
19 func TestCreateAccount(t *testing.T) {
20 m := mockAccountManager(t)
21 ctx := context.Background()
23 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
25 testutil.FatalErr(t, err)
28 found, err := m.findByID(ctx, account.ID)
30 t.Errorf("unexpected error %v", err)
32 if !testutil.DeepEqual(account, found) {
33 t.Errorf("expected account %s to be recorded as %s", account, found)
37 func TestCreateAccountReusedAlias(t *testing.T) {
38 m := mockAccountManager(t)
39 ctx := context.Background()
40 m.createTestAccount(ctx, t, "test-alias", nil)
42 _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
43 if errors.Root(err) != ErrDuplicateAlias {
44 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
48 func TestDeleteAccount(t *testing.T) {
49 m := mockAccountManager(t)
50 ctx := context.Background()
52 account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", nil)
54 testutil.FatalErr(t, err)
57 account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", nil)
59 testutil.FatalErr(t, err)
63 AccountInfo string `json:"account_info"`
65 {AccountInfo: account1.Alias},
66 {AccountInfo: account2.ID},
69 if err = m.DeleteAccount(cases[0]); err != nil {
70 testutil.FatalErr(t, err)
73 found, err := m.findByID(ctx, account1.ID)
75 t.Errorf("expected account %v should be deleted", found)
78 if err = m.DeleteAccount(cases[1]); err != nil {
79 testutil.FatalErr(t, err)
82 found, err = m.findByID(ctx, account2.ID)
84 t.Errorf("expected account %v should be deleted", found)
88 func TestFindByID(t *testing.T) {
89 m := mockAccountManager(t)
90 ctx := context.Background()
91 account := m.createTestAccount(ctx, t, "", nil)
93 found, err := m.findByID(ctx, account.ID)
95 testutil.FatalErr(t, err)
98 if !testutil.DeepEqual(account, found) {
99 t.Errorf("expected found account to be %v, instead found %v", account, found)
103 func TestFindByAlias(t *testing.T) {
104 m := mockAccountManager(t)
105 ctx := context.Background()
106 account := m.createTestAccount(ctx, t, "some-alias", nil)
108 found, err := m.FindByAlias(ctx, "some-alias")
110 testutil.FatalErr(t, err)
113 if !testutil.DeepEqual(account, found) {
114 t.Errorf("expected found account to be %v, instead found %v", account, found)
118 func mockAccountManager(t *testing.T) *Manager {
119 dirPath, err := ioutil.TempDir(".", "")
123 defer os.RemoveAll(dirPath)
125 testDB := dbm.NewDB("testdb", "leveldb", "temp")
126 defer os.RemoveAll("temp")
128 store := txdb.NewStore(testDB)
129 txPool := protocol.NewTxPool()
130 chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
135 return NewManager(testDB, chain)
138 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
139 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags)
141 testutil.FatalErr(t, err)