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/testutil"
19 func TestCreateAccountWithUppercase(t *testing.T) {
20 m := mockAccountManager(t)
22 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias)
28 if account.Alias != strings.ToLower(alias) {
29 t.Fatal("created account alias should be lowercase")
33 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
34 m := mockAccountManager(t)
35 alias := " with space "
36 account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias)
42 if account.Alias != strings.TrimSpace(alias) {
43 t.Fatal("created account alias should be lowercase")
46 nilAccount, err := m.FindByAlias(nil, alias)
47 if nilAccount != nil {
48 t.Fatal("expected nil")
51 target, err := m.FindByAlias(nil, strings.ToLower(strings.TrimSpace(alias)))
53 t.Fatal("expected Account, but got nil")
57 func TestCreateAccount(t *testing.T) {
58 m := mockAccountManager(t)
59 ctx := context.Background()
61 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
63 testutil.FatalErr(t, err)
66 found, err := m.FindByID(ctx, account.ID)
68 t.Errorf("unexpected error %v", err)
70 if !testutil.DeepEqual(account, found) {
71 t.Errorf("expected account %v to be recorded as %v", account, found)
75 func TestCreateAccountReusedAlias(t *testing.T) {
76 m := mockAccountManager(t)
77 ctx := context.Background()
78 m.createTestAccount(ctx, t, "test-alias", nil)
80 _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
81 if errors.Root(err) != ErrDuplicateAlias {
82 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
86 func TestDeleteAccount(t *testing.T) {
87 m := mockAccountManager(t)
88 ctx := context.Background()
90 account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1")
92 testutil.FatalErr(t, err)
95 account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2")
97 testutil.FatalErr(t, err)
100 if err = m.DeleteAccount(account1.Alias); err != nil {
101 testutil.FatalErr(t, err)
104 found, err := m.FindByID(ctx, account1.ID)
106 t.Errorf("expected account %v should be deleted", found)
109 if err = m.DeleteAccount(account2.ID); err != nil {
110 testutil.FatalErr(t, err)
113 found, err = m.FindByID(ctx, account2.ID)
115 t.Errorf("expected account %v should be deleted", found)
119 func TestFindByID(t *testing.T) {
120 m := mockAccountManager(t)
121 ctx := context.Background()
122 account := m.createTestAccount(ctx, t, "", nil)
124 found, err := m.FindByID(ctx, account.ID)
126 testutil.FatalErr(t, err)
129 if !testutil.DeepEqual(account, found) {
130 t.Errorf("expected found account to be %v, instead found %v", account, found)
134 func TestFindByAlias(t *testing.T) {
135 m := mockAccountManager(t)
136 ctx := context.Background()
137 account := m.createTestAccount(ctx, t, "some-alias", nil)
139 found, err := m.FindByAlias(ctx, "some-alias")
141 testutil.FatalErr(t, err)
144 if !testutil.DeepEqual(account, found) {
145 t.Errorf("expected found account to be %v, instead found %v", account, found)
149 func mockAccountManager(t *testing.T) *Manager {
150 dirPath, err := ioutil.TempDir(".", "")
154 defer os.RemoveAll(dirPath)
156 testDB := dbm.NewDB("testdb", "leveldb", "temp")
157 defer os.RemoveAll("temp")
159 store := leveldb.NewStore(testDB)
160 txPool := protocol.NewTxPool()
161 chain, err := protocol.NewChain(store, txPool)
166 return NewManager(testDB, chain)
169 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
170 account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias)
172 testutil.FatalErr(t, err)