Skip to content

Commit

Permalink
test: FORMS-1444 external api routes (bcgov#1474)
Browse files Browse the repository at this point in the history
* test: FORMS-1444 form external api routes

* refactor: move controller mocks and uuids into tests
  • Loading branch information
WalterMoar authored Aug 15, 2024
1 parent 56583b7 commit 413668a
Show file tree
Hide file tree
Showing 5 changed files with 409 additions and 258 deletions.
137 changes: 79 additions & 58 deletions app/tests/unit/forms/admin/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,64 +27,6 @@ userAccess.currentUser = jest.fn((_req, _res, next) => {
next();
});

controller.createFormComponentsProactiveHelp = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.deleteApiKey = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.getExternalAPIStatusCodes = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.getExternalAPIs = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.getFCProactiveHelpImageUrl = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.getFormUserRoles = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.getUsers = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.listFormComponentsProactiveHelp = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.listForms = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.readApiDetails = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.readForm = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.readVersion = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.restoreForm = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.setFormUserRoles = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.updateExternalAPI = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.updateFormComponentsProactiveHelp = jest.fn((_req, res) => {
res.sendStatus(200);
});
userController.read = jest.fn((_req, res) => {
res.sendStatus(200);
});

const componentId = uuid.v4();
const externalApiId = uuid.v4();
const formId = uuid.v4();
const formVersionId = uuid.v4();
const userId = uuid.v4();

//
// Create the router and a simple Express server.
//
Expand All @@ -102,6 +44,10 @@ describe(`${basePath}/externalAPIs`, () => {
const path = `${basePath}/externalAPIs`;

it('should have correct middleware for GET', async () => {
controller.getExternalAPIs = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -111,9 +57,14 @@ describe(`${basePath}/externalAPIs`, () => {
});

describe(`${basePath}/externalAPIs/:externalApiId`, () => {
const externalApiId = uuid.v4();
const path = `${basePath}/externalAPIs/${externalApiId}`;

it('should have correct middleware for PUT', async () => {
controller.updateExternalAPI = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.put(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -126,6 +77,10 @@ describe(`${basePath}/externalAPIs/statusCodes`, () => {
const path = `${basePath}/externalAPIs/statusCodes`;

it('should have correct middleware for GET', async () => {
controller.getExternalAPIStatusCodes = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -135,9 +90,14 @@ describe(`${basePath}/externalAPIs/statusCodes`, () => {
});

describe(`${basePath}/formcomponents/proactivehelp/:publishStatus/:componentId`, () => {
const componentId = uuid.v4();
const path = `${basePath}/formcomponents/proactivehelp/:publishStatus/${componentId}`;

it('should have correct middleware for PUT', async () => {
controller.updateFormComponentsProactiveHelp = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.put(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -147,9 +107,14 @@ describe(`${basePath}/formcomponents/proactivehelp/:publishStatus/:componentId`,
});

describe(`${basePath}/formcomponents/proactivehelp/imageUrl/:componentId`, () => {
const componentId = uuid.v4();
const path = `${basePath}/formcomponents/proactivehelp/imageUrl/${componentId}`;

it('should have correct middleware for GET', async () => {
controller.getFCProactiveHelpImageUrl = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -162,6 +127,10 @@ describe(`${basePath}/formcomponents/proactivehelp/list`, () => {
const path = `${basePath}/formcomponents/proactivehelp/list`;

it('should have correct middleware for GET', async () => {
controller.listFormComponentsProactiveHelp = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -174,6 +143,10 @@ describe(`${basePath}/formcomponents/proactivehelp/object`, () => {
const path = `${basePath}/formcomponents/proactivehelp/object`;

it('should have correct middleware for POST', async () => {
controller.createFormComponentsProactiveHelp = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.post(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -186,6 +159,10 @@ describe(`${basePath}/forms`, () => {
const path = `${basePath}/forms`;

it('should have correct middleware for GET', async () => {
controller.listForms = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -195,9 +172,14 @@ describe(`${basePath}/forms`, () => {
});

describe(`${basePath}/forms/:formId`, () => {
const formId = uuid.v4();
const path = `${basePath}/forms/${formId}`;

it('should have correct middleware for GET', async () => {
controller.readForm = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -207,9 +189,14 @@ describe(`${basePath}/forms/:formId`, () => {
});

describe(`${basePath}/forms/:formId/addUser`, () => {
const formId = uuid.v4();
const path = `${basePath}/forms/${formId}/addUser`;

it('should have correct middleware for PUT', async () => {
controller.setFormUserRoles = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.put(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -219,9 +206,14 @@ describe(`${basePath}/forms/:formId/addUser`, () => {
});

describe(`${basePath}/forms/:formId/apiKey`, () => {
const formId = uuid.v4();
const path = `${basePath}/forms/${formId}/apiKey`;

it('should have correct middleware for DELETE', async () => {
controller.deleteApiKey = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.delete(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -230,6 +222,10 @@ describe(`${basePath}/forms/:formId/apiKey`, () => {
});

it('should have correct middleware for GET', async () => {
controller.readApiDetails = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -239,9 +235,14 @@ describe(`${basePath}/forms/:formId/apiKey`, () => {
});

describe(`${basePath}/forms/:formId/formUsers`, () => {
const formId = uuid.v4();
const path = `${basePath}/forms/${formId}/formUsers`;

it('should have correct middleware for GET', async () => {
controller.getFormUserRoles = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -251,9 +252,14 @@ describe(`${basePath}/forms/:formId/formUsers`, () => {
});

describe(`${basePath}/forms/:formId/restore`, () => {
const formId = uuid.v4();
const path = `${basePath}/forms/${formId}/restore`;

it('should have correct middleware for PUT', async () => {
controller.restoreForm = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.put(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -263,9 +269,15 @@ describe(`${basePath}/forms/:formId/restore`, () => {
});

describe(`${basePath}/forms/:formId/versions/:formVersionId`, () => {
const formId = uuid.v4();
const formVersionId = uuid.v4();
const path = `${basePath}/forms/${formId}/versions/${formVersionId}`;

it('should have correct middleware for GET', async () => {
controller.readVersion = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -278,6 +290,10 @@ describe(`${basePath}/users`, () => {
const path = `${basePath}/users`;

it('should have correct middleware for GET', async () => {
controller.getUsers = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand All @@ -287,9 +303,14 @@ describe(`${basePath}/users`, () => {
});

describe(`${basePath}/users/:userId`, () => {
const userId = uuid.v4();
const path = `${basePath}/users/${userId}`;

it('should have correct middleware for GET', async () => {
userController.read = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(mockJwtServiceProtect).toHaveBeenCalledTimes(1);
Expand Down
20 changes: 8 additions & 12 deletions app/tests/unit/forms/bcgeoaddress/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ const { expressHelper } = require('../../../common/helper');

const controller = require('../../../../src/forms/bcgeoaddress/controller');

//
// Mock out all the middleware - we're testing that the routes are set up
// correctly, not the functionality of the middleware.
//

controller.advanceSearchBCGeoAddress = jest.fn((_req, res) => {
res.sendStatus(200);
});
controller.searchBCGeoAddress = jest.fn((_req, res) => {
res.sendStatus(200);
});

//
// Create the router and a simple Express server.
//
Expand All @@ -33,6 +21,10 @@ describe(`${basePath}/address`, () => {
const path = `${basePath}/address`;

it('should have correct middleware for GET', async () => {
controller.searchBCGeoAddress = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(controller.searchBCGeoAddress).toHaveBeenCalledTimes(1);
Expand All @@ -43,6 +35,10 @@ describe(`${basePath}/advance/address`, () => {
const path = `${basePath}/advance/address`;

it('should have correct middleware for GET', async () => {
controller.advanceSearchBCGeoAddress = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(controller.advanceSearchBCGeoAddress).toHaveBeenCalledTimes(1);
Expand Down
22 changes: 12 additions & 10 deletions app/tests/unit/forms/file/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ validateParameter.validateFileId = jest.fn((_req, _res, next) => {
next();
});

controller.create = jest.fn((_req, _res, next) => {
next();
});
controller.delete = jest.fn((_req, _res, next) => {
next();
});
controller.read = jest.fn((_req, _res, next) => {
next();
});

//
// Create the router and a simple Express server.
//
Expand All @@ -79,6 +69,10 @@ describe(`${basePath}`, () => {
const path = `${basePath}`;

it('should have correct middleware for POST', async () => {
controller.create = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.post(path);

expect(validateParameter.validateFileId).toBeCalledTimes(0);
Expand All @@ -98,6 +92,10 @@ describe(`${basePath}/:id`, () => {
const path = `${basePath}/${fileId}`;

it('should have correct middleware for DELETE', async () => {
controller.delete = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.delete(path);

expect(validateParameter.validateFileId).toBeCalledTimes(1);
Expand All @@ -112,6 +110,10 @@ describe(`${basePath}/:id`, () => {
});

it('should have correct middleware for GET', async () => {
controller.read = jest.fn((_req, res) => {
res.sendStatus(200);
});

await appRequest.get(path);

expect(validateParameter.validateFileId).toBeCalledTimes(1);
Expand Down
Loading

0 comments on commit 413668a

Please sign in to comment.