Skip to content

Commit

Permalink
ensure page parameter for v2 users index works as expected #468
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Oct 25, 2024
1 parent 3193d0c commit 80dd33b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/controllers/v2/users_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const show = async req => {
};

const index = async req => {
InaturalistAPI.setPerPage( req, { default: 10, max: 100 } );
const { page, perPage } = InaturalistAPI.paginationData( req, { default: 10, max: 200 } );
const filters = [];
if ( req.query.following ) {
const followingUser = await User.findByLoginOrID( req.query.following );
Expand Down Expand Up @@ -90,14 +90,15 @@ const index = async req => {
},
_source: { excludes: User.elasticExcludeFields },
sort: { id: "asc" },
size: req.query.per_page
from: ( perPage * page ) - perPage,
size: perPage
}
} );

return InaturalistAPI.resultsHash( {
total: response.hits.total.value,
per_page: req.query.per_page,
page: Number( req.query.page ),
per_page: perPage,
page,
results: _.map( response.hits.hits, h => new User( h._source ) )
} );
};
Expand Down
18 changes: 18 additions & 0 deletions test/integration/v2/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ describe( "Users", ( ) => {
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );

it( "sorts by ID ascending by default", function ( done ) {
const firstUser = _.sortBy( fixtures.elasticsearch.users.user, "id" )[0];
request( this.app ).get( "/v2/users?per_page=1" )
.expect( res => {
expect( res.body.results[0].id ).to.eq( firstUser.id );
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );

it( "accepts a page parameter", function ( done ) {
const secondUser = _.sortBy( fixtures.elasticsearch.users.user, "id" )[1];
request( this.app ).get( "/v2/users?per_page=1&page=2" )
.expect( res => {
expect( res.body.results[0].id ).to.eq( secondUser.id );
} ).expect( "Content-Type", /json/ )
.expect( 200, done );
} );
} );

describe( "update", ( ) => {
Expand Down

0 comments on commit 80dd33b

Please sign in to comment.