user tests, incapsulate user id settter
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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{
|
||||
|
||||
+10
-3
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user