forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permission_test.go
55 lines (49 loc) · 1.23 KB
/
permission_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package gopher
import (
//"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// 获取路由.
func getRoute() *mux.Router {
r := mux.NewRouter()
for _, route := range routes {
r.HandleFunc(route.URL, handlerFun(route))
}
return r
}
// 测试权限拦截.
func TestPermission(t *testing.T) {
db, _ := mgo.Dial(Config.DB)
defer func() {
db.DB("gopher").C(USERS).Remove(bson.M{"username": "没有中文名"})
}()
r := getRoute()
res := httptest.NewRecorder()
req, err := http.NewRequest("GET", Config.Host+"/topic/new", nil)
if err != nil {
t.Fatal(err)
}
r.ServeHTTP(res, req)
if res.Code != http.StatusFound || res.Header().Get("Location") != "/signin" {
t.Fatal("Autenticated permission failed.")
}
req, err = http.NewRequest("GET", Config.Host+"/admin/nodes", nil)
if err != nil {
t.Fatal(err)
}
db.DB("gopher").C(USERS).Insert(bson.M{"username": "没有中文名"})
session, _ := store.Get(req, "user")
session.Values["username"] = "没有中文名"
res = httptest.NewRecorder()
r.ServeHTTP(res, req)
body := res.Body.String()
if !regexp.MustCompile("对不起,你没有权限进行该操作").MatchString(body) {
t.Fail()
}
}