OSDN Git Service

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