Пример #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)
}
Пример #2
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))
}