Fixing global vars in handlers

This commit is contained in:
Andrea Fazzi 2020-01-13 16:33:20 +01:00
parent a2af636a85
commit 3e8300c2a3
2 changed files with 10 additions and 8 deletions

View file

@ -247,6 +247,7 @@ func get(w http.ResponseWriter, r *http.Request, model string, pattern PathPatte
claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
role := claims["role"].(string)
if !hasPermission(role, pattern.Path(model)) {
log.Println("ERRORE")
setFlashMessage(w, r, "notAuthorized")
renderer.Render[format](w, r, fmt.Errorf("%s", "Errore di autorizzazione"))
} else {

View file

@ -3,7 +3,6 @@ package handlers
import (
"context"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"net/url"
@ -59,10 +58,9 @@ func (t *testSuite) BeforeAll() {
connected := false
for !connected {
time.Sleep(10 * time.Second)
time.Sleep(5 * time.Second)
db, err = orm.New("oef:oef@/oef_test?charset=utf8&parseTime=True&loc=Local")
if err != nil {
time.Sleep(5 * time.Second)
continue
}
connected = true
@ -123,6 +121,7 @@ func (t *testSuite) BeforeAll() {
req.SetBasicAuth("admin", "admin")
rr := httptest.NewRecorder()
signingKey = []byte("secret")
tokenHandler().ServeHTTP(rr, req)
var data struct {
@ -136,6 +135,10 @@ func (t *testSuite) BeforeAll() {
token = data.Token
if err := orm.MapHandlers(models); err != nil {
panic(err)
}
}
func (t *testSuite) TestReadAllContests() {
@ -152,15 +155,13 @@ func (t *testSuite) TestReadAllContests() {
rr := httptest.NewRecorder()
ctx := req.Context()
claims := &jwt.MapClaims{}
log.Print(token)
tkn, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
return config.Config.Keys.JWTSigningKey, nil
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
return []byte("secret"), nil
})
if err != nil {
panic(err)
}
ctx := req.Context()
ctx = context.WithValue(ctx, "user", tkn)
req = req.WithContext(ctx)