|
5 | 5 | validateEventIsNotStale, |
6 | 6 | validateInterventionEvent, |
7 | 7 | validateIfIdentityAcquired, |
| 8 | + attemptToParseJson, |
8 | 9 | } from '../validate-event'; |
9 | 10 | import logger from '../../commons/logger'; |
10 | 11 | import { addMetric } from '../../commons/metrics'; |
@@ -203,6 +204,46 @@ describe('event-validation', () => { |
203 | 204 | }); |
204 | 205 | }); |
205 | 206 |
|
| 207 | + it('should throw an error if event is stale', async () => { |
| 208 | + const staleEvent = { |
| 209 | + timestamp: timestamp.seconds - 5000, |
| 210 | + event_timestamp_ms: timestamp.milliseconds - 5000, |
| 211 | + event_name: TriggerEventsEnum.TICF_ACCOUNT_INTERVENTION, |
| 212 | + event_id: '123', |
| 213 | + component_id: 'TICF_CRI', |
| 214 | + user: { user_id: 'urn:fdc:gov.uk:2022:USER_ONE' }, |
| 215 | + extensions: { |
| 216 | + intervention: { |
| 217 | + intervention_code: '01' as InterventionCodeEnum1, |
| 218 | + intervention_reason: 'something', |
| 219 | + originating_component_id: 'CMS', |
| 220 | + originator_reference_id: '1234567', |
| 221 | + requester_id: '1234567', |
| 222 | + }, |
| 223 | + }, |
| 224 | + }; |
| 225 | + |
| 226 | + await expect(async () => { |
| 227 | + await validateEventIsNotStale( |
| 228 | + EventsEnum.FRAUD_SUSPEND_ACCOUNT, |
| 229 | + staleEvent, |
| 230 | + { |
| 231 | + blocked: false, |
| 232 | + suspended: false, |
| 233 | + resetPassword: false, |
| 234 | + reproveIdentity: false, |
| 235 | + }, |
| 236 | + dynamoDBResult, |
| 237 | + ); |
| 238 | + }).rejects.toThrow(new ValidationError('Event received predates last applied event for this user.')); |
| 239 | + expect(addMetric).toHaveBeenCalledWith(MetricNames.INTERVENTION_EVENT_STALE); |
| 240 | + expect(sendAuditEvent).toHaveBeenCalledWith('AIS_EVENT_IGNORED_STALE', 'FRAUD_SUSPEND_ACCOUNT', staleEvent, { |
| 241 | + stateResult: { blocked: false, reproveIdentity: false, resetPassword: false, suspended: false }, |
| 242 | + interventionName: 'AIS_NO_INTERVENTION', |
| 243 | + nextAllowableInterventions: ['01', '03', '04', '05', '06'], |
| 244 | + }); |
| 245 | + }); |
| 246 | + |
206 | 247 | it('should not throw if event is not stale', async () => { |
207 | 248 | const nonStaleEvent = { |
208 | 249 | timestamp: timestamp.seconds, |
@@ -361,3 +402,16 @@ describe('event-validation', () => { |
361 | 402 | expect(addMetric).toHaveBeenCalledWith(MetricNames.IDENTITY_NOT_SUFFICIENTLY_PROVED); |
362 | 403 | }); |
363 | 404 | }); |
| 405 | + |
| 406 | +describe('attemptToParseJson', () => { |
| 407 | + it('should parse valid JSON', () => { |
| 408 | + const jsonString = '{"key": "value"}'; |
| 409 | + const result = attemptToParseJson(jsonString); |
| 410 | + expect(result).toEqual({ key: 'value' }); |
| 411 | + }); |
| 412 | + |
| 413 | + it('should return null for invalid JSON', () => { |
| 414 | + const jsonString = '{"key": "value"'; |
| 415 | + expect(() => attemptToParseJson(jsonString)).toThrow('record body could not be parsed to valid JSON.'); |
| 416 | + }); |
| 417 | +}); |
0 commit comments