Beispiel #1
0
// Handles calls to list user accounts
// GET /v1/users
func (s *Service) listUsersHandler(w http.ResponseWriter, r *http.Request) {
	// Get the authenticated user from the request context
	authenticatedUser, err := GetAuthenticatedUser(r)
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Check permissions
	if err = checkListUsersPermissions(authenticatedUser); err != nil {
		response.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	// Get pagination params
	page, limit, sorts, err := pagination.GetParams(r, []string{"id"})
	if err != nil {
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Count total number of results
	count, err := s.PaginatedUsersCount()
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Get pagination links
	first, last, previous, next, err := pagination.GetLinks(r.URL, count, page, limit)
	if err != nil {
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Get paginated results
	users, err := s.FindPaginatedUsers(
		pagination.GetOffsetForPage(count, page, limit),
		limit,
		sorts,
	)
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Create response
	self := util.GetCurrentURL(r)
	listUsersResponse, err := NewListUsersResponse(
		count, page,
		self, first, last, previous, next,
		users,
	)
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// Write JSON response
	response.WriteJSON(w, listUsersResponse, http.StatusOK)
}
Beispiel #2
0
func TestGetParams(t *testing.T) {
	var (
		page       int
		limit      int
		sorts      map[string]string
		r          *http.Request
		err        error
		sortableBy []string
	)

	// Test default values
	r, err = http.NewRequest("GET", "http://1.2.3.4/v1/foo/bar", nil)
	if err != nil {
		log.Fatal(err)
	}
	page, limit, sorts, err = pagination.GetParams(r, sortableBy)
	if assert.NoError(t, err) {
		assert.Equal(t, 1, page)
		assert.Equal(t, 25, limit)
		assert.Equal(t, 0, len(sorts))
	}

	// Test page <= 0
	r, err = http.NewRequest("GET", "http://1.2.3.4/v1/foo/bar?page=0", nil)
	if err != nil {
		log.Fatal(err)
	}
	page, limit, sorts, err = pagination.GetParams(r, sortableBy)
	if assert.Error(t, err) {
		assert.Equal(t, pagination.ErrPageTooSmall, err)
	}

	// Test limit too small
	r, err = http.NewRequest("GET", "http://1.2.3.4/v1/foo/bar?page=1&limit=0", nil)
	if err != nil {
		log.Fatal(err)
	}
	page, limit, sorts, err = pagination.GetParams(r, sortableBy)
	if assert.Error(t, err) {
		assert.Equal(t, pagination.ErrLimitTooSmall, err)
	}

	// Test limit too big
	r, err = http.NewRequest("GET", "http://1.2.3.4/v1/foo/bar?page=1&limit=1000", nil)
	if err != nil {
		log.Fatal(err)
	}
	page, limit, sorts, err = pagination.GetParams(r, sortableBy)
	if assert.Error(t, err) {
		assert.Equal(t, pagination.ErrLimitTooBig, err)
	}

	// Test valid page and limit
	r, err = http.NewRequest("GET", "http://1.2.3.4/v1/foo/bar?page=10&limit=50", nil)
	if err != nil {
		log.Fatal(err)
	}
	page, limit, sorts, err = pagination.GetParams(r, sortableBy)
	if assert.NoError(t, err) {
		assert.Equal(t, 10, page)
		assert.Equal(t, 50, limit)
		assert.Equal(t, 0, len(sorts))
	}
}