Skip to content

Commit 3ba4acc

Browse files
committed
add us-east provider
1 parent b352062 commit 3ba4acc

File tree

3 files changed

+47
-51
lines changed

3 files changed

+47
-51
lines changed

infrastructure/terraform/components/app/module_download_authorizer_lambda.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ module "download_authorizer_lambda" {
1212
log_retention_in_days = var.log_retention_in_days
1313
# source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/lambda?ref=v2.0.2"
1414

15-
# providers = {
16-
# aws = aws.us-east-1
17-
# }
15+
providers = {
16+
aws = aws.us-east-1
17+
}
1818

1919
# function_name = "download-authorizer"
2020
# description = "Download authorizer for s3 download bucket"

lambdas/authorizer/src/__tests__/index.test.ts

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mock } from 'jest-mock-extended';
33
import { logger } from 'nhs-notify-web-template-management-utils/logger';
44
import { handler } from '../index';
55
import { LambdaCognitoAuthorizer } from 'nhs-notify-web-template-management-utils/lambda-cognito-authorizer';
6+
import { CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider';
67

78
const requestContext = {
89
accountId: '000000000000',
@@ -12,15 +13,21 @@ const requestContext = {
1213

1314
const methodArn = 'arn:aws:execute-api:eu-west-2:000000000000:api-id/stage/*';
1415

15-
const warnMock = jest.spyOn(logger, 'warn');
16-
const errorMock = jest.spyOn(logger, 'error');
16+
jest.mock('nhs-notify-web-template-management-utils/logger');
17+
const mockLogger = jest.mocked(logger);
1718

1819
jest.mock('nhs-notify-web-template-management-utils/lambda-cognito-authorizer');
19-
2020
const lambdaCognitoAuthorizer = mock<LambdaCognitoAuthorizer>();
21+
jest
22+
.mocked(LambdaCognitoAuthorizer)
23+
.mockImplementation(() => lambdaCognitoAuthorizer);
24+
25+
jest.mock('@aws-sdk/client-cognito-identity-provider');
26+
const cognitoClientMock = mock<CognitoIdentityProviderClient>();
2127

22-
const authorizerConstructorMock = jest.mocked(LambdaCognitoAuthorizer);
23-
// .mockReturnValue(lambdaCognitoAuthorizer);
28+
jest
29+
.mocked(CognitoIdentityProviderClient)
30+
.mockImplementation(() => cognitoClientMock);
2431

2532
const allowPolicy = {
2633
principalId: 'api-caller',
@@ -56,7 +63,7 @@ const denyPolicy = {
5663
const originalEnv = { ...process.env };
5764

5865
beforeEach(() => {
59-
jest.resetAllMocks();
66+
jest.clearAllMocks();
6067
process.env.USER_POOL_ID = 'user-pool-id';
6168
process.env.USER_POOL_CLIENT_ID = 'user-pool-client-id';
6269
});
@@ -65,6 +72,33 @@ afterEach(() => {
6572
process.env = originalEnv;
6673
});
6774

75+
test('returns Allow policy on valid token', async () => {
76+
lambdaCognitoAuthorizer.authorize.mockResolvedValue({
77+
success: true,
78+
subject: 'sub',
79+
});
80+
81+
const res = await handler(
82+
mock<APIGatewayRequestAuthorizerEvent>({
83+
requestContext,
84+
headers: { Authorization: 'jwt' },
85+
type: 'REQUEST',
86+
}),
87+
mock<Context>(),
88+
jest.fn()
89+
);
90+
91+
expect(res).toEqual(allowPolicy);
92+
expect(mockLogger.warn).not.toHaveBeenCalled();
93+
expect(mockLogger.error).not.toHaveBeenCalled();
94+
95+
expect(lambdaCognitoAuthorizer.authorize).toHaveBeenCalledWith(
96+
'user-pool-id',
97+
'user-pool-client-id',
98+
'jwt'
99+
);
100+
});
101+
68102
test('returns Deny policy on lambda misconfiguration', async () => {
69103
process.env.USER_POOL_ID = '';
70104

@@ -79,7 +113,7 @@ test('returns Deny policy on lambda misconfiguration', async () => {
79113
);
80114

81115
expect(res).toEqual(denyPolicy);
82-
expect(errorMock).toHaveBeenCalledWith('Lambda misconfiguration');
116+
expect(mockLogger.error).toHaveBeenCalledWith('Lambda misconfiguration');
83117
});
84118

85119
test('returns Deny policy if no Authorization token in header', async () => {
@@ -96,10 +130,9 @@ test('returns Deny policy if no Authorization token in header', async () => {
96130
expect(res).toEqual(denyPolicy);
97131
});
98132

99-
test.only('returns Allow policy on valid token', async () => {
133+
test('returns Deny policy when authorization fails', async () => {
100134
lambdaCognitoAuthorizer.authorize.mockResolvedValue({
101-
success: true,
102-
subject: 'sub',
135+
success: false,
103136
});
104137

105138
const res = await handler(
@@ -112,39 +145,5 @@ test.only('returns Allow policy on valid token', async () => {
112145
jest.fn()
113146
);
114147

115-
expect(res).toEqual(allowPolicy);
116-
expect(warnMock).not.toHaveBeenCalled();
117-
expect(errorMock).not.toHaveBeenCalled();
148+
expect(res).toEqual(denyPolicy);
118149
});
119-
120-
// test('returns Deny policy on expired token', async () => {
121-
// const jwt = sign(
122-
// {
123-
// token_use: 'access',
124-
// client_id: 'user-pool-client-id',
125-
// iss: 'https://cognito-idp.eu-west-2.amazonaws.com/user-pool-id',
126-
// exp: 1_640_995_200,
127-
// },
128-
// 'key',
129-
// {
130-
// keyid: 'key-id',
131-
// }
132-
// );
133-
134-
// const res = await handler(
135-
// mock<APIGatewayRequestAuthorizerEvent>({
136-
// requestContext,
137-
// headers: { Authorization: jwt },
138-
// type: 'REQUEST',
139-
// }),
140-
// mock<Context>(),
141-
// jest.fn()
142-
// );
143-
144-
// expect(res).toEqual(denyPolicy);
145-
// expect(errorMock).toHaveBeenCalledWith(
146-
// expect.objectContaining({
147-
// message: 'jwt expired',
148-
// })
149-
// );
150-
// });

lambdas/authorizer/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ export const handler: APIGatewayRequestAuthorizerHandler = async ({
6060
logger
6161
);
6262

63-
console.log(lambdaCognitoAuthorizer);
64-
65-
6663
const authResult = await lambdaCognitoAuthorizer.authorize(
6764
userPoolId,
6865
userPoolClientId,

0 commit comments

Comments
 (0)