-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2634936
commit 480cb37
Showing
2 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* Unit tests for the health check routes */ | ||
import request from 'supertest'; | ||
import { API_URLS_V1, AUTH_RESPONSES } from '../../src/constants'; | ||
import App from '../../src/app'; | ||
import Config from 'simple-app-config'; | ||
import HealthCheckController from '../../src/controllers/healthCheckController'; | ||
|
||
/* Mock the controller functions */ | ||
jest.mock('../../src/controllers/healthCheckController', () => ({ | ||
checkHealth: jest.fn().mockImplementation((req, res) => { | ||
res.sendStatus(200); | ||
}) | ||
})); | ||
|
||
describe('Health Check Routes Tests', () => { | ||
let appInstance: App; | ||
|
||
beforeAll(() => { | ||
appInstance = new App(); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
describe('GET /healthCheck', () => { | ||
it('should call HealthCheckController.checkHealth', async () => { | ||
/* Make the API call */ | ||
const expressInstance = appInstance.getExpressApp(); | ||
await request(expressInstance).get(`${API_URLS_V1.HEALTH_CHECK}/healthCheck`).send({}); | ||
|
||
/* Test against expected */ | ||
expect(HealthCheckController.checkHealth).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should fail when the rate limit is exceeded', async () => { | ||
/* Call API `threshold` times so that next call will cause rating limiting */ | ||
const expressInstance = appInstance.getExpressApp(); | ||
const threshold: number = Config.get('RATE_LIMITING.HEALTH_CHECKS.HEALTH_CHECK.THRESHOLD'); | ||
for (let i = 0; i < threshold; i++) { | ||
await request(expressInstance).get(`${API_URLS_V1.HEALTH_CHECK}/healthCheck`).send({}); | ||
} | ||
|
||
/* Call API */ | ||
const response = await request(expressInstance) | ||
.get(`${API_URLS_V1.HEALTH_CHECK}/healthCheck`) | ||
.send({}); | ||
|
||
/* Test against expected */ | ||
expect(HealthCheckController.checkHealth).toHaveBeenCalled(); | ||
expect(response.statusCode).toBe(429); | ||
expect(response.body.message).toBe(AUTH_RESPONSES._429_RATE_LIMIT_EXCEEDED); | ||
}); | ||
}); | ||
}); |