|
1 | | -import type { CloudFrontRequestEvent } from 'aws-lambda'; |
| 1 | +import type { CloudFrontRequest, CloudFrontRequestEvent } from 'aws-lambda'; |
2 | 2 | import { mock } from 'jest-mock-extended'; |
3 | 3 | import { logger } from 'nhs-notify-web-template-management-utils/logger'; |
4 | | -import { handler } from '../index'; |
| 4 | +import { denial, handler, parseRequest } from '../index'; |
5 | 5 | import { LambdaCognitoAuthorizer } from 'nhs-notify-web-template-management-utils/lambda-cognito-authorizer'; |
6 | 6 | import { CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider'; |
7 | 7 |
|
@@ -85,4 +85,72 @@ describe('download authorizer handler', () => { |
85 | 85 | subject |
86 | 86 | ); |
87 | 87 | }); |
| 88 | + |
| 89 | + test('returns denial if cognito configuration is not present in custom headers', async () => { |
| 90 | + const uri = '/subject/template-id/proof1.pdf'; |
| 91 | + const cookie = |
| 92 | + 'CognitoIdentityServiceProvider.user-pool-client-id.subject.AccessToken=jwt'; |
| 93 | + |
| 94 | + const event = mock<CloudFrontRequestEvent>( |
| 95 | + makeEvent(uri, cookie, { |
| 96 | + 'x-user-pool-id': undefined, |
| 97 | + 'x-user-pool-client-id': undefined, |
| 98 | + }) |
| 99 | + ); |
| 100 | + |
| 101 | + const res = await handler(event); |
| 102 | + |
| 103 | + expect(res).toEqual(denial); |
| 104 | + expect(mockLogger.error).toHaveBeenCalledWith('Lambda misconfiguration'); |
| 105 | + |
| 106 | + expect(lambdaCognitoAuthorizer.authorize).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + test('returns denial if required cookie is not available', async () => { |
| 110 | + const uri = '/subject/template-id/proof1.pdf'; |
| 111 | + const cookie = 'k=v; k2=v2'; |
| 112 | + |
| 113 | + const event = mock<CloudFrontRequestEvent>(makeEvent(uri, cookie)); |
| 114 | + |
| 115 | + const res = await handler(event); |
| 116 | + |
| 117 | + expect(res).toEqual(denial); |
| 118 | + expect(mockLogger.warn).toHaveBeenCalledWith('Cookie is missing'); |
| 119 | + |
| 120 | + expect(lambdaCognitoAuthorizer.authorize).not.toHaveBeenCalled(); |
| 121 | + }); |
| 122 | + |
| 123 | + test('returns denial if authorization fails', async () => { |
| 124 | + const uri = '/subject/template-id/proof1.pdf'; |
| 125 | + const cookie = `CognitoIdentityServiceProvider.${userPoolClientId}.subject.AccessToken=jwt`; |
| 126 | + |
| 127 | + lambdaCognitoAuthorizer.authorize.mockResolvedValue({ |
| 128 | + success: false, |
| 129 | + }); |
| 130 | + |
| 131 | + const event = mock<CloudFrontRequestEvent>(makeEvent(uri, cookie)); |
| 132 | + |
| 133 | + const res = await handler(event); |
| 134 | + |
| 135 | + expect(res).toEqual(denial); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('parseRequest', () => { |
| 140 | + test('path defaults to empty string if owner segment cant be extracted', () => { |
| 141 | + const uri = ''; |
| 142 | + const request = mock<CloudFrontRequest>( |
| 143 | + makeEvent(uri, 'cookie').Records[0].cf.request |
| 144 | + ); |
| 145 | + |
| 146 | + expect(parseRequest(request).ownerPathComponent).toBe(''); |
| 147 | + }); |
| 148 | + |
| 149 | + test('cookie header defaults to empty string if it is not present on request', () => { |
| 150 | + const request = mock<CloudFrontRequest>( |
| 151 | + makeEvent('/subject/file.txt', undefined).Records[0].cf.request |
| 152 | + ); |
| 153 | + |
| 154 | + expect(parseRequest(request).authorizationToken).toBe(undefined); |
| 155 | + }); |
88 | 156 | }); |
0 commit comments