func (controller *contactsController) editGet(rw http.ResponseWriter, req *http.Request) (int, error) { vars := mux.Vars(req) contactID, err := strconv.ParseInt(vars["contactID"], 10, 64) if err != nil { return http.StatusInternalServerError, err } // Get the contact to edit contact := new(database.Contact) err = contact.GetContact(controller.DB, contactID) if err != nil { return http.StatusInternalServerError, err } isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) contactEdit := new(viewmodels.ContactsEditViewModel) contactEdit.Name = contact.Name contactEdit.ContactID = contact.ContactID contactEdit.EmailAddress = contact.EmailAddress contactEdit.SmsNumber = contact.SmsNumber contactEdit.EmailActive = contact.EmailActive contactEdit.SmsActive = contact.SmsActive contactEdit.SelectedSites, err = getContactSiteIDs(controller, contact) if err != nil { return http.StatusInternalServerError, err } sites, errGet := getAllSites(controller) if errGet != nil { return http.StatusInternalServerError, errGet } vm := viewmodels.EditContactViewModel(contactEdit, sites, isAuthenticated, user, make(map[string]string)) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.editTemplate.Execute(rw, vm) }
func (controller *contactsController) newGet(rw http.ResponseWriter, req *http.Request) (int, error) { isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) contactEdit := new(viewmodels.ContactsEditViewModel) contactEdit.EmailActive = false contactEdit.SmsActive = false sites, err := getAllSites(controller) if err != nil { return http.StatusInternalServerError, err } vm := viewmodels.NewContactViewModel(contactEdit, sites, true, isAuthenticated, user, make(map[string]string)) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.newTemplate.Execute(rw, vm) }
func TestValidateContactValid(t *testing.T) { c := new(viewmodels.ContactsEditViewModel) c.Name = "" c.SmsActive = false c.EmailActive = false c.SmsNumber = "" c.EmailAddress = "" valErrors := validateContactForm(c) if !strings.Contains(valErrors["Name"], "non zero value required") { t.Error("Name should show error for required.") } c.Name = "Jane Doe" c.SmsActive = true c.EmailActive = false c.SmsNumber = "" valErrors = validateContactForm(c) if !strings.Contains(valErrors["SmsNumber"], "Text Message Number must be provided") { t.Error("Text Message Validation should show error for required if active.") } c.SmsActive = false c.EmailActive = true c.EmailAddress = "" valErrors = validateContactForm(c) if !strings.Contains(valErrors["EmailAddress"], "Email Address must be provided") { t.Error("Email Address Validation should show error for required if active.") } c.SmsActive = false c.EmailActive = true c.EmailAddress = "jack.example.com" valErrors = validateContactForm(c) if !strings.Contains(valErrors["EmailAddress"], "does not validate as email") { t.Error("Email Address Validation should show error for invalid email.") } c.SmsActive = true c.EmailActive = false c.SmsNumber = "Foobar" valErrors = validateContactForm(c) if !strings.Contains(valErrors["SmsNumber"], "The Text Message Number must be provided in E.164 format") { t.Error("Text Message Validation should show error for incorrect number.") } c.SmsNumber = "1234567890123456" valErrors = validateContactForm(c) if !strings.Contains(valErrors["SmsNumber"], "The Text Message Number must be provided in E.164 format") { t.Error("Text Message Validation should show error for incorrect number.") } c.SmsNumber = "+15127712936" c.Name = "Jane Doe" c.SmsActive = true c.EmailActive = true c.EmailAddress = "*****@*****.**" valErrors = validateContactForm(c) if len(valErrors) > 0 { t.Error("No errors should be flagged for the set of inputs.") } }