OSDN Git Service

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