OSDN Git Service

fix style with goimports except p2p directory (#520)
[bytom/bytom.git] / account / accounts_test.go
1 package account
2
3 import (
4         "context"
5         "io/ioutil"
6         "os"
7         "reflect"
8         "strings"
9         "testing"
10
11         dbm "github.com/tendermint/tmlibs/db"
12
13         "github.com/bytom/crypto/ed25519/chainkd"
14         "github.com/bytom/database/leveldb"
15         "github.com/bytom/errors"
16         "github.com/bytom/protocol"
17         "github.com/bytom/protocol/bc"
18         "github.com/bytom/testutil"
19 )
20
21 func TestCreateAccountWithUppercase(t *testing.T) {
22         m := mockAccountManager(t)
23         alias := "UPPER"
24         account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias, nil)
25
26         if err != nil {
27                 t.Fatal(err)
28         }
29
30         if account.Alias != strings.ToLower(alias) {
31                 t.Fatal("created account alias should be lowercase")
32         }
33 }
34
35 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
36         m := mockAccountManager(t)
37         alias := " with space "
38         account, err := m.Create(nil, []chainkd.XPub{testutil.TestXPub}, 1, alias, nil)
39
40         if err != nil {
41                 t.Fatal(err)
42         }
43
44         if account.Alias != strings.TrimSpace(alias) {
45                 t.Fatal("created account alias should be lowercase")
46         }
47 }
48
49 func TestCreateAccount(t *testing.T) {
50         m := mockAccountManager(t)
51         ctx := context.Background()
52
53         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
54         if err != nil {
55                 testutil.FatalErr(t, err)
56         }
57
58         found, err := m.findByID(ctx, account.ID)
59         if err != nil {
60                 t.Errorf("unexpected error %v", err)
61         }
62         if !testutil.DeepEqual(account, found) {
63                 t.Errorf("expected account %v to be recorded as %v", account, found)
64         }
65 }
66
67 func TestCreateAccountReusedAlias(t *testing.T) {
68         m := mockAccountManager(t)
69         ctx := context.Background()
70         m.createTestAccount(ctx, t, "test-alias", nil)
71
72         _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias", nil)
73         if errors.Root(err) != ErrDuplicateAlias {
74                 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
75         }
76 }
77
78 func TestDeleteAccount(t *testing.T) {
79         m := mockAccountManager(t)
80         ctx := context.Background()
81
82         account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1", nil)
83         if err != nil {
84                 testutil.FatalErr(t, err)
85         }
86
87         account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2", nil)
88         if err != nil {
89                 testutil.FatalErr(t, err)
90         }
91
92         cases := []struct {
93                 AccountInfo string `json:"account_info"`
94         }{
95                 {AccountInfo: account1.Alias},
96                 {AccountInfo: account2.ID},
97         }
98
99         if err = m.DeleteAccount(cases[0]); err != nil {
100                 testutil.FatalErr(t, err)
101         }
102
103         found, err := m.findByID(ctx, account1.ID)
104         if err != nil {
105                 t.Errorf("expected account %v should be deleted", found)
106         }
107
108         if err = m.DeleteAccount(cases[1]); err != nil {
109                 testutil.FatalErr(t, err)
110         }
111
112         found, err = m.findByID(ctx, account2.ID)
113         if err != nil {
114                 t.Errorf("expected account %v should be deleted", found)
115         }
116 }
117
118 func TestUpdateAccountTags(t *testing.T) {
119         dirPath, err := ioutil.TempDir(".", "")
120         if err != nil {
121                 t.Fatal(err)
122         }
123         defer os.RemoveAll(dirPath)
124
125         testDB := dbm.NewDB("testdb", "leveldb", "temp")
126         defer os.RemoveAll("temp")
127
128         store := leveldb.NewStore(testDB)
129         txPool := protocol.NewTxPool()
130         chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
131         if err != nil {
132                 t.Fatal(err)
133         }
134
135         m := NewManager(testDB, chain)
136         ctx := context.Background()
137
138         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "account-alias",
139                 map[string]interface{}{
140                         "test_tag": "v0",
141                 })
142         if err != nil {
143                 testutil.FatalErr(t, err)
144         }
145
146         // Update by ID
147         wantTags := map[string]interface{}{
148                 "test_tag": "v1",
149         }
150
151         if m.UpdateTags(ctx, account.ID, wantTags) != nil {
152                 testutil.FatalErr(t, err)
153         }
154
155         account1, err := m.FindByAlias(ctx, account.Alias)
156         if err != nil {
157                 testutil.FatalErr(t, err)
158         }
159
160         gotTags := account1.Tags
161         if !reflect.DeepEqual(gotTags, wantTags) {
162                 t.Fatalf("tags:\ngot:  %v\nwant: %v", gotTags, wantTags)
163         }
164
165         // Update by alias
166         wantTags = map[string]interface{}{
167                 "test_tag": "v2",
168         }
169
170         if m.UpdateTags(ctx, account.Alias, wantTags) != nil {
171                 testutil.FatalErr(t, err)
172         }
173
174         account2, err := m.FindByAlias(ctx, account.Alias)
175         if err != nil {
176                 testutil.FatalErr(t, err)
177         }
178
179         gotTags = account2.Tags
180         if !reflect.DeepEqual(gotTags, wantTags) {
181                 t.Fatalf("tags:\ngot:  %v\nwant: %v", gotTags, wantTags)
182         }
183 }
184
185 func TestFindByID(t *testing.T) {
186         m := mockAccountManager(t)
187         ctx := context.Background()
188         account := m.createTestAccount(ctx, t, "", nil)
189
190         found, err := m.findByID(ctx, account.ID)
191         if err != nil {
192                 testutil.FatalErr(t, err)
193         }
194
195         if !testutil.DeepEqual(account, found) {
196                 t.Errorf("expected found account to be %v, instead found %v", account, found)
197         }
198 }
199
200 func TestFindByAlias(t *testing.T) {
201         m := mockAccountManager(t)
202         ctx := context.Background()
203         account := m.createTestAccount(ctx, t, "some-alias", nil)
204
205         found, err := m.FindByAlias(ctx, "some-alias")
206         if err != nil {
207                 testutil.FatalErr(t, err)
208         }
209
210         if !testutil.DeepEqual(account, found) {
211                 t.Errorf("expected found account to be %v, instead found %v", account, found)
212         }
213 }
214
215 func mockAccountManager(t *testing.T) *Manager {
216         dirPath, err := ioutil.TempDir(".", "")
217         if err != nil {
218                 t.Fatal(err)
219         }
220         defer os.RemoveAll(dirPath)
221
222         testDB := dbm.NewDB("testdb", "leveldb", "temp")
223         defer os.RemoveAll("temp")
224
225         store := leveldb.NewStore(testDB)
226         txPool := protocol.NewTxPool()
227         chain, err := protocol.NewChain(bc.Hash{}, store, txPool)
228         if err != nil {
229                 t.Fatal(err)
230         }
231
232         return NewManager(testDB, chain)
233 }
234
235 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
236         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias, tags)
237         if err != nil {
238                 testutil.FatalErr(t, err)
239         }
240
241         return account
242
243 }