Exemple #1
0
func GetCustomer(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var c customer.Customer

	if err = c.GetCustomerIdFromKey(dtx.APIKey); err != nil {
		apierror.GenerateError("Trouble getting customer ID", err, rw, r)
		return ""
	}

	if err = c.GetCustomer(dtx.APIKey); err != nil {
		apierror.GenerateError("Trouble getting customer", err, rw, r, http.StatusServiceUnavailable)
		return ""
	}

	lowerKey := strings.ToLower(dtx.APIKey)
	for i, u := range c.Users {
		for _, k := range u.Keys {
			if strings.ToLower(k.Key) == lowerKey {
				c.Users[i].Current = true
			}
		}
	}

	return encoding.Must(enc.Encode(c))
}
Exemple #2
0
func approveuser(r *http.Request) bool {
	api := r.URL.Query().Get("key")
	if api == "" {
		return false
	}
	c := customer.Customer{}
	err := c.GetCustomerIdFromKey(api)
	if err != nil || c.Id == 0 {
		return false
	}
	return true
}
func GetByPrivateKey(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var err error
	var webProperties webProperty_model.WebProperties

	privateKey := r.FormValue("key")
	cust := customer.Customer{}

	if err = cust.GetCustomerIdFromKey(privateKey); err != nil {
		apierror.GenerateError("Trouble getting customer", err, rw, r)
		return ""
	}

	if webProperties, err = webProperty_model.GetByCustomer(cust.Id, dtx); err != nil {
		apierror.GenerateError("Trouble getting web property", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(webProperties))
}
Exemple #4
0
func DeleteCustomer(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var c customer.Customer
	var err error

	id := r.FormValue("id")
	if id == "" {
		id = params["id"]
	}

	if c.Id, err = strconv.Atoi(id); err != nil {
		apierror.GenerateError("Trouble getting customer ID", err, rw, r)
		return ""
	}

	if err = c.Delete(); err != nil {
		apierror.GenerateError("Trouble deleting customer", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Exemple #5
0
func GetPriceByCustomer(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var ps customer.CustomerPrices
	var c customer.Customer

	id := r.FormValue("id")
	if id == "" {
		id = params["id"]
	}

	if c.Id, err = strconv.Atoi(id); err != nil {
		apierror.GenerateError("Trouble getting customer ID", err, rw, r)
		return ""
	}

	if ps, err = c.GetPricesByCustomer(); err != nil {
		apierror.GenerateError("Trouble getting prices by customer ID", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(ps))
}
Exemple #6
0
func GetSales(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params) string {
	var err error
	var ps customer.Prices
	var c customer.Customer

	id := r.FormValue("id")
	if id == "" {
		id = params["id"]
	}

	if c.Id, err = strconv.Atoi(id); err != nil {
		apierror.GenerateError("Trouble getting customer ID", err, rw, r)
		return ""
	}

	start := r.FormValue("start")
	end := r.FormValue("end")

	startDate, err := time.Parse(inputTimeFormat, start)
	if err != nil {
		apierror.GenerateError("Trouble getting sales start date", err, rw, r)
		return ""
	}

	endDate, err := time.Parse(inputTimeFormat, end)
	if err != nil {
		apierror.GenerateError("Trouble getting sales end date", err, rw, r)
		return ""
	}

	ps, err = c.GetPricesBySaleRange(startDate, endDate)
	if err != nil {
		apierror.GenerateError("Trouble getting prices by sales range", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(ps))
}
Exemple #7
0
func SaveCustomer(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var c customer.Customer
	var err error

	if r.FormValue("id") != "" || params["id"] != "" {
		id := r.FormValue("id")
		if id == "" {
			id = params["id"]
		}

		if c.Id, err = strconv.Atoi(id); err != nil {
			apierror.GenerateError("Trouble getting customer ID", err, rw, r)
			return ""
		}

		if err = c.Basics(dtx.APIKey); err != nil {
			apierror.GenerateError("Trouble getting customer", err, rw, r)
			return ""
		}
	}

	//json
	var requestBody []byte
	if requestBody, err = ioutil.ReadAll(r.Body); err != nil {
		apierror.GenerateError("Trouble reading request body while saving customer", err, rw, r)
		return ""
	}

	if err = json.Unmarshal(requestBody, &c); err != nil {
		apierror.GenerateError("Trouble unmarshalling json request body while saving customer", err, rw, r)
		return ""
	}

	//create or update
	if c.Id > 0 {
		err = c.Update()
	} else {
		err = c.Create()
	}

	if err != nil {
		msg := "Trouble creating customer"
		if c.Id > 0 {
			msg = "Trouble updating customer"
		}
		apierror.GenerateError(msg, err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Exemple #8
0
func TestCustomerPrice(t *testing.T) {
	var err error
	var p customer.Price
	var ps customer.Prices
	var c customer.Customer
	c.Name = "Dog Bountyhunter"
	c.Create()

	Convey("Testing customer/Price", t, func() {
		//test create customer price
		form := url.Values{"custID": {strconv.Itoa(c.Id)}, "partID": {"11000"}, "price": {"123456"}}
		v := form.Encode()
		body := strings.NewReader(v)
		thyme := time.Now()
		testThatHttp.Request("post", "/customer/prices", "", "", CreateUpdatePrice, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
		So(err, ShouldBeNil)
		So(p, ShouldHaveSameTypeAs, customer.Price{})
		So(p.ID, ShouldBeGreaterThan, 0)

		//test update customer price
		form = url.Values{"isSale": {"true"}, "saleStart": {"01/01/2001"}, "saleEnd": {"01/01/2015"}}
		v = form.Encode()
		body = strings.NewReader(v)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/prices/", ":id", strconv.Itoa(p.ID), CreateUpdatePrice, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
		So(err, ShouldBeNil)
		So(p, ShouldHaveSameTypeAs, customer.Price{})
		So(p.IsSale, ShouldEqual, 1)
		start, _ := time.Parse(inputTimeFormat, "01/01/2001")
		So(p.SaleStart, ShouldResemble, start)

		//test get customer price
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/prices/", ":id", strconv.Itoa(p.ID), GetPrice, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
		So(err, ShouldBeNil)
		So(p, ShouldHaveSameTypeAs, customer.Price{})

		//test get all customer price
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/prices", "", "", GetAllPrices, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*8) //Long
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ps)
		So(err, ShouldBeNil)
		So(ps, ShouldHaveSameTypeAs, customer.Prices{})

		//test get customer price by part
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/prices/part/", ":id", strconv.Itoa(p.ID), GetPricesByPart, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ps)
		So(err, ShouldBeNil)
		So(ps, ShouldHaveSameTypeAs, customer.Prices{})

		//test get customer price by customer
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/pricesByCustomer/", ":id", strconv.Itoa(c.Id), GetPriceByCustomer, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
		So(err, ShouldBeNil)
		So(p, ShouldHaveSameTypeAs, customer.Price{})

		//test get sales
		form = url.Values{"id": {strconv.Itoa(c.Id)}, "start": {"01/01/2000"}, "end": {"01/01/2016"}}
		v = form.Encode()
		body = strings.NewReader(v)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/prices/sale", "", "", GetSales, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &ps)
		So(err, ShouldBeNil)
		So(ps, ShouldHaveSameTypeAs, customer.Prices{})

		//test delete customer price
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/prices/", ":id", strconv.Itoa(p.ID), DeletePrice, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &p)
		So(err, ShouldBeNil)
		So(p, ShouldHaveSameTypeAs, customer.Price{})
	})
	//teardown
	c.Delete()
}
Exemple #9
0
func BenchmarkCRUDCustomerPrice(b *testing.B) {
	var p customer.Price
	var c customer.Customer
	c.Name = "Axl Rose"
	c.Create()
	qs := make(url.Values, 0)

	Convey("CustomerPrice", b, func() {
		form := url.Values{"custID": {strconv.Itoa(c.Id)}, "partID": {"11000"}, "price": {"123456"}}
		//create
		(&httprunner.BenchmarkOptions{
			Method:             "POST",
			Route:              "/customer/prices",
			ParameterizedRoute: "/customer/prices",
			Handler:            CreateUpdatePrice,
			QueryString:        &qs,
			JsonBody:           p,
			FormBody:           form,
			Runs:               b.N,
		}).RequestBenchmark()

		//get
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer/prices",
			ParameterizedRoute: "/customer/prices/" + strconv.Itoa(p.ID),
			Handler:            GetPrice,
			QueryString:        &qs,
			JsonBody:           p,
			FormBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//get all
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer/prices",
			ParameterizedRoute: "/customer/prices",
			Handler:            GetAllPrices,
			QueryString:        &qs,
			JsonBody:           p,
			FormBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//get by part
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer/prices/part",
			ParameterizedRoute: "/customer/prices/part/" + strconv.Itoa(p.ID),
			Handler:            GetPricesByPart,
			QueryString:        &qs,
			JsonBody:           p,
			FormBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//get by
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer/pricesByCustomer",
			ParameterizedRoute: "/customer/pricesByCustomer/" + strconv.Itoa(c.Id),
			Handler:            GetPriceByCustomer,
			QueryString:        &qs,
			JsonBody:           p,
			FormBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//delete
		(&httprunner.BenchmarkOptions{
			Method:             "DELETE",
			Route:              "/customer/prices",
			ParameterizedRoute: "/customer/prices/" + strconv.Itoa(p.ID),
			Handler:            DeleteLocation,
			QueryString:        &qs,
			JsonBody:           p,
			FormBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()
	})
	//teardown
	c.Delete()
}
Exemple #10
0
func TestCustomer(t *testing.T) {
	var err error
	var c customer.Customer
	var cu customer.CustomerUser
	once.Do(initDtx)
	defer apicontextmock.DeMock(dtx)

	cu.Id = dtx.UserID
	err = cu.Get(dtx.APIKey)
	if err != nil {
		t.Log(err)
	}
	c.Users = append(c.Users, cu)

	Convey("Testing customer/Customer", t, func() {
		//test create customer
		c.Name = "Jason Voorhees"
		c.Email = "*****@*****.**"
		bodyBytes, _ := json.Marshal(c)
		bodyJson := bytes.NewReader(bodyBytes)
		thyme := time.Now()
		testThatHttp.Request("put", "/customer", "", "?key=", SaveCustomer, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

		//test update customer
		c.Fax = "666-1313"
		c.State.Id = 1
		bodyBytes, _ = json.Marshal(c)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("put", "/customer/", ":id", strconv.Itoa(c.Id)+"?key=", SaveCustomer, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

		thyme = time.Now()
		testThatHttp.RequestWithDtx("post", "/customer", "", "?key=", GetCustomer, nil, "", dtx)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

		// get customer locations
		thyme = time.Now()
		testThatHttp.RequestWithDtx("get", "/customer/locations", "", "?key=", GetLocations, nil, "", dtx)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*6)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c.Locations)
		So(err, ShouldBeNil)
		So(c.Locations, ShouldHaveSameTypeAs, []customer.CustomerLocation{})

		// //get user
		thyme = time.Now()
		testThatHttp.RequestWithDtx("post", "/customer/user", "", "?key="+dtx.APIKey, GetUser, nil, "", nil)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
		So(err, ShouldBeNil)
		So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})

		//get users
		thyme = time.Now()
		testThatHttp.RequestWithDtx("get", "/customer/users", "", "?key=", GetUsers, nil, "", dtx)
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*5)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var cus []customer.CustomerUser
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cus)
		So(err, ShouldBeNil)
		So(cus, ShouldHaveSameTypeAs, []customer.CustomerUser{})

		// get customer price
		// price.CustID = c.Id
		// price.Create()
		// thyme = time.Now()
		// testThatHttp.Request("get", "/new/customer/price/", ":id", strconv.Itoa(p.ID)+"?key="+apiKey, GetCustomerPrice, nil, "")
		// So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		// So(testThatHttp.Response.Code, ShouldEqual, 200)
		// var price float64
		// err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &price)
		// So(err, ShouldBeNil)
		// So(price, ShouldHaveSameTypeAs, 7.1)

		// //get customer cart reference
		// ci.CustID = c.Id
		// ci.Create()
		// thyme = time.Now()
		// testThatHttp.Request("get", "/new/customer/cartRef/", ":id", strconv.Itoa(p.ID)+"?key="+apiKey, GetCustomerCartReference, nil, "")
		// So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		// So(testThatHttp.Response.Code, ShouldEqual, 200)
		// var reference int
		// err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &reference)
		// So(err, ShouldBeNil)
		// So(reference, ShouldHaveSameTypeAs, 7)

		//test delete customer
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/", ":id", strconv.Itoa(c.Id)+"?key=", DeleteCustomer, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})
		So(c.Id, ShouldBeGreaterThan, 0)

	})
	//cleanup
	err = cu.Delete()
	err = c.Delete()

}
Exemple #11
0
func TestCustomerContent(t *testing.T) {
	flag.Parse()
	//customer - for db setup only
	var c customer.Customer
	var content custcontent.CustomerContent
	var partContent custcontent.PartContent
	var categoryContent custcontent.CustomerContent
	var ct custcontent.ContentType
	var crs custcontent.CustomerContentRevisions
	var contents []custcontent.CustomerContent
	var catContent custcontent.CategoryContent
	var catContents []custcontent.CategoryContent
	var err error
	var apiKey string

	catContent.CategoryId = 1

	ct.Type = "test"
	ct.Create()

	c.Name = "test cust"
	c.Create()

	Convey("Testing customer/Customer_content", t, func() {
		//test create part content
		content.Text = "new content"
		content.ContentType.Id = ct.Id
		bodyBytes, _ := json.Marshal(content)
		bodyJson := bytes.NewReader(bodyBytes)
		thyme := time.Now()
		testThatHttp.Request("post", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+dtx.APIKey, CreatePartContent, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		if testThatHttp.Response.Code == 200 { //returns 500 when ninnemana user doesn't exist
			So(testThatHttp.Response.Code, ShouldEqual, 200)
			err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &content)
			So(err, ShouldBeNil)
			So(content, ShouldHaveSameTypeAs, custcontent.CustomerContent{})
		}

		//create category content
		categoryContent.Text = "new content"
		categoryContent.ContentType.Id = ct.Id
		bodyBytes, _ = json.Marshal(categoryContent)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/cms/category/", ":id", strconv.Itoa(catContent.CategoryId)+"?key="+apiKey, CreateCategoryContent, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &categoryContent)
		So(err, ShouldBeNil)
		So(categoryContent, ShouldHaveSameTypeAs, custcontent.CustomerContent{})

		//test update part content
		content.Text = "newerer content"
		bodyBytes, _ = json.Marshal(content)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("put", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+apiKey, UpdatePartContent, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &content)
		So(err, ShouldBeNil)
		So(content, ShouldHaveSameTypeAs, custcontent.CustomerContent{})

		//test update category content
		categoryContent.Text = "newerer content"
		bodyBytes, _ = json.Marshal(categoryContent)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("put", "/customer/cms/part/", ":id", strconv.Itoa(catContent.CategoryId)+"?key="+apiKey, UpdateCategoryContent, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &categoryContent)
		So(err, ShouldBeNil)
		So(categoryContent, ShouldHaveSameTypeAs, custcontent.CustomerContent{})

		//test get part content (unique)
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+apiKey, UniquePartContent, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
		So(err, ShouldBeNil)
		So(contents, ShouldHaveSameTypeAs, []custcontent.CustomerContent{})

		//test get all part content
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/part", "", "?key="+apiKey, AllPartContent, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
		So(err, ShouldBeNil)
		So(contents, ShouldHaveSameTypeAs, []custcontent.CustomerContent{})

		//test get category content (all content)
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/part", "", "?key="+apiKey, AllCategoryContent, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
		So(err, ShouldBeNil)
		So(contents, ShouldHaveSameTypeAs, []custcontent.CustomerContent{})

		//test get unique category content
		catContent.Content = append(catContent.Content, content) //setup some category Content
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/category/", ":id", strconv.Itoa(catContent.CategoryId)+"?key="+apiKey, UniqueCategoryContent, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &catContents)
		So(err, ShouldBeNil)
		So(catContents, ShouldHaveSameTypeAs, []custcontent.CategoryContent{})

		//test get all content
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms", "", "?key="+apiKey, GetAllContent, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &contents)
		So(err, ShouldBeNil)
		So(contents, ShouldHaveSameTypeAs, []custcontent.CustomerContent{})

		//test get content by id
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/", ":id", strconv.Itoa(content.Id)+"?key="+apiKey, GetContentById, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &content)
		So(err, ShouldBeNil)
		So(content, ShouldHaveSameTypeAs, custcontent.CustomerContent{})

		//test get content revisions by id
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/", ":id/revisions", strconv.Itoa(content.Id)+"/revisions?key="+apiKey, GetContentRevisionsById, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &crs)
		So(err, ShouldBeNil)
		So(crs, ShouldHaveSameTypeAs, custcontent.CustomerContentRevisions{})

		//test get all content types
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/cms/content_types", "", "?key="+apiKey, GetAllContentTypes, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var cts []custcontent.ContentType
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cts)
		So(err, ShouldBeNil)
		So(cts, ShouldHaveSameTypeAs, []custcontent.ContentType{})
		So(len(cts), ShouldBeGreaterThan, 0)

		//test delete part content
		bodyBytes, _ = json.Marshal(content)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/cms/part/", ":id", strconv.Itoa(11000)+"?key="+apiKey, DeletePartContent, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &partContent)
		So(err, ShouldBeNil)
		So(partContent, ShouldHaveSameTypeAs, custcontent.PartContent{})

		//test delete category content
		bodyBytes, _ = json.Marshal(categoryContent)
		bodyJson = bytes.NewReader(bodyBytes)
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/cms/category/", ":id", strconv.Itoa(catContent.CategoryId)+"?key="+apiKey, DeleteCategoryContent, bodyJson, "application/json")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &content)
		So(err, ShouldBeNil)
		So(content, ShouldHaveSameTypeAs, custcontent.CustomerContent{})

	})
	//teardown
	err = content.DeleteById()
	err = categoryContent.DeleteById()

	for _, con := range catContent.Content {
		err = con.DeleteById()
	}
	err = c.Delete()

	err = ct.Delete()

}
Exemple #12
0
func BenchmarkCRUDCustomer(b *testing.B) {

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

	Convey("Customer", b, func() {
		var c customer.Customer
		c.Name = "Freddy Krueger"
		c.Email = "*****@*****.**"
		// var locs customer.CustomerLocations

		//create
		(&httprunner.BenchmarkOptions{
			Method:             "POST",
			Route:              "/customer",
			ParameterizedRoute: "/customer",
			Handler:            SaveCustomer,
			QueryString:        &qs,
			JsonBody:           c,
			Runs:               b.N,
		}).RequestBenchmark()

		//get
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer",
			ParameterizedRoute: "/customer/" + strconv.Itoa(c.Id),
			Handler:            GetCustomer,
			QueryString:        &qs,
			JsonBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//get locations
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer",
			ParameterizedRoute: "/customer",
			Handler:            GetLocations,
			QueryString:        &qs,
			JsonBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//get user
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer/user",
			ParameterizedRoute: "/customer/user",
			Handler:            GetUser,
			QueryString:        &qs,
			JsonBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//get users
		(&httprunner.BenchmarkOptions{
			Method:             "GET",
			Route:              "/customer/users",
			ParameterizedRoute: "/customer/users",
			Handler:            GetUsers,
			QueryString:        &qs,
			JsonBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

		//delete
		(&httprunner.BenchmarkOptions{
			Method:             "DELETE",
			Route:              "/customer",
			ParameterizedRoute: "/customer/" + strconv.Itoa(c.Id),
			Handler:            DeleteCustomer,
			QueryString:        &qs,
			JsonBody:           nil,
			Runs:               b.N,
		}).RequestBenchmark()

	})

}
func CreateUpdateWebProperty(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder, params martini.Params, dtx *apicontext.DataContext) string {
	var w webProperty_model.WebProperty
	var err error

	cust := customer.Customer{}

	if err = cust.GetCustomerIdFromKey(dtx.APIKey); err != nil {
		apierror.GenerateError("Trouble getting customer ID from API Key", err, rw, r)
		return ""
	}

	w.CustID = cust.Id

	//determine content type
	contType := r.Header.Get("Content-Type")
	if contType == "application/json" {
		//json
		requestBody, err := ioutil.ReadAll(r.Body)
		if err != nil {
			apierror.GenerateError("Trouble reading request body while saving web property", err, rw, r)
			return ""
		}

		if err = json.Unmarshal(requestBody, &w); err != nil {
			apierror.GenerateError("Trouble unmarshalling request body while saving web property", err, rw, r)
			return ""
		}
	} else {
		if r.FormValue("id") != "" || params["id"] != "" {
			idStr := r.FormValue("id")
			if idStr == "" {
				idStr = params["id"]
			}

			if w.ID, err = strconv.Atoi(idStr); err != nil {
				apierror.GenerateError("Trouble getting web property ID", err, rw, r)
				return ""
			}

			if err = w.Get(dtx); err != nil {
				apierror.GenerateError("Trouble getting web property", err, rw, r)
				return ""
			}
		}

		name := r.FormValue("name")
		url := r.FormValue("url")
		isEnabled := r.FormValue("isEnabled")
		sellerID := r.FormValue("sellerID")
		webPropertyTypeID := r.FormValue("webPropertyTypeID")
		isFinalApproved := r.FormValue("isApproved")
		enabledDate := r.FormValue("enabledDate")
		isDenied := r.FormValue("isDenied")
		requestedDate := r.FormValue("requestedDate")
		typeID := r.FormValue("typeID")

		if name != "" {
			w.Name = name
		}
		if url != "" {
			w.Url = url
		}
		if isEnabled != "" {
			if w.IsEnabled, err = strconv.ParseBool(isEnabled); err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.isEnabled", err, rw, r)
				return ""
			}
		}
		if sellerID != "" {
			w.SellerID = sellerID
		}
		if webPropertyTypeID != "" {
			if w.WebPropertyType.ID, err = strconv.Atoi(webPropertyTypeID); err != nil {
				apierror.GenerateError("Trouble getting web property type ID", err, rw, r)
				return ""
			}
		}
		if isFinalApproved != "" {
			if w.IsFinalApproved, err = strconv.ParseBool(isFinalApproved); err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.isApproved", err, rw, r)
				return ""
			}
		}
		if enabledDate != "" {
			en, err := time.Parse(timeFormat, enabledDate)
			if err != nil {
				apierror.GenerateError("Trouble parsing date for webproperty.enabledDate", err, rw, r)
				return ""
			}
			w.IsEnabledDate = &en
		}
		if isDenied != "" {
			if w.IsDenied, err = strconv.ParseBool(isDenied); err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.isDenied", err, rw, r)
				return ""
			}
		}
		if requestedDate != "" {
			req, err := time.Parse(timeFormat, requestedDate)
			if err != nil {
				apierror.GenerateError("Trouble parsing boolean value for webproperty.requestedDate", err, rw, r)
				return ""
			}
			w.RequestedDate = &req
		}
		if typeID != "" {
			if w.WebPropertyType.ID, err = strconv.Atoi(typeID); err != nil {
				apierror.GenerateError("Trouble getting type ID", err, rw, r)
				return ""
			}
		}
	}

	if w.ID > 0 {
		err = w.Update(dtx)
	} else {
		//notes (text) and property requirements (reqID) can be created when web property is created
		notes := r.Form["note"]
		for _, v := range notes {
			var n webProperty_model.WebPropertyNote
			n.Text = v
			w.WebPropertyNotes = append(w.WebPropertyNotes, n)
		}
		reqIDs := r.Form["requirement"]
		for _, v := range reqIDs {
			var n webProperty_model.WebPropertyRequirement
			n.RequirementID, err = strconv.Atoi(v)
			w.WebPropertyRequirements = append(w.WebPropertyRequirements, n)
		}
		err = w.Create(dtx)
	}

	if err != nil {
		msg := "Trouble creating web property"
		if w.ID > 0 {
			msg = "Trouble updating web property"
		}
		apierror.GenerateError(msg, err, rw, r)
		return ""
	}
	return encoding.Must(enc.Encode(w))
}
Exemple #14
0
func TestCustomerUser(t *testing.T) {

	var err error
	var cu customer.CustomerUser
	var c customer.Customer
	c.Name = "Dog Bountyhunter"
	c.BrandIDs = append(c.BrandIDs, 1)
	c.Create()

	var pub, pri, auth apiKeyType.ApiKeyType
	if database.GetCleanDBFlag() != "" {
		t.Log(database.GetCleanDBFlag())
		//setup apiKeyTypes
		pub.Type = "Public"
		pri.Type = "Private"
		auth.Type = "Authentication"
		pub.Create()
		pri.Create()
		auth.Create()
	}
	Convey("Testing customer/User", t, func() {
		//test create customer user
		form := url.Values{"name": {"Mitt Romney"}, "email": {"*****@*****.**"}, "pass": {"robthepoor"}, "customerID": {strconv.Itoa(c.Id)}, "isActive": {"true"}, "locationID": {"1"}, "isSudo": {"true"}, "cust_ID": {strconv.Itoa(c.Id)}}
		v := form.Encode()
		body := strings.NewReader(v)
		thyme := time.Now()
		testThatHttp.Request("post", "/customer/user/register", "", "", RegisterUser, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
		So(err, ShouldBeNil)
		So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})
		So(cu.Id, ShouldNotBeEmpty)
		//key stuff - get apiKey
		var apiKey string
		for _, k := range cu.Keys {
			if strings.ToLower(k.Type) == "public" {
				apiKey = k.Key
			}
		}

		//test update customer user
		form = url.Values{"name": {"Michelle Bachman"}}
		v = form.Encode()
		body = strings.NewReader(v)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/user/", ":id", cu.Id, UpdateCustomerUser, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
		So(err, ShouldBeNil)
		So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})
		So(cu.Name, ShouldNotEqual, "Mitt Romney")

		//test authenticateUser
		err = c.JoinUser(cu)
		So(err, ShouldBeNil)
		form = url.Values{"email": {"*****@*****.**"}, "password": {"robthepoor"}}
		v = form.Encode()
		body = strings.NewReader(v)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/auth", "", "", AuthenticateUser, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})

		//test keyed user authentication
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/auth", "", "?key="+apiKey, KeyedUserAuthentication, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &c)
		So(err, ShouldBeNil)
		So(c, ShouldHaveSameTypeAs, customer.Customer{})

		//test get user by id
		thyme = time.Now()
		testThatHttp.Request("get", "/customer/", ":id", cu.Id+"?key="+apiKey, GetUserById, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
		So(err, ShouldBeNil)
		So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})

		//test change user password
		form = url.Values{"email": {"*****@*****.**"}, "oldPass": {"robthepoor"}, "newPass": {"prolife"}}
		v = form.Encode()
		body = strings.NewReader(v)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/user/changePassword", "", "?key="+apiKey, ChangePassword, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()*2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var result string
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &result)
		So(err, ShouldBeNil)
		So(result, ShouldHaveSameTypeAs, "Success")

		//test reset  user password
		form = url.Values{"email": {"*****@*****.**"}, "customerID": {strconv.Itoa(c.CustomerId)}}
		v = form.Encode()
		body = strings.NewReader(v)
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/user/resetPassword", "", "?key="+apiKey, ResetPassword, body, "application/x-www-form-urlencoded")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &result)
		So(err, ShouldBeNil)
		So(result, ShouldHaveSameTypeAs, "Success")

		//test generate api key
		thyme = time.Now()
		testThatHttp.Request("post", "/customer/user/", ":id/key/:type", cu.Id+"/key/PRIVATE?key="+apiKey, GenerateApiKey, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var newKey customer.ApiCredentials
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &newKey)
		So(err, ShouldBeNil)
		So(newKey.Key, ShouldHaveSameTypeAs, "string")

		//test delete customer users by customerId
		var cu2 customer.CustomerUser
		cu2.Create([]int{1})
		c.JoinUser(cu2)
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/allUsersByCustomerID/", ":id", strconv.Itoa(c.Id), DeleteCustomerUsersByCustomerID, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		var response string
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &response)
		So(err, ShouldBeNil)
		So(response, ShouldHaveSameTypeAs, "this is a string")

		//test delete customer user
		thyme = time.Now()
		testThatHttp.Request("delete", "/customer/user/", ":id", cu.Id, DeleteCustomerUser, nil, "")
		So(time.Since(thyme).Nanoseconds(), ShouldBeLessThan, time.Second.Nanoseconds()/2)
		So(testThatHttp.Response.Code, ShouldEqual, 200)
		err = json.Unmarshal(testThatHttp.Response.Body.Bytes(), &cu)
		So(err, ShouldBeNil)
		So(cu, ShouldHaveSameTypeAs, customer.CustomerUser{})
		So(cu.Id, ShouldNotBeEmpty)
		cu2.Delete()
	})
	//teardown
	err = c.Delete()
	if err != nil {
		t.Log(err)
	}

	if database.EmptyDb != nil {
		err = pub.Delete()
		if err != nil {
			t.Log(err)
		}
		err = pri.Delete()
		if err != nil {
			t.Log(err)
		}
		err = auth.Delete()
		if err != nil {
			t.Log(err)
		}
	}

	err = cu.Delete()
	if err != nil {
		t.Log(err)
	}

}