|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { |
| 3 | + createAuthHelper, |
| 4 | + type TestUser, |
| 5 | + testUsers, |
| 6 | +} from '../helpers/auth/cognito-auth-helper'; |
| 7 | +import { TemplateStorageHelper } from '../helpers/db/template-storage-helper'; |
| 8 | +import { |
| 9 | + isoDateRegExp, |
| 10 | + uuidRegExp, |
| 11 | +} from 'nhs-notify-web-template-management-test-helper-utils'; |
| 12 | +import { TemplateAPIPayloadFactory } from '../helpers/factories/template-api-payload-factory'; |
| 13 | +import { testClients } from '../helpers/client/client-helper'; |
| 14 | + |
| 15 | +test.describe('POST /v1/template', () => { |
| 16 | + const authHelper = createAuthHelper(); |
| 17 | + const templateStorageHelper = new TemplateStorageHelper(); |
| 18 | + let user1: TestUser; |
| 19 | + |
| 20 | + test.beforeAll(async () => { |
| 21 | + user1 = await authHelper.getTestUser(testUsers.User1.userId); |
| 22 | + }); |
| 23 | + |
| 24 | + test.afterAll(async () => { |
| 25 | + await templateStorageHelper.deleteAdHocTemplates(); |
| 26 | + }); |
| 27 | + |
| 28 | + test.describe('NHS_APP templates', () => { |
| 29 | + test('returns 201 if template is valid', async ({ request }) => { |
| 30 | + const template = TemplateAPIPayloadFactory.getCreateTemplatePayload({ |
| 31 | + templateType: 'NHS_APP', |
| 32 | + }); |
| 33 | + |
| 34 | + const start = new Date(); |
| 35 | + |
| 36 | + const response = await request.post( |
| 37 | + `${process.env.API_BASE_URL}/v1/template`, |
| 38 | + { |
| 39 | + headers: { |
| 40 | + Authorization: await user1.getAccessToken(), |
| 41 | + }, |
| 42 | + data: template, |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + expect(response.status()).toBe(201); |
| 47 | + |
| 48 | + const created = await response.json(); |
| 49 | + |
| 50 | + templateStorageHelper.addAdHocTemplateKey({ |
| 51 | + id: created.template.id, |
| 52 | + owner: user1.userId, |
| 53 | + }); |
| 54 | + |
| 55 | + expect(created).toEqual({ |
| 56 | + statusCode: 201, |
| 57 | + template: { |
| 58 | + campaignId: testClients[user1.clientKey]?.campaignId, |
| 59 | + createdAt: expect.stringMatching(isoDateRegExp), |
| 60 | + id: expect.stringMatching(uuidRegExp), |
| 61 | + message: template.message, |
| 62 | + name: template.name, |
| 63 | + templateStatus: 'NOT_YET_SUBMITTED', |
| 64 | + templateType: template.templateType, |
| 65 | + updatedAt: expect.stringMatching(isoDateRegExp), |
| 66 | + }, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(created.template.createdAt).toBeDateRoughlyBetween([ |
| 70 | + start, |
| 71 | + new Date(), |
| 72 | + ]); |
| 73 | + expect(created.template.createdAt).toEqual(created.template.updatedAt); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + test.describe('SMS templates', () => { |
| 78 | + test('returns 201 if template is valid', async ({ request }) => { |
| 79 | + const template = TemplateAPIPayloadFactory.getCreateTemplatePayload({ |
| 80 | + templateType: 'SMS', |
| 81 | + }); |
| 82 | + |
| 83 | + const start = new Date(); |
| 84 | + |
| 85 | + const response = await request.post( |
| 86 | + `${process.env.API_BASE_URL}/v1/template`, |
| 87 | + { |
| 88 | + headers: { |
| 89 | + Authorization: await user1.getAccessToken(), |
| 90 | + }, |
| 91 | + data: template, |
| 92 | + } |
| 93 | + ); |
| 94 | + |
| 95 | + expect(response.status()).toBe(201); |
| 96 | + |
| 97 | + const created = await response.json(); |
| 98 | + |
| 99 | + templateStorageHelper.addAdHocTemplateKey({ |
| 100 | + id: created.template.id, |
| 101 | + owner: user1.userId, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(created).toEqual({ |
| 105 | + statusCode: 201, |
| 106 | + template: { |
| 107 | + campaignId: testClients[user1.clientKey]?.campaignId, |
| 108 | + createdAt: expect.stringMatching(isoDateRegExp), |
| 109 | + id: expect.stringMatching(uuidRegExp), |
| 110 | + message: template.message, |
| 111 | + name: template.name, |
| 112 | + templateStatus: 'NOT_YET_SUBMITTED', |
| 113 | + templateType: template.templateType, |
| 114 | + updatedAt: expect.stringMatching(isoDateRegExp), |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + expect(created.template.createdAt).toBeDateRoughlyBetween([ |
| 119 | + start, |
| 120 | + new Date(), |
| 121 | + ]); |
| 122 | + expect(created.template.createdAt).toEqual(created.template.updatedAt); |
| 123 | + }); |
| 124 | + }); |
| 125 | + |
| 126 | + test.describe('EMAIL templates', () => { |
| 127 | + test('returns 201 if template is valid', async ({ request }) => { |
| 128 | + const template = TemplateAPIPayloadFactory.getCreateTemplatePayload({ |
| 129 | + templateType: 'EMAIL', |
| 130 | + }); |
| 131 | + |
| 132 | + const start = new Date(); |
| 133 | + |
| 134 | + const response = await request.post( |
| 135 | + `${process.env.API_BASE_URL}/v1/template`, |
| 136 | + { |
| 137 | + headers: { |
| 138 | + Authorization: await user1.getAccessToken(), |
| 139 | + }, |
| 140 | + data: template, |
| 141 | + } |
| 142 | + ); |
| 143 | + |
| 144 | + expect(response.status()).toBe(201); |
| 145 | + |
| 146 | + const created = await response.json(); |
| 147 | + |
| 148 | + templateStorageHelper.addAdHocTemplateKey({ |
| 149 | + id: created.template.id, |
| 150 | + owner: user1.userId, |
| 151 | + }); |
| 152 | + |
| 153 | + expect(created).toEqual({ |
| 154 | + statusCode: 201, |
| 155 | + template: { |
| 156 | + campaignId: testClients[user1.clientKey]?.campaignId, |
| 157 | + createdAt: expect.stringMatching(isoDateRegExp), |
| 158 | + id: expect.stringMatching(uuidRegExp), |
| 159 | + message: template.message, |
| 160 | + name: template.name, |
| 161 | + subject: template.subject, |
| 162 | + templateStatus: 'NOT_YET_SUBMITTED', |
| 163 | + templateType: template.templateType, |
| 164 | + updatedAt: expect.stringMatching(isoDateRegExp), |
| 165 | + }, |
| 166 | + }); |
| 167 | + |
| 168 | + expect(created.template.createdAt).toBeDateRoughlyBetween([ |
| 169 | + start, |
| 170 | + new Date(), |
| 171 | + ]); |
| 172 | + expect(created.template.createdAt).toEqual(created.template.updatedAt); |
| 173 | + }); |
| 174 | + }); |
| 175 | +}); |
0 commit comments