9 acc "github.com/bytom/vapor/account"
10 "github.com/bytom/vapor/blockchain/signers"
11 "github.com/bytom/vapor/config"
12 "github.com/bytom/vapor/crypto/ed25519/chainkd"
13 "github.com/bytom/vapor/database"
14 dbm "github.com/bytom/vapor/database/leveldb"
15 "github.com/bytom/vapor/errors"
16 "github.com/bytom/vapor/event"
17 "github.com/bytom/vapor/protocol"
18 "github.com/bytom/vapor/testutil"
21 func TestCreateAccountWithUppercase(t *testing.T) {
22 m := mockAccountManager(t)
24 account, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
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.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
44 if account.Alias != strings.TrimSpace(alias) {
45 t.Fatal("created account alias should be lowercase")
48 nilAccount, err := m.Manager.FindByAlias(alias)
49 if nilAccount != nil {
50 t.Fatal("expected nil")
53 target, err := m.Manager.FindByAlias(strings.ToLower(strings.TrimSpace(alias)))
55 t.Fatal("expected Account, but got nil")
59 func TestCreateAccount(t *testing.T) {
60 m := mockAccountManager(t)
61 account, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
66 found, err := m.Manager.FindByID(account.ID)
68 t.Errorf("unexpected error %v", err)
71 if !testutil.DeepEqual(account, found) {
72 t.Errorf("expected account %v to be recorded as %v", account, found)
76 func TestCreateAccountReusedAlias(t *testing.T) {
77 m := mockAccountManager(t)
78 m.createTestAccount(t, "test-alias", nil)
80 _, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
81 if errors.Root(err) != acc.ErrDuplicateAlias {
82 t.Errorf("expected %s when reusing an alias, got %v", acc.ErrDuplicateAlias, err)
86 func TestUpdateAccountAlias(t *testing.T) {
87 oldAlias := "test-alias"
88 newAlias := "my-alias"
90 m := mockAccountManager(t)
91 account := m.createTestAccount(t, oldAlias, nil)
92 err := m.Manager.UpdateAccountAlias("testID", newAlias)
94 t.Errorf("expected error when using an invalid account id")
97 err = m.Manager.UpdateAccountAlias(account.ID, oldAlias)
98 if errors.Root(err) != acc.ErrDuplicateAlias {
99 t.Errorf("expected %s when using a duplicate alias, got %v", acc.ErrDuplicateAlias, err)
102 err = m.Manager.UpdateAccountAlias(account.ID, newAlias)
104 t.Errorf("expected account %v alias should be update", account)
107 updatedAccount, err := m.Manager.FindByID(account.ID)
109 t.Errorf("unexpected error %v", err)
112 if updatedAccount.Alias != newAlias {
113 t.Errorf("alias:\ngot: %v\nwant: %v", updatedAccount.Alias, newAlias)
116 if _, err = m.Manager.FindByAlias(oldAlias); errors.Root(err) != acc.ErrFindAccount {
117 t.Errorf("expected %s when using a old alias, got %v", acc.ErrFindAccount, err)
121 func TestDeleteAccount(t *testing.T) {
122 m := mockAccountManager(t)
124 account1, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", signers.BIP0044)
129 account2, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", signers.BIP0044)
134 found, err := m.Manager.FindByID(account1.ID)
136 t.Errorf("expected account %v should be deleted", found)
139 if err = m.Manager.DeleteAccount(account2.ID); err != nil {
143 found, err = m.Manager.FindByID(account2.ID)
145 t.Errorf("expected account %v should be deleted", found)
149 func TestFindByID(t *testing.T) {
150 m := mockAccountManager(t)
151 account := m.createTestAccount(t, "", nil)
153 found, err := m.Manager.FindByID(account.ID)
158 if !testutil.DeepEqual(account, found) {
159 t.Errorf("expected found account to be %v, instead found %v", account, found)
163 func TestFindByAlias(t *testing.T) {
164 m := mockAccountManager(t)
165 account := m.createTestAccount(t, "some-alias", nil)
167 found, err := m.Manager.FindByAlias("some-alias")
172 if !testutil.DeepEqual(account, found) {
173 t.Errorf("expected found account to be %v, instead found %v", account, found)
177 type mockAccManager struct {
181 func mockAccountManager(t *testing.T) *mockAccManager {
182 dirPath, err := ioutil.TempDir(".", "")
186 defer os.RemoveAll(dirPath)
188 testDB := dbm.NewDB("testdb", "memdb", dirPath)
189 dispatcher := event.NewDispatcher()
190 store := database.NewStore(testDB)
191 accountStore := database.NewAccountStore(testDB)
192 txPool := protocol.NewTxPool(store, dispatcher)
193 config.CommonConfig = config.DefaultConfig()
194 chain, err := protocol.NewChain(store, txPool, dispatcher)
199 return &mockAccManager{acc.NewManager(accountStore, chain)}
202 func (m *mockAccManager) createTestAccount(t testing.TB, alias string, tags map[string]interface{}) *acc.Account {
203 account, err := m.Manager.Create([]chainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)