diff --git a/docs/e2e-testing.md b/docs/e2e-testing.md index b58ee38..0af6d56 100644 --- a/docs/e2e-testing.md +++ b/docs/e2e-testing.md @@ -81,8 +81,8 @@ The join form specs (the table below) are listed with their test counts. The set | Theme | `theme.spec.ts` | Every setting: colour defaults, each colour reaching the form's styles (the primary colour on its buttons), custom CSS applied. | | Copy | `copy.spec.ts` | Every setting: each one's default (or none), the fallbacks for blank sidebar, stage label and joining verb, and where each shows (details, plan and bank-details steps, confirmation step, sidebar). Known problems: JOIN-162 (Home Address Copy never shows), JOIN-163 (day-of-month copy unused), JOIN-164 (organisation details marked required but not enforced). | | Integrations | `integrations-crm.spec.ts`, `integrations-payments.spec.ts`, `integrations-logging.spec.ts` | Every setting. Joins through the real form reach Zetkin (join form, or the API with OAuth; sandbox/live), Mailchimp, Action Network, Auth0 and the join-complete webhook with the credentials and IDs set here; the step webhook; webhook URLs never reach the form. Stripe direct debit and Direct Debit only (the payment methods Stripe's fields are set up with), the subscriptions download; GoCardless sandbox/live; Chargebee hosted pages; getAddress.io and Ideal Postcodes lookups and their key checks; Sentry, Google Cloud Logging and Loggly receiving the plugin's logs (all fake API). Known problem: JOIN-167 (subscriptions download breaks with no Stripe invoices). | -| Logging | | Not yet | -| Debug | | Not yet | +| Logging | `logging-debug.spec.ts` | Every setting. Log Retention: default of 10 days, the set number of days of log files kept, refused unless a whole number of at least 1 (by the browser and by the plugin), and the tab's note on where the number came from. The latest log is shown on the tab. Where else logs go is on the Integrations tab. Known problem: JOIN-172 (the tab can show a stale log file). | +| Debug | `logging-debug.spec.ts` | Every setting: Disable Test Data leaves the join form empty in a test-data build. | ## What is not covered @@ -92,11 +92,11 @@ The `captureJoinBodyViaStripeRedirect` helper bypasses the payment stage entirel ### GoCardless flow -Zero coverage. GoCardless is the primary payment method for Direct Debit members. The hosted redirect flow, API flow, and mandate creation are all untested. There is no equivalent of `captureJoinBodyViaStripeRedirect` for GoCardless. +Partly covered by the settings tests (`tests/settings/integrations-payments.spec.ts`, `features-integrations.spec.ts`): members are sent to GoCardless's hosted billing flow, set up with the access token and environment, and Custom Pages take bank details in the form. Coming back from GoCardless, mandate creation and the join that follows are untested; there is no equivalent of `captureJoinBodyViaStripeRedirect` for GoCardless. ### Chargebee flow -Zero coverage. Neither the hosted pages flow nor the API flow is tested. +Partly covered by the settings tests (`tests/settings/integrations-payments.spec.ts`): with hosted pages on, card payments go to a Chargebee page created with the site and API key; without, the form shows its card fields. Coming back from Chargebee and the join that follows are untested. ### Update flow (`IS_UPDATE_FLOW=true`) @@ -116,7 +116,7 @@ Now covered by the settings tests (`tests/settings/features.spec.ts`). ### Address lookup -`USE_POSTCODE_LOOKUP` enables a postcode-to-address autocomplete. Not tested. +`USE_POSTCODE_LOOKUP` enables a postcode-to-address autocomplete. Now covered by the settings tests (`tests/settings/integrations-payments.spec.ts`), for both getAddress.io and Ideal Postcodes. ### `/join` response handling diff --git a/packages/join-e2e/tests/settings/integrations-logging.spec.ts b/packages/join-e2e/tests/settings/integrations-logging.spec.ts index d7efeef..7f8d5ae 100644 --- a/packages/join-e2e/tests/settings/integrations-logging.spec.ts +++ b/packages/join-e2e/tests/settings/integrations-logging.spec.ts @@ -1,6 +1,16 @@ -import { APIRequestContext } from '@playwright/test'; import { generateKeyPairSync } from 'crypto'; -import { expect, fakeApiRequests, getSettings, joinFormEnv, openJoinForm, test, wpEvalJson } from './fixtures'; +import { + PHP_LOG_FILES, + expect, + fakeApiRequests, + getSettings, + joinFormEnv, + logInfo, + logInvalidPlan, + openJoinForm, + test, + wpEvalJson, +} from './fixtures'; /** * Settings page, Integrations tab: where the plugin's logs go (Sentry, Google @@ -12,20 +22,6 @@ import { expect, fakeApiRequests, getSettings, joinFormEnv, openJoinForm, test, * the service's fake. */ -/** Makes the plugin log an info line ("Recording CK Join form step"). */ -async function logSomething(request: APIRequestContext): Promise { - const response = await request.post('/wp-json/join/v1/step', { data: { email: 'someone@example.com', stage: 'details' } }); - expect(response.ok()).toBe(true); -} - -/** Makes the plugin log an error ("Invalid membership plan: ..."). */ -async function logAnError(request: APIRequestContext): Promise { - const response = await request.post('/wp-json/join/v1/join', { - data: { email: 'someone@example.com', membership: 'no-such-plan' }, - }); - expect(response.status()).toBe(500); -} - /** * The requests that reached a service's fake, once at least one has. Some * handlers (Loggly's buffer, Google's batch logger) only send as PHP finishes @@ -38,18 +34,14 @@ async function sentTo(host: string): Promise - wpEvalJson(` - $dir = \\CommonKnowledge\\JoinBlock\\Logging::getLogDirectory(); - clearstatcache(); - return array_sum(array_map('filesize', glob("$dir/debug-*.log") ?: [])); - `); + wpEvalJson(`clearstatcache(); return array_sum(array_map('filesize', ${PHP_LOG_FILES}));`); const LOGGING_HOSTS = ['logs-01.loggly.com', 'sentry.e2e.example', 'oauth2.googleapis.com', 'logging.googleapis.com']; test('by default, logs are written to disk and sent nowhere else', async ({ request }) => { const before = logFilesSize(); - await logSomething(request); - await logAnError(request); + await logInfo(request); + await logInvalidPlan(request); expect(logFilesSize()).toBeGreaterThan(before); const sent = await fakeApiRequests(); @@ -72,7 +64,7 @@ test.describe('Loggly', () => { }); const before = logFilesSize(); - await logSomething(request); + await logInfo(request); const sent = await sentTo('logs-01.loggly.com'); expect(sent[0]).toMatchObject({ method: 'POST', path: '/bulk/e2e-loggly-token/' }); @@ -86,7 +78,7 @@ test.describe('Loggly', () => { await settings.setText('Loggly Customer Token', 'e2e-loggly-token'); await settings.save(); - await logSomething(request); + await logInfo(request); const [first] = await sentTo('logs-01.loggly.com'); expect(first.headers['x-loggly-tag']).toBe('join-block'); @@ -120,8 +112,8 @@ test.describe('Sentry', () => { // Info first, then an error. Once the error's event has arrived, any event // for the info line, sent during the earlier request, would have too. - await logSomething(request); - await logAnError(request); + await logInfo(request); + await logInvalidPlan(request); const events = await sentTo('sentry.e2e.example'); expect(events.map((event) => event.body).join('\n')).not.toContain('Recording CK Join form step'); const [event] = events; @@ -171,7 +163,7 @@ test.describe('Google Cloud Logging', () => { google_cloud_key_file_contents: keyFile, }); - await logSomething(request); + await logInfo(request); const writes = (await sentTo('logging.googleapis.com')).filter( (request) => request.method === 'POST' && request.path === '/v2/entries:write', diff --git a/packages/join-e2e/tests/settings/legacy/logging-debug.json b/packages/join-e2e/tests/settings/legacy/logging-debug.json new file mode 100644 index 0000000..2642f88 --- /dev/null +++ b/packages/join-e2e/tests/settings/legacy/logging-debug.json @@ -0,0 +1,4 @@ +{ + "_join_flow_log_retention_days": "4", + "_disable_test_data": "yes" +} diff --git a/packages/join-e2e/tests/settings/logging-debug.spec.ts b/packages/join-e2e/tests/settings/logging-debug.spec.ts new file mode 100644 index 0000000..ccbd743 --- /dev/null +++ b/packages/join-e2e/tests/settings/logging-debug.spec.ts @@ -0,0 +1,260 @@ +import { APIRequestContext, Page } from '@playwright/test'; +import { + JoinSettingsPage, + PHP_LOG_DIR, + PHP_LOG_FILES, + STANDARD_JOIN_PAGE, + backUpLogFiles, + expect, + getSettings, + joinFormEnv, + loadLegacySettings, + logInfo, + logInvalidPlan, + openJoinForm, + restoreLogFiles, + test, + wpEvalJson, +} from './fixtures'; + +/** + * Settings page, Logging and Debug tabs. + * + * Logging: how many days of log files are kept, and the log shown on the tab. + * Where else logs go (Sentry, Google Cloud, Loggly) is on the Integrations tab + * (integrations-logging.spec.ts). + * + * Debug: turning off the join form's test data. + */ + +// Tests that replace the log files back them up first (replaceLogFiles), and +// they are put back afterwards. Other tests leave them alone. +let logFilesBackedUp = false; + +function replaceLogFiles(): void { + if (!logFilesBackedUp) { + backUpLogFiles(); + logFilesBackedUp = true; + } +} + +test.afterEach(() => { + if (logFilesBackedUp) { + restoreLogFiles(); + logFilesBackedUp = false; + } +}); + +/** + * Replaces the log files with one for each of the `days` days before today + * (as if the site had been logging that long), so the next log line starts a + * new day's file and the plugin rotates the old ones. Returns the dates laid + * down and the dates kept after rotation, both newest first. + */ +async function rotateLogFiles(request: APIRequestContext, days: number) { + replaceLogFiles(); + const laidDown = wpEvalJson(` + $dir = ${PHP_LOG_DIR}; + $existing = ${PHP_LOG_FILES}; + preg_match('/debug-([0-9a-f]+)-/', basename($existing[0] ?? 'debug-e2e-'), $match); + $hash = $match[1] ?? bin2hex(random_bytes(18)); + array_map('unlink', $existing); + $dates = []; + for ($i = 1; $i <= ${days}; $i++) { + $dates[] = date('Y-m-d', strtotime("-$i days")); + file_put_contents("$dir/debug-$hash-" . end($dates) . '.log', "old\\n"); + } + return $dates; + `); + await logInfo(request); + const kept = wpEvalJson(`return array_map(fn ($file) => substr(basename($file, '.log'), -10), ${PHP_LOG_FILES});`) + .sort() + .reverse(); + return { laidDown, kept }; +} + +/** + * Checks rotation kept `days` days of files: the new day's, and the newest of + * those laid down. (Worked out from the dates rotateLogFiles saw, so a run + * across midnight still passes.) + */ +function expectKept({ laidDown, kept }: { laidDown: string[]; kept: string[] }, days: number): void { + expect(kept).toHaveLength(days); + expect(kept[0] > laidDown[0]).toBe(true); + expect(kept.slice(1)).toEqual(laidDown.slice(0, days - 1)); +} + +/** + * Whether JOIN_FLOW_LOG_RETENTION_DAYS (e.g. from packages/join-block/.env) + * replaces the default. Read from the Logging tab, which the web server + * renders: its environment is the one that rotates the logs, and it can differ + * from WP-CLI's. + */ +async function retentionFromEnvironment(settings: JoinSettingsPage): Promise { + await settings.open('Logging'); + return (await settings.page.getByText('from JOIN_FLOW_LOG_RETENTION_DAYS=').count()) > 0; +} + +/** Enters an invalid Log Retention, and another change on the Debug tab, without saving. */ +async function enterInvalidRetention(settings: JoinSettingsPage, value: string): Promise { + await settings.tab('Logging'); + await settings.setText('Log Retention (days)', value); + await settings.tab('Debug'); + await settings.setCheckbox('Disable Test Data', true); +} + +/** + * Opens the join form and says whether it filled itself with test data: + * whether this build was made with REACT_APP_USE_TEST_DATA (npm test makes it + * so). The form says so in the console when it uses test data. + */ +async function openFormBuiltWithTestData(page: Page): Promise { + let said = false; + const listener = (message: { text(): string }) => { + if (message.text().includes('REACT_APP_USE_TEST_DATA environment variable set')) said = true; + }; + page.on('console', listener); + await openJoinForm(page); + page.off('console', listener); + return said; +} + +const NOT_BUILT_WITH_TEST_DATA = 'the join form was not built with REACT_APP_USE_TEST_DATA (npm test builds it so)'; + +test.describe('Logging', () => { + test('logs are kept for 10 days unless Log Retention says otherwise', async ({ settings, request }) => { + test.skip( + await retentionFromEnvironment(settings), + 'JOIN_FLOW_LOG_RETENTION_DAYS is set (e.g. in packages/join-block/.env), which replaces the default', + ); + expect(getSettings(['join_flow_log_retention_days']).join_flow_log_retention_days).toBe(''); + + await expect( + settings.page.getByText( + 'Logs are retained for 10 days (default, as neither the Log Retention setting nor JOIN_FLOW_LOG_RETENTION_DAYS is set).', + ), + ).toBeVisible(); + + expectKept(await rotateLogFiles(request, 14), 10); + }); + + test('Log Retention (days) sets how many days of log files are kept', async ({ settings, request }) => { + await settings.open('Logging'); + await settings.setText('Log Retention (days)', '3'); + await settings.save(); + + expect(getSettings(['join_flow_log_retention_days']).join_flow_log_retention_days).toBe('3'); + + await settings.tab('Logging'); + await expect( + settings.page.getByText('Logs are retained for 3 days (from the Log Retention setting above).'), + ).toBeVisible(); + + expectKept(await rotateLogFiles(request, 6), 3); + }); + + for (const value of ['0', '2.5', '-1']) { + test(`Log Retention of ${value} is refused, and nothing is saved`, async ({ settings }) => { + await settings.open('Logging'); + await enterInvalidRetention(settings, value); + await settings.expectSaveRefused(); + + // The plugin checks it too, for saves that skip the browser's checks. + // (If the refusal above was a reload, the page shows the saved values + // again, so enter the invalid ones again.) + await enterInvalidRetention(settings, value); + expect(await settings.saveSkippingPageChecksExpectingErrors()).toEqual([ + 'Log Retention must be a whole number of days, at least 1 (see Logging tab).', + ]); + expect(getSettings(['join_flow_log_retention_days', 'disable_test_data'])).toEqual({ + join_flow_log_retention_days: '', + disable_test_data: false, + }); + }); + } + + test('the tab shows the latest log', async ({ settings, request }) => { + // Start from no log files, so the plugin writes the only one. With others + // in the folder, the tab can show a stale one (JOIN-172, below). + replaceLogFiles(); + wpEvalJson(`array_map('unlink', ${PHP_LOG_FILES}); return true;`); + const plan = `log-viewer-${Date.now()}`; + await logInvalidPlan(request, plan); + + await settings.open('Logging'); + await expect(settings.page.locator('pre', { hasText: `Invalid membership plan: ${plan}` })).toBeVisible(); + }); + + // The tab picks its file by a different rule from the plugin's, so with + // another log file in the folder it can show a stale one (Linear JOIN-172). + // Re-enable once it's fixed. + test.fixme('the tab shows the log being written, even with another log file in the folder', async ({ + settings, + request, + }) => { + replaceLogFiles(); + await logInfo(request); + // A stale file under another name, as an earlier install might leave. + wpEvalJson(` + $dir = ${PHP_LOG_DIR}; + file_put_contents("$dir/debug-zzzz-" . date('Y-m-d', strtotime('-1 day')) . '.log', "stale\\n"); + return true; + `); + const plan = `log-viewer-${Date.now()}`; + await logInvalidPlan(request, plan); + + await settings.open('Logging'); + await expect(settings.page.locator('pre', { hasText: `Invalid membership plan: ${plan}` })).toBeVisible(); + }); +}); + +test.describe('Debug', () => { + test('the join form is filled with test data when it was built with it', async ({ page }) => { + expect(getSettings(['disable_test_data']).disable_test_data).toBe(false); + expect((await joinFormEnv(page, STANDARD_JOIN_PAGE)).DISABLE_TEST_DATA).toBe(false); + + test.skip(!(await openFormBuiltWithTestData(page)), NOT_BUILT_WITH_TEST_DATA); + await expect(page.locator('input#firstName')).toHaveValue('Test'); + }); + + test('Disable Test Data leaves the join form empty', async ({ settings, page }) => { + // Without test data in the build the form is empty anyway, so this would prove nothing. + test.skip(!(await openFormBuiltWithTestData(page)), NOT_BUILT_WITH_TEST_DATA); + + await settings.open('Debug'); + await settings.setCheckbox('Disable Test Data', true); + await settings.save(); + + expect(getSettings(['disable_test_data']).disable_test_data).toBe(true); + expect((await joinFormEnv(page, STANDARD_JOIN_PAGE)).DISABLE_TEST_DATA).toBe(true); + + await openJoinForm(page); + await expect(page.locator('input#firstName')).toHaveValue(''); + await expect(page.locator('input#email')).toHaveValue(''); + }); +}); + +test.describe('Logging and Debug settings saved before the settings page was rebuilt', () => { + // See tests/settings/legacy/README.md. + test('are still read and still apply', async ({ settings, page, request }) => { + const builtWithTestData = await openFormBuiltWithTestData(page); + loadLegacySettings('logging-debug'); + + expect(getSettings(['join_flow_log_retention_days', 'disable_test_data'])).toEqual({ + join_flow_log_retention_days: '4', + disable_test_data: true, + }); + + // Disable Test Data only shows when the build has test data to disable. + if (builtWithTestData) { + await openJoinForm(page); + await expect(page.locator('input#firstName')).toHaveValue(''); + } + + await settings.open('Logging'); + await expect( + settings.page.getByText('Logs are retained for 4 days (from the Log Retention setting above).'), + ).toBeVisible(); + expectKept(await rotateLogFiles(request, 8), 4); + }); +}); diff --git a/packages/join-e2e/tests/settings/support.ts b/packages/join-e2e/tests/settings/support.ts index d79cb43..4348820 100644 --- a/packages/join-e2e/tests/settings/support.ts +++ b/packages/join-e2e/tests/settings/support.ts @@ -142,6 +142,60 @@ export async function postJoin(request: APIRequestContext, data: Record { + const response = await request.post('/wp-json/join/v1/step', { data: { email: 'someone@example.com', stage: 'details' } }); + expect(response.ok()).toBe(true); +} + +/** Makes the plugin log an error naming `plan` ("Invalid membership plan: "). */ +export async function logInvalidPlan(request: APIRequestContext, plan = 'no-such-plan'): Promise { + const response = await request.post('/wp-json/join/v1/join', { data: { email: 'someone@example.com', membership: plan } }); + expect(response.status()).toBe(500); +} + +/** + * PHP for the plugin's log directory, for use in wpEvalJson(). Fails if the + * plugin has none (neither uploads nor wp-content writable), rather than let + * the helpers below work on files at the filesystem root. + */ +export const PHP_LOG_DIR = `(\\CommonKnowledge\\JoinBlock\\Logging::getLogDirectory() ?: throw new \\RuntimeException('The plugin has no log directory'))`; + +/** PHP for the paths of the plugin's log files, for use in wpEvalJson(). */ +export const PHP_LOG_FILES = `(glob(${PHP_LOG_DIR} . '/debug-*.log') ?: [])`; + +const PHP_LOG_BACKUP = `WP_CONTENT_DIR . '/e2e-log-backup'`; + +/** PHP that moves the backed-up log files back in place of the current ones. */ +const PHP_RESTORE_LOG_FILES = ` + $dir = ${PHP_LOG_DIR}; + array_map('unlink', ${PHP_LOG_FILES}); + foreach (glob(${PHP_LOG_BACKUP} . '/*') ?: [] as $file) rename($file, "$dir/" . basename($file)); +`; + +/** + * Copies the plugin's log files aside, for tests that change them (see + * restoreLogFiles). Restoring moves them back, so the backup folder is empty + * between tests. If it isn't, an earlier run stopped before restoring: what is + * there are the real logs, so they are put back first rather than replaced. + */ +export function backUpLogFiles(): void { + wpEvalJson(` + $backup = ${PHP_LOG_BACKUP}; + wp_mkdir_p($backup); + if (glob("$backup/*")) { + ${PHP_RESTORE_LOG_FILES} + } + foreach (${PHP_LOG_FILES} as $file) copy($file, "$backup/" . basename($file)); + return true; + `); +} + +/** Puts the plugin's log files back as backUpLogFiles() found them. */ +export function restoreLogFiles(): void { + wpEvalJson(`${PHP_RESTORE_LOG_FILES} return true;`); +} + /** Delivers an event to the plugin's Stripe webhook endpoint, as Stripe would. */ export async function postStripeWebhook(request: APIRequestContext, type: string, data: Record) { const response = await request.post('/wp-json/join/v1/stripe/webhook', { data: { type, data } });