OSDN Git Service

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