Skip to content

Commit

Permalink
Merge branch 'main' into fix/forms-1445-ext-api-drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
usingtechnology committed Aug 15, 2024
2 parents 4ea807e + 6d57324 commit c437205
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 3 deletions.
33 changes: 33 additions & 0 deletions app/tests/unit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,36 @@ describe('my tests', () => {
```

Similar to `.only` is the `.skip` modifier to skip a test or group of tests.

## Testing Strategy

The testing strategy for the backend unit tests can be broken down into the different layers of the backend. For all tests we should:

- ensure that the tests are consistent
- ensure that we have 100% test coverage
- ensure that we have complete test coverage: we should be testing additional corner cases even once we reach 100% test coverage
- test the interface, not the implementation
- test the unit under test, not its dependencies

### Middleware Testing

The tests for the middleware files should:

- mock all services calls used by the middleware, including both exception and minimal valid results
- test all response codes produced by the middleware

### Route Testing

The tests for the `route.js` files should:

- mock all middleware used by the file
- each route test should check that every middleware is called the proper number of times
- each route test should mock the controller function that it calls
- mock controller functions with `res.sendStatus(200)`, as doing something like `next()` will call multiple controller functions when route paths are overloaded
- check that the mocked controller function is called - this will catch when a new route path accidentally overloads an old one
- for consistency and ease of comparison, alphabetize the expect clauses ("alphabetize when possible")

Note:

- Some middleware is called when the `routes.js` is loaded, not when the route is called. For example, the parameters to `userAccess.hasFormPermissions` cannot be tested by calling the route using it. Even if we reload the `routes.js` for each route test, it would be hard to tell which call to `hasFormPermissions` was the call for the route under test
- Maybe we should refactor and create a set of standard middleware mocks that live outside the `routes.spec.js` files
5 changes: 5 additions & 0 deletions app/tests/unit/forms/form/externalApi/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe(`${basePath}/:formId/externalAPIs`, () => {
expect(controller.listExternalAPIs).toBeCalledTimes(1);
expect(hasFormPermissionsMock).toBeCalledTimes(1);
expect(rateLimiter.apiKeyRateLimiter).toBeCalledTimes(0);
expect(userAccess.currentUser).toBeCalledTimes(1);
expect(validateParameter.validateExternalAPIId).toBeCalledTimes(0);
expect(validateParameter.validateFormId).toBeCalledTimes(1);
});
Expand All @@ -98,6 +99,7 @@ describe(`${basePath}/:formId/externalAPIs`, () => {
expect(controller.createExternalAPI).toBeCalledTimes(1);
expect(hasFormPermissionsMock).toBeCalledTimes(1);
expect(rateLimiter.apiKeyRateLimiter).toBeCalledTimes(0);
expect(userAccess.currentUser).toBeCalledTimes(1);
expect(validateParameter.validateExternalAPIId).toBeCalledTimes(0);
expect(validateParameter.validateFormId).toBeCalledTimes(1);
});
Expand Down Expand Up @@ -125,6 +127,7 @@ describe(`${basePath}/:formId/externalAPIs/:externalAPIId`, () => {
expect(controller.deleteExternalAPI).toBeCalledTimes(1);
expect(hasFormPermissionsMock).toBeCalledTimes(1);
expect(rateLimiter.apiKeyRateLimiter).toBeCalledTimes(0);
expect(userAccess.currentUser).toBeCalledTimes(1);
expect(validateParameter.validateExternalAPIId).toBeCalledTimes(1);
expect(validateParameter.validateFormId).toBeCalledTimes(1);
});
Expand Down Expand Up @@ -152,6 +155,7 @@ describe(`${basePath}/:formId/externalAPIs/:externalAPIId`, () => {
expect(controller.updateExternalAPI).toBeCalledTimes(1);
expect(hasFormPermissionsMock).toBeCalledTimes(1);
expect(rateLimiter.apiKeyRateLimiter).toBeCalledTimes(0);
expect(userAccess.currentUser).toBeCalledTimes(1);
expect(validateParameter.validateExternalAPIId).toBeCalledTimes(1);
expect(validateParameter.validateFormId).toBeCalledTimes(1);
});
Expand Down Expand Up @@ -219,6 +223,7 @@ describe(`${basePath}/:formId/externalAPIs/statusCodes`, () => {
expect(controller.listExternalAPIStatusCodes).toBeCalledTimes(1);
expect(hasFormPermissionsMock).toBeCalledTimes(1);
expect(rateLimiter.apiKeyRateLimiter).toBeCalledTimes(0);
expect(userAccess.currentUser).toBeCalledTimes(1);
expect(validateParameter.validateExternalAPIId).toBeCalledTimes(0);
expect(validateParameter.validateFormId).toBeCalledTimes(1);
});
Expand Down
Loading

0 comments on commit c437205

Please sign in to comment.