Exemplo n.º 1
0
func TestTechSupport(te *testing.T) {

	dtx, err := apicontextmock.Mock()
	if err != nil {
		te.Log(err)
	}

	Convey("TechSupport", te, func() {
		var t techSupport.TechSupport
		var ts []techSupport.TechSupport

		qs := make(url.Values, 0)
		qs.Add("key", dtx.APIKey)

		t.Contact.LastName = "test contact"
		t.Contact.FirstName = "test contact first"
		t.Contact.Email = "*****@*****.**"
		t.Contact.Type = "test"
		t.Contact.Subject = "test"
		t.Contact.Message = "test"
		t.Contact.Brand.ID = dtx.BrandID
		t.BrandID = dtx.BrandID

		err = t.Contact.Add(dtx)
		So(err, ShouldBeNil)

		response := httprunner.ParameterizedJsonRequest("POST", "/techSupport/:contactReceiverTypeID/:sendEmail", "/techSupport/1/false", &qs, t, CreateTechSupport)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &t), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/techSupport/:id", "/techSupport/"+strconv.Itoa(t.ID), &qs, nil, GetTechSupport)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &t), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/techSupport/contact/:id", "/techSupport/contact/"+strconv.Itoa(t.Contact.ID), &qs, nil, GetTechSupportByContact)
		te.Log(response.Body)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &ts), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("GET", "/techSupport", "/techSupport", &qs, nil, GetAllTechSupport)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &ts), ShouldEqual, nil)

		response = httprunner.ParameterizedRequest("DELETE", "/techSupport/:id", "/techSupport/"+strconv.Itoa(t.ID), &qs, nil, DeleteTechSupport)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &t), ShouldEqual, nil)

		err = t.Contact.Delete()
		So(err, ShouldBeNil)

	})
	_ = apicontextmock.DeMock(dtx)
}
Exemplo n.º 2
0
func DeleteTechSupport(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var t techSupport.TechSupport
	t.ID, err = strconv.Atoi(params["id"])
	if err != nil {
		apierror.GenerateError("Error parsing Id.", err, rw, req)
	}
	err = t.Delete()
	if err != nil {
		apierror.GenerateError("Error deleting Tech Support.", err, rw, req)
	}
	//Return JSON
	return encoding.Must(enc.Encode(t))
}
Exemplo n.º 3
0
func GetTechSupportByContact(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var t techSupport.TechSupport
	id := params["id"]
	t.Contact.ID, err = strconv.Atoi(id)
	if err != nil {
		apierror.GenerateError("Error parsing contact Id.", err, rw, req)
	}

	ts, err := t.GetByContact(dtx)
	if err != nil {
		apierror.GenerateError("Error getting Tech Support by Contact Id.", err, rw, req)
	}
	return encoding.Must(enc.Encode(ts))
}
Exemplo n.º 4
0
func GetTechSupport(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var t techSupport.TechSupport
	id := params["id"]
	t.ID, err = strconv.Atoi(id)
	if err != nil {
		apierror.GenerateError("Error parsing Id.", err, rw, req)
	}

	err = t.Get()
	if err != nil {
		apierror.GenerateError("Error getting Tech Support.", err, rw, req)
	}
	return encoding.Must(enc.Encode(t))
}
Exemplo n.º 5
0
func CreateTechSupport(rw http.ResponseWriter, req *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	contType := req.Header.Get("Content-Type")

	var t techSupport.TechSupport
	var err error
	contactTypeID, err := strconv.Atoi(params["contactReceiverTypeID"]) //to whom the emails go
	sendEmail, err := strconv.ParseBool(params["sendEmail"])

	// if contType == "application/json" {
	if strings.Contains(contType, "application/json") {
		//json
		requestBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			apierror.GenerateError("Error parsing JSON.", err, rw, req)
		}

		err = json.Unmarshal(requestBody, &t)
		if err != nil {
			apierror.GenerateError("Error unmarshalling request body.", err, rw, req)
		}
	} else {
		//else, form
		t.VehicleMake = req.FormValue("vehicle_make")
		t.VehicleModel = req.FormValue("vehicle_model")
		t.VehicleYear, err = strconv.Atoi(req.FormValue("vehicle_year"))
		d, err := time.Parse(timeFormat, req.FormValue("purchase_date"))
		t.PurchaseDate = d
		t.PurchasedFrom = req.FormValue("purchased_from")
		t.DealerName = req.FormValue("dealer_name")
		t.ProductCode = req.FormValue("product_code")
		t.DateCode = req.FormValue("date_code")
		t.Issue = req.FormValue("issue")

		t.Contact.FirstName = req.FormValue("first_name")
		t.Contact.LastName = req.FormValue("last_name")
		t.Contact.Email = req.FormValue("email")
		t.Contact.Phone = req.FormValue("phone")
		t.Contact.Subject = req.FormValue("subject")
		t.Contact.Message = req.FormValue("message")
		t.Contact.Type = req.FormValue("type")
		t.Contact.Address1 = req.FormValue("address1")
		t.Contact.Address2 = req.FormValue("address2")
		t.Contact.City = req.FormValue("city")
		t.Contact.State = req.FormValue("state")
		t.Contact.PostalCode = req.FormValue("postal_code")
		t.Contact.Country = req.FormValue("country")
		if err != nil {
			apierror.GenerateError("Error parsing purchase date.", err, rw, req)
		}
	}
	t.BrandID = dtx.BrandID
	err = t.Create()
	if err != nil {
		apierror.GenerateError("Error creating Tech Support.", err, rw, req)
	}

	if sendEmail == true {
		//Send Email
		body :=
			"Name: " + t.Contact.FirstName + " " + t.Contact.LastName + "\n" +
				"Email: " + t.Contact.Email + "\n" +
				"Phone: " + t.Contact.Phone + "\n" +
				"Make: " + t.VehicleMake + "\n" +
				"Model: " + t.VehicleModel + "\n" +
				"Year: " + strconv.Itoa(t.VehicleYear) + "\n" +
				"Purchase Date: " + t.PurchaseDate.String() + "\n" +
				"Purchased From: " + t.PurchasedFrom + "\n" +
				"Dealer Name: " + t.DealerName + "\n" +
				"Product Code: " + t.ProductCode + "\n" +
				"Date Code: " + t.DateCode + "\n\n" +
				"Issue: " + t.Issue + "\n"

		var ct contact.ContactType
		ct.ID = contactTypeID
		subject := "Email from Tech Support Request Form"
		err = contact.SendEmail(ct, subject, body) //contact type id, subject, techSupport
		if err != nil {
			apierror.GenerateError("Error sending email to Tech Support.", err, rw, req)
		}
	}
	//Return JSON
	return encoding.Must(enc.Encode(t))
}