Skip to content

Commit 02cd3ff

Browse files
committed
api tests using user with no client
1 parent 0c8f273 commit 02cd3ff

File tree

3 files changed

+138
-8
lines changed

3 files changed

+138
-8
lines changed

tests/test-team/helpers/auth/cognito-auth-helper.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ import {
1111
import { faker } from '@faker-js/faker';
1212
import { CredentialsFile } from './credentials-file';
1313

14+
type TestUserStaticDetails = {
15+
userId: string;
16+
clientId: string | undefined;
17+
};
18+
19+
type TestUserDetails = TestUserStaticDetails & {
20+
email: string;
21+
};
22+
1423
export type TestUserCredential = {
15-
user: { email: string; userId: string; clientId: string | undefined };
24+
user: TestUserDetails;
1625
password: string;
1726
accessToken: string;
1827
refreshToken: string;
1928
};
2029

21-
type TestUserStaticDetails = {
22-
userId: string;
23-
clientId: string | undefined;
24-
};
25-
2630
export const testUsers = {
2731
/**
2832
* User1 is generally the signed in user
@@ -70,8 +74,7 @@ export const testUsers = {
7074

7175
type TestUserId = keyof typeof testUsers;
7276

73-
export type TestUser = TestUserStaticDetails & {
74-
email: string;
77+
export type TestUser = TestUserDetails & {
7578
password: string;
7679
/**
7780
* Gets an access token for a test user

tests/test-team/template-mgmt-api-tests/create-letter-template.api.spec.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ test.describe('POST /v1/letter-template', () => {
1616
const authHelper = createAuthHelper();
1717
const templateStorageHelper = new TemplateStorageHelper();
1818
let user1: TestUser;
19+
let user6: TestUser;
1920

2021
test.beforeAll(async () => {
2122
user1 = await authHelper.getTestUser(testUsers.User1.userId);
23+
user6 = await authHelper.getTestUser(testUsers.User6.userId);
2224
});
2325

2426
test.afterAll(async () => {
@@ -189,6 +191,81 @@ test.describe('POST /v1/letter-template', () => {
189191
expect(result.template.createdAt).not.toEqual(result.template.updatedAt);
190192
});
191193

194+
test('user without a clientId assigned can create a template', async ({
195+
request,
196+
}) => {
197+
const { templateData, multipart, contentType } =
198+
TemplateAPIPayloadFactory.getCreateLetterTemplatePayload(
199+
{
200+
templateType: 'LETTER',
201+
},
202+
[
203+
{
204+
_type: 'json',
205+
partName: 'template',
206+
},
207+
{
208+
_type: 'file',
209+
partName: 'letterPdf',
210+
fileName: 'template.pdf',
211+
fileType: 'application/pdf',
212+
file: pdfUploadFixtures.noCustomPersonalisation.pdf.open(),
213+
},
214+
]
215+
);
216+
217+
const start = new Date();
218+
219+
const response = await request.post(
220+
`${process.env.API_BASE_URL}/v1/letter-template`,
221+
{
222+
data: multipart,
223+
headers: {
224+
Authorization: await user6.getAccessToken(),
225+
'Content-Type': contentType,
226+
},
227+
}
228+
);
229+
230+
const result = await response.json();
231+
const debug = JSON.stringify(result, null, 2);
232+
233+
expect(response.status(), debug).toBe(201);
234+
235+
templateStorageHelper.addAdHocTemplateKey({
236+
id: result.template.id,
237+
owner: user1.userId,
238+
});
239+
240+
expect(result).toEqual({
241+
statusCode: 201,
242+
template: {
243+
createdAt: expect.stringMatching(isoDateRegExp),
244+
id: expect.stringMatching(uuidRegExp),
245+
name: templateData.name,
246+
language: 'en',
247+
letterType: 'x0',
248+
templateStatus: 'PENDING_VALIDATION',
249+
templateType: templateData.templateType,
250+
updatedAt: expect.stringMatching(isoDateRegExp),
251+
files: {
252+
pdfTemplate: {
253+
currentVersion: expect.stringMatching(uuidRegExp),
254+
fileName: 'template.pdf',
255+
virusScanStatus: 'PENDING',
256+
},
257+
proofs: {},
258+
},
259+
},
260+
});
261+
262+
expect(result.template.createdAt).toBeDateRoughlyBetween([
263+
start,
264+
new Date(),
265+
]);
266+
expect(result.template.createdAt).not.toEqual(result.template.updatedAt);
267+
});
268+
192269
test('returns 401 if no auth token', async ({ request }) => {
193270
const response = await request.post(
194271
`${process.env.API_BASE_URL}/v1/letter-template`

tests/test-team/template-mgmt-api-tests/create-template.api.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ test.describe('POST /v1/template', () => {
1515
const authHelper = createAuthHelper();
1616
const templateStorageHelper = new TemplateStorageHelper();
1717
let user1: TestUser;
18+
let user6: TestUser;
1819

1920
test.beforeAll(async () => {
2021
user1 = await authHelper.getTestUser(testUsers.User1.userId);
22+
user6 = await authHelper.getTestUser(testUsers.User6.userId);
2123
});
2224

2325
test.afterAll(async () => {
@@ -157,6 +159,54 @@ test.describe('POST /v1/template', () => {
157159
expect(created.template.createdAt).toEqual(created.template.updatedAt);
158160
});
159161

162+
test('user without a clientId assigned can create a template', async ({
163+
request,
164+
}) => {
165+
const template = TemplateAPIPayloadFactory.getCreateTemplatePayload({
166+
templateType: 'NHS_APP',
167+
});
168+
169+
const start = new Date();
170+
171+
const response = await request.post(
172+
`${process.env.API_BASE_URL}/v1/template`,
173+
{
174+
headers: {
175+
Authorization: await user6.getAccessToken(),
176+
},
177+
data: template,
178+
}
179+
);
180+
181+
expect(response.status()).toBe(201);
182+
183+
const created = await response.json();
184+
185+
templateStorageHelper.addAdHocTemplateKey({
186+
id: created.template.id,
187+
owner: user1.userId,
188+
});
189+
190+
expect(created).toEqual({
191+
statusCode: 201,
192+
template: {
193+
createdAt: expect.stringMatching(isoDateRegExp),
194+
id: expect.stringMatching(uuidRegExp),
195+
message: template.message,
196+
name: template.name,
197+
templateStatus: 'NOT_YET_SUBMITTED',
198+
templateType: template.templateType,
199+
updatedAt: expect.stringMatching(isoDateRegExp),
200+
},
201+
});
202+
203+
expect(created.template.createdAt).toBeDateRoughlyBetween([
204+
start,
205+
new Date(),
206+
]);
207+
expect(created.template.createdAt).toEqual(created.template.updatedAt);
208+
});
209+
160210
test('ignores template status if given - template cannot be submitted at create time', async ({
161211
request,
162212
}) => {

0 commit comments

Comments
 (0)