Skip to content

Commit

Permalink
Merge pull request #240 from bcgov/SC3485
Browse files Browse the repository at this point in the history
Update pagination validation and add test cases
  • Loading branch information
kyle1morel authored Jan 12, 2024
2 parents 2e9d8ec + 61a7f01 commit 668398e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/src/docs/v1.api-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,12 @@ paths:
type: array
items:
$ref: "#/components/schemas/DB-Object"
headers:
X-Total-Rows:
schema:
type: integer
description: Total number of objects matching the search query independent of pagination scope
required: true
"401":
$ref: "#/components/responses/Unauthorized"
"403":
Expand Down Expand Up @@ -2057,6 +2063,13 @@ components:
type: string
description: The filename of the original file uploaded
example: foobar.txt
permissions:
type: array
items:
type: string
description: An array of explicit permCodes the current user has. Empty array if authenticated via Basic auth or if user lacks explicit permissions for the object. Attribute only appears if request contains permissions query parameter
example:
- READ
- $ref: "#/components/schemas/DB-TimestampUserData"
DB-ObjectPermission:
title: DB Object Permission
Expand Down
26 changes: 24 additions & 2 deletions app/src/validators/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,30 @@ const scheme = {
string: oneOrMany(Joi.string().max(255)),

pagination: (sortList) => ({
page: Joi.number().min(1),
limit: Joi.number().min(0),
page: Joi.alternatives()
.conditional('limit', {
not: true,
then: Joi
.number()
.min(1)
.required()
.messages({
'any.required': '`Must specify page number`',
}),
otherwise: Joi.number().min(1)
}),
limit: Joi.alternatives()
.conditional('page', {
not: true,
then: Joi
.number()
.min(1)
.required()
.messages({
'any.required': '`Must specify page limit`',
}),
otherwise: Joi.number().min(1)
}),
sort: Joi.string().valid(...sortList),
order: Joi.string().valid(...Object.values(SortOrder)),
}),
Expand Down
22 changes: 20 additions & 2 deletions app/tests/unit/validators/object.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ describe('searchObjects', () => {
const page = query.keys.page;

it('is a number', () => {
expect(page.type).toEqual('number');
expect(page.type).toEqual('alternatives');
expect(page.matches).toBeTruthy();
});

it('enforces min value 1', () => {
Expand All @@ -534,8 +535,10 @@ describe('searchObjects', () => {
const limit = query.keys.limit;

it('is a number', () => {
expect(limit.type).toEqual('number');
expect(limit.type).toEqual('alternatives');
expect(limit.matches).toBeTruthy();
});

it('enforces min value 0', () => {
expect.objectContaining({
name: 'min',
Expand All @@ -544,14 +547,29 @@ describe('searchObjects', () => {
})
});
});

it('limit is provided and greater then or equal to 0', () => {
const limitObject = schema.searchObjects.query;
const result = limitObject.validate({
limit: 0
});
expect(result.value.limit).toBeGreaterThanOrEqual(0);
});
});

describe('sort', () => {
const sort = query.keys.sort;
const sortName = ['id', 'path', 'public', 'active', 'createdBy', 'updatedBy', 'updatedAt', 'bucketId', 'name'];

it('is a string', () => {
expect(sort.type).toEqual('string');
});

it('must be one of ' + sortName, () => {
expect(sort.allow).toEqual(
expect.arrayContaining(sortName)
);
});
});

describe('order', () => {
Expand Down

0 comments on commit 668398e

Please sign in to comment.