From 1e395456da7a21d5342f5e5d6f6bef384852814b Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 21 Mar 2018 01:46:24 -0500 Subject: [PATCH] user tests, incapsulate user id settter --- app/rest/api/middleware.go | 33 --------------------------------- app/rest/auth/auth.go | 9 ++------- app/rest/cache_test.go | 5 +---- app/rest/tree_test.go | 2 +- app/rest/user.go | 13 ++++++++++--- app/rest/user_test.go | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 48 deletions(-) create mode 100644 app/rest/user_test.go diff --git a/app/rest/api/middleware.go b/app/rest/api/middleware.go index 776e7201..b57aa9ef 100644 --- a/app/rest/api/middleware.go +++ b/app/rest/api/middleware.go @@ -2,7 +2,6 @@ package api import ( "bytes" - "context" "fmt" "io/ioutil" "log" @@ -15,7 +14,6 @@ import ( "time" "github.com/go-chi/chi/middleware" - "github.com/pkg4go/rewrite" "github.com/umputun/remark/app/rest" ) @@ -158,34 +156,3 @@ func Logger(flags ...LoggerFlag) func(http.Handler) http.Handler { return f } - -// Rewrite middleware with from->to rule. Supports regex (like nginx) and prevents multiple rewrites -func Rewrite(from, to string) func(http.Handler) http.Handler { - rule, err := rewrite.NewRule(from, to) - if err != nil { - log.Printf("[WARN] can't parse rewrite rule %s - > %s, %s", from, to, err) - } - - f := func(h http.Handler) http.Handler { - fn := func(w http.ResponseWriter, r *http.Request) { - - ctx := r.Context() - // prevent double rewrites - if ctx != nil { - if _, ok := ctx.Value(rest.ContextKey("rewrite")).(bool); ok { - h.ServeHTTP(w, r) - return - } - } - - if err == nil { - rule.Rewrite(r) - ctx = context.WithValue(ctx, rest.ContextKey("rewrite"), true) - r = r.WithContext(ctx) - } - h.ServeHTTP(w, r) - } - return http.HandlerFunc(fn) - } - return f -} diff --git a/app/rest/auth/auth.go b/app/rest/auth/auth.go index eba41405..458aca8e 100644 --- a/app/rest/auth/auth.go +++ b/app/rest/auth/auth.go @@ -2,7 +2,6 @@ package auth import ( - "context" "encoding/base64" "net/http" "strings" @@ -39,9 +38,7 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { if a.basicDevUser(w, r) { // fail-back to dev user if enabled user := devUser - ctx := r.Context() - ctx = context.WithValue(ctx, rest.ContextKey("user"), user) - r = r.WithContext(ctx) + r = rest.SetUserInfo(r, user) h.ServeHTTP(w, r) return } @@ -72,9 +69,7 @@ func (a *Authenticator) Auth(reqAuth bool) func(http.Handler) http.Handler { } } - ctx := r.Context() - ctx = context.WithValue(ctx, rest.ContextKey("user"), user) - r = r.WithContext(ctx) + r = rest.SetUserInfo(r, user) } h.ServeHTTP(w, r) } diff --git a/app/rest/cache_test.go b/app/rest/cache_test.go index 40328e71..0851ebc8 100644 --- a/app/rest/cache_test.go +++ b/app/rest/cache_test.go @@ -1,7 +1,6 @@ package rest import ( - "context" "net/http" "testing" "time" @@ -45,10 +44,8 @@ func TestLoadingCache_URLKey(t *testing.T) { key := URLKey(r) assert.Equal(t, "http://blah/123", key) - ctx := context.Background() user := store.User{Admin: true} - ctx = context.WithValue(ctx, ContextKey("user"), user) - r = r.WithContext(ctx) + r = SetUserInfo(r, user) key = URLKey(r) assert.Equal(t, "admin!!http://blah/123", key) } diff --git a/app/rest/tree_test.go b/app/rest/tree_test.go index 209bbb18..f5afc369 100644 --- a/app/rest/tree_test.go +++ b/app/rest/tree_test.go @@ -12,7 +12,7 @@ import ( "github.com/umputun/remark/app/store" ) -func TestStore_MakeTree(t *testing.T) { +func TestMakeTree(t *testing.T) { // unsorted by purpose comments := []store.Comment{ diff --git a/app/rest/user.go b/app/rest/user.go index 40492c8d..7264faf3 100644 --- a/app/rest/user.go +++ b/app/rest/user.go @@ -1,6 +1,7 @@ package rest import ( + "context" "crypto/sha1" "errors" "fmt" @@ -11,8 +12,7 @@ import ( "github.com/umputun/remark/app/store" ) -// ContextKey is a type to match on context -type ContextKey string +type contextKey string // GetUserInfo returns user from request context func GetUserInfo(r *http.Request) (user store.User, err error) { @@ -22,13 +22,20 @@ func GetUserInfo(r *http.Request) (user store.User, err error) { return store.User{}, errors.New("no info about user") } - if u, ok := ctx.Value(ContextKey("user")).(store.User); ok { + if u, ok := ctx.Value(contextKey("user")).(store.User); ok { return u, nil } return store.User{}, errors.New("user can't be parsed") } +// SetUserInfo sets user into request context +func SetUserInfo(r *http.Request, user store.User) *http.Request { + ctx := r.Context() + ctx = context.WithValue(ctx, contextKey("user"), user) + return r.WithContext(ctx) +} + // EncodeID hashes user id to sha1 func EncodeID(id string) string { h := sha1.New() diff --git a/app/rest/user_test.go b/app/rest/user_test.go new file mode 100644 index 00000000..7c65330b --- /dev/null +++ b/app/rest/user_test.go @@ -0,0 +1,36 @@ +package rest + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/umputun/remark/app/store" +) + +func TestEncodeID(t *testing.T) { + tbl := []struct { + id string + hash string + }{ + {"myid", "6e34471f84557e1713012d64a7477c71bfdac631"}, + {"", "da39a3ee5e6b4b0d3255bfef95601890afd80709"}, + {"blah blah", "135a1e01bae742c4a576b20fd41a683f6483ca43"}, + } + + for i, tt := range tbl { + assert.Equal(t, tt.hash, EncodeID(tt.id), "case #%d", i) + } +} + +func TestGetUserInfo(t *testing.T) { + r, err := http.NewRequest("GET", "http://blah.com", nil) + assert.Nil(t, err) + _, err = GetUserInfo(r) + assert.NotNil(t, err, "no user info") + + r = SetUserInfo(r, store.User{Name: "test", ID: "id"}) + u, err := GetUserInfo(r) + assert.Nil(t, err) + assert.Equal(t, store.User{Name: "test", ID: "id"}, u) +}