Skip to content

Commit

Permalink
add tests to verify missing properties in the schema
Browse files Browse the repository at this point in the history
  • Loading branch information
am29d committed Oct 25, 2024
1 parent 64285f3 commit 22404fc
Show file tree
Hide file tree
Showing 16 changed files with 351 additions and 17 deletions.
17 changes: 16 additions & 1 deletion packages/parser/tests/unit/schema/alb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @group unit/parser/schema/
*/
import { AlbMultiValueHeadersSchema, AlbSchema } from '../../../src/schemas/';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('ALB ', () => {
it('should parse alb event', () => {
Expand All @@ -24,4 +24,19 @@ describe('ALB ', () => {
albMultiValueHeadersEvent
);
});

describe('should detect missing properties in schema for ', () => {
it('alb event', () => {
const albEvent = TestEvents.albEvent;
const strictSchema = AlbSchema.strict();
expect(() => strictSchema.parse(albEvent)).not.toThrow();
});
it('alb event with multi value headers', () => {
const albMultiValueHeadersEvent = TestEvents.albMultiValueHeadersEvent;
const strictSchema = makeSchemaStrictForTesting(
AlbMultiValueHeadersSchema
);
expect(() => strictSchema.parse(albMultiValueHeadersEvent)).not.toThrow();
});
});
});
53 changes: 52 additions & 1 deletion packages/parser/tests/unit/schema/apigw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
APIGatewayRequestAuthorizerEventSchema,
APIGatewayTokenAuthorizerEventSchema,
} from '../../../src/schemas/index.js';
import { getTestEvent } from './utils.js';
import { getTestEvent, makeSchemaStrictForTesting } from './utils.js';

describe('API Gateway REST Schemas', () => {
const eventsPath = 'apigw-rest';
Expand Down Expand Up @@ -150,4 +150,55 @@ describe('API Gateway REST Schemas', () => {
expect(parsedEvent).toEqual(event);
});
});

describe('should detect missing properties in schema for ', () => {
it.each([
'console-test-ui',
'iam-auth',
'jwt-authorizer-auth',
'lambda-authorizer-auth',
'no-auth',
'websocket',
])(' %p example event', (filename) => {
// Prepare
const event = getTestEvent({ eventsPath, filename: filename });

const strictSchema = makeSchemaStrictForTesting(
APIGatewayProxyEventSchema
);

// Act & Assess
expect(() => strictSchema.parse(event)).not.toThrow();
});

it('authorizer-request example event', () => {
// Prepare
const event = getTestEvent({
eventsPath,
filename: 'authorizer-request',
});

const strictSchema = makeSchemaStrictForTesting(
APIGatewayRequestAuthorizerEventSchema
);

// Act & Assess
expect(() => strictSchema.parse(event)).not.toThrow();
});

it('authorizer-token example event', () => {
// Prepare
const event = getTestEvent({
eventsPath,
filename: 'authorizer-token',
});

const strictSchema = makeSchemaStrictForTesting(
APIGatewayTokenAuthorizerEventSchema
);

// Act & Assess
expect(() => strictSchema.parse(event)).not.toThrow();
});
});
});
32 changes: 31 additions & 1 deletion packages/parser/tests/unit/schema/apigwv2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
APIGatewayProxyEventV2Schema,
APIGatewayRequestAuthorizerEventV2Schema,
} from '../../../src/schemas/index.js';
import { getTestEvent } from './utils.js';
import { getTestEvent, makeSchemaStrictForTesting } from './utils.js';

describe('API Gateway HTTP (v2) Schemas', () => {
const eventsPath = 'apigw-http';
Expand Down Expand Up @@ -100,4 +100,34 @@ describe('API Gateway HTTP (v2) Schemas', () => {
expect(parsedEvent).toEqual(event);
});
});

