Skip to content

Commit 0cdb47d

Browse files
committed
client test
1 parent 0f6b861 commit 0cdb47d

File tree

5 files changed

+218
-11
lines changed

5 files changed

+218
-11
lines changed

lambdas/backend-api/src/__tests__/templates/api/get-routing-config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('GetRoutingConfig handler', () => {
106106

107107
expect(mocks.routingConfigClient.getRoutingConfig).toHaveBeenCalledWith(
108108
'3690d344-731f-4f60-9047-2c63c96623a2',
109-
'nhs-notify-client-id'
109+
{ userId: 'sub', clientId: 'nhs-notify-client-id' }
110110
);
111111
});
112112

@@ -135,7 +135,7 @@ describe('GetRoutingConfig handler', () => {
135135

136136
expect(mocks.routingConfigClient.getRoutingConfig).toHaveBeenCalledWith(
137137
'3690d344-731f-4f60-9047-2c63c96623a2',
138-
'nhs-notify-client-id'
138+
{ userId: 'sub', clientId: 'nhs-notify-client-id' }
139139
);
140140
});
141141
});

lambdas/backend-api/src/__tests__/templates/app/routing-config-client.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import type { RoutingConfigRepository } from '@backend-api/templates/infra/routi
33
import { RoutingConfigClient } from '@backend-api/templates/app/routing-config-client';
44
import { routingConfig } from '../fixtures/routing-config';
55
import { createMockLogger } from 'nhs-notify-web-template-management-test-helper-utils/mock-logger';
6+
import {
7+
CreateUpdateRoutingConfig,
8+
RoutingConfig,
9+
} from 'nhs-notify-backend-client';
10+
import { ZodError } from 'zod/v4';
611

712
const user = { userId: 'userid', clientId: 'nhs-notify-client-id' };
813

@@ -94,4 +99,128 @@ describe('RoutingConfigClient', () => {
9499
);
95100
});
96101
});
102+
103+
describe('createRoutingConfig', () => {
104+
test('returns created routing config', async () => {
105+
const { client, mocks } = setup();
106+
107+
const date = new Date();
108+
109+
const input: CreateUpdateRoutingConfig = {
110+
name: 'rc',
111+
campaignId: 'campaign',
112+
cascade: [
113+
{
114+
cascadeGroups: ['standard'],
115+
channel: 'SMS',
116+
channelType: 'primary',
117+
defaultTemplateId: 'sms',
118+
},
119+
],
120+
cascadeGroupOverrides: [{ name: 'standard' }],
121+
};
122+
123+
const rc: RoutingConfig = {
124+
...input,
125+
clientId: user.clientId,
126+
createdAt: date.toISOString(),
127+
id: 'id',
128+
status: 'DRAFT',
129+
updatedAt: date.toISOString(),
130+
};
131+
132+
mocks.routingConfigRepository.create.mockResolvedValueOnce({
133+
data: rc,
134+
});
135+
136+
const result = await client.createRoutingConfig(input, user);
137+
138+
expect(mocks.routingConfigRepository.create).toHaveBeenCalledWith(
139+
input,
140+
user
141+
);
142+
143+
expect(result).toEqual({
144+
data: rc,
145+
});
146+
});
147+
148+
test('returns 400 error when input is invalid', async () => {
149+
const { client, mocks } = setup();
150+
151+
const result = await client.createRoutingConfig(
152+
{ a: 1 } as unknown as CreateUpdateRoutingConfig,
153+
user
154+
);
155+
156+
expect(mocks.routingConfigRepository.create).not.toHaveBeenCalled();
157+
158+
expect(result).toEqual({
159+
error: {
160+
actualError: {
161+
fieldErrors: {
162+
campaignId: [
163+
'Invalid input: expected string, received undefined',
164+
],
165+
cascade: ['Invalid input: expected array, received undefined'],
166+
cascadeGroupOverrides: [
167+
'Invalid input: expected array, received undefined',
168+
],
169+
name: ['Invalid input: expected string, received undefined'],
170+
},
171+
formErrors: [],
172+
},
173+
errorMeta: {
174+
code: 400,
175+
description: 'Request failed validation',
176+
details: {
177+
campaignId: 'Invalid input: expected string, received undefined',
178+
cascade: 'Invalid input: expected array, received undefined',
179+
cascadeGroupOverrides:
180+
'Invalid input: expected array, received undefined',
181+
name: 'Invalid input: expected string, received undefined',
182+
},
183+
},
184+
},
185+
});
186+
});
187+
188+
test('returns failures from the repository', async () => {
189+
const { client, mocks } = setup();
190+
191+
const input: CreateUpdateRoutingConfig = {
192+
name: 'rc',
193+
campaignId: 'campaign',
194+
cascade: [
195+
{
196+
cascadeGroups: ['standard'],
197+
channel: 'SMS',
198+
channelType: 'primary',
199+
defaultTemplateId: 'sms',
200+
},
201+
],
202+
cascadeGroupOverrides: [{ name: 'standard' }],
203+
};
204+
205+
mocks.routingConfigRepository.create.mockResolvedValueOnce({
206+
error: { errorMeta: { code: 500, description: 'ddb err' } },
207+
});
208+
209+
const result = await client.createRoutingConfig(input, user);
210+
211+
expect(mocks.routingConfigRepository.create).toHaveBeenCalledWith(
212+
input,
213+
user
214+
);
215+
216+
expect(result).toEqual({
217+
error: {
218+
errorMeta: {
219+
code: 500,
220+
description: 'ddb err',
221+
},
222+
},
223+
});
224+
});
225+
});
97226
});

