Skip to content

Commit

Permalink
fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sugat009 committed Dec 30, 2024
1 parent 49553b7 commit 22da4b9
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 42 deletions.
2 changes: 1 addition & 1 deletion api/src/controllers/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ctx = require('../services/data-context');
const serverUtils = require('../server-utils');

const getContact = ({ with_lineage }) => ctx.bind(with_lineage === 'true' ? Contact.v1.getWithLineage : Contact.v1.get);
const getContactIds = () => ctx.bind(Contact.v1.getIdsPage);
const getContactIds = () => ctx.bind(Contact.v1.getUuidsPage);

const checkUserPermissions = async (req) => {
const userCtx = await auth.getUserCtx(req);
Expand Down
2 changes: 1 addition & 1 deletion api/src/controllers/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const serverUtils = require('../server-utils');
const { Report, Qualifier } = require('@medic/cht-datasource');

const getReport = () => ctx.bind(Report.v1.get);
const getReportIds = () => ctx.bind(Report.v1.getIdsPage);
const getReportIds = () => ctx.bind(Report.v1.getUuidsPage);

const checkUserPermissions = async (req) => {
const userCtx = await auth.getUserCtx(req);
Expand Down
10 changes: 6 additions & 4 deletions shared-libs/cht-datasource/test/contact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,9 @@ describe('contact', () => {
.to.be.rejectedWith(`The limit must be a positive number: [${String(limitValue)}]`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Contact.v1.getUuidsPage, Remote.Contact.v1.getUuidsPage))
.to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Contact.v1.getUuidsPage, Remote.Contact.v1.getUuidsPage)
).to.be.true;
expect(isContactTypeQualifier.notCalled).to.be.true;
expect(isFreetextQualifier.notCalled).to.be.true;
expect(getIdsPage.notCalled).to.be.true;
Expand All @@ -332,8 +333,9 @@ describe('contact', () => {
.to.be.rejectedWith(`Invalid cursor token: [${String(skipValue)}]`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Contact.v1.getUuidsPage, Remote.Contact.v1.getUuidsPage))
.to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Contact.v1.getUuidsPage, Remote.Contact.v1.getUuidsPage)
).to.be.true;
expect(isContactTypeQualifier.notCalled).to.be.true;
expect(isFreetextQualifier.notCalled).to.be.true;
expect(getIdsPage.notCalled).to.be.true;
Expand Down
4 changes: 3 additions & 1 deletion shared-libs/cht-datasource/test/local/contact.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ describe('local contact', () => {
});

it('throws an error if contact type is invalid', async () => {
await expect(Contact.v1.getUuidsPage(localContext)(invalidContactTypeQualifier, cursor, limit)).to.be.rejectedWith(
await expect(
Contact.v1.getUuidsPage(localContext)(invalidContactTypeQualifier, cursor, limit)
).to.be.rejectedWith(
`Invalid contact type [${invalidContactTypeQualifier.contactType}].`
);

Expand Down
16 changes: 12 additions & 4 deletions shared-libs/cht-datasource/test/report.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ describe('report', () => {

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)).to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)
).to.be.true;
expect(getIdsPage.calledOnceWithExactly(freetextQualifier, null, limit)).to.be.true;
expect(isFreetextQualifier.calledOnceWithExactly(freetextQualifier)).to.be.true;
});
Expand All @@ -108,7 +110,9 @@ describe('report', () => {

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)).to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)
).to.be.true;
expect(getIdsPage.calledOnceWithExactly(freetextQualifier, cursor, limit)).to.be.true;
expect(isFreetextQualifier.calledOnceWithExactly(freetextQualifier)).to.be.true;
});
Expand All @@ -122,7 +126,9 @@ describe('report', () => {

expect(result).to.equal(pageData);
expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)).to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)
).to.be.true;
expect(getIdsPage.calledOnceWithExactly(freetextQualifier, cursor, limit)).to.be.true;
expect(isFreetextQualifier.calledOnceWithExactly(freetextQualifier)).to.be.true;
});
Expand All @@ -146,7 +152,9 @@ describe('report', () => {
.to.be.rejectedWith(`Invalid freetext [${JSON.stringify(invalidFreetextQualifier)}].`);

expect(assertDataContext.calledOnceWithExactly(dataContext)).to.be.true;
expect(adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)).to.be.true;
expect(
adapt.calledOnceWithExactly(dataContext, Local.Report.v1.getUuidsPage, Remote.Report.v1.getUuidsPage)
).to.be.true;
expect(isFreetextQualifier.calledOnceWithExactly(invalidFreetextQualifier)).to.be.true;
expect(getIdsPage.notCalled).to.be.true;
});
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/api/controllers/contact.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ describe('Contact API', () => {
});
});

