mirror of
https://github.com/volatiletech/authboss.git
synced 2025-09-16 09:06:20 +02:00
Remove validation from context.
- This is unnecessary now that request and context are more split up.
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
package authboss
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type mockUser struct {
|
||||
@@ -56,22 +56,19 @@ func (m mockClientStore) GetErr(key string) (string, error) {
|
||||
func (m mockClientStore) Put(key, val string) { m[key] = val }
|
||||
func (m mockClientStore) Del(key string) { delete(m, key) }
|
||||
|
||||
func mockRequestContext(ab *Authboss, postKeyValues ...string) (*Context, *http.Request) {
|
||||
keyValues := &bytes.Buffer{}
|
||||
func mockRequest(postKeyValues ...string) *http.Request {
|
||||
urlValues := make(url.Values)
|
||||
for i := 0; i < len(postKeyValues); i += 2 {
|
||||
if i != 0 {
|
||||
keyValues.WriteByte('&')
|
||||
}
|
||||
fmt.Fprintf(keyValues, "%s=%s", postKeyValues[i], postKeyValues[i+1])
|
||||
urlValues.Set(postKeyValues[i], postKeyValues[i+1])
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "http://localhost", keyValues)
|
||||
req, err := http.NewRequest("POST", "http://localhost", strings.NewReader(urlValues.Encode()))
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
return ab.NewContext(), req
|
||||
return req
|
||||
}
|
||||
|
||||
type mockValidator struct {
|
||||
|
@@ -82,10 +82,10 @@ func (reg *Register) registerHandler(ctx *authboss.Context, w http.ResponseWrite
|
||||
}
|
||||
|
||||
func (reg *Register) registerPostHandler(ctx *authboss.Context, w http.ResponseWriter, r *http.Request) error {
|
||||
key, _ := ctx.FirstPostFormValue(reg.PrimaryID)
|
||||
password, _ := ctx.FirstPostFormValue(authboss.StorePassword)
|
||||
key := r.FormValue(reg.PrimaryID)
|
||||
password := r.FormValue(authboss.StorePassword)
|
||||
|
||||
validationErrs := ctx.Validate(reg.Policies, reg.ConfirmFields...)
|
||||
validationErrs := ctx.Validate(req, reg.Policies, reg.ConfirmFields...)
|
||||
|
||||
if user, err := ctx.Storer.Get(key); err != nil && err != authboss.ErrUserNotFound {
|
||||
return err
|
||||
@@ -101,7 +101,7 @@ func (reg *Register) registerPostHandler(ctx *authboss.Context, w http.ResponseW
|
||||
}
|
||||
|
||||
for _, f := range reg.PreserveFields {
|
||||
data[f], _ = ctx.FirstFormValue(f)
|
||||
data[f] = r.FormValue(f)
|
||||
}
|
||||
|
||||
return reg.templates.Render(ctx, w, r, tplRegister, data)
|
||||
|
@@ -65,7 +65,7 @@ func (f FieldError) Error() string {
|
||||
}
|
||||
|
||||
// Validate validates a request using the given ruleset.
|
||||
func (ctx *Context) Validate(r *http.Request, ruleset []Validator, confirmFields ...string) ErrorList {
|
||||
func Validate(r *http.Request, ruleset []Validator, confirmFields ...string) ErrorList {
|
||||
errList := make(ErrorList, 0)
|
||||
|
||||
for _, validator := range ruleset {
|
||||
|
@@ -64,10 +64,9 @@ func TestErrorList_Map(t *testing.T) {
|
||||
func TestValidate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ab := New()
|
||||
ctx, req := mockRequestContext(ab, StoreUsername, "john", StoreEmail, "john@john.com")
|
||||
req := mockRequest(StoreUsername, "john", StoreEmail, "john@john.com")
|
||||
|
||||
errList := ctx.Validate(req, []Validator{
|
||||
errList := Validate(req, []Validator{
|
||||
mockValidator{
|
||||
FieldName: StoreUsername,
|
||||
Errs: ErrorList{FieldError{StoreUsername, errors.New("must be longer than 4")}},
|
||||
@@ -96,21 +95,20 @@ func TestValidate(t *testing.T) {
|
||||
func TestValidate_Confirm(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ab := New()
|
||||
ctx, req := mockRequestContext(ab, StoreUsername, "john", "confirmUsername", "johnny")
|
||||
errs := ctx.Validate(req, nil, StoreUsername, "confirmUsername").Map()
|
||||
req := mockRequest(StoreUsername, "john", "confirmUsername", "johnny")
|
||||
errs := Validate(req, nil, StoreUsername, "confirmUsername").Map()
|
||||
if errs["confirmUsername"][0] != "Does not match username" {
|
||||
t.Error("Expected a different error for confirmUsername:", errs["confirmUsername"][0])
|
||||
}
|
||||
|
||||
ctx, req = mockRequestContext(ab, StoreUsername, "john", "confirmUsername", "john")
|
||||
errs = ctx.Validate(req, nil, StoreUsername, "confirmUsername").Map()
|
||||
req = mockRequest(StoreUsername, "john", "confirmUsername", "john")
|
||||
errs = Validate(req, nil, StoreUsername, "confirmUsername").Map()
|
||||
if len(errs) != 0 {
|
||||
t.Error("Expected no errors:", errs)
|
||||
}
|
||||
|
||||
ctx, req = mockRequestContext(ab, StoreUsername, "john", "confirmUsername", "john")
|
||||
errs = ctx.Validate(req, nil, StoreUsername).Map()
|
||||
req = mockRequest(StoreUsername, "john", "confirmUsername", "john")
|
||||
errs = Validate(req, nil, StoreUsername).Map()
|
||||
if len(errs) != 0 {
|
||||
t.Error("Expected no errors:", errs)
|
||||
}
|
||||
|
Reference in New Issue
Block a user