adapt-authoring-api 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -538,6 +538,10 @@ class AbstractApiModule extends AbstractModule {
538
538
  * @param {Object} mongoOpts The MongoDB options
539
539
  */
540
540
  async setUpPagination (req, res, mongoOpts) {
541
+ if (mongoOpts.limit === 0) {
542
+ delete mongoOpts.limit
543
+ return
544
+ }
541
545
  const maxPageSize = this.getConfig('maxPageSize') ?? this.app.config.get('adapt-authoring-api.maxPageSize')
542
546
  let pageSize = mongoOpts.limit ?? this.getConfig('defaultPageSize') ?? this.app.config.get('adapt-authoring-api.defaultPageSize')
543
547
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-api",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "Abstract module for creating APIs",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-api",
6
6
  "license": "GPL-3.0",
@@ -230,4 +230,69 @@ describe('AbstractApiModule', () => {
230
230
  assert.equal(queryRoute.modifying, false)
231
231
  })
232
232
  })
233
+
234
+ describe('#setUpPagination()', () => {
235
+ function createPaginationInstance (docCount = 0, config = {}) {
236
+ const headers = {}
237
+ const instance = createInstance({
238
+ getConfig: (key) => config[key],
239
+ app: {
240
+ config: {
241
+ get: (key) => {
242
+ const defaults = { 'adapt-authoring-api.defaultPageSize': 100, 'adapt-authoring-api.maxPageSize': 250 }
243
+ return defaults[key]
244
+ }
245
+ },
246
+ waitForModule: async () => ({
247
+ getCollection: () => ({
248
+ countDocuments: async () => docCount
249
+ })
250
+ })
251
+ }
252
+ })
253
+ const req = { originalUrl: '/api/content/query', query: {}, apiData: { collectionName: 'content', query: {} } }
254
+ const res = { set: (k, v) => { headers[k] = v } }
255
+ return { instance, req, res, headers }
256
+ }
257
+
258
+ it('should skip pagination when limit is 0', async () => {
259
+ const { instance, req, res, headers } = createPaginationInstance(500)
260
+ const mongoOpts = { limit: 0 }
261
+ await instance.setUpPagination(req, res, mongoOpts)
262
+ assert.equal(mongoOpts.limit, undefined)
263
+ assert.equal(headers['X-Adapt-Page'], undefined)
264
+ assert.equal(headers['X-Adapt-PageSize'], undefined)
265
+ })
266
+
267
+ it('should apply default pagination when limit is not 0', async () => {
268
+ const { instance, req, res, headers } = createPaginationInstance(50)
269
+ const mongoOpts = {}
270
+ await instance.setUpPagination(req, res, mongoOpts)
271
+ assert.equal(mongoOpts.limit, 100)
272
+ assert.equal(headers['X-Adapt-Page'], 1)
273
+ assert.equal(headers['X-Adapt-PageSize'], 100)
274
+ })
275
+
276
+ it('should set Link header when results span multiple pages', async () => {
277
+ const { instance, req, res, headers } = createPaginationInstance(250)
278
+ const mongoOpts = {}
279
+ await instance.setUpPagination(req, res, mongoOpts)
280
+ assert.ok(headers.Link)
281
+ assert.ok(headers.Link.includes('rel="next"'))
282
+ })
283
+
284
+ it('should not set Link header for single-page results', async () => {
285
+ const { instance, req, res, headers } = createPaginationInstance(50)
286
+ const mongoOpts = {}
287
+ await instance.setUpPagination(req, res, mongoOpts)
288
+ assert.equal(headers.Link, undefined)
289
+ })
290
+
291
+ it('should cap pageSize at maxPageSize', async () => {
292
+ const { instance, req, res, headers } = createPaginationInstance(500)
293
+ const mongoOpts = { limit: 999 }
294
+ await instance.setUpPagination(req, res, mongoOpts)
295
+ assert.equal(headers['X-Adapt-PageSize'], 250)
296
+ })
297
+ })
233
298
  })