From 3e8300c2a37f9c65ec64a4924454abe1cc075284 Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Mon, 13 Jan 2020 16:33:20 +0100 Subject: [PATCH] Fixing global vars in handlers --- handlers/handlers.go | 1 + handlers/handlers_test.go | 17 +++++++++-------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 5c48fdcb..70a68f3e 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -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 { diff --git a/handlers/handlers_test.go b/handlers/handlers_test.go index b15b30cb..dcb1e7de 100644 --- a/handlers/handlers_test.go +++ b/handlers/handlers_test.go @@ -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)