-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into bcassessment-lookup
- Loading branch information
Showing
13 changed files
with
1,561 additions
and
66 deletions.
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
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
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
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,19 @@ | ||
import KeycloakService from '@/services/keycloak/keycloakService'; | ||
import logger from '@/utilities/winstonLogger'; | ||
import cron from 'node-cron'; | ||
|
||
const cronSyncKeycloakRoles = () => { | ||
// minute, hour, day of month, month, day of week | ||
const cronSchedule = '*/10 * * * *'; // Every 10 minutes | ||
cron.schedule(cronSchedule, async () => { | ||
logger.info(`Starting Sync Keycloak Roles routine at ${new Date().toISOString()}.`); | ||
try { | ||
await KeycloakService.syncKeycloakRoles(); | ||
logger.info(`Sync Keycloak Roles routine complete at ${new Date().toISOString()}.`); | ||
} catch (e) { | ||
logger.warn(e.message); | ||
} | ||
}); | ||
}; | ||
|
||
export default cronSyncKeycloakRoles; |
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
44 changes: 44 additions & 0 deletions
44
express-api/tests/unit/utilities/cronJobs/syncKeycloakRoles.test.ts
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,44 @@ | ||
import cronSyncKeycloakRoles from '@/utilities/cronJobs/syncKeycloakRoles'; | ||
import cron, { ScheduledTask } from 'node-cron'; | ||
import logger from '@/utilities/winstonLogger'; | ||
|
||
// Mocking keycloak service. | ||
const _syncKeycloakRoles = jest.fn(); | ||
jest.mock('@/services/keycloak/keycloakService.ts', () => ({ | ||
syncKeycloakRoles: () => _syncKeycloakRoles(), | ||
})); | ||
|
||
// cron.schedule has a weird return type expected. | ||
// This makes sure the inner function is still called. | ||
const _scheduleSpy = jest | ||
.spyOn(cron, 'schedule') | ||
.mockImplementation((cronExpression: string, func: () => void) => { | ||
func(); | ||
return func as unknown as ScheduledTask; | ||
}); | ||
|
||
const _loggerSpy = jest.spyOn(logger, 'warn'); | ||
|
||
describe('UNIT - cronSyncKeycloakRoles', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call the schedule task one time', () => { | ||
cronSyncKeycloakRoles(); | ||
expect(_scheduleSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should call the syncKeycloakRoles service one time', () => { | ||
cronSyncKeycloakRoles(); | ||
expect(_syncKeycloakRoles).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should log a warning if the sync fails', () => { | ||
_syncKeycloakRoles.mockImplementationOnce(() => { | ||
throw new Error(); | ||
}); | ||
cronSyncKeycloakRoles(); | ||
expect(_loggerSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
Oops, something went wrong.