lambdas/backend-api/src/__tests__/templates/infra/routing-config-repository.test.ts

Lines changed: 84 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
1-
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
1+
import {
2+
DynamoDBDocumentClient,
3+
GetCommand,
4+
PutCommand,
5+
} from '@aws-sdk/lib-dynamodb';
26
import 'aws-sdk-client-mock-jest';
37
import { mockClient } from 'aws-sdk-client-mock';
48
import { RoutingConfigRepository } from '@backend-api/templates/infra/routing-config-repository';
59
import { routingConfig } from '../fixtures/routing-config';
10+
import {
11+
CreateUpdateRoutingConfig,
12+
RoutingConfig,
13+
} from 'nhs-notify-backend-client';
14+
15+
jest.mock('node:crypto', () => ({ randomUUID: () => 'id' }));
16+
17+
const date = new Date(2024, 11, 27);
18+
19+
beforeAll(() => {
20+
jest.useFakeTimers();
21+
jest.setSystemTime(date);
22+
});
623

724
const TABLE_NAME = 'routing-config-table-name';
825
const user = { userId: 'user', clientId: 'nhs-notify-client-id' };
@@ -40,7 +57,7 @@ describe('RoutingConfigRepository', () => {
4057
TableName: TABLE_NAME,
4158
Key: {
4259
id: 'b9b6d56b-421e-462f-9ce5-3012e3fdb27f',
43-
owner: 'CLIENT#nhs-notify-client-id',
60+
owner: `CLIENT#${user.clientId}`,
4461
},
4562
});
4663
});
@@ -97,9 +114,70 @@ describe('RoutingConfigRepository', () => {
97114
});
98115
});
99116

100-
// describe('create', () => {
101-
// test('should ', async () => {
117+
describe('create', () => {
118+
const input: CreateUpdateRoutingConfig = {
119+
name: 'rc',
120+
campaignId: 'campaign',
121+
cascade: [
122+
{
123+
cascadeGroups: ['standard'],
124+
channel: 'SMS',
125+
channelType: 'primary',
126+
defaultTemplateId: 'sms',
127+
},
128+
],
129+
cascadeGroupOverrides: [{ name: 'standard' }],
130+
};
131+
132+
const rc: RoutingConfig = {
133+
...input,
134+
clientId: user.clientId,
135+
createdAt: date.toISOString(),
136+
id: 'id',
137+
status: 'DRAFT',
138+
updatedAt: date.toISOString(),
139+
};
140+
141+
const putPayload = {
142+
...rc,
143+
owner: `CLIENT#${user.clientId}`,
144+
createdBY: user.userId,
145+
updatedBy: user.userId,
146+
};
147+
148+
test('should create routing config', async () => {
149+
const { repo, mocks } = setup();
150+
151+
mocks.dynamo
152+
.on(PutCommand, {
153+
TableName: TABLE_NAME,
154+
Item: putPayload,
155+
})
156+
.resolves({});
157+
158+
const result = await repo.create(input, user);
159+
160+
expect(result).toEqual({ data: rc });
161+
});
162+
163+
test('returns failure if put fails', async () => {
164+
const { repo, mocks } = setup();
165+
166+
const err = new Error('ddb_err');
102167

103-
// });
104-
// });
168+
mocks.dynamo.on(PutCommand).rejects(err);
169+
170+
const result = await repo.create(input, user);
171+
172+
expect(result).toEqual({
173+
error: {
174+
actualError: err,
175+
errorMeta: {
176+
code: 500,
177+
description: 'Failed to create routing config',
178+
},
179+
},
180+
});
181+
});
182+
});
105183
});

lambdas/backend-api/src/templates/api/get-routing-config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function createHandler({
2525

2626
const { data, error } = await routingConfigClient.getRoutingConfig(
2727
routingConfigId,
28-
clientId
28+
{ clientId, userId }
2929
);
3030

3131
if (error) {

lambdas/backend-api/src/templates/app/routing-config-client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ export class RoutingConfigClient {
3636
return validationResult;
3737
}
3838

39-
const createResult = (await this.routingConfigRepository.create(
39+
const createResult = await this.routingConfigRepository.create(
4040
validationResult.data,
4141
user
42-
)) as Result<RoutingConfig>;
42+
);
4343

4444
if (createResult.error) {
4545
log

0 commit comments

Comments
 (0)