OSDN Git Service

Main network and test network distinguish address prefix
[bytom/bytom.git] / account / accounts_test.go
1 package account
2
3 import (
4         "context"
5         "io/ioutil"
6         "os"
7         "strings"
8         "testing"
9
10         dbm "github.com/tendermint/tmlibs/db"
11
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"
17 )
18
19 func TestCreateAccountWithUppercase(t *testing.T) {
20         m := mockAccountManager(t)
21         alias := "UPPER"
22         account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias)
23
24         if err != nil {
25                 t.Fatal(err)
26         }
27
28         if account.Alias != strings.ToLower(alias) {
29                 t.Fatal("created account alias should be lowercase")
30         }
31 }
32
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)
37
38         if err != nil {
39                 t.Fatal(err)
40         }
41
42         if account.Alias != strings.TrimSpace(alias) {
43                 t.Fatal("created account alias should be lowercase")
44         }
45 }
46
47 func TestCreateAccount(t *testing.T) {
48         m := mockAccountManager(t)
49         ctx := context.Background()
50
51         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
52         if err != nil {
53                 testutil.FatalErr(t, err)
54         }
55
56         found, err := m.findByID(ctx, account.ID)
57         if err != nil {
58                 t.Errorf("unexpected error %v", err)
59         }
60         if !testutil.DeepEqual(account, found) {
61                 t.Errorf("expected account %v to be recorded as %v", account, found)
62         }
63 }
64
65 func TestCreateAccountReusedAlias(t *testing.T) {
66         m := mockAccountManager(t)
67         ctx := context.Background()
68         m.createTestAccount(ctx, t, "test-alias", nil)
69
70         _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
71         if errors.Root(err) != ErrDuplicateAlias {
72                 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
73         }
74 }
75
76 func TestDeleteAccount(t *testing.T) {
77         m := mockAccountManager(t)
78         ctx := context.Background()
79
80         account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1")
81         if err != nil {
82                 testutil.FatalErr(t, err)
83         }
84
85         account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2")
86         if err != nil {
87                 testutil.FatalErr(t, err)
88         }
89
90         cases := []struct {
91                 AccountInfo string `json:"account_info"`
92         }{
93                 {AccountInfo: account1.Alias},
94                 {AccountInfo: account2.ID},
95         }
96
97         if err = m.DeleteAccount(cases[0]); err != nil {
98                 testutil.FatalErr(t, err)
99         }
100
101         found, err := m.findByID(ctx, account1.ID)
102         if err != nil {
103                 t.Errorf("expected account %v should be deleted", found)
104         }
105
106         if err = m.DeleteAccount(cases[1]); err != nil {
107                 testutil.FatalErr(t, err)
108         }
109
110         found, err = m.findByID(ctx, account2.ID)
111         if err != nil {
112                 t.Errorf("expected account %v should be deleted", found)
113         }
114 }
115
116 func TestFindByID(t *testing.T) {
117         m := mockAccountManager(t)
118         ctx := context.Background()
119         account := m.createTestAccount(ctx, t, "", nil)
120
121         found, err := m.findByID(ctx, account.ID)
122         if err != nil {
123                 testutil.FatalErr(t, err)
124         }
125
126         if !testutil.DeepEqual(account, found) {
127                 t.Errorf("expected found account to be %v, instead found %v", account, found)
128         }
129 }
130
131 func TestFindByAlias(t *testing.T) {
132         m := mockAccountManager(t)
133         ctx := context.Background()
134         account := m.createTestAccount(ctx, t, "some-alias", nil)
135
136         found, err := m.FindByAlias(ctx, "some-alias")
137         if err != nil {
138                 testutil.FatalErr(t, err)
139         }
140
141         if !testutil.DeepEqual(account, found) {
142                 t.Errorf("expected found account to be %v, instead found %v", account, found)
143         }
144 }
145
146 func mockAccountManager(t *testing.T) *Manager {
147         dirPath, err := ioutil.TempDir(".", "")
148         if err != nil {
149                 t.Fatal(err)
150         }
151         defer os.RemoveAll(dirPath)
152
153         testDB := dbm.NewDB("testdb", "leveldb", "temp")
154         defer os.RemoveAll("temp")
155
156         store := leveldb.NewStore(testDB)
157         txPool := protocol.NewTxPool()
158         chain, err := protocol.NewChain(store, txPool)
159         if err != nil {
160                 t.Fatal(err)
161         }
162
163         return NewManager(testDB, chain)
164 }
165
166 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
167         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias)
168         if err != nil {
169                 testutil.FatalErr(t, err)
170         }
171
172         return account
173
174 }