Esempio n. 1
0
func (controller *contactsController) deletePost(rw http.ResponseWriter, req *http.Request) (int, error) {
	err := req.ParseForm()
	if err != nil {
		return http.StatusInternalServerError, err
	}

	decoder := schema.NewDecoder()
	// Ignore unknown keys to prevent errors from the CSRF token.
	decoder.IgnoreUnknownKeys(true)
	formContact := new(viewmodels.ContactsEditViewModel)
	err = decoder.Decode(formContact, req.PostForm)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	valErrors := validateContactForm(formContact)
	if len(valErrors) > 0 {
		isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
		var noSites = []database.Site{}
		vm := viewmodels.EditContactViewModel(formContact, noSites, isAuthenticated,
			user, valErrors)
		vm.CsrfField = csrf.TemplateField(req)
		return http.StatusOK, controller.deleteTemplate.Execute(rw, vm)
	}

	// Get the contact to delete
	contact := new(database.Contact)
	err = contact.GetContact(controller.DB, formContact.ContactID)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	mapContacts(contact, formContact)
	err = contact.DeleteContact(controller.DB)
	if err != nil {
		return http.StatusInternalServerError, err
	}

	// Refresh the pinger with the changes.
	// TODO: Check whether this contact is associated with any active site first.
	err = controller.pinger.UpdateSiteSettings()
	if err != nil {
		return http.StatusInternalServerError, err
	}

	http.Redirect(rw, req, "/settings/contacts", http.StatusSeeOther)
	return http.StatusSeeOther, nil
}
// TestDeleteContacts tests creating a site and two contacts
// in the database and then deleting one of the contacts.
func TestDeleteContact(t *testing.T) {
	db, err := database.InitializeTestDB("")
	if err != nil {
		t.Fatal("Failed to create database:", err)
	}
	defer db.Close()

	// First create a site to associate with the contacts.
	// Note: SiteID is ignored for create but is used in the test comparison
	s := database.Site{SiteID: 1, Name: "Test", IsActive: true, URL: "http://www.google.com", PingIntervalSeconds: 60, TimeoutSeconds: 30, IsSiteUp: true}
	err = s.CreateSite(db)
	if err != nil {
		t.Fatal("Failed to create new site:", err)
	}

	// Create first contact - ContactID is for referencing the contact get test
	c := database.Contact{Name: "Joe Contact", EmailAddress: "*****@*****.**", SmsNumber: "5125551212",
		SmsActive: false, EmailActive: false, ContactID: 1}
	err = c.CreateContact(db)
	if err != nil {
		t.Fatal("Failed to create new contact:", err)
	}
	// Associate to the site ID
	err = s.AddContactToSite(db, c.ContactID)
	if err != nil {
		t.Fatal("Failed to associate contact with site:", err)
	}

	// Create second contact
	c2 := database.Contact{Name: "Jill Contact", EmailAddress: "*****@*****.**", SmsNumber: "5125551213",
		SmsActive: false, EmailActive: false}
	err = c2.CreateContact(db)
	if err != nil {
		t.Fatal("Failed to create new site:", err)
	}
	// Associate the contact to the site
	err = s.AddContactToSite(db, c2.ContactID)
	if err != nil {
		t.Error("Failed to associate contact2 with site:", err)
	}

	err = s.GetSiteContacts(db, s.SiteID)
	if err != nil {
		t.Error("Failed to retrieve site contacts:", err)
	}
	if len(s.Contacts) != 2 {
		t.Error("There should two contacts before deletion.")
	}

	// Delete the second contact
	err = c2.DeleteContact(db)
	if err != nil {
		t.Fatal("Failed to delete contact 2:", err)
	}

	// Verify that it was deleted OK and not associated with the site, and
	// that contact1 is still there.
	err = s.GetSiteContacts(db, s.SiteID)
	if err != nil {
		t.Error("Failed to retrieve site contacts:", err)
	}

	if len(s.Contacts) != 1 {
		t.Error("There should only be one contact for the site after deletion.")
	}
	if !reflect.DeepEqual(c, s.Contacts[0]) {
		t.Error("Remaining contact not equal to input:\n", s.Contacts[0], c)
	}

	// Also verify that the contacts are correct.
	var contacts database.Contacts
	err = contacts.GetContacts(db)
	if err != nil {
		t.Fatal("Failed to get all contacts.", err)
	}

	if len(contacts) != 1 {
		t.Error("There should only be one contact in the DB after deletion.")
	}

	if contacts[0].SiteCount != 1 {
		t.Error("Site count should be 1.")
	}
}