OSDN Git Service

Merge pull request #657 from Bytom/coinbase-tx-test
[bytom/bytom.git] / accesstoken / accesstoken_test.go
1 package accesstoken
2
3 import (
4         "context"
5         "os"
6         "strings"
7         "testing"
8
9         dbm "github.com/tendermint/tmlibs/db"
10
11         "github.com/bytom/errors"
12 )
13
14 func TestCreate(t *testing.T) {
15         testDB := dbm.NewDB("testdb", "leveldb", "temp")
16         defer os.RemoveAll("temp")
17         cs := NewStore(testDB)
18         ctx := context.Background()
19
20         cases := []struct {
21                 id, typ string
22                 want    error
23         }{
24                 {"a", "client", nil},
25                 {"b", "network", nil},
26                 {"", "client", ErrBadID},
27                 {"bad:id", "client", ErrBadID},
28                 {"a", "network", ErrDuplicateID}, // this aborts the transaction, so no tests can follow
29         }
30
31         for _, c := range cases {
32                 _, err := cs.Create(ctx, c.id, c.typ)
33                 if errors.Root(err) != c.want {
34                         t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want)
35                 }
36         }
37 }
38
39 func TestList(t *testing.T) {
40         ctx := context.Background()
41         testDB := dbm.NewDB("testdb", "leveldb", "temp")
42         defer os.RemoveAll("temp")
43         cs := NewStore(testDB)
44
45         tokenMap := make(map[string]*Token)
46         tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test")
47         tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test")
48         tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test")
49
50         got, err := cs.List(ctx)
51         if err != nil {
52                 t.Errorf("List errored: get list error")
53         }
54
55         if len(got) != len(tokenMap) {
56                 t.Error("List errored: get invalid length")
57         }
58         for _, v := range got {
59                 if v.Token != tokenMap[v.ID].Token {
60                         t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token)
61                 }
62                 continue
63         }
64 }
65
66 func TestCheck(t *testing.T) {
67         ctx := context.Background()
68         testDB := dbm.NewDB("testdb", "leveldb", "temp")
69         defer os.RemoveAll("temp")
70         cs := NewStore(testDB)
71
72         token := mustCreateToken(ctx, t, cs, "x", "client")
73         tokenParts := strings.Split(token.Token, ":")
74
75         if err := cs.Check(ctx, tokenParts[0], tokenParts[1]); err != nil {
76                 t.Fatal(err)
77         }
78
79         if err := cs.Check(ctx, "x", "badsecret"); err != ErrInvalidToken {
80                 t.Fatal("invalid token check passed")
81         }
82 }
83
84 func TestDelete(t *testing.T) {
85         ctx := context.Background()
86         testDB := dbm.NewDB("testdb", "leveldb", "temp")
87         defer os.RemoveAll("temp")
88         cs := NewStore(testDB)
89
90         const id = "Y"
91         mustCreateToken(ctx, t, cs, id, "client")
92
93         err := cs.Delete(ctx, id)
94         if err != nil {
95                 t.Fatal(err)
96         }
97
98         value := cs.DB.Get([]byte(id))
99         if len(value) > 0 {
100                 t.Fatal("delete fail")
101         }
102 }
103
104 func TestDeleteWithInvalidId(t *testing.T) {
105         ctx := context.Background()
106         testDB := dbm.NewDB("testdb", "leveldb", "temp")
107         defer os.RemoveAll("temp")
108         cs := NewStore(testDB)
109
110         err := cs.Delete(ctx, "@")
111         if errors.Root(err) != ErrBadID {
112                 t.Errorf("Deletion with invalid id success, while it should not")
113         }
114 }
115
116 func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token {
117         token, err := cs.Create(ctx, id, typ)
118         if err != nil {
119                 t.Fatal(err)
120         }
121         return token
122 }