Beispiel #1
0
func (a *Adaptor) ValidateIPs(userID int, ips []string) (bool, *adaptor.AdaptorError) {
	var formattedErr *adaptor.AdaptorError
	if len(ips) < 1 {
		formattedErr = adaptor.NewErrorWithStatus("No ips provided", http.StatusBadRequest)
		return false, formattedErr
	}

	params := url.Values{
		"userid":  []string{strconv.Itoa(userID)},
		"ip_list": ips,
	}

	var validIPs int
	err := a.apidClient.DoFunction("validateExternalIps", params, &validIPs)

	if err != nil {
		formattedErr = adaptor.NewError(err.Error())
		if strings.Contains(err.Error(), "ips were invalid") {
			formattedErr = adaptor.NewErrorWithStatus("One or more ips were invalid", http.StatusBadRequest)
		}
		return false, formattedErr
	}

	if validIPs < 1 {
		formattedErr = adaptor.NewErrorWithStatus("User does not have any IPs", http.StatusNotFound)
		return false, formattedErr
	}

	return true, nil
}
Beispiel #2
0
func (a *Adaptor) EditUserProfile(userProfile *client.UserProfile) (bool, *adaptor.AdaptorError) {
	var editSuccess int

	params, queryErr := query.Values(userProfile)

	if queryErr != nil {
		ln.Err("error query user profile", ln.Map{"err": queryErr.Error()})
		return false, adaptor.NewError("error query user profile")
	}

	err := a.apidClient.DoFunction("editUserProfile", params, &editSuccess)

	if err != nil {
		ln.Err("error updating user profile", ln.Map{"err": err.Error(), "user_profile": userProfile})
		return false, adaptor.NewError("error updating user profile")
	}

	if editSuccess == 0 {
		// check if the user exists
		user, adaptorError := a.GetUserProfile(userProfile.UserID)

		if adaptorError != nil {
			ln.Err("error when getting user profile", ln.Map{"err": adaptorError.Error(), "user_id": userProfile.UserID})
			return false, adaptor.NewError("error when getting user profile")
		}
		if user == nil {
			return false, adaptor.NewErrorWithStatus("user profile not found", http.StatusNotFound)
		}
		// no fields were changed
		return true, nil
	}

	return true, nil
}
Beispiel #3
0
func TestCreateUserPasswordValidationError(t *testing.T) {
	expectedNewUserID := 1234

	// value to be passed into apid
	expectedUsername := "******"

	// expected output from apidadaptor (pass along password errors)
	expectedApidErr := "your password sucks bro. because reasons."
	expectedErr := adaptor.NewErrorWithStatus("password invalid - "+expectedApidErr, http.StatusBadRequest)

	fakeClient := clientfakes.NewFakeClient()
	fakeClient.RegisterFunction("addUser", func(params url.Values) (interface{}, error) {
		username := params.Get("username")
		if username == expectedUsername {
			// apid returns a json hash with the key exists on duplicate entries
			return 0, fmt.Errorf(expectedApidErr)
		}
		return expectedNewUserID, nil
	})

	adaptor := New(fakeClient)

	_, adaptorErr := adaptor.CreateUser(
		client.Signup{
			Username: expectedUsername,
			Email:    "*****@*****.**",
			Password: "******",
		})

	if assert.NotNil(t, adaptorErr, adaptorErr.Error()) {
		assert.Equal(t, expectedErr.Error(), adaptorErr.Error())
		assert.Equal(t, expectedErr.SuggestedStatusCode, adaptorErr.SuggestedStatusCode)
	}
}
Beispiel #4
0
func (a *Adaptor) getReasonID(reason string) (int, *adaptor.AdaptorError) {
	validReasons := a.GetChurnReasons()
	validReasonsList := make([]string, len(validReasons))

	for i, valid := range validReasons {
		if strings.ToLower(valid.Reason) == strings.ToLower(reason) {
			return valid.ID, nil
		}
		validReasonsList[i] = valid.Reason
	}

	msg := fmt.Sprintf("invalid reason given [%v], must be from [%s]", reason, strings.Join(validReasonsList, ", "))
	return 0, adaptor.NewErrorWithStatus(msg, http.StatusBadRequest)
}
Beispiel #5
0
func TestValidateIps(t *testing.T) {
	cases := []struct {
		message      string
		apidResponse int
		apidError    error
		expected     bool
		expectedErr  *adaptor.AdaptorError
		ips          []string
	}{
		{
			message:      "valid ips",
			apidResponse: 2,
			expected:     true,
			ips:          []string{"1.1.1.1", "2.2.2.2"},
		}, {
			message:     "invalid ips",
			apidError:   errors.New("ips were invalid"),
			expected:    false,
			expectedErr: adaptor.NewErrorWithStatus("One or more ips were invalid", http.StatusBadRequest),
			ips:         []string{"7.7.7.7"},
		}, {
			message:     "empty request",
			expected:    false,
			expectedErr: adaptor.NewErrorWithStatus("No ips provided", http.StatusBadRequest),
			ips:         []string{},
		}, {
			message:     "apid failure",
			apidError:   errors.New("apid failure"),
			expected:    false,
			expectedErr: adaptor.NewError("apid failure"),
			ips:         []string{"7.7.7.7"},
		}, {
			message:      "user does not have ips",
			ips:          []string{"1.1.1.1"},
			apidResponse: 0,
			expectedErr:  adaptor.NewErrorWithStatus("User does not have any IPs", http.StatusNotFound),
		},
	}

	for _, c := range cases {
		parentUserID := 180
		fakeClient := clientfakes.NewFakeClient()
		fakeClient.RegisterFunction("validateExternalIps", func(params url.Values) (interface{}, error) {
			return c.apidResponse, c.apidError
		})

		adaptor := New(fakeClient)
		actual, adaptorErr := adaptor.ValidateIPs(parentUserID, c.ips)

		// check actual response
		msg := fmt.Sprintf("validate IP response for %s", c.message)
		if !assert.Equal(t, actual, c.expected, msg) {
			return
		}

		// check error message
		if c.expectedErr != nil {
			msg = fmt.Sprintf("should have an error message for %s", c.message)
			assert.Equal(t, adaptorErr.Err, c.expectedErr.Err, msg)

			msg = fmt.Sprintf("should have an error code for %s", c.message)
			assert.Equal(t, adaptorErr.SuggestedStatusCode, c.expectedErr.SuggestedStatusCode, msg)
		}
	}
}