describe('should detect missing properties in schema for ', () => {
it.each([
'iam-auth',
'jwt-authorizer-auth',
'lambda-authorizer-auth',
'no-auth',
])('event %s', (filename) => {
// Prepare
const event = getTestEvent({ eventsPath, filename });
const strictSchema = makeSchemaStrictForTesting(
APIGatewayProxyEventV2Schema
);
// Act & Assess
expect(() => strictSchema.parse(event)).not.toThrow();
});

it('authorizer-request event', () => {
// Prepare
const event = getTestEvent({
eventsPath,
filename: 'authorizer-request',
});
const strictSchema = makeSchemaStrictForTesting(
APIGatewayRequestAuthorizerEventV2Schema
);
// Act & Assess
expect(() => strictSchema.parse(event)).not.toThrow();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
CloudFormationCustomResourceDeleteSchema,
CloudFormationCustomResourceUpdateSchema,
} from '../../../src/schemas/';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('CloudFormationCustomResource ', () => {
it('should parse create event', () => {
Expand Down Expand Up @@ -42,4 +42,45 @@ describe('CloudFormationCustomResource ', () => {
)
).toEqual(cloudFormationCustomResourceDeleteEvent);
});

describe('should detect missing properties in schema for ', () => {
it('CloudFormationCustomResourceCreateSchema', () => {
const cloudFormationCustomResourceCreateEvent =
TestEvents.cloudFormationCustomResourceCreateEvent;

const strictSchema = makeSchemaStrictForTesting(
CloudFormationCustomResourceCreateSchema
);

expect(() =>
strictSchema.parse(cloudFormationCustomResourceCreateEvent)
).not.toThrow();
});

it('CloudFormationCustomResourceUpdateSchema', () => {
const cloudFormationCustomResourceUpdateEvent =
TestEvents.cloudFormationCustomResourceUpdateEvent;

const strictSchema = makeSchemaStrictForTesting(
CloudFormationCustomResourceUpdateSchema
);

expect(() =>
strictSchema.parse(cloudFormationCustomResourceUpdateEvent)
).not.toThrow();
});

it('CloudFormationCustomResourceDeleteSchema', () => {
const cloudFormationCustomResourceDeleteEvent =
TestEvents.cloudFormationCustomResourceDeleteEvent;

const strictSchema = makeSchemaStrictForTesting(
CloudFormationCustomResourceDeleteSchema
);

expect(() =>
strictSchema.parse(cloudFormationCustomResourceDeleteEvent)
).not.toThrow();
});
});
});
8 changes: 7 additions & 1 deletion packages/parser/tests/unit/schema/dynamodb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { DynamoDBStreamSchema } from '../../../src/schemas/';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('DynamoDB ', () => {
const dynamoStreamEvent = TestEvents.dynamoStreamEvent;
Expand All @@ -14,4 +14,10 @@ describe('DynamoDB ', () => {
dynamoStreamEvent
);
});

it('should detect missing properties in schema', () => {
const strictSchema = makeSchemaStrictForTesting(DynamoDBStreamSchema);

expect(() => strictSchema.parse(dynamoStreamEvent)).not.toThrow();
});
});
9 changes: 8 additions & 1 deletion packages/parser/tests/unit/schema/eventbridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
*/

import { EventBridgeSchema } from '../../../src/schemas/';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('EventBridge ', () => {
it('should parse eventbridge event', () => {
const eventBridgeEvent = TestEvents.eventBridgeEvent;

expect(EventBridgeSchema.parse(eventBridgeEvent)).toEqual(eventBridgeEvent);
});

it('should detect missing properties in schema', () => {
const eventBridgeEvent = TestEvents.eventBridgeEvent;
const strictSchema = makeSchemaStrictForTesting(EventBridgeSchema);

expect(() => strictSchema.parse(eventBridgeEvent)).not.toThrow();
});
});
20 changes: 19 additions & 1 deletion packages/parser/tests/unit/schema/kafka.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../../src/schemas/';
import type { KafkaSelfManagedEvent } from '../../../src/types';
import type { KafkaRecord } from '../../../src/types/schema';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('Kafka ', () => {
const expectedTestEvent = {
Expand Down Expand Up @@ -71,4 +71,22 @@ describe('Kafka ', () => {
);
expect(parsedRecord.topic).toEqual('mytopic');
});

describe('should detect missing properties in schema for', () => {
it('KafkaMskEventSchema', () => {
const kafkaEventMsk = TestEvents.kafkaEventMsk;

const strictSchema = makeSchemaStrictForTesting(KafkaMskEventSchema);
expect(() => strictSchema.parse(kafkaEventMsk)).not.toThrow();
});

it('KafkaSelfManagedEventSchema', () => {
const kafkaEventSelfManaged = TestEvents.kafkaEventSelfManaged;

const strictSchema = makeSchemaStrictForTesting(
KafkaSelfManagedEventSchema
);
expect(() => strictSchema.parse(kafkaEventSelfManaged)).not.toThrow();
});
});
});
16 changes: 14 additions & 2 deletions packages/parser/tests/unit/schema/kinesis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
KinesisFirehoseRecord,
KinesisFirehoseSqsRecord,
} from '../../../src/types/schema';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('Kinesis ', () => {
it('should parse kinesis event', () => {
Expand All @@ -30,6 +30,7 @@ describe('Kinesis ', () => {

expect(parsed.Records[0].kinesis.data).toEqual('Hello, this is a test.');
});

it('should parse single kinesis record', () => {
const kinesisStreamEventOneRecord = TestEvents.kinesisStreamEventOneRecord;
const parsed = KinesisDataStreamSchema.parse(kinesisStreamEventOneRecord);
Expand All @@ -39,18 +40,21 @@ describe('Kinesis ', () => {
username: 'test',
});
});

it('should parse Firehose event', () => {
const kinesisFirehoseKinesisEvent = TestEvents.kinesisFirehoseKinesisEvent;
const parsed = KinesisFirehoseSchema.parse(kinesisFirehoseKinesisEvent);
expect(parsed.records[0].data).toEqual('Hello World');
});

it('should parse Kinesis Firehose PutEvents event', () => {
const kinesisFirehosePutEvent = TestEvents.kinesisFirehosePutEvent;
const parsed = KinesisFirehoseSchema.parse(kinesisFirehosePutEvent);
expect(JSON.parse(parsed.records[1].data)).toEqual({
Hello: 'World',
});
});

it('should parse Firehose event with SQS event', () => {
const kinesisFirehoseSQSEvent = TestEvents.kinesisFirehoseSQSEvent;
const parsed = KinesisFirehoseSqsSchema.parse(kinesisFirehoseSQSEvent);
Expand All @@ -59,6 +63,7 @@ describe('Kinesis ', () => {
body: 'Test message.',
});
});

it('should parse Kinesis event with CloudWatch event', () => {
const kinesisStreamCloudWatchLogsEvent =
TestEvents.kinesisStreamCloudWatchLogsEvent;
Expand All @@ -73,6 +78,7 @@ describe('Kinesis ', () => {
logStream: '2022/11/10/[$LATEST]26b6a45d574f442ea28438923cbf7bf7',
});
});

it('should return original value if cannot parse KinesisFirehoseSqsRecord', () => {
const kinesisFirehoseSQSEvent = TestEvents.kinesisFirehoseSQSEvent as {
records: { data: string }[];
Expand All @@ -81,13 +87,13 @@ describe('Kinesis ', () => {
const parsed = KinesisFirehoseSqsSchema.parse(kinesisFirehoseSQSEvent);
expect(parsed.records[0].data).toEqual('not a valid json');
});

it('should parse a kinesis record from a kinesis event', () => {
const kinesisStreamEvent: KinesisDataStreamEvent =
TestEvents.kinesisStreamEvent as KinesisDataStreamEvent;
const parsedRecord = KinesisDataStreamRecord.parse(
kinesisStreamEvent.Records[0]
);

expect(parsedRecord.eventName).toEqual('aws:kinesis:record');
});

Expand All @@ -110,4 +116,10 @@ describe('Kinesis ', () => {
'49640912821178817833517986466168945147170627572855734274000000'
);
});

it('should catch any unknown fields in the example event', () => {
const kinesisStreamEvent = TestEvents.kinesisStreamEvent;
const strictSchema = makeSchemaStrictForTesting(KinesisDataStreamSchema);
expect(() => strictSchema.parse(kinesisStreamEvent)).not.toThrow();
});
});
12 changes: 10 additions & 2 deletions packages/parser/tests/unit/schema/lambda.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

import { LambdaFunctionUrlSchema } from '../../../src/schemas/';
import { TestEvents } from './utils.js';
import { TestEvents, makeSchemaStrictForTesting } from './utils.js';

describe('Lambda ', () => {
it('should parse lambda event', () => {
const lambdaFunctionUrlEvent = TestEvents.apiGatewayProxyV2Event;
const lambdaFunctionUrlEvent = TestEvents.lambdaFunctionUrlEvent;

expect(LambdaFunctionUrlSchema.parse(lambdaFunctionUrlEvent)).toEqual(
lambdaFunctionUrlEvent
Expand All @@ -21,4 +21,12 @@ describe('Lambda ', () => {

expect(LambdaFunctionUrlSchema.parse(urlIAMEvent)).toEqual(urlIAMEvent);
});

it('should detect missing properties in schema for lambda event', () => {
const lambdaFunctionUrlEvent = TestEvents.lambdaFunctionUrlEvent;

const strictSchema = makeSchemaStrictForTesting(LambdaFunctionUrlSchema);

expect(() => strictSchema.parse(lambdaFunctionUrlEvent)).not.toThrow();
});
});
Loading

0 comments on commit 22404fc

Please sign in to comment.