OSDN Git Service

Modify access token db to sqldb
[bytom/vapor.git] / accesstoken / accesstoken_test.go
1 package accesstoken
2
3 import (
4         "context"
5         "os"
6         "strings"
7         "testing"
8
9         "github.com/jinzhu/gorm"
10
11         "github.com/vapor/database/orm"
12
13         dbm "github.com/vapor/database/db"
14         _ "github.com/vapor/database/leveldb"
15         _ "github.com/vapor/database/sqlite"
16         "github.com/vapor/errors"
17 )
18
19 func TestCreate(t *testing.T) {
20         testDB := dbm.NewSqlDB("sql", "sqlitedb", "temp")
21         defer func() {
22                 testDB.Db().Close()
23                 os.RemoveAll("temp")
24         }()
25
26         testDB.Db().AutoMigrate(&orm.AccessToken{})
27
28         cs := NewStore(testDB)
29
30         cases := []struct {
31                 id, typ string
32                 want    error
33         }{
34                 {"a", "client", nil},
35                 {"b", "network", nil},
36                 {"", "client", ErrBadID},
37                 {"bad:id", "client", ErrBadID},
38                 {"a", "network", ErrDuplicateID}, // this aborts the transaction, so no tests can follow
39         }
40
41         for _, c := range cases {
42                 _, err := cs.Create(c.id, c.typ)
43                 if errors.Root(err) != c.want {
44                         t.Errorf("Create(%s, %s) error = %s want %s", c.id, c.typ, err, c.want)
45                 }
46         }
47 }
48
49 func TestList(t *testing.T) {
50         ctx := context.Background()
51         testDB := dbm.NewSqlDB("sql", "sqlitedb", "temp")
52         defer func() {
53                 testDB.Db().Close()
54                 os.RemoveAll("temp")
55         }()
56
57         testDB.Db().AutoMigrate(&orm.AccessToken{})
58         cs := NewStore(testDB)
59
60         tokenMap := make(map[string]*Token)
61         tokenMap["ab"] = mustCreateToken(ctx, t, cs, "ab", "test")
62         tokenMap["bc"] = mustCreateToken(ctx, t, cs, "bc", "test")
63         tokenMap["cd"] = mustCreateToken(ctx, t, cs, "cd", "test")
64
65         got, err := cs.List()
66         if err != nil {
67                 t.Errorf("List errored: get list error")
68         }
69
70         if len(got) != len(tokenMap) {
71                 t.Error("List errored: get invalid length")
72         }
73         for _, v := range got {
74                 if v.Token != tokenMap[v.ID].Token {
75                         t.Errorf("List error: ID: %s, expected token: %s, DB token: %s", v.ID, *tokenMap[v.ID], v.Token)
76                 }
77                 continue
78         }
79 }
80
81 func TestCheck(t *testing.T) {
82         ctx := context.Background()
83         testDB := dbm.NewSqlDB("sql", "sqlitedb", "temp")
84         defer func() {
85                 testDB.Db().Close()
86                 os.RemoveAll("temp")
87         }()
88
89         testDB.Db().AutoMigrate(&orm.AccessToken{})
90         cs := NewStore(testDB)
91
92         token := mustCreateToken(ctx, t, cs, "x", "client")
93         tokenParts := strings.Split(token.Token, ":")
94
95         if err := cs.Check(tokenParts[0], tokenParts[1]); err != nil {
96                 t.Fatal(err)
97         }
98
99         if err := cs.Check("x", "badsecret"); err != ErrInvalidToken {
100                 t.Fatal("invalid token check passed")
101         }
102 }
103
104 func TestDelete(t *testing.T) {
105         ctx := context.Background()
106         testDB := dbm.NewSqlDB("sql", "sqlitedb", "temp")
107         defer func() {
108                 testDB.Db().Close()
109                 os.RemoveAll("temp")
110         }()
111
112         testDB.Db().AutoMigrate(&orm.AccessToken{})
113         cs := NewStore(testDB)
114
115         const id = "Y"
116         mustCreateToken(ctx, t, cs, id, "client")
117
118         err := cs.Delete(id)
119         if err != nil {
120                 t.Fatal(err)
121         }
122
123         accessToken := orm.AccessToken{ID: id}
124
125         err = cs.DB.Db().Where(&accessToken).Find(&accessToken).Error
126         if err != gorm.ErrRecordNotFound {
127                 t.Fatal(err)
128         }
129
130         if err == nil {
131                 t.Fatal("delete fail")
132         }
133
134         /*
135                 cs.List
136
137                 value := cs.DB.Get([]byte(id))
138                 if len(value) > 0 {
139                         t.Fatal("delete fail")
140                 }
141         */
142 }
143
144 func TestDeleteWithInvalidId(t *testing.T) {
145         testDB := dbm.NewSqlDB("sql", "sqlitedb", "temp")
146         defer func() {
147                 testDB.Db().Close()
148                 os.RemoveAll("temp")
149         }()
150
151         testDB.Db().AutoMigrate(&orm.AccessToken{})
152         cs := NewStore(testDB)
153
154         err := cs.Delete("@")
155         if errors.Root(err) != ErrBadID {
156                 t.Errorf("Deletion with invalid id success, while it should not")
157         }
158 }
159
160 func mustCreateToken(ctx context.Context, t *testing.T, cs *CredentialStore, id, typ string) *Token {
161         token, err := cs.Create(id, typ)
162         if err != nil {
163                 t.Fatal(err)
164         }
165         return token
166 }