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

Use router module in user-benefits API handler #2601

Closed
wants to merge 1 commit into from
Closed
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
78 changes: 38 additions & 40 deletions handlers/user-benefits/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { ValidationError } from '@modules/errors';
import { buildAuthenticate } from '@modules/identity/apiGateway';
import type { IdentityUserDetails } from '@modules/identity/identity';
import { Lazy } from '@modules/lazy';
import type { UserBenefitsResponse } from '@modules/product-benefits/schemas';
import { getUserBenefits } from '@modules/product-benefits/userBenefits';
import { getProductCatalogFromApi } from '@modules/product-catalog/api';
import { ProductCatalogHelper } from '@modules/product-catalog/productCatalog';
import { Router } from '@modules/routing/router';
import type { Stage } from '@modules/stage';
import type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
import type {
APIGatewayProxyEvent,
APIGatewayProxyResult,
Handler,
} from 'aws-lambda';
import { getTrialInformation } from './trials';

const stage = process.env.STAGE as Stage;
Expand Down Expand Up @@ -36,47 +40,41 @@ const getUserBenefitsResponse = async (
};
};

export const handler = async (
export const userBenefitsHandler = async (
event: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> => {
console.log(`Input is ${JSON.stringify(event)}`);
if (!(event.path === '/benefits/me' && event.httpMethod === 'GET')) {
return {
body: 'Not Found',
statusCode: 404,
};
const maybeAuthenticatedEvent = await authenticate(event);

if (maybeAuthenticatedEvent.type === 'failure') {
return maybeAuthenticatedEvent.response;
}
try {
const maybeAuthenticatedEvent = await authenticate(event);

if (maybeAuthenticatedEvent.type === 'failure') {
return maybeAuthenticatedEvent.response;
}
const userBenefitsResponse = await getUserBenefitsResponse(
stage,
new ProductCatalogHelper(await productCatalog.get()),
maybeAuthenticatedEvent.userDetails,
);
return {
body: JSON.stringify(userBenefitsResponse),
// https://www.fastly.com/documentation/guides/concepts/edge-state/cache/cache-freshness/#preventing-content-from-being-cached
headers: {
'Cache-Control': 'private, no-store',
},
statusCode: 200,
};
};

const router = new Router([
{
httpMethod: 'GET',
path: '/benefits/me',
handler: userBenefitsHandler,
},
]);

const userBenefitsResponse = await getUserBenefitsResponse(
stage,
new ProductCatalogHelper(await productCatalog.get()),
maybeAuthenticatedEvent.userDetails,
);
return {
body: JSON.stringify(userBenefitsResponse),
// https://www.fastly.com/documentation/guides/concepts/edge-state/cache/cache-freshness/#preventing-content-from-being-cached
headers: {
'Cache-Control': 'private, no-store',
},
statusCode: 200,
};
} catch (error) {
console.log('Caught exception with message: ', error);
if (error instanceof ValidationError) {
return {
body: error.message,
statusCode: 400,
};
}
return {
body: 'Internal server error',
statusCode: 500,
};
}
export const handler: Handler = async (
event: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> => {
console.log(`Input is ${JSON.stringify(event)}`);
return router.routeRequest(event);
};
36 changes: 11 additions & 25 deletions handlers/user-benefits/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { UserBenefitsResponse } from '@modules/product-benefits/schemas';
import type { APIGatewayProxyEvent } from 'aws-lambda';
import { handler } from '../src/index';
import { userBenefitsHandler } from '../src/index';

jest.mock('@modules/identity/apiGateway', () => ({
buildAuthenticate: () => (event: APIGatewayProxyEvent) => {
Expand All @@ -27,34 +27,20 @@
getUserBenefits: () => ['adFree'],
}));

describe('handler', () => {
it('returns a 404 for an unrecognized path or HTTP method', async () => {
describe('userBenefitsHandler', () => {
it('returns a 200 with user benefits', async () => {
const requestEvent = {
path: '/bad/path',
path: '/benefits/me',
httpMethod: 'GET',
headers: {},
headers: {
Authorization: 'Bearer good-token',
Dismissed Show dismissed Hide dismissed
},
} as unknown as APIGatewayProxyEvent;

const response = await handler(requestEvent);
const response = await userBenefitsHandler(requestEvent);

expect(response.statusCode).toEqual(404);
});

describe('/benefits/me', () => {
it('returns a 200 with user benefits', async () => {
const requestEvent = {
path: '/benefits/me',
httpMethod: 'GET',
headers: {
Authorization: 'Bearer good-token',
},
} as unknown as APIGatewayProxyEvent;

const response = await handler(requestEvent);

expect(response.statusCode).toEqual(200);
const parsedBody = JSON.parse(response.body) as UserBenefitsResponse;
expect(parsedBody.benefits).toEqual(['adFree']);
});
expect(response.statusCode).toEqual(200);
const parsedBody = JSON.parse(response.body) as UserBenefitsResponse;
expect(parsedBody.benefits).toEqual(['adFree']);
});
});
Loading