verify the username, email and nickname are valid when registering via OAuth 2.0
This commit is contained in:
@@ -112,6 +112,7 @@ func startWebServer(c *core.CliContext) error {
|
||||
_ = v.RegisterValidation("notBlank", validators.NotBlank)
|
||||
_ = v.RegisterValidation("validUsername", validators.ValidUsername)
|
||||
_ = v.RegisterValidation("validEmail", validators.ValidEmail)
|
||||
_ = v.RegisterValidation("validNickname", validators.ValidNickname)
|
||||
_ = v.RegisterValidation("validCurrency", validators.ValidCurrency)
|
||||
_ = v.RegisterValidation("validHexRGBColor", validators.ValidHexRGBColor)
|
||||
_ = v.RegisterValidation("validAmountFilter", validators.ValidAmountFilter)
|
||||
|
||||
@@ -146,7 +146,7 @@ func (a *ForgetPasswordsApi) UserResetPasswordHandler(c *core.WebContext) (any,
|
||||
|
||||
if user.Email != request.Email {
|
||||
log.Warnf(c, "[forget_passwords.UserResetPasswordHandler] request email not equals the user email")
|
||||
return nil, errs.ErrEmptyIsInvalid
|
||||
return nil, errs.ErrEmailIsInvalid
|
||||
}
|
||||
|
||||
if a.users.IsPasswordEqualsUserPassword(request.Password, user) {
|
||||
|
||||
@@ -234,6 +234,18 @@ func (a *OAuth2AuthenticationApi) CallbackHandler(c *core.WebContext) (string, *
|
||||
nickName = userName
|
||||
}
|
||||
|
||||
if !utils.IsValidUsername(userName) {
|
||||
return a.redirectToFailedCallbackPage(c, errs.ErrUserNameIsInvalid)
|
||||
}
|
||||
|
||||
if !utils.IsValidEmail(email) {
|
||||
return a.redirectToFailedCallbackPage(c, errs.ErrEmailIsInvalid)
|
||||
}
|
||||
|
||||
if !utils.IsValidNickName(nickName) {
|
||||
return a.redirectToFailedCallbackPage(c, errs.ErrNickNameIsInvalid)
|
||||
}
|
||||
|
||||
if _, exists := locales.AllLanguages[oauth2UserInfo.LanguageCode]; exists {
|
||||
languageCode = oauth2UserInfo.LanguageCode
|
||||
}
|
||||
|
||||
+3
-1
@@ -23,7 +23,7 @@ var (
|
||||
ErrUserRegistrationNotAllowed = NewNormalError(NormalSubcategoryUser, 14, http.StatusBadRequest, "user registration not allowed")
|
||||
ErrUserDefaultAccountIsInvalid = NewNormalError(NormalSubcategoryUser, 15, http.StatusBadRequest, "user default account is invalid")
|
||||
ErrUserIsDisabled = NewNormalError(NormalSubcategoryUser, 16, http.StatusBadRequest, "user is disabled")
|
||||
ErrEmptyIsInvalid = NewNormalError(NormalSubcategoryUser, 17, http.StatusBadRequest, "email is invalid")
|
||||
ErrEmailIsInvalid = NewNormalError(NormalSubcategoryUser, 17, http.StatusBadRequest, "email is invalid")
|
||||
ErrEmailIsEmptyOrInvalid = NewNormalError(NormalSubcategoryUser, 18, http.StatusBadRequest, "email is empty or invalid")
|
||||
ErrNewPasswordEqualsOldInvalid = NewNormalError(NormalSubcategoryUser, 19, http.StatusBadRequest, "new password equals old password")
|
||||
ErrEmailIsNotVerified = NewNormalError(NormalSubcategoryUser, 20, http.StatusBadRequest, "email is not verified")
|
||||
@@ -39,4 +39,6 @@ var (
|
||||
ErrExceedMaxUserAvatarFileSize = NewNormalError(NormalSubcategoryUser, 30, http.StatusBadRequest, "exceed the maximum size of user avatar file")
|
||||
ErrNotPermittedToPerformThisAction = NewNormalError(NormalSubcategoryUser, 31, http.StatusBadRequest, "not permitted to perform this action")
|
||||
ErrCannotLoginByPassword = NewNormalError(NormalSubcategoryUser, 32, http.StatusBadRequest, "cannot login by password")
|
||||
ErrUserNameIsInvalid = NewNormalError(NormalSubcategoryUser, 33, http.StatusBadRequest, "user name is invalid")
|
||||
ErrNickNameIsInvalid = NewNormalError(NormalSubcategoryUser, 34, http.StatusBadRequest, "nick name is invalid")
|
||||
)
|
||||
|
||||
+2
-2
@@ -161,7 +161,7 @@ type UserLoginRequest struct {
|
||||
type UserRegisterRequest struct {
|
||||
Username string `json:"username" binding:"required,notBlank,max=32,validUsername"`
|
||||
Email string `json:"email" binding:"required,notBlank,max=100,validEmail"`
|
||||
Nickname string `json:"nickname" binding:"required,notBlank,max=64"`
|
||||
Nickname string `json:"nickname" binding:"required,notBlank,max=64,validNickname"`
|
||||
Password string `json:"password" binding:"required,min=6,max=128"`
|
||||
Language string `json:"language" binding:"required,min=2,max=16"`
|
||||
DefaultCurrency string `json:"defaultCurrency" binding:"required,len=3,validCurrency"`
|
||||
@@ -190,7 +190,7 @@ type UserResendVerifyEmailRequest struct {
|
||||
// UserProfileUpdateRequest represents all parameters of user updating profile request
|
||||
type UserProfileUpdateRequest struct {
|
||||
Email string `json:"email" binding:"omitempty,notBlank,max=100,validEmail"`
|
||||
Nickname string `json:"nickname" binding:"omitempty,notBlank,max=64"`
|
||||
Nickname string `json:"nickname" binding:"omitempty,notBlank,max=64,validNickname"`
|
||||
Password string `json:"password" binding:"omitempty,min=6,max=128"`
|
||||
OldPassword string `json:"oldPassword" binding:"omitempty,min=6,max=128"`
|
||||
DefaultAccountId int64 `json:"defaultAccountId,string" binding:"omitempty,min=1"`
|
||||
|
||||
@@ -16,12 +16,17 @@ var (
|
||||
|
||||
// IsValidUsername reports whether username is valid
|
||||
func IsValidUsername(username string) bool {
|
||||
return usernamePattern.MatchString(username)
|
||||
return len(username) <= 32 && usernamePattern.MatchString(username)
|
||||
}
|
||||
|
||||
// IsValidEmail reports whether email is valid
|
||||
func IsValidEmail(email string) bool {
|
||||
return emailPattern.MatchString(email)
|
||||
return len(email) <= 100 && emailPattern.MatchString(email)
|
||||
}
|
||||
|
||||
// IsValidNickName reports whether user nick name is valid
|
||||
func IsValidNickName(nickname string) bool {
|
||||
return len(nickname) <= 64
|
||||
}
|
||||
|
||||
// IsValidHexRGBColor reports whether color is valid
|
||||
@@ -8,365 +8,318 @@ import (
|
||||
|
||||
func TestIsValidUsername_ValidUserName(t *testing.T) {
|
||||
username := "foobar"
|
||||
expectedValue := true
|
||||
actualValue := IsValidUsername(username)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
username = "--foo_bar--"
|
||||
expectedValue = true
|
||||
actualValue = IsValidUsername(username)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidUsername_InvalidUserName(t *testing.T) {
|
||||
username := "foo~bar~"
|
||||
expectedValue := false
|
||||
actualValue := IsValidUsername(username)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
username = "012345678901234567890123456789012"
|
||||
actualValue = IsValidUsername(username)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidEmail_ValidEmail(t *testing.T) {
|
||||
email := "foo@bar.com"
|
||||
expectedValue := true
|
||||
actualValue := IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
email = "foo@1.2.3.4"
|
||||
expectedValue = true
|
||||
actualValue = IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
email = "foo_bar@foo.bar"
|
||||
expectedValue = true
|
||||
actualValue = IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidEmail_InvalidEmail(t *testing.T) {
|
||||
email := "foo"
|
||||
expectedValue := false
|
||||
actualValue := IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
email = "@bar"
|
||||
expectedValue = false
|
||||
actualValue = IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
email = "foo@bar"
|
||||
expectedValue = false
|
||||
actualValue = IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
email = "foo@bar."
|
||||
expectedValue = false
|
||||
actualValue = IsValidEmail(email)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
email = "012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789@foobar.com"
|
||||
actualValue = IsValidEmail(email)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidNickName_ValidNickName(t *testing.T) {
|
||||
nickname := "0123456789012345678901234567890123456789012345678901234567890123"
|
||||
actualValue := IsValidNickName(nickname)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidNickName_InvalidNickName(t *testing.T) {
|
||||
username := "01234567890123456789012345678901234567890123456789012345678901234"
|
||||
actualValue := IsValidNickName(username)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidHexRGBColor_ValidHexRGBColor(t *testing.T) {
|
||||
color := "000000"
|
||||
expectedValue := true
|
||||
actualValue := IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
color = "000"
|
||||
expectedValue = true
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
color = "e0e0e0"
|
||||
expectedValue = true
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
color = "ffffff"
|
||||
expectedValue = true
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
color = "FFFFFF"
|
||||
expectedValue = true
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidHexRGBColor_InvalidHexRGBColor(t *testing.T) {
|
||||
color := "f"
|
||||
expectedValue := false
|
||||
actualValue := IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
color = "fffffff"
|
||||
expectedValue = false
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
color = "gggggg"
|
||||
expectedValue = false
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
color = "#ffffff"
|
||||
expectedValue = false
|
||||
actualValue = IsValidHexRGBColor(color)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidLongDateTimeFormat_ValidLongDateTimeFormat(t *testing.T) {
|
||||
datetime := "2024-09-01 12:34:56"
|
||||
expectedValue := true
|
||||
actualValue := IsValidLongDateTimeFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024-10-01 00:00:00"
|
||||
expectedValue = true
|
||||
actualValue = IsValidLongDateTimeFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "9999-12-31 23:59:59"
|
||||
expectedValue = true
|
||||
actualValue = IsValidLongDateTimeFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidLongDateTimeFormat_InvalidLongDateTimeFormat(t *testing.T) {
|
||||
datetime := "2024-09-01"
|
||||
expectedValue := false
|
||||
actualValue := IsValidLongDateTimeFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateTimeFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12:34"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateTimeFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidLongDateTimeWithoutSecondFormat_ValidLongDateTimeWithoutSecondFormat(t *testing.T) {
|
||||
datetime := "2024-09-01 12:34"
|
||||
expectedValue := true
|
||||
actualValue := IsValidLongDateTimeWithoutSecondFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024-10-01 00:00"
|
||||
expectedValue = true
|
||||
actualValue = IsValidLongDateTimeWithoutSecondFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "9999-12-31 23:59"
|
||||
expectedValue = true
|
||||
actualValue = IsValidLongDateTimeWithoutSecondFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidLongDateTimeWithoutSecondFormat_InvalidLongDateTimeWithoutSecondFormat(t *testing.T) {
|
||||
datetime := "2024-09-01"
|
||||
expectedValue := false
|
||||
actualValue := IsValidLongDateTimeWithoutSecondFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateTimeWithoutSecondFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12:34:56"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateTimeWithoutSecondFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidLongDateFormat_ValidLongDateFormat(t *testing.T) {
|
||||
datetime := "2024-09-01"
|
||||
expectedValue := true
|
||||
actualValue := IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "9999-12-31"
|
||||
expectedValue = true
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidLongDateFormat_InvalidLongDateFormat(t *testing.T) {
|
||||
datetime := "24-09-01"
|
||||
expectedValue := false
|
||||
actualValue := IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-9-1"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-1"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-9-01"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12:34"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
|
||||
datetime = "2024-09-01 12:34:56"
|
||||
expectedValue = false
|
||||
actualValue = IsValidLongDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.False(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidYearMonthDayLongOrShortDateFormat_ValidFormat(t *testing.T) {
|
||||
datetime := "2024-09-01"
|
||||
expectedValue := true
|
||||
actualValue := IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "24-09-01"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024-09-1"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024-9-01"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024-9-1"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "9999-12-31"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024/09/01"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024.09.01"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "2024'09.01"
|
||||
expectedValue = true
|
||||
actualValue = IsValidYearMonthDayLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidMonthDayYearLongOrShortDateFormat_ValidFormat(t *testing.T) {
|
||||
datetime := "09-01-2024"
|
||||
expectedValue := true
|
||||
actualValue := IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "09-01-24"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "09-1-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "9-01-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "9-1-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "12-31-9999"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "09/01/2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "09.01.2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "09/01'2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidMonthDayYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
|
||||
func TestIsValidDayMonthYearLongDateFormat_ValidLongDateFormat(t *testing.T) {
|
||||
datetime := "01-09-2024"
|
||||
expectedValue := true
|
||||
actualValue := IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "01-09-24"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "1-09-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "01-9-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "1-9-2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "31-12-9999"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "01/09/2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "01.09.2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
|
||||
datetime = "01/09'2024"
|
||||
expectedValue = true
|
||||
actualValue = IsValidDayMonthYearLongOrShortDateFormat(datetime)
|
||||
assert.Equal(t, expectedValue, actualValue)
|
||||
assert.True(t, actualValue)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package validators
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
|
||||
"github.com/mayswind/ezbookkeeping/pkg/utils"
|
||||
)
|
||||
|
||||
// ValidNickname returns whether the given nick name is valid
|
||||
func ValidNickname(fl validator.FieldLevel) bool {
|
||||
if value, ok := fl.Field().Interface().(string); ok {
|
||||
if utils.IsValidNickName(value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package validators
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestValidNickname(t *testing.T) {
|
||||
validate := validator.New()
|
||||
err := validate.RegisterValidation("validNickname", ValidNickname)
|
||||
assert.Nil(t, err)
|
||||
|
||||
nickname := "0123456789012345678901234567890123456789012345678901234567890123"
|
||||
err = validate.Var(nickname, "validNickname")
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestInvalidNickname(t *testing.T) {
|
||||
validate := validator.New()
|
||||
err := validate.RegisterValidation("validNickname", ValidNickname)
|
||||
assert.Nil(t, err)
|
||||
|
||||
nickname := "01234567890123456789012345678901234567890123456789012345678901234"
|
||||
err = validate.Var(nickname, "validNickname")
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "Hochgeladener Benutzeravatar überschreitet die maximal zulässige Dateigröße",
|
||||
"not permitted to perform this action": "Sie sind nicht berechtigt, diese Aktion auszuführen",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Unbefugter Zugriff",
|
||||
"current token is invalid": "Aktuelles Token ist ungültig",
|
||||
"current token is expired": "Aktuelles Token ist abgelaufen",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "The uploaded user avatar exceeds the maximum allowed file size",
|
||||
"not permitted to perform this action": "You are not permitted to perform this action",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Unauthorized access",
|
||||
"current token is invalid": "Current token is invalid",
|
||||
"current token is expired": "Current token is expired",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "El avatar de usuario subido supera el tamaño de archivo máximo permitido",
|
||||
"not permitted to perform this action": "No tienes permiso para realizar esta acción.",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Acceso no autorizado",
|
||||
"current token is invalid": "El token actual no es válido",
|
||||
"current token is expired": "El token actual ha caducado",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "L'avatar utilisateur téléchargé dépasse la taille de fichier maximale autorisée",
|
||||
"not permitted to perform this action": "Vous n'êtes pas autorisé à effectuer cette action",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Accès non autorisé",
|
||||
"current token is invalid": "Le token actuel est invalide",
|
||||
"current token is expired": "Le token actuel a expiré",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "L'avatar utente caricato supera la dimensione massima consentita del file",
|
||||
"not permitted to perform this action": "Non sei autorizzato a eseguire questa azione",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Accesso non autorizzato",
|
||||
"current token is invalid": "Token corrente non valido",
|
||||
"current token is expired": "Token corrente scaduto",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "アップロードされたユーザーアバターは最大ファイルサイズを超えています",
|
||||
"not permitted to perform this action": "このアクションを実行は許可されていません",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "不正アクセス",
|
||||
"current token is invalid": "現在のトークンは無効です",
|
||||
"current token is expired": "現在のトークンの有効期限が切れています",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "업로드된 사용자 아바타가 허용된 최대 파일 크기를 초과합니다",
|
||||
"not permitted to perform this action": "이 작업을 수행할 수 있는 권한이 없습니다",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "권한이 없는 접근입니다",
|
||||
"current token is invalid": "현재 토큰이 유효하지 않습니다",
|
||||
"current token is expired": "현재 토큰이 만료되었습니다",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "Geüploade avatar overschrijdt de maximaal toegestane bestandsgrootte",
|
||||
"not permitted to perform this action": "Je hebt geen toestemming om deze actie uit te voeren",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Ongeautoriseerde toegang",
|
||||
"current token is invalid": "Huidige token is ongeldig",
|
||||
"current token is expired": "Huidige token is verlopen",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "O avatar de usuário enviado excede o tamanho máximo permitido de arquivo",
|
||||
"not permitted to perform this action": "Você não tem permissão para realizar esta ação",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Acesso não autorizado",
|
||||
"current token is invalid": "Token atual é inválido",
|
||||
"current token is expired": "Token atual está expirado",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "Загруженный аватар пользователя превышает максимально допустимый размер файла",
|
||||
"not permitted to perform this action": "Вам не разрешено выполнять это действие",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Несанкционированный доступ",
|
||||
"current token is invalid": "Текущий токен недействителен",
|
||||
"current token is expired": "Текущий токен истек",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "ขนาดไฟล์รูปประจำตัวผู้ใช้เกินขนาดสูงสุดที่อนุญาต",
|
||||
"not permitted to perform this action": "คุณไม่ได้รับอนุญาตให้ดำเนินการนี้",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "การเข้าถึงไม่ได้รับอนุญาต",
|
||||
"current token is invalid": "โทเค็นปัจจุบันไม่ถูกต้อง",
|
||||
"current token is expired": "โทเค็นปัจจุบันหมดอายุ",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "Завантажений аватар перевищує максимально допустимий розмір",
|
||||
"not permitted to perform this action": "Вам не дозволено виконувати цю дію",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Несанкціонований доступ",
|
||||
"current token is invalid": "Поточний токен недійсний",
|
||||
"current token is expired": "Поточний токен прострочений",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "Avatar người dùng đã tải lên vượt quá kích thước tệp tối đa cho phép",
|
||||
"not permitted to perform this action": "Bạn không được phép thực hiện hành động này",
|
||||
"cannot login by password": "You cannot login by password",
|
||||
"user name is invalid": "User name is invalid",
|
||||
"nick name is invalid": "User nickname is invalid",
|
||||
"unauthorized access": "Truy cập trái phép",
|
||||
"current token is invalid": "Mã thông báo hiện tại không hợp lệ",
|
||||
"current token is expired": "Mã thông báo hiện tại đã hết hạn",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "上传的用户头像超出了允许的最大文件大小",
|
||||
"not permitted to perform this action": "您不能执行该操作",
|
||||
"cannot login by password": "您不能使用密码登录",
|
||||
"user name is invalid": "用户名无效",
|
||||
"nick name is invalid": "用户昵称无效",
|
||||
"unauthorized access": "未授权的登录",
|
||||
"current token is invalid": "当前认证令牌无效",
|
||||
"current token is expired": "当前认证令牌已过期",
|
||||
|
||||
@@ -1089,6 +1089,8 @@
|
||||
"exceed the maximum size of user avatar file": "上傳的使用者頭像超過允許的最大檔案大小",
|
||||
"not permitted to perform this action": "您不能執行該操作",
|
||||
"cannot login by password": "您不能使用密碼登入",
|
||||
"user name is invalid": "使用者名稱無效",
|
||||
"nick name is invalid": "使用者暱稱無效",
|
||||
"unauthorized access": "未授權的登入",
|
||||
"current token is invalid": "目前認證令牌無效",
|
||||
"current token is expired": "目前認證令牌已過期",
|
||||
|
||||
Reference in New Issue
Block a user