Exemplo n.º 1
0
//registers an inactive user; emails user and webdev that a new inactive user exists - used by dealers site
func RegisterUser(rw http.ResponseWriter, r *http.Request, enc encoding.Encoder) string {
	var err error

	name := r.FormValue("name")
	email := r.FormValue("email")
	pass := r.FormValue("pass")
	customerID, err := strconv.Atoi(r.FormValue("customerID"))
	// isActive, err := strconv.ParseBool(r.FormValue("isActive"))
	locationID, err := strconv.Atoi(r.FormValue("locationID"))
	// isSudo, err := strconv.ParseBool(r.FormValue("isSudo"))
	cust_ID, err := strconv.Atoi(r.FormValue("cust_ID"))
	notCustomer, err := strconv.ParseBool(r.FormValue("notCustomer"))

	if email == "" || pass == "" {
		err = errors.New("Email and password are required.")
		apierror.GenerateError("Email and password are required", err, rw, r)
		return ""
	}

	var user customer.CustomerUser
	user.Email = email
	user.Password = pass
	if name != "" {
		user.Name = name
	}
	if customerID != 0 {
		user.OldCustomerID = customerID
	}
	if locationID != 0 {
		user.Location.Id = locationID
	}
	if cust_ID != 0 {
		user.CustomerID = cust_ID
	}
	user.Active = false
	user.Sudo = false
	user.Current = notCustomer

	//check for existence of user
	err = user.FindByEmail()
	if err == nil {
		apierror.GenerateError("A user with that email address already exists.", err, rw, r)
		return ""
	}
	err = nil

	user.Brands, err = brand.GetUserBrands(cust_ID)
	if err != nil {
		apierror.GenerateError("Trouble getting user brands.", err, rw, r)
		return ""
	}
	var brandIds []int
	for _, brand := range user.Brands {
		brandIds = append(brandIds, brand.ID)
	}

	if err = user.Create(brandIds); err != nil {
		apierror.GenerateError("Trouble registering new customer user", err, rw, r)
		return ""
	}

	//email
	if err = user.SendRegistrationEmail(); err != nil {
		apierror.GenerateError("Trouble emailing new customer user", err, rw, r)
		return ""
	}

	if err = user.SendRegistrationRequestEmail(); err != nil {
		apierror.GenerateError("Trouble emailing webdevelopment regarding new customer user", err, rw, r)
		return ""
	}

	return encoding.Must(enc.Encode(user))
}
Exemplo n.º 2
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)
	}

}