OSDN Git Service

Merge pull request #519 from Bytom/dev
[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 m := strings.Compare(v.Token, tokenMap[v.ID].Token); m != 0 {
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         valid, err := cs.Check(ctx, tokenParts[0], tokenParts[1])
76         if err != nil {
77                 t.Fatal(err)
78         }
79         if !valid {
80                 t.Fatal("expected token and secret to be valid")
81         }
82
83         valid, err = cs.Check(ctx, "x", "badsecret")
84         if err != nil {
85                 t.Fatal(err)
86         }
87         if valid {
88                 t.Fatal("expected bad secret to not be valid")
89         }
90 }
91
92 func TestDelete(t *testing.T) {
93         ctx := context.Background()
94         testDB := dbm.NewDB("testdb", "leveldb", "temp")
95         defer os.RemoveAll("temp")
96         cs := NewStore(testDB)
97
98         const id = "Y"
99         mustCreateToken(ctx, t, cs, id, "client")
100
101         err := cs.Delete(ctx, id)
102         if err != nil {
103                 t.Fatal(err)
104         }
105
106         value := cs.DB.Get([]byte(id))
107         if len(value) > 0 {
108                 t.Fatal("delete fail")
109         }
110 }
111
112 func TestDeleteWithInvalidId(t *testing.T) {
113         ctx := context.Background()
114         testDB := dbm.NewDB("testdb", "leveldb", "temp")
115         defer os.RemoveAll("temp")
116         cs := NewStore(testDB)
117
118         err := cs.Delete(ctx, "@")
119         if errors.Root(err) != ErrBadID {
120                 t.Errorf("Deletion with invalid id success, while it should not")
121         }
122 }
123
124 func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token {
125         token, err := cs.Create(ctx, id, typ)
126         if err != nil {
127                 t.Fatal(err)
128         }
129         return token
130 }