Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cors origins #1

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Summary

What is the background of this pull request?

## Changes

- What are the changes made in this pull request?
- Change this and that, etc...

## Issues

What are the related issues or stories?
17 changes: 17 additions & 0 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Pull Request Tests

on:
pull_request:
types: [opened, reopened, synchronize]

jobs:
tests:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20.x
- run: npm ci --no-fund --no-audit
- run: npm test
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"dependencies": {
"@govtechsg/dnsprove": "^2.8.0",
"@middy/core": "^3.4.0",
"@middy/http-cors": "^3.4.0",
"tldts": "^6.1.41",
"zod": "^3.23.8"
},

"devDependencies": {
"@serverless/typescript": "^3.0.0",
"@types/aws-lambda": "^8.10.71",
Expand Down
57 changes: 52 additions & 5 deletions src/functions/resolve/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ describe("resolve handler test", () => {
queryStringParameters: Record<string, string> | null,
): APIGatewayProxyEventV2 =>
({
queryStringParameters,
version: "2.0",
queryStringParameters: queryStringParameters,
requestContext: {
http: {
method: "GET",
},
},
}) as APIGatewayProxyEventV2;

const mockContext = {
Expand Down Expand Up @@ -38,7 +44,7 @@ describe("resolve handler test", () => {
const result = await handler(mockEvent, mockContext);

expect(queryDns).toHaveBeenCalledWith("opencerts.io", expect.any(Array));
expect(result).toEqual({
expect(result).toMatchObject({
statusCode: 200,
body: JSON.stringify(mockQueryDnsResult),
});
Expand All @@ -49,7 +55,7 @@ describe("resolve handler test", () => {

const result = await handler(mockEvent, mockContext);

expect(result).toEqual({
expect(result).toMatchObject({
statusCode: 400,
body: JSON.stringify({
message: "Invalid parameters",
Expand All @@ -63,7 +69,7 @@ describe("resolve handler test", () => {

const result = await handler(mockEvent, mockContext);

expect(result).toEqual({
expect(result).toMatchObject({
statusCode: 400,
body: JSON.stringify({
message: "Invalid parameters",
Expand All @@ -81,7 +87,7 @@ describe("resolve handler test", () => {
const result = await handler(mockEvent, mockContext);

expect(queryDns).toHaveBeenCalledWith("opencerts.io", expect.any(Array));
expect(result).toEqual({
expect(result).toMatchObject({
statusCode: 502,
body: JSON.stringify({
message: mockError.message,
Expand Down Expand Up @@ -110,4 +116,45 @@ describe("resolve handler test", () => {
statusCode: 502,
});
});

describe("CORS headers", () => {
it("should include www.opencerts.io origin in the response", async () => {
const mockEvent = createMockEvent({ name: "opencerts.io" });
mockEvent.headers = { Origin: "https://www.opencerts.io" };

const result = await handler(mockEvent, mockContext);

expect(result).toMatchObject({
headers: {
"Access-Control-Allow-Origin": "https://www.opencerts.io",
},
});
});

it("should include dev.opencerts.io origin in the response", async () => {
const mockEvent = createMockEvent({ name: "opencerts.io" });
mockEvent.headers = { Origin: "https://dev.opencerts.io" };

const result = await handler(mockEvent, mockContext);

expect(result).toMatchObject({
headers: {
"Access-Control-Allow-Origin": "https://dev.opencerts.io",
},
});
});

it("should default origin to www.opencerts.io in the response", async () => {
const mockEvent = createMockEvent({ name: "opencerts.io" });
mockEvent.headers = { Origin: "https://www.not-opencerts.io" };

const result = await handler(mockEvent, mockContext);

expect(result).toMatchObject({
headers: {
"Access-Control-Allow-Origin": "https://www.opencerts.io",
},
});
});
});
});
8 changes: 8 additions & 0 deletions src/functions/resolve/cors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import httpCors from '@middy/http-cors';

export const cors = httpCors({
origins: [
'https://www.opencerts.io',
'https://dev.opencerts.io'
]
});
3 changes: 2 additions & 1 deletion src/functions/resolve/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { queryDns, googleDnsResolver, cloudflareDnsResolver } from "@govtechsg/d
import { formatJSONResponse } from "@libs/api-gateway";
import { errorResponse, SchemaValidationError, validateSchema } from "@functions/resolve/helpers";
import { dnsResultSchema, queryParamSchema } from "@functions/resolve/schemas";
import { cors } from "@functions/resolve/cors";

const resolve: APIGatewayProxyHandlerV2 = async (event) => {
try {
Expand All @@ -28,4 +29,4 @@ const resolve: APIGatewayProxyHandlerV2 = async (event) => {
}
};

export const main = middy(resolve);
export const main = middy(resolve).use(cors);
15 changes: 5 additions & 10 deletions src/functions/resolve/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { handlerPath } from '@libs/handler-resolver';
import { handlerPath } from "@libs/handler-resolver";
import { AWS } from "@serverless/typescript";

const config: AWS['functions']['Function'] = {
const config: AWS["functions"]["Function"] = {
handler: `${handlerPath(__dirname)}/handler.main`,
events: [
{
http: {
method: 'get',
path: 'resolve',
cors: {
origin: 'https://www.opencerts.io',
headers: ['Accept'],
allowCredentials: false,
},
method: "get",
path: "resolve",
},
},
],
};

export default config
export default config;
Loading