9 dbm "github.com/tendermint/tmlibs/db"
11 "github.com/bytom/crypto/ed25519/chainkd"
12 "github.com/bytom/database/leveldb"
13 "github.com/bytom/errors"
14 "github.com/bytom/protocol"
15 "github.com/bytom/testutil"
18 func TestCreateAccountWithUppercase(t *testing.T) {
19 m := mockAccountManager(t)
21 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias)
27 if account.Alias != strings.ToLower(alias) {
28 t.Fatal("created account alias should be lowercase")
32 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
33 m := mockAccountManager(t)
34 alias := " with space "
35 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias)
41 if account.Alias != strings.TrimSpace(alias) {
42 t.Fatal("created account alias should be lowercase")
45 nilAccount, err := m.FindByAlias(alias)
46 if nilAccount != nil {
47 t.Fatal("expected nil")
50 target, err := m.FindByAlias(strings.ToLower(strings.TrimSpace(alias)))
52 t.Fatal("expected Account, but got nil")
56 func TestCreateAccount(t *testing.T) {
57 m := mockAccountManager(t)
58 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
60 testutil.FatalErr(t, err)
63 found, err := m.FindByID(account.ID)
65 t.Errorf("unexpected error %v", err)
67 if !testutil.DeepEqual(account, found) {
68 t.Errorf("expected account %v to be recorded as %v", account, found)
72 func TestCreateAccountReusedAlias(t *testing.T) {
73 m := mockAccountManager(t)
74 m.createTestAccount(t, "test-alias", nil)
76 _, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
77 if errors.Root(err) != ErrDuplicateAlias {
78 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
82 func TestDeleteAccount(t *testing.T) {
83 m := mockAccountManager(t)
85 account1, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias1")
87 testutil.FatalErr(t, err)
90 account2, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias2")
92 testutil.FatalErr(t, err)
95 if err = m.DeleteAccount(account1.Alias); err != nil {
96 testutil.FatalErr(t, err)
99 found, err := m.FindByID(account1.ID)
101 t.Errorf("expected account %v should be deleted", found)
104 if err = m.DeleteAccount(account2.ID); err != nil {
105 testutil.FatalErr(t, err)
108 found, err = m.FindByID(account2.ID)
110 t.Errorf("expected account %v should be deleted", found)
114 func TestFindByID(t *testing.T) {
115 m := mockAccountManager(t)
116 account := m.createTestAccount(t, "", nil)
118 found, err := m.FindByID(account.ID)
120 testutil.FatalErr(t, err)
123 if !testutil.DeepEqual(account, found) {
124 t.Errorf("expected found account to be %v, instead found %v", account, found)
128 func TestFindByAlias(t *testing.T) {
129 m := mockAccountManager(t)
130 account := m.createTestAccount(t, "some-alias", nil)
132 found, err := m.FindByAlias("some-alias")
134 testutil.FatalErr(t, err)
137 if !testutil.DeepEqual(account, found) {
138 t.Errorf("expected found account to be %v, instead found %v", account, found)
142 func mockAccountManager(t *testing.T) *Manager {
143 dirPath, err := ioutil.TempDir(".", "")
147 defer os.RemoveAll(dirPath)
149 testDB := dbm.NewDB("testdb", "leveldb", "temp")
150 defer os.RemoveAll("temp")
152 store := leveldb.NewStore(testDB)
153 txPool := protocol.NewTxPool()
154 chain, err := protocol.NewChain(store, txPool)
159 return NewManager(testDB, chain)
162 func (m *Manager) createTestAccount(t testing.TB, alias string, tags map[string]interface{}) *Account {
163 account, err := m.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias)
165 testutil.FatalErr(t, err)