Refactored user forms struct + tests
Cette révision appartient à :
Parent
b142ee75a4
révision
1316062c9a
3 fichiers modifiés avec 96 ajouts et 32 suppressions
|
@ -2,59 +2,59 @@ package userValidator
|
|||
|
||||
// RegistrationForm is used when creating a user.
|
||||
type RegistrationForm struct {
|
||||
Username string `form:"username" needed:"true" len_min:"3" len_max:"20"`
|
||||
Email string `form:"email"`
|
||||
Password string `form:"password" needed:"true" len_min:"6" len_max:"72" equalInput:"ConfirmPassword"`
|
||||
ConfirmPassword string `form:"password_confirmation" hum_name:"Password Confirmation" omit:"true" needed:"true"`
|
||||
CaptchaID string `form:"captchaID" omit:"true" needed:"true"`
|
||||
TermsAndConditions bool `form:"t_and_c" omit:"true" needed:"true" equal:"true" hum_name:"Terms and Conditions"`
|
||||
Username string `validate:"required,min=3,max=20"`
|
||||
Email string
|
||||
Password string `validate:"required,min=6,max=72,eqfield=ConfirmPassword"`
|
||||
ConfirmPassword string `validate:"required" omit:"true"` // Omit when binding to user model since user model doesn't have those field
|
||||
CaptchaID string `validate:"required" omit:"true"`
|
||||
TermsAndConditions string `validate:"eq=true" omit:"true"`
|
||||
}
|
||||
|
||||
// LoginForm is used when a user logs in.
|
||||
type LoginForm struct {
|
||||
Username string `form:"username" needed:"true" json:"username"`
|
||||
Password string `form:"password" needed:"true" json:"password"`
|
||||
Username string `validate:"required" json:"username"`
|
||||
Password string `validate:"required" json:"password"`
|
||||
}
|
||||
|
||||
// UserForm is used when updating a user.
|
||||
type UserForm struct {
|
||||
Username string `form:"username" json:"username" needed:"true" len_min:"3" len_max:"20"`
|
||||
Email string `form:"email" json:"email"`
|
||||
Language string `form:"language" json:"language" default:"en-us"`
|
||||
CurrentPassword string `form:"current_password" json:"current_password" len_min:"6" len_max:"72" omit:"true"`
|
||||
Password string `form:"password" json:"password" len_min:"6" len_max:"72" equalInput:"ConfirmPassword"`
|
||||
ConfirmPassword string `form:"password_confirmation" json:"password_confirmation" hum_name:"Password Confirmation" omit:"true"`
|
||||
Status int `form:"status" json:"status" default:"0"`
|
||||
Theme string `form:"theme" json:"theme"`
|
||||
Username string `validate:"required" json:"username" needed:"true" len_min:"3" len_max:"20"`
|
||||
Email string `json:"email"`
|
||||
Language string `validate:"default=en-us" json:"language"`
|
||||
CurrentPassword string `validate:"required,min=6,max=72" json:"current_password" omit:"true"`
|
||||
Password string `validate:"required,min=6,max=72" json:"password" len_min:"6" len_max:"72" equalInput:"ConfirmPassword"`
|
||||
ConfirmPassword string `validate:"required" json:"password_confirmation" omit:"true"`
|
||||
Status int `validate:"default=0" json:"status"`
|
||||
Theme string `json:"theme"`
|
||||
}
|
||||
|
||||
// UserSettingsForm is used when updating a user.
|
||||
type UserSettingsForm struct {
|
||||
NewTorrent bool `form:"new_torrent" json:"new_torrent" default:"true"`
|
||||
NewTorrentEmail bool `form:"new_torrent_email" json:"new_torrent_email" default:"true"`
|
||||
NewComment bool `form:"new_comment" json:"new_comment" default:"true"`
|
||||
NewCommentEmail bool `form:"new_comment_email" json:"new_comment_email" default:"false"`
|
||||
NewResponses bool `form:"new_responses" json:"new_responses" default:"true"`
|
||||
NewResponsesEmail bool `form:"new_responses_email" json:"new_responses_email" default:"false"`
|
||||
NewFollower bool `form:"new_follower" json:"new_follower" default:"true"`
|
||||
NewFollowerEmail bool `form:"new_follower_email" json:"new_follower_email" default:"true"`
|
||||
Followed bool `form:"followed" json:"followed" default:"false"`
|
||||
FollowedEmail bool `form:"followed_email" json:"followed_email" default:"false"`
|
||||
NewTorrent bool `validate:"default=true" json:"new_torrent"`
|
||||
NewTorrentEmail bool `validate:"default=true" json:"new_torrent_email"`
|
||||
NewComment bool `validate:"default=true" json:"new_comment"`
|
||||
NewCommentEmail bool `validate:"default=false" json:"new_comment_email"`
|
||||
NewResponses bool `validate:"default=true" json:"new_responses"`
|
||||
NewResponsesEmail bool `validate:"default=false" json:"new_responses_email"`
|
||||
NewFollower bool `validate:"default=true" json:"new_follower"`
|
||||
NewFollowerEmail bool `validate:"default=true" json:"new_follower_email"`
|
||||
Followed bool `validate:"default=false" json:"followed"`
|
||||
FollowedEmail bool `validate:"default=false" json:"followed_email"`
|
||||
}
|
||||
|
||||
// PasswordForm is used when updating a user password.
|
||||
type PasswordForm struct {
|
||||
CurrentPassword string `form:"currentPassword" needed:"true"`
|
||||
Password string `form:"newPassword" needed:"true"`
|
||||
CurrentPassword string `validate:"required"`
|
||||
Password string `validate:"required"`
|
||||
}
|
||||
|
||||
// SendPasswordResetForm is used when sending a password reset token.
|
||||
type SendPasswordResetForm struct {
|
||||
Email string `form:"email" needed:"true"`
|
||||
Email string `validate:"required"`
|
||||
}
|
||||
|
||||
// PasswordResetForm is used when reseting a password.
|
||||
type PasswordResetForm struct {
|
||||
PasswordResetToken string `form:"token" needed:"true"`
|
||||
Password string `form:"newPassword" needed:"true"`
|
||||
PasswordResetToken string `validate:"required"`
|
||||
Password string `validate:"required"`
|
||||
}
|
||||
|
|
57
util/validator/user/forms_test.go
Fichier normal
57
util/validator/user/forms_test.go
Fichier normal
|
@ -0,0 +1,57 @@
|
|||
package userValidator
|
||||
|
||||
import (
|
||||
msg "github.com/NyaaPantsu/nyaa/util/messages"
|
||||
"github.com/NyaaPantsu/nyaa/util/validator"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestForms(t *testing.T) {
|
||||
t.Parallel()
|
||||
req, err := http.NewRequest("GET", "/", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c := &gin.Context{Request: req}
|
||||
messages := msg.GetMessages(c)
|
||||
registration := &RegistrationForm{
|
||||
"lol", "", "testing", "testing", "xxx", "true",
|
||||
}
|
||||
login := &LoginForm{"lol", "testing"}
|
||||
user := &UserForm{"lol", "", "", "testing", "testing", "testing", 0, ""}
|
||||
userSettings := &UserSettingsForm{}
|
||||
password := &PasswordForm{"testing", "testing"}
|
||||
passwordReset := &SendPasswordResetForm{"lol@gt.com"}
|
||||
passwordResetForm := &PasswordResetForm{"testing", "testing"}
|
||||
|
||||
validator.ValidateForm(registration, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on RegistrationForm struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
validator.ValidateForm(login, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on LoginForm struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
validator.ValidateForm(user, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on User struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
validator.ValidateForm(userSettings, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on UserSettingsForm struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
validator.ValidateForm(password, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on PasswordForm struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
validator.ValidateForm(passwordReset, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on SendPasswordResetForm struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
validator.ValidateForm(passwordResetForm, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Error on PasswordResetForm struct, please check validation arguments: %v", messages.GetAllErrors())
|
||||
}
|
||||
}
|
|
@ -22,6 +22,7 @@ type TestForm struct {
|
|||
DefaultVal int `validate:"default=3,required"`
|
||||
ConfirmVal string `validate:"eqfield=ConfirmeVal,min=7,max=8,required"`
|
||||
ConfirmeVal string `validate:"required"`
|
||||
Optional string
|
||||
}
|
||||
|
||||
func TestValidateForm(t *testing.T) {
|
||||
|
@ -39,12 +40,18 @@ func TestValidateForm(t *testing.T) {
|
|||
t.Errorf("No errors when parsing empty invalid form: %v", testform)
|
||||
}
|
||||
messages.ClearAllErrors()
|
||||
testform.DefaultVal, testform.ConfirmVal, testform.ConfirmeVal = 1, "testingl", "testingl"
|
||||
testform.DefaultVal, testform.ConfirmVal, testform.ConfirmeVal, testform.Optional = 1, "testingl", "testingl", "test"
|
||||
ValidateForm(&testform, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Errors when parsing valid form %v\n with errors %v", testform, messages.GetAllErrors())
|
||||
}
|
||||
messages.ClearAllErrors()
|
||||
testform.Optional = ""
|
||||
ValidateForm(&testform, messages)
|
||||
if messages.HasErrors() {
|
||||
t.Errorf("Errors when testing an empty optional field in form %v\n with errors %v", testform, messages.GetAllErrors())
|
||||
}
|
||||
messages.ClearAllErrors()
|
||||
testform.ConfirmVal = "test"
|
||||
testform.ConfirmeVal = "test"
|
||||
ValidateForm(&testform, messages)
|
||||
|
|
Référencer dans un nouveau ticket