OSDN Git Service

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