Exemplo n.º 1
0
func UpdateContact(rw http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder) string {
	var err error
	var c contact.Contact
	if c.ID, err = strconv.Atoi(params["id"]); err != nil {
		apierror.GenerateError("Trouble getting contact ID", err, rw, req)
	}

	if err = c.Get(); err != nil {
		apierror.GenerateError("Trouble getting contact", err, rw, req)
	}

	contType := req.Header.Get("Content-Type")
	if contType == "application/json" {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Trouble getting JSON request body for updating contact", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &c)
		if err != nil {
			apierror.GenerateError("Trouble unmarshalling JSON request body for updating contact", err, rw, req)
		}
	} else {
		if req.FormValue("first_name") != "" {
			c.FirstName = req.FormValue("first_name")
		}

		if req.FormValue("last_name") != "" {
			c.LastName = req.FormValue("last_name")
		}

		if req.FormValue("email") != "" {
			c.Email = req.FormValue("email")
		}

		if req.FormValue("phone") != "" {
			c.Phone = req.FormValue("phone")
		}

		if req.FormValue("subject") != "" {
			c.Subject = req.FormValue("subject")
		}

		if req.FormValue("message") != "" {
			c.Message = req.FormValue("message")
		}

		if req.FormValue("type") != "" {
			c.Type = req.FormValue("type")
		}

		if req.FormValue("address1") != "" {
			c.Address1 = req.FormValue("address1")
		}

		if req.FormValue("address2") != "" {
			c.Address2 = req.FormValue("address2")
		}

		if req.FormValue("city") != "" {
			c.City = req.FormValue("city")
		}

		if req.FormValue("state") != "" {
			c.State = req.FormValue("state")
		}

		if req.FormValue("postal_code") != "" {
			c.PostalCode = req.FormValue("postal_code")
		}

		if req.FormValue("country") != "" {
			c.Country = req.FormValue("country")
		}
		if req.FormValue("brandID") != "" {
			c.Brand.ID, err = strconv.Atoi(req.FormValue("brandID"))
		}
	}
	if err = c.Update(); err != nil {
		apierror.GenerateError("Trouble updating contact", err, rw, req)
	}
	return encoding.Must(enc.Encode(c))
}
Exemplo n.º 2
0
func TestContact(t *testing.T) {
	var c contact.Contact
	var ct contact.ContactType
	var cr contact.ContactReceiver
	var err error
	Convey("Testing Contact", t, func() {

		//test create contact type using form
		form := url.Values{"name": {"test type"}, "brandId": {"1"}}
		v := form.Encode()
		body := strings.NewReader(v)
		testThatHttp.Request("post", "/contact/types", "", "", AddContactType, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ct)
		So(err, ShouldBeNil)
		So(ct, ShouldHaveSameTypeAs, contact.ContactType{})

		//test create contact receiver using form
		form = url.Values{"first_name": {"test name"}, "last_name": {"test last name"}, "email": {"*****@*****.**"}, "contact_types": {strconv.Itoa(ct.ID)}}
		v = form.Encode()
		body = strings.NewReader(v)
		testThatHttp.Request("post", "/contact/receivers", "", "", AddContactReceiver, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cr)
		So(err, ShouldBeNil)
		So(cr, ShouldHaveSameTypeAs, contact.ContactReceiver{})

		//test create contact using json
		flag.Set("noEmail", "true") //do not send email during tests
		c.LastName = "smith"
		c.FirstName = "fred"
		c.Type = strconv.Itoa(ct.ID)
		c.Email = "*****@*****.**"
		c.Message = "test mes"
		c.Subject = "test sub"
		bodyBytes, _ := json.Marshal(c)
		bodyJson := bytes.NewReader(bodyBytes)
		testThatHttp.Request("post", "/contact/", ":contactTypeID", strconv.Itoa(ct.ID), AddDealerContact, bodyJson, "application/json")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, contact.Contact{})

		//test update contact using form
		form = url.Values{"last_name": {"formLastName"}}
		v = form.Encode()
		body = strings.NewReader(v)
		testThatHttp.Request("put", "/contact/", ":id", strconv.Itoa(c.ID), UpdateContact, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, contact.Contact{})

		//test update contact using json
		c.LastName = "jsonLastName"
		bodyBytes, _ = json.Marshal(c)
		bodyJson = bytes.NewReader(bodyBytes)
		testThatHttp.Request("put", "/contact/", ":id", strconv.Itoa(c.ID), UpdateContact, bodyJson, "application/json")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, contact.Contact{})

		//test update contact type using form
		form = url.Values{"name": {"formName"}, "show": {"true"}}
		v = form.Encode()
		body = strings.NewReader(v)
		testThatHttp.Request("put", "/contact/types/", ":id", strconv.Itoa(ct.ID), UpdateContactType, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ct)
		So(err, ShouldBeNil)
		So(ct, ShouldHaveSameTypeAs, contact.ContactType{})

		//test update contact receiver using form
		form = url.Values{"first_name": {"new test name"}, "last_name": {"new test last name"}, "contact_types": {strconv.Itoa(ct.ID)}}
		v = form.Encode()
		body = strings.NewReader(v)
		testThatHttp.Request("put", "/contact/receivers/", ":id", strconv.Itoa(cr.ID), UpdateContactReceiver, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cr)
		So(err, ShouldBeNil)
		So(cr, ShouldHaveSameTypeAs, contact.ContactReceiver{})

		//test get contact
		testThatHttp.Request("get", "/contact/", ":id", strconv.Itoa(c.ID), GetContact, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)

		//test get all contacts
		form = url.Values{"page": {"1"}, "count": {"1"}}
		v = form.Encode()
		body = strings.NewReader(v)
		testThatHttp.Request("get", "/contact", "", "", GetAllContacts, body, "application/x-www-form-urlencoded")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var cs contact.Contacts
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cs)
		So(err, ShouldBeNil)
		So(cs, ShouldHaveSameTypeAs, contact.Contacts{})
		So(len(cs), ShouldBeGreaterThanOrEqualTo, 0)

		//test get contact type
		testThatHttp.Request("get", "/contact/types/", ":id", strconv.Itoa(ct.ID), GetContactType, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ct)
		So(err, ShouldBeNil)
		So(ct, ShouldHaveSameTypeAs, contact.ContactType{})

		//test get all contact type
		testThatHttp.Request("get", "/contact/types", "", "", GetAllContactTypes, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var cts contact.ContactTypes
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cts)
		So(err, ShouldBeNil)
		So(cts, ShouldHaveSameTypeAs, contact.ContactTypes{})

		//test get receivers by contact type
		testThatHttp.Request("get", "/contact/types/receivers/", ":id", strconv.Itoa(ct.ID), GetReceiversByContactType, nil, "")
		var crs contact.ContactReceivers
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &crs)
		So(err, ShouldBeNil)
		So(crs, ShouldHaveSameTypeAs, contact.ContactReceivers{})
		So(len(crs), ShouldBeGreaterThan, 0)

		//test get contact receiver
		testThatHttp.Request("get", "/contact/receiver/", ":id", strconv.Itoa(cr.ID), GetContactReceiver, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cr)
		So(err, ShouldBeNil)

		//test get all contact receiver
		testThatHttp.Request("get", "/contact/receiver", "", "", GetAllContactReceivers, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &crs)
		So(err, ShouldBeNil)
		So(crs, ShouldHaveSameTypeAs, contact.ContactReceivers{})
		So(len(crs), ShouldBeGreaterThan, 0)

		//test delete contact
		testThatHttp.Request("delete", "/contact/", ":id", strconv.Itoa(c.ID), DeleteContact, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)

		//test delete contact type
		testThatHttp.Request("delete", "/contact/types/", ":id", strconv.Itoa(ct.ID), DeleteContactType, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ct)
		So(err, ShouldBeNil)

		//test delete contact receiver
		testThatHttp.Request("delete", "/contact/receiver/", ":id", strconv.Itoa(cr.ID), DeleteContactReceiver, nil, "")
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cr)
		So(err, ShouldBeNil)

	})
}