Beispiel #1
0
// Edit an existing customer for a
// given shop.
func EditCustomer(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer
	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &c); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	customerId := params["id"]

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}
	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id

	if err = c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Beispiel #2
0
func Test_AccountLogin(t *testing.T) {

	Convey("with shop identifier", t, func() {
		shopID := cart.InsertTestData()
		So(shopID, ShouldNotBeNil)
		val := shopID.Hex()
		qs := make(url.Values, 0)
		qs.Add("shop", val)

		cust := cart.Customer{
			ShopId:    *shopID,
			FirstName: "Alex",
			LastName:  "Ninneman",
			Email:     time.Now().Format(time.RFC3339Nano) + "@gmail.com",
		}

		cust.Password = "******"
		response = httprunner.JsonRequest("POST", "/shopify/account", &qs, cust, AddAccount)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &cust), ShouldBeNil)

		cust.Password = ""
		response = httprunner.Req(AccountLogin, "POST", "", "/shopify/account/login", &qs, nil, cust)
		So(response.Code, ShouldEqual, 500)
		So(json.Unmarshal(response.Body.Bytes(), &apierror.ApiErr{}), ShouldBeNil)

		cust.Password = "******"
		response = httprunner.Req(AccountLogin, "POST", "", "/shopify/account/login", &qs, nil, cust)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &cust), ShouldBeNil)
	})
}
Beispiel #3
0
// Edit an account for a given shop.
func EditAccount(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop, token string) string {

	var c cart.Customer
	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &c); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	c.ShopId = shop.Id

	c.Id, err = cart.IdentifierFromToken(token)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Beispiel #4
0
// Create a customer for a
// given shop.
func AddAccount(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer
	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &c); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	c.ShopId = shop.Id

	if err = c.Insert(req.Referer()); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Beispiel #5
0
func BenchmarkGetCustomerAddresses(b *testing.B) {
	shopID := cart.InsertTestData()
	if shopID == nil {
		b.Error("failed to create a shop")
		b.Fail()
	}

	val := shopID.Hex()
	qs := make(url.Values, 0)
	qs.Add("shop", val)

	cust := cart.Customer{
		ShopId:    *shopID,
		FirstName: "Alex",
		LastName:  "Ninneman",
		Email:     time.Now().Format(time.RFC3339Nano) + "@gmail.com",
		Password:  "******",
	}
	if err := cust.Insert("http://www.example.com"); err != nil {
		b.Error(err.Error())
		b.Fail()
	}

	(&httprunner.BenchmarkOptions{
		Method:             "GET",
		Route:              "/shopify/customers/" + cust.Id.Hex() + "/addresses",
		ParameterizedRoute: "/shopify/customers/:id/addresses",
		Handler:            GetAddresses,
		QueryString:        &qs,
		Runs:               b.N,
	}).RequestBenchmark()
}
Beispiel #6
0
func GetAddresses(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {
	customerId := params["id"]
	limit := 50
	page := 1
	qs := req.URL.Query()

	if l := qs.Get("limit"); l != "" {
		lmt, err := strconv.Atoi(l)
		if err == nil && lmt != 0 {
			limit = lmt
		}
	}
	if p := qs.Get("page"); p != "" {
		pg, err := strconv.Atoi(p)
		if err == nil && pg != 0 {
			page = pg
		}
	}

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c := cart.Customer{
		Id:     bson.ObjectIdHex(customerId),
		ShopId: shop.Id,
	}

	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	addr := c.Addresses
	if len(c.Addresses) > 0 {
		addr = c.Addresses[:limit]
		if page > 1 && len(c.Addresses) >= ((page-1)*limit) {
			addr = c.Addresses[((page - 1) / limit):limit]
		}
	}

	return encoding.Must(enc.Encode(addr))
}
Beispiel #7
0
func Test_EditAccount(t *testing.T) {

	Convey("with shop identifier", t, func() {
		shopID := cart.InsertTestData()
		So(shopID, ShouldNotBeNil)
		val := shopID.Hex()
		qs := make(url.Values, 0)
		qs.Add("shop", val)

		cust := cart.Customer{
			ShopId:    *shopID,
			FirstName: "Alex",
			LastName:  "Ninneman",
			Email:     time.Now().Format(time.RFC3339Nano) + "@gmail.com",
		}

		cust.Password = "******"
		response = httprunner.JsonRequest("POST", "/shopify/account", &qs, cust, AddAccount)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &cust), ShouldBeNil)

		cust.Email = time.Now().Format(time.RFC3339Nano) + "@gmail.com"
		header := map[string]interface{}{
			"Authorization": "Bearer as;ldskfja;lfdj",
		}
		response = httprunner.Req(EditAccount, "PUT", "", "/shopify/account", &qs, nil, cust, header)
		So(response.Code, ShouldEqual, 500)
		So(json.Unmarshal(response.Body.Bytes(), &apierror.ApiErr{}), ShouldBeNil)

		header = map[string]interface{}{
			"Authorization": "Bearer " + cust.Token,
		}
		cust.FirstName = ""
		response = httprunner.Req(EditAccount, "PUT", "", "/shopify/account", &qs, nil, cust, header)
		So(response.Code, ShouldEqual, 500)
		So(json.Unmarshal(response.Body.Bytes(), &apierror.ApiErr{}), ShouldBeNil)

		cust.FirstName = "Alex"
		response = httprunner.Req(EditAccount, "PUT", "", "/shopify/account", &qs, nil, cust, header)
		So(response.Code, ShouldEqual, 200)
		So(json.Unmarshal(response.Body.Bytes(), &cust), ShouldBeNil)
	})
}
Beispiel #8
0
// Get an account for a given shop
func GetAccount(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop, token string) string {
	cust := cart.Customer{
		ShopId: shop.Id,
	}
	var err error

	cust.Id, err = cart.IdentifierFromToken(token)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = cust.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(cust))
}
Beispiel #9
0
// Delete a customer for a given shop.
// Note: Can't delete if the customer has existing orders.
func DeleteCustomer(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer
	customerId := params["id"]

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}
	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id

	if err := c.Delete(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return ""
}
Beispiel #10
0
// Get a specific customer for a
// given shop.
func GetCustomer(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	customerId := params["id"]

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c := cart.Customer{
		Id:     bson.ObjectIdHex(customerId),
		ShopId: shop.Id,
	}
	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Beispiel #11
0
func DeleteAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError("", err, w, req)
		return ""
	}

	addressId := params["address"]
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	// Make sure this isn't the default address
	if c.DefaultAddress != nil && c.DefaultAddress.Id.Hex() == addressId {
		apierror.GenerateError("removing a default address is not allowed", nil, w, req)
		return ""
	}

	found := false
	for i, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			c.Addresses = append(c.Addresses[:i], c.Addresses[i+1:]...)
			found = true
			break
		}
	}

	if !found {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	if err := c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return ""
}
Beispiel #12
0
func AddAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var address cart.CustomerAddress
	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &address); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	// audit the address
	if err := address.Validate(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	addressId := bson.NewObjectId()
	address.Id = &addressId
	c.Addresses = append(c.Addresses, address)

	if err = c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(address))
}
Beispiel #13
0
func GetAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {
	customerId := params["id"]
	addressId := params["address"]

	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	c := cart.Customer{
		Id:     bson.ObjectIdHex(customerId),
		ShopId: shop.Id,
	}

	if err := c.Get(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	var address *cart.CustomerAddress
	for _, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			address = &addr
			break
		}
	}

	if address == nil {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(address))
}
Beispiel #14
0
func SetDefaultAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError("", err, w, req)
		return ""
	}

	addressId := params["address"]
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	found := false
	for _, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			c.DefaultAddress = &addr
			found = true
			break
		}
	}

	if !found {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	if err := c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(c))
}
Beispiel #15
0
func EditAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string {

	var address cart.CustomerAddress
	var c cart.Customer

	customerId := params["id"]
	if !bson.IsObjectIdHex(customerId) {
		apierror.GenerateError("invalid customer reference", nil, w, req)
		return ""
	}

	c.Id = bson.ObjectIdHex(customerId)
	c.ShopId = shop.Id
	if err := c.Get(); err != nil {
		apierror.GenerateError("", err, w, req)
		return ""
	}

	addressId := params["address"]
	if !bson.IsObjectIdHex(addressId) {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}
	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	if err = json.Unmarshal(data, &address); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	// audit the address
	if err := address.Validate(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	found := false
	for i, addr := range c.Addresses {
		if addr.Id.Hex() == addressId {
			c.Addresses[i] = address
			found = true
			break
		}
	}

	if !found {
		apierror.GenerateError("invalid address reference", nil, w, req)
		return ""
	}

	if err = c.Update(); err != nil {
		apierror.GenerateError(err.Error(), err, w, req)
		return ""
	}

	return encoding.Must(enc.Encode(address))
}