示例#1
0
func (controller *usersController) newGet(rw http.ResponseWriter, req *http.Request) (int, error) {
	isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
	userEdit := new(viewmodels.UsersEditViewModel)
	userEdit.Role = "user"
	vm := viewmodels.NewUserViewModel(userEdit, controller.roles, isAuthenticated, user, make(map[string]string))
	vm.CsrfField = csrf.TemplateField(req)
	return http.StatusOK, controller.newTemplate.Execute(rw, vm)
}
示例#2
0
func (controller *usersController) editGet(rw http.ResponseWriter, req *http.Request) (int, error) {
	vars := mux.Vars(req)
	username := vars["username"]
	// Get the user to edit
	editUser, err := controller.authBackend.User(username)
	if err != nil {
		return http.StatusInternalServerError, err
	}
	isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
	userEdit := new(viewmodels.UsersEditViewModel)
	userEdit.Email = editUser.Email
	userEdit.Role = editUser.Role
	userEdit.Username = editUser.Username
	vm := viewmodels.EditUserViewModel(userEdit, controller.roles, isAuthenticated, user, make(map[string]string))
	vm.CsrfField = csrf.TemplateField(req)
	return http.StatusOK, controller.editTemplate.Execute(rw, vm)
}
func TestValidatePasswordValid(t *testing.T) {
	u := new(viewmodels.UsersEditViewModel)
	u.Email = "*****@*****.**"
	u.Username = ""
	valErrors := validateUserForm(u, true)
	if !strings.Contains(valErrors["Username"], "non zero value required") {
		t.Error("UserName should show error for required.")
	}

	u.Username = "******"
	u.Email = "jack.example.com"
	valErrors = validateUserForm(u, true)
	if !strings.Contains(valErrors["Email"], "does not validate as email") {
		t.Error("Email Address Validation should show error for invalid email.")
	}

	u.Username = "******"
	u.Email = "*****@*****.**"
	u.Password = "******"
	valErrors = validateUserForm(u, false)
	if !strings.Contains(valErrors["Password"], "Password must be at least 6 characters in length") {
		t.Error("Password Validation should show error for short password.")
	}

	u.Password = "******"
	valErrors = validateUserForm(u, false)
	if !strings.Contains(valErrors["Password"], "Password must be at least 6 characters in length") {
		t.Error("Password Validation should show error for short password.")
	}

	u.Username = "******"
	u.Email = "*****@*****.**"
	u.Password = "******"
	u.Password2 = "long enough2"
	valErrors = validateUserForm(u, false)
	if !strings.Contains(valErrors["Password2"], "Repeated Password must be the same as Password.") {
		t.Error("Password Validation should show error for short password.")
	}

	u.Username = "******"
	u.Email = "*****@*****.**"
	u.Password = "******"
	u.Password2 = "long enough"
	valErrors = validateUserForm(u, false)
	if len(valErrors) > 0 {
		t.Error("No errors should be flagged for the set of inputs.")
	}
}