コード例 #1
0
ファイル: user.go プロジェクト: wolfeidau/authinator
// ValidateUserUpdate validate user update requests
func ValidateUserUpdate(newUser, oldUser *models.User) field.ErrorList {
	allErrs := field.ErrorList{}

	path := field.NewPath("User")

	allErrs = append(allErrs, validateImmutibleFields(newUser, oldUser, path, []string{"ID", "Email", "Login"})...)
	allErrs = append(allErrs, validateInvalidFields(newUser, path, []string{"Password"})...)

	return allErrs
}
コード例 #2
0
ファイル: user.go プロジェクト: wolfeidau/authinator
// ValidateUserRegister validate user registration requests
func ValidateUserRegister(newUser *models.User) field.ErrorList {
	allErrs := field.ErrorList{}

	path := field.NewPath("User")

	allErrs = append(allErrs, validateInvalidFields(newUser, path, []string{"ID"})...)
	allErrs = append(allErrs, validateRequiredFields(newUser, path, []string{"Email", "Login", "Password"})...)

	allErrs = append(allErrs, validateFieldLength(newUser.Email, path, 5, 255, "Email")...)
	allErrs = append(allErrs, validateFieldLength(newUser.Login, path, 5, 255, "Login")...)
	allErrs = append(allErrs, validateFieldLength(newUser.Password, path, 5, 255, "Password")...)

	return allErrs
}
コード例 #3
0
ファイル: user_test.go プロジェクト: wolfeidau/authinator
func TestValidateUserRegister(t *testing.T) {
	testCases := []struct {
		newUser  *models.User
		expected field.ErrorList
	}{
		{
			newUser: &models.User{
				Login:    models.String("wolfeidau"),
				Email:    models.String("*****@*****.**"),
				Name:     models.String("Mark Wolf"),
				Password: models.String("Somewh3r3 there is a cow!"),
			},
			expected: field.ErrorList{},
		},
		{
			newUser: &models.User{
				ID:       models.String("123"),
				Login:    models.String("wolfeidau"),
				Email:    models.String("*****@*****.**"),
				Name:     models.String("Mark Wolf"),
				Password: models.String("Somewh3r3 there is a cow!"),
			},
			expected: field.ErrorList{
				&field.Error{Type: field.ErrorTypeForbidden, Field: field.NewPath("User", "ID").String(), BadValue: "", Detail: "User updates must not supply ID"},
			},
		},
		{
			newUser: &models.User{
				ID:    models.String("123"),
				Name:  models.String("Mark Wolf"),
				Email: models.String("*****@*****.**"),
			},
			expected: field.ErrorList{
				&field.Error{Type: field.ErrorTypeForbidden, Field: field.NewPath("User", "ID").String(), BadValue: "", Detail: "User updates must not supply ID"},
				&field.Error{Type: field.ErrorTypeRequired, Field: field.NewPath("User", "Login").String(), BadValue: "", Detail: "User updates must supply Login"},
				&field.Error{Type: field.ErrorTypeRequired, Field: field.NewPath("User", "Password").String(), BadValue: "", Detail: "User updates must supply Password"},
				&field.Error{Type: field.ErrorTypeInvalid, Field: field.NewPath("User", "Login").String(), BadValue: "", Detail: "User: Login must be between 5 and 255 characters"},
				&field.Error{Type: field.ErrorTypeInvalid, Field: field.NewPath("User", "Password").String(), BadValue: "", Detail: "User: Password must be between 5 and 255 characters"},
			},
		},
	}

	for _, testCase := range testCases {
		errList := ValidateUserRegister(testCase.newUser)

		if !reflect.DeepEqual(errList, testCase.expected) {
			t.Errorf("expected\n%s\ngot\n%s\n", toJSON(testCase.expected), toJSON(errList))
		}
	}
}
コード例 #4
0
ファイル: user_test.go プロジェクト: wolfeidau/authinator
func TestValidateUpdateUser(t *testing.T) {
	testCases := []struct {
		newUser  *models.User
		oldUser  *models.User
		expected field.ErrorList
	}{
		{
			newUser:  models.NewUser("123", "wolfeidau", "*****@*****.**", "Mark Wolfe"),
			oldUser:  models.NewUser("123", "wolfeidau", "*****@*****.**", "Mark Wolfe"),
			expected: field.ErrorList{},
		},
		{
			newUser: &models.User{
				Name: models.String("Mark Wolf"),
			},
			oldUser:  models.NewUser("123", "wolfeidau", "*****@*****.**", "Mark Wolfe"),
			expected: field.ErrorList{},
		},
		{
			newUser: &models.User{
				Name:     models.String("Mark Wolf"),
				Password: models.String("Somewh3r3 there is a cow!"),
			},
			oldUser: models.NewUser("123", "wolfeidau", "*****@*****.**", "Mark Wolfe"),
			expected: field.ErrorList{
				&field.Error{Type: field.ErrorTypeForbidden, Field: field.NewPath("User", "Password").String(), BadValue: "", Detail: "User updates must not supply Password"},
			},
		},
	}

	for _, testCase := range testCases {
		errList := ValidateUserUpdate(testCase.newUser, testCase.oldUser)

		if !reflect.DeepEqual(errList, testCase.expected) {
			t.Errorf("expected\n%s\ngot\n%s\n", toJSON(testCase.expected), toJSON(errList))
		}
	}
}