OSDN Git Service

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