|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + * |
| 5 | + * Bug reproduction: Validation screen doesn't display data correctly for envelopes with 2+ files |
| 6 | + */ |
| 7 | + |
| 8 | +import { expect, test } from '@playwright/test' |
| 9 | +import type { APIRequestContext, Page } from '@playwright/test' |
| 10 | +import { configureOpenSsl, setAppConfig } from '../support/nc-provisioning' |
| 11 | +import { createMailpitClient, extractSignLink, waitForEmailTo } from '../support/mailpit' |
| 12 | + |
| 13 | +type EnvelopeSigningScenario = { |
| 14 | + envelopeName: string |
| 15 | + signerEmail: string |
| 16 | + signerName: string |
| 17 | +} |
| 18 | + |
| 19 | +type OcsEnvelopeChildSigner = { |
| 20 | + signRequestId?: number |
| 21 | + email?: string |
| 22 | + displayName?: string |
| 23 | +} |
| 24 | + |
| 25 | +type OcsEnvelopeChildFile = { |
| 26 | + id?: number |
| 27 | + name?: string |
| 28 | + signers?: OcsEnvelopeChildSigner[] |
| 29 | +} |
| 30 | + |
| 31 | +type OcsEnvelopeResponse = { |
| 32 | + uuid?: string |
| 33 | + files?: OcsEnvelopeChildFile[] |
| 34 | +} |
| 35 | + |
| 36 | +function buildSigningScenario(): EnvelopeSigningScenario { |
| 37 | + return { |
| 38 | + envelopeName: `Envelope Validation Bug - ${Date.now()}`, |
| 39 | + signerEmail: 'signer-validation@libresign.coop', |
| 40 | + signerName: 'Validation Tester', |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +async function requestLibreSignApiAsAdmin( |
| 45 | + request: APIRequestContext, |
| 46 | + method: 'POST' | 'PATCH', |
| 47 | + path: string, |
| 48 | + body: Record<string, unknown>, |
| 49 | +) { |
| 50 | + const adminUser = process.env.NEXTCLOUD_ADMIN_USER ?? 'admin' |
| 51 | + const adminPassword = process.env.NEXTCLOUD_ADMIN_PASSWORD ?? 'admin' |
| 52 | + const auth = 'Basic ' + Buffer.from(`${adminUser}:${adminPassword}`).toString('base64') |
| 53 | + const response = await request.fetch(`./ocs/v2.php/apps/libresign/api/v1${path}`, { |
| 54 | + method, |
| 55 | + headers: { |
| 56 | + 'OCS-ApiRequest': 'true', |
| 57 | + Accept: 'application/json', |
| 58 | + Authorization: auth, |
| 59 | + 'Content-Type': 'application/json', |
| 60 | + }, |
| 61 | + data: JSON.stringify(body), |
| 62 | + failOnStatusCode: false, |
| 63 | + }) |
| 64 | + |
| 65 | + if (!response.ok()) { |
| 66 | + throw new Error(`LibreSign OCS request failed: ${method} ${path} -> ${response.status()} ${await response.text()}`) |
| 67 | + } |
| 68 | + |
| 69 | + return response.json() as Promise<{ ocs: { data: OcsEnvelopeResponse } }> |
| 70 | +} |
| 71 | + |
| 72 | +async function enableEnvelopeScenario(request: APIRequestContext) { |
| 73 | + await configureOpenSsl(request, 'LibreSign Test', { |
| 74 | + C: 'BR', |
| 75 | + OU: ['Organization Unit'], |
| 76 | + ST: 'Rio de Janeiro', |
| 77 | + O: 'LibreSign', |
| 78 | + L: 'Rio de Janeiro', |
| 79 | + }) |
| 80 | + |
| 81 | + await setAppConfig(request, 'libresign', 'envelope_enabled', '1') |
| 82 | + await setAppConfig( |
| 83 | + request, |
| 84 | + 'libresign', |
| 85 | + 'identify_methods', |
| 86 | + JSON.stringify([ |
| 87 | + { name: 'account', enabled: false, mandatory: false }, |
| 88 | + { name: 'email', enabled: true, mandatory: true, signatureMethods: { clickToSign: { enabled: true } }, can_create_account: false }, |
| 89 | + ]), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +async function createEnvelopeWithMultipleFiles( |
| 94 | + request: APIRequestContext, |
| 95 | + scenario: EnvelopeSigningScenario, |
| 96 | +) { |
| 97 | + const pdfResponse = await request.get('https://raw.githubusercontent.com/LibreSign/libresign/main/tests/php/fixtures/pdfs/small_valid.pdf', { |
| 98 | + failOnStatusCode: true, |
| 99 | + }) |
| 100 | + const pdfBase64 = Buffer.from(await pdfResponse.body()).toString('base64') |
| 101 | + |
| 102 | + const createResponse = await requestLibreSignApiAsAdmin(request, 'POST', '/request-signature', { |
| 103 | + name: scenario.envelopeName, |
| 104 | + files: [ |
| 105 | + { name: 'document-1.pdf', base64: pdfBase64 }, |
| 106 | + { name: 'document-2.pdf', base64: pdfBase64 }, |
| 107 | + ], |
| 108 | + signers: [{ |
| 109 | + displayName: scenario.signerName, |
| 110 | + identifyMethods: [{ |
| 111 | + method: 'email', |
| 112 | + value: scenario.signerEmail, |
| 113 | + mandatory: 1, |
| 114 | + }], |
| 115 | + }], |
| 116 | + }) |
| 117 | + |
| 118 | + const envelope = createResponse.ocs.data |
| 119 | + |
| 120 | + if (!envelope.uuid) { |
| 121 | + throw new Error('Failed to create envelope with multiple files') |
| 122 | + } |
| 123 | + |
| 124 | + // Activate the envelope |
| 125 | + await requestLibreSignApiAsAdmin(request, 'PATCH', '/request-signature', { |
| 126 | + uuid: envelope.uuid, |
| 127 | + status: 1, |
| 128 | + }) |
| 129 | + |
| 130 | + return envelope |
| 131 | +} |
| 132 | + |
| 133 | +async function waitForSignerInvitationLink(signerEmail: string) { |
| 134 | + const email = await waitForEmailTo( |
| 135 | + createMailpitClient(), |
| 136 | + signerEmail, |
| 137 | + 'LibreSign: There is a file for you to sign', |
| 138 | + ) |
| 139 | + const signLink = extractSignLink(email.Text) |
| 140 | + if (!signLink) { |
| 141 | + throw new Error('Sign link not found in email') |
| 142 | + } |
| 143 | + return signLink |
| 144 | +} |
| 145 | + |
| 146 | +async function openInvitationAsExternalSigner(page: Page, signLink: string) { |
| 147 | + // API setup runs as admin. Clear cookies so the browser behaves like the real external signer. |
| 148 | + await page.context().clearCookies() |
| 149 | + await page.goto(signLink) |
| 150 | +} |
| 151 | + |
| 152 | +async function defineClickToSignature(page: Page) { |
| 153 | + // Wait for click-to-sign button |
| 154 | + await expect(page.getByRole('button', { name: 'Sign the document.' })).toBeVisible({ timeout: 5_000 }) |
| 155 | +} |
| 156 | + |
| 157 | +async function finishSigning(page: Page) { |
| 158 | + const signButton = page.getByRole('button', { name: 'Sign the document.' }) |
| 159 | + await signButton.scrollIntoViewIfNeeded() |
| 160 | + await signButton.click() |
| 161 | + await page.getByRole('button', { name: 'Sign document' }).click() |
| 162 | +} |
| 163 | + |
| 164 | +test('validation screen should display all data correctly for envelope with 2 files', async ({ page }) => { |
| 165 | + const scenario = buildSigningScenario() |
| 166 | + const mailpit = createMailpitClient() |
| 167 | + |
| 168 | + await test.step('Given the system is configured to allow envelope signing via e-mail', async () => { |
| 169 | + await enableEnvelopeScenario(page.request) |
| 170 | + }) |
| 171 | + |
| 172 | + await test.step('And an envelope with two files is created', async () => { |
| 173 | + await mailpit.deleteMessages() |
| 174 | + await createEnvelopeWithMultipleFiles(page.request, scenario) |
| 175 | + }) |
| 176 | + |
| 177 | + await test.step('When the external signer opens the invitation link', async () => { |
| 178 | + const signLink = await waitForSignerInvitationLink(scenario.signerEmail) |
| 179 | + await openInvitationAsExternalSigner(page, signLink) |
| 180 | + }) |
| 181 | + |
| 182 | + await test.step('And completes the signing process with click-to-sign', async () => { |
| 183 | + await defineClickToSignature(page) |
| 184 | + await finishSigning(page) |
| 185 | + }) |
| 186 | + |
| 187 | + await test.step('Then the validation screen should display the envelope information correctly', async () => { |
| 188 | + // Wait for validation page to load |
| 189 | + await page.waitForURL('**/validation/**') |
| 190 | + |
| 191 | + // Verify envelope information section is visible |
| 192 | + await expect(page.getByText('Envelope information')).toBeVisible() |
| 193 | + |
| 194 | + // Verify envelope name is displayed |
| 195 | + const envelopeName = page.locator('h2.app-sidebar-header__mainname') |
| 196 | + await expect(envelopeName).toHaveText(scenario.envelopeName) |
| 197 | + |
| 198 | + // Verify documents in envelope section exists |
| 199 | + await expect(page.getByText('Documents in this envelope')).toBeVisible() |
| 200 | + |
| 201 | + await expect(page.getByText('Number of documents:')).toBeVisible() |
| 202 | + |
| 203 | + // Get the documents list |
| 204 | + const documentsList = page.locator('ul.documents-list li.document-item') |
| 205 | + const documentsCount = await documentsList.count() |
| 206 | + |
| 207 | + console.log(`Found ${documentsCount} documents in the list`) |
| 208 | + expect(documentsCount).toBe(2) |
| 209 | + |
| 210 | + // Success message should be visible |
| 211 | + await expect(page.getByText('Congratulations you have digitally signed a document using LibreSign')).toBeVisible() |
| 212 | + }) |
| 213 | +}) |
0 commit comments