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) claims := r.Context().Value("user").(*jwt.Token).Claims.(jwt.MapClaims)
role := claims["role"].(string) role := claims["role"].(string)
if !hasPermission(role, pattern.Path(model)) { if !hasPermission(role, pattern.Path(model)) {
log.Println("ERRORE")
setFlashMessage(w, r, "notAuthorized") setFlashMessage(w, r, "notAuthorized")
renderer.Render[format](w, r, fmt.Errorf("%s", "Errore di autorizzazione")) renderer.Render[format](w, r, fmt.Errorf("%s", "Errore di autorizzazione"))
} else { } else {

View file

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