generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcount-routing-configs.test.ts
More file actions
117 lines (95 loc) · 3.21 KB
/
count-routing-configs.test.ts
File metadata and controls
117 lines (95 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { APIGatewayProxyEvent, Context } from 'aws-lambda';
import { mock } from 'jest-mock-extended';
import type { Logger } from 'nhs-notify-web-template-management-utils/logger';
import { createHandler } from '@backend-api/templates/api/count-routing-configs';
import { RoutingConfigClient } from '@backend-api/templates/app/routing-config-client';
jest.mock('nhs-notify-web-template-management-utils/logger', () => ({
logger: mock<Logger>({
child: jest.fn().mockReturnThis(),
}),
}));
const setup = () => {
const routingConfigClient = mock<RoutingConfigClient>();
const handler = createHandler({ routingConfigClient });
return { handler, mocks: { routingConfigClient } };
};
describe('CountRoutingConfigs handler', () => {
test.each([
['undefined', undefined],
['missing user', { clientId: 'client-id', user: undefined }],
['missing client', { clientId: undefined, user: 'user-id' }],
])(
'should return 400 - Invalid request when requestContext is %s',
async (_, ctx) => {
const { handler, mocks } = setup();
const event = mock<APIGatewayProxyEvent>({
requestContext: { authorizer: ctx },
});
const result = await handler(event, mock<Context>(), jest.fn());
expect(result).toEqual({
statusCode: 400,
body: JSON.stringify({
statusCode: 400,
technicalMessage: 'Invalid request',
}),
});
expect(
mocks.routingConfigClient.countRoutingConfigs
).not.toHaveBeenCalled();
}
);
test('should return error when counting routing configs fails', async () => {
const { handler, mocks } = setup();
mocks.routingConfigClient.countRoutingConfigs.mockResolvedValueOnce({
error: {
errorMeta: {
code: 500,
description: 'Internal server error',
},
},
});
const event = mock<APIGatewayProxyEvent>();
event.requestContext.authorizer = {
user: 'sub',
clientId: 'nhs-notify-client-id',
};
event.queryStringParameters = {
status: 'DRAFT',
};
const result = await handler(event, mock<Context>(), jest.fn());
expect(result).toEqual({
statusCode: 500,
body: JSON.stringify({
statusCode: 500,
technicalMessage: 'Internal server error',
}),
});
expect(mocks.routingConfigClient.countRoutingConfigs).toHaveBeenCalledWith(
'nhs-notify-client-id',
{ status: 'DRAFT' }
);
});
test('should return count of routing configs', async () => {
const { handler, mocks } = setup();
mocks.routingConfigClient.countRoutingConfigs.mockResolvedValueOnce({
data: { count: 99 },
});
const event = mock<APIGatewayProxyEvent>();
event.requestContext.authorizer = {
user: 'sub',
clientId: 'nhs-notify-client-id',
};
event.queryStringParameters = {
status: 'COMPLETED',
};
const result = await handler(event, mock<Context>(), jest.fn());
expect(result).toEqual({
statusCode: 200,
body: JSON.stringify({ statusCode: 200, data: { count: 99 } }),
});
expect(mocks.routingConfigClient.countRoutingConfigs).toHaveBeenCalledWith(
'nhs-notify-client-id',
{ status: 'COMPLETED' }
);
});
});