|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { TemplateStorageHelper } from '../helpers/db/template-storage-helper'; |
| 3 | +import { |
| 4 | + createAuthHelper, |
| 5 | + testUsers, |
| 6 | + type TestUser, |
| 7 | +} from '../helpers/auth/cognito-auth-helper'; |
| 8 | +import { pdfUploadFixtures } from '../fixtures/pdf-upload/multipart-pdf-letter-fixtures'; |
| 9 | +import { TemplateFactory } from '../helpers/factories/template-factory'; |
| 10 | +import path from 'node:path'; |
| 11 | +import { randomUUID } from 'node:crypto'; |
| 12 | +import { SftpHelper } from '../helpers/sftp/sftp-helper'; |
| 13 | +import { SqsHelper } from '../helpers/sqs/sqs-helper'; |
| 14 | +import { testClients } from '../helpers/client/client-helper'; |
| 15 | + |
| 16 | +const MOCK_LETTER_SUPPLIER = 'WTMMOCK'; |
| 17 | + |
| 18 | +const sqsHelper = new SqsHelper(); |
| 19 | +const sftpHelper = new SftpHelper(); |
| 20 | + |
| 21 | +test.describe('SFTP proof request send', () => { |
| 22 | + const templateStorageHelper = new TemplateStorageHelper(); |
| 23 | + let user: TestUser; |
| 24 | + |
| 25 | + test.beforeAll(async () => { |
| 26 | + user = await createAuthHelper().getTestUser(testUsers.User1.userId); |
| 27 | + }); |
| 28 | + |
| 29 | + test.afterAll(async () => { |
| 30 | + await templateStorageHelper.deleteAdHocTemplates(); |
| 31 | + await sftpHelper.end(); |
| 32 | + }); |
| 33 | + |
| 34 | + test('Sends PDF and test batch to SFTP, updates template', async () => { |
| 35 | + const templateId = randomUUID(); |
| 36 | + |
| 37 | + const personalisationParameters = [ |
| 38 | + 'date', |
| 39 | + 'nhsNumber', |
| 40 | + 'fullName', |
| 41 | + 'address_line_1', |
| 42 | + 'address_line_2', |
| 43 | + 'address_line_3', |
| 44 | + 'address_line_4', |
| 45 | + 'address_line_5', |
| 46 | + 'address_line_6', |
| 47 | + 'address_line_7', |
| 48 | + 'appointment_date', |
| 49 | + 'appointment_time', |
| 50 | + 'appointment_location', |
| 51 | + 'contact_telephone_number', |
| 52 | + ]; |
| 53 | + |
| 54 | + const template = { |
| 55 | + ...TemplateFactory.createLetterTemplate( |
| 56 | + templateId, |
| 57 | + user, |
| 58 | + 'send-proof-letter', |
| 59 | + 'PENDING_PROOF_REQUEST' |
| 60 | + ), |
| 61 | + // the template's 'personalisationParameters' have no effect on the test |
| 62 | + // the sender lambda does not read the template |
| 63 | + personalisationParameters, |
| 64 | + }; |
| 65 | + |
| 66 | + const key = { |
| 67 | + id: templateId, |
| 68 | + owner: user.userId, |
| 69 | + }; |
| 70 | + |
| 71 | + const pdfVersionId = template.files?.pdfTemplate?.currentVersion; |
| 72 | + const csvVersionId = template.files?.testDataCsv?.currentVersion; |
| 73 | + |
| 74 | + const campaignId = testClients[user.clientKey]?.campaignId; |
| 75 | + |
| 76 | + expect(pdfVersionId).toBeDefined(); |
| 77 | + expect(csvVersionId).toBeDefined(); |
| 78 | + |
| 79 | + const pdf = pdfUploadFixtures.withPersonalisation.pdf.open(); |
| 80 | + const csv = pdfUploadFixtures.withPersonalisation.csv.open(); |
| 81 | + |
| 82 | + await Promise.all([ |
| 83 | + templateStorageHelper.seedTemplateData([template]), |
| 84 | + templateStorageHelper.putScannedPdfTemplateFile(key, pdfVersionId!, pdf), |
| 85 | + templateStorageHelper.putScannedCsvTestDataFile(key, csvVersionId!, csv), |
| 86 | + ]); |
| 87 | + |
| 88 | + templateStorageHelper.addAdHocTemplateKey(key); |
| 89 | + |
| 90 | + const proofRequest = { |
| 91 | + campaignId, |
| 92 | + language: template.language, |
| 93 | + letterType: template.letterType, |
| 94 | + pdfVersionId, |
| 95 | + personalisationParameters, |
| 96 | + supplier: MOCK_LETTER_SUPPLIER, |
| 97 | + templateId: templateId, |
| 98 | + templateName: template.name, |
| 99 | + testDataVersionId: csvVersionId, |
| 100 | + user: { userId: user.userId, clientId: user.clientId }, |
| 101 | + }; |
| 102 | + |
| 103 | + await sqsHelper.sendMessage( |
| 104 | + process.env.REQUEST_PROOF_QUEUE_URL, |
| 105 | + proofRequest |
| 106 | + ); |
| 107 | + |
| 108 | + await expect(async () => { |
| 109 | + const updatedTemplate = await templateStorageHelper.getTemplate(key); |
| 110 | + const debugUpdated = JSON.stringify(updatedTemplate); |
| 111 | + |
| 112 | + expect(updatedTemplate.updatedAt, debugUpdated).not.toBe( |
| 113 | + template.updatedAt |
| 114 | + ); |
| 115 | + }).toPass({ timeout: 5000 }); |
| 116 | + |
| 117 | + const sftpCredentials = await sftpHelper.connect(); |
| 118 | + |
| 119 | + const sftpBase = path.join( |
| 120 | + sftpCredentials.baseUploadDir, |
| 121 | + process.env.SFTP_ENVIRONMENT |
| 122 | + ); |
| 123 | + |
| 124 | + const expandedTemplateId = [ |
| 125 | + user.clientId, |
| 126 | + campaignId, |
| 127 | + templateId, |
| 128 | + template.language, |
| 129 | + template.letterType, |
| 130 | + ].join('_'); |
| 131 | + |
| 132 | + const pdfLocation = path.join( |
| 133 | + sftpBase, |
| 134 | + 'templates', |
| 135 | + expandedTemplateId, |
| 136 | + `${expandedTemplateId}.pdf` |
| 137 | + ); |
| 138 | + |
| 139 | + const batchId = `${expandedTemplateId}-0000000000000_${pdfVersionId!.replaceAll('-', '').slice(0, 27)}`; |
| 140 | + |
| 141 | + const batchLocation = path.join( |
| 142 | + sftpBase, |
| 143 | + 'batches', |
| 144 | + expandedTemplateId, |
| 145 | + `${batchId}.csv` |
| 146 | + ); |
| 147 | + |
| 148 | + const manifestLocation = path.join( |
| 149 | + sftpBase, |
| 150 | + 'batches', |
| 151 | + expandedTemplateId, |
| 152 | + `${batchId}_MANIFEST.csv` |
| 153 | + ); |
| 154 | + |
| 155 | + await expect(async () => { |
| 156 | + const locations = [pdfLocation, batchLocation, manifestLocation]; |
| 157 | + |
| 158 | + const files = await Promise.all( |
| 159 | + locations.map((location) => sftpHelper.stat(location)) |
| 160 | + ); |
| 161 | + |
| 162 | + for (const [i, file] of files.entries()) { |
| 163 | + const dbg = locations[i]; |
| 164 | + expect(file.isFile, dbg).toBeTruthy(); |
| 165 | + expect(file.size, dbg).toBeGreaterThan(0); |
| 166 | + } |
| 167 | + }).toPass({ timeout: 10_000 }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments