mirror of
https://github.com/volatiletech/authboss.git
synced 2025-09-16 09:06:20 +02:00
Delete useless module.
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
Validate
|
||||
=========
|
||||
|
@@ -1,82 +0,0 @@
|
||||
// Package validate supports validation of usernames, email addresses, and passwords.
|
||||
package validate
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/authboss.v0"
|
||||
)
|
||||
|
||||
var V *Validate
|
||||
|
||||
func init() {
|
||||
V = &Validate{}
|
||||
authboss.RegisterModule("validate", V)
|
||||
}
|
||||
|
||||
const (
|
||||
policyEmail = "email"
|
||||
policyUsername = "username"
|
||||
policyPassword = "password"
|
||||
)
|
||||
|
||||
type Validate struct {
|
||||
Email authboss.Validator
|
||||
Username authboss.Validator
|
||||
Password authboss.Validator
|
||||
}
|
||||
|
||||
func (v *Validate) Initialize() error {
|
||||
policies := authboss.FilterValidators(authboss.Cfg.Policies, policyEmail, policyUsername, policyPassword)
|
||||
|
||||
if v.Email = policies[0]; v.Email.Field() != policyEmail {
|
||||
return fmt.Errorf("validate: missin g policy: %s", policyEmail)
|
||||
}
|
||||
|
||||
if v.Username = policies[1]; v.Username.Field() != policyUsername {
|
||||
return fmt.Errorf("validate: missing policy: %s", policyUsername)
|
||||
}
|
||||
|
||||
if v.Password = policies[2]; v.Password.Field() != policyPassword {
|
||||
return fmt.Errorf("validate: missing policy: %s", policyPassword)
|
||||
}
|
||||
|
||||
authboss.Cfg.Callbacks.Before(authboss.EventRegister, v.BeforeRegister)
|
||||
authboss.Cfg.Callbacks.Before(authboss.EventRecoverStart, v.BeforeRegister)
|
||||
authboss.Cfg.Callbacks.Before(authboss.EventRecoverEnd, v.BeforeRegister)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Validate) Routes() authboss.RouteTable { return nil }
|
||||
func (v *Validate) Storage() authboss.StorageOptions { return nil }
|
||||
|
||||
func (v *Validate) BeforeRegister(ctx *authboss.Context) error {
|
||||
errList := make(authboss.ErrorList, 0)
|
||||
|
||||
if v.Email != nil {
|
||||
email, ok := ctx.FirstPostFormValue("email")
|
||||
if ok {
|
||||
errs := v.Email.Errors(email)
|
||||
errList = append(errList, errs...)
|
||||
}
|
||||
}
|
||||
|
||||
if v.Username != nil {
|
||||
username, ok := ctx.FirstPostFormValue("username")
|
||||
if ok {
|
||||
errs := v.Username.Errors(username)
|
||||
errList = append(errList, errs...)
|
||||
}
|
||||
}
|
||||
|
||||
if v.Password != nil {
|
||||
password, ok := ctx.FirstPostFormValue("password")
|
||||
if ok {
|
||||
errs := v.Password.Errors(password)
|
||||
errList = append(errList, errs...)
|
||||
}
|
||||
}
|
||||
|
||||
return errList
|
||||
}
|
@@ -1,69 +0,0 @@
|
||||
package validate
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/authboss.v0"
|
||||
)
|
||||
|
||||
func TestValidate_Initialiaze(t *testing.T) {
|
||||
authboss.NewConfig()
|
||||
authboss.Cfg.Policies = []authboss.Validator{
|
||||
authboss.Rules{FieldName: policyEmail},
|
||||
authboss.Rules{FieldName: policyUsername},
|
||||
authboss.Rules{FieldName: policyPassword},
|
||||
}
|
||||
|
||||
err := V.Initialize()
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
|
||||
if V.Email == nil {
|
||||
t.Error("Should have set Email validator.")
|
||||
}
|
||||
if V.Username == nil {
|
||||
t.Error("Should have set Username validator.")
|
||||
}
|
||||
if V.Password == nil {
|
||||
t.Error("Should have set Password validator.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidate_BeforeRegister(t *testing.T) {
|
||||
authboss.NewConfig()
|
||||
authboss.Cfg.Policies = []authboss.Validator{
|
||||
authboss.Rules{FieldName: policyEmail, MinLength: 15},
|
||||
authboss.Rules{FieldName: policyUsername, MaxLength: 1},
|
||||
authboss.Rules{FieldName: policyPassword, MinLength: 8},
|
||||
}
|
||||
|
||||
err := V.Initialize()
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
|
||||
body := `email=joe@joe.ca&password=hi&username=hello`
|
||||
req, err := http.NewRequest("POST", "http://localhost", bytes.NewBufferString(body))
|
||||
if err != nil {
|
||||
t.Error("Unexpected Error:", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
ctx, err := authboss.ContextFromRequest(req)
|
||||
if err != nil {
|
||||
t.Error("Unexpected error:", err)
|
||||
}
|
||||
|
||||
err = V.BeforeRegister(ctx)
|
||||
if err == nil {
|
||||
t.Error("Expected three validation errors.")
|
||||
}
|
||||
|
||||
list := err.(authboss.ErrorList)
|
||||
if len(list) != 3 {
|
||||
t.Error("Expected three validation errors.")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user