describe('GET /api/v1/contact/id', async () => {
describe('GET /api/v1/contact/uuid', async () => {
const fourLimit = 4;
const twoLimit = 2;
const invalidContactType = 'invalidPerson';
const freetext = 'contact';
const placeFreetext = 'clinic';
const endpoint = '/api/v1/contact/id';
const endpoint = '/api/v1/contact/uuid';

it('returns a page of people type contact ids for no limit and cursor passed', async () => {
const queryParams = {
Expand Down Expand Up @@ -489,15 +489,15 @@ describe('Contact API', () => {

it(`throws error when user does not have can_view_contacts permission`, async () => {
const opts = {
path: `/api/v1/contact/id`,
path: endpoint,
auth: { username: userNoPerms.username, password: userNoPerms.password },
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});

it(`throws error when user is not an online user`, async () => {
const opts = {
path: `/api/v1/contact/id`,
path: endpoint,
auth: { username: offlineUser.username, password: offlineUser.password },
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
Expand All @@ -509,7 +509,7 @@ describe('Contact API', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/contact/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -523,7 +523,7 @@ describe('Contact API', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/contact/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -537,7 +537,7 @@ describe('Contact API', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/contact/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand Down
14 changes: 7 additions & 7 deletions tests/integration/api/controllers/report.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ describe('Report API', () => {
});
});

describe('GET /api/v1/report/id', async () => {
describe('GET /api/v1/report/uuid', async () => {
const freetext = 'report';
const limit = 4;
const endpoint = '/api/v1/report/id';
const endpoint = '/api/v1/report/uuid';

it('returns a page of report ids for no limit and cursor passed', async () => {
const queryParams = {
Expand Down Expand Up @@ -177,14 +177,14 @@ describe('Report API', () => {

it(`throws error when user does not have can_view_reports permission`, async () => {
const opts = {
path: `/api/v1/report/id`, auth: {username: userNoPerms.username, password: userNoPerms.password},
path: endpoint, auth: {username: userNoPerms.username, password: userNoPerms.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});

it(`throws error when user is not an online user`, async () => {
const opts = {
path: `/api/v1/report/id`, auth: {username: offlineUser.username, password: offlineUser.password},
path: endpoint, auth: {username: offlineUser.username, password: offlineUser.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});
Expand All @@ -195,7 +195,7 @@ describe('Report API', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/report/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -208,7 +208,7 @@ describe('Report API', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/report/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -221,7 +221,7 @@ describe('Report API', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/report/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand Down
17 changes: 9 additions & 8 deletions tests/integration/shared-libs/cht-datasource/contact.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ describe('Contact', () => {
});
});

describe('getIdsPage', async () => {
const getIdsPage = Contact.v1.getIdsPage(dataContext);
describe('getUuidsPage', async () => {
const getIdsPage = Contact.v1.getUuidsPage(dataContext);
const fourLimit = 4;
const twoLimit = 2;
const stringifiedLimit = '7';
const cursor = null;
const invalidContactType = 'invalidPerson';
const freetext = 'contact';
const placeFreetext = 'clinic';
const endpoint = '/api/v1/contact/uuid';

it('returns a page of people type contact ids for no limit and cursor passed', async () => {
const responsePage = await getIdsPage(Qualifier.byContactType(personType));
Expand Down Expand Up @@ -306,14 +307,14 @@ describe('Contact', () => {

it(`throws error when user does not have can_view_contacts permission`, async () => {
const opts = {
path: `/api/v1/contact/id`, auth: {username: userNoPerms.username, password: userNoPerms.password},
path: endpoint, auth: {username: userNoPerms.username, password: userNoPerms.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});

it(`throws error when user is not an online user`, async () => {
const opts = {
path: `/api/v1/contact/id`, auth: {username: offlineUser.username, password: offlineUser.password},
path: endpoint, auth: {username: offlineUser.username, password: offlineUser.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});
Expand All @@ -324,7 +325,7 @@ describe('Contact', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/contact/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -337,7 +338,7 @@ describe('Contact', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/contact/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -350,15 +351,15 @@ describe('Contact', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/contact/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
.to.be.rejectedWith(`400 - {"code":400,"error":"Invalid cursor token: [${-1}]."}`);
});
});

describe('Contact.v1.getIds', async () => {
describe('Contact.v1.getUuids', async () => {
it('fetches all data by iterating through generator', async () => {
const docs = [];

Expand Down
19 changes: 10 additions & 9 deletions tests/integration/shared-libs/cht-datasource/report.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,20 @@ describe('Report', () => {
].forEach(([ description, user ]) => {
it(`throws error when user ${description}`, async () => {
const opts = {
path: `/api/v1/report/${patient._id}`, auth: {username: user.username, password: user.password},
path: `/api/v1/report/${report0._id}`, auth: {username: user.username, password: user.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});
});
});

describe('getIdsPage', async () => {
const getReport = Report.v1.getIdsPage(dataContext);
describe('getUuidsPage', async () => {
const getReport = Report.v1.getUuidsPage(dataContext);
const freetext = 'report';
const limit = 4;
const stringifiedLimit = '6';
const cursor = null;
const endpoint = '/api/v1/report/uuid';

it('returns a page of report ids for no limit and cursor passed', async () => {
const responsePage = await getReport(Qualifier.byFreetext(freetext));
Expand Down Expand Up @@ -154,14 +155,14 @@ describe('Report', () => {

it(`throws error when user does not have can_view_reports permission`, async () => {
const opts = {
path: `/api/v1/report/id`, auth: {username: userNoPerms.username, password: userNoPerms.password},
path: endpoint, auth: {username: userNoPerms.username, password: userNoPerms.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});

it(`throws error when user is not an online user`, async () => {
const opts = {
path: `/api/v1/report/id`, auth: {username: offlineUser.username, password: offlineUser.password},
path: endpoint, auth: {username: offlineUser.username, password: offlineUser.password},
};
await expect(utils.request(opts)).to.be.rejectedWith('403 - {"code":403,"error":"Insufficient privileges"}');
});
Expand All @@ -172,7 +173,7 @@ describe('Report', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/report/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -185,7 +186,7 @@ describe('Report', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/report/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
Expand All @@ -198,15 +199,15 @@ describe('Report', () => {
};
const queryString = new URLSearchParams(queryParams).toString();
const opts = {
path: `/api/v1/report/id?${queryString}`,
path: `${endpoint}?${queryString}`,
};

await expect(utils.request(opts))
.to.be.rejectedWith(`400 - {"code":400,"error":"Invalid cursor token: [${-1}]."}`);
});
});

describe('getIds', async () => {
describe('getUuids', async () => {
it('fetches all data by iterating through generator', async () => {
const freetext = 'report';
const docs = [];
Expand Down

0 comments on commit 22da4b9

Please sign in to comment.