OSDN Git Service

delete mergeAction
[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         nilAccount, err := m.FindByAlias(nil, alias)
47         if nilAccount != nil {
48                 t.Fatal("expected nil")
49         }
50
51         target, err := m.FindByAlias(nil, strings.ToLower(strings.TrimSpace(alias)))
52         if target == nil {
53                 t.Fatal("expected Account, but got nil")
54         }
55 }
56
57 func TestCreateAccount(t *testing.T) {
58         m := mockAccountManager(t)
59         ctx := context.Background()
60
61         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
62         if err != nil {
63                 testutil.FatalErr(t, err)
64         }
65
66         found, err := m.FindByID(ctx, account.ID)
67         if err != nil {
68                 t.Errorf("unexpected error %v", err)
69         }
70         if !testutil.DeepEqual(account, found) {
71                 t.Errorf("expected account %v to be recorded as %v", account, found)
72         }
73 }
74
75 func TestCreateAccountReusedAlias(t *testing.T) {
76         m := mockAccountManager(t)
77         ctx := context.Background()
78         m.createTestAccount(ctx, t, "test-alias", nil)
79
80         _, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias")
81         if errors.Root(err) != ErrDuplicateAlias {
82                 t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
83         }
84 }
85
86 func TestDeleteAccount(t *testing.T) {
87         m := mockAccountManager(t)
88         ctx := context.Background()
89
90         account1, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias1")
91         if err != nil {
92                 testutil.FatalErr(t, err)
93         }
94
95         account2, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, "test-alias2")
96         if err != nil {
97                 testutil.FatalErr(t, err)
98         }
99
100         if err = m.DeleteAccount(account1.Alias); err != nil {
101                 testutil.FatalErr(t, err)
102         }
103
104         found, err := m.FindByID(ctx, account1.ID)
105         if err != nil {
106                 t.Errorf("expected account %v should be deleted", found)
107         }
108
109         if err = m.DeleteAccount(account2.ID); err != nil {
110                 testutil.FatalErr(t, err)
111         }
112
113         found, err = m.FindByID(ctx, account2.ID)
114         if err != nil {
115                 t.Errorf("expected account %v should be deleted", found)
116         }
117 }
118
119 func TestFindByID(t *testing.T) {
120         m := mockAccountManager(t)
121         ctx := context.Background()
122         account := m.createTestAccount(ctx, t, "", nil)
123
124         found, err := m.FindByID(ctx, account.ID)
125         if err != nil {
126                 testutil.FatalErr(t, err)
127         }
128
129         if !testutil.DeepEqual(account, found) {
130                 t.Errorf("expected found account to be %v, instead found %v", account, found)
131         }
132 }
133
134 func TestFindByAlias(t *testing.T) {
135         m := mockAccountManager(t)
136         ctx := context.Background()
137         account := m.createTestAccount(ctx, t, "some-alias", nil)
138
139         found, err := m.FindByAlias(ctx, "some-alias")
140         if err != nil {
141                 testutil.FatalErr(t, err)
142         }
143
144         if !testutil.DeepEqual(account, found) {
145                 t.Errorf("expected found account to be %v, instead found %v", account, found)
146         }
147 }
148
149 func mockAccountManager(t *testing.T) *Manager {
150         dirPath, err := ioutil.TempDir(".", "")
151         if err != nil {
152                 t.Fatal(err)
153         }
154         defer os.RemoveAll(dirPath)
155
156         testDB := dbm.NewDB("testdb", "leveldb", "temp")
157         defer os.RemoveAll("temp")
158
159         store := leveldb.NewStore(testDB)
160         txPool := protocol.NewTxPool()
161         chain, err := protocol.NewChain(store, txPool)
162         if err != nil {
163                 t.Fatal(err)
164         }
165
166         return NewManager(testDB, chain)
167 }
168
169 func (m *Manager) createTestAccount(ctx context.Context, t testing.TB, alias string, tags map[string]interface{}) *Account {
170         account, err := m.Create(ctx, []chainkd.XPub{testutil.TestXPub}, 1, alias)
171         if err != nil {
172                 testutil.FatalErr(t, err)
173         }
174
175         return account
176
177 }