Ejemplo n.º 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)
}
Ejemplo n.º 2
0
func testGetLinks(t *testing.T, testURL string) {
	var (
		urlObject *url.URL
		first     string
		last      string
		previous  string
		next      string
		err       error
	)

	// Test URL object
	urlObject, err = url.Parse(testURL)
	if err != nil {
		log.Fatal(err)
	}

	// Test with zero results
	first, last, previous, next, err = pagination.GetLinks(
		urlObject,
		0, // count
		1, // page
		2, // limit
	)
	if assert.Nil(t, err) {
		assert.Equal(t, "/foobar?hello=world&page=1", first)
		assert.Equal(t, "/foobar?hello=world&page=1", last)
		assert.Equal(t, "", previous)
		assert.Equal(t, "", next)
	}

	// Test first page
	first, last, previous, next, err = pagination.GetLinks(
		urlObject,
		10, // count
		1,  // page
		2,  // limit
	)
	if assert.Nil(t, err) {
		assert.Equal(t, "/foobar?hello=world&page=1", first)
		assert.Equal(t, "/foobar?hello=world&page=5", last)
		assert.Equal(t, "", previous)
		assert.Equal(t, "/foobar?hello=world&page=2", next)
	}

	// Test middle page
	first, last, previous, next, err = pagination.GetLinks(
		urlObject,
		10, // count
		2,  // page
		2,  // limit
	)
	if assert.Nil(t, err) {
		assert.Equal(t, "/foobar?hello=world&page=1", first)
		assert.Equal(t, "/foobar?hello=world&page=5", last)
		assert.Equal(t, "/foobar?hello=world&page=1", previous)
		assert.Equal(t, "/foobar?hello=world&page=3", next)
	}

	// Test last page
	first, last, previous, next, err = pagination.GetLinks(
		urlObject,
		10, // count
		5,  // page
		2,  // limit
	)
	if assert.Nil(t, err) {
		assert.Equal(t, "/foobar?hello=world&page=1", first)
		assert.Equal(t, "/foobar?hello=world&page=5", last)
		assert.Equal(t, "/foobar?hello=world&page=4", previous)
		assert.Equal(t, "", next)
	}

	// Test page too big
	first, last, previous, next, err = pagination.GetLinks(
		urlObject,
		10, // count
		6,  // page
		2,  // limit
	)
	if assert.NotNil(t, err) {
		assert.Equal(t, pagination.ErrPageTooBig, err)
	}

	// Test when limit > count
	first, last, previous, next, err = pagination.GetLinks(
		urlObject,
		10, // count
		1,  // page
		12, // limit
	)
	if assert.Nil(t, err) {
		assert.Equal(t, "/foobar?hello=world&page=1", first)
		assert.Equal(t, "/foobar?hello=world&page=1", last)
		assert.Equal(t, "", previous)
		assert.Equal(t, "", next)
	}
}