Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

SSR: Port old forms/import tests from Cypress to Playwright.

Kailash Nadh7a560028f8c8
diff --git a/static/admin/views/forms.html b/static/admin/views/forms.html
index 23cb8d8f..fcf610be 100644
--- a/static/admin/views/forms.html
+++ b/static/admin/views/forms.html
@@ -10,7 +10,7 @@
</div>
<div class="col-6 col-end align-right hstack justify-end">
{{- if $.ConfigJS.PublicSubscription.Enabled }}
- <a href="{{ $.ConfigJS.RootURL }}/subscription/form" target="_blank" rel="noopener noreferrer" class="button outline small" data-cy="url" title="{{ $.ConfigJS.RootURL }}/subscription/form">
+ <a href="{{ $.ConfigJS.RootURL }}/subscription/form" target="_blank" rel="noopener noreferrer" class="button outline small" data-testid="url" title="{{ $.ConfigJS.RootURL }}/subscription/form">
{{ Icon "arrow-up-right" }} {{ .L.T "forms.publicSubPage" }}
</a>
{{- end }}
@@ -25,7 +25,7 @@
<div class="col-4 col-bleed">
<p class="text-light">{{ .L.T "forms.selectHelp" }}</p>
- <ul class="no lists" data-cy="lists">
+ <ul class="no lists" data-testid="lists">
{{- range $i, $list := $d.PublicLists }}
<li>
<label><input type="checkbox" value="{{ $list.UUID }}" x-model="checked" {{- if eq $i 0 }} autofocus{{- end }} /> {{ $list.Name }}</label>
@@ -37,7 +37,7 @@
<h4>{{ .L.T "forms.redirectURL" }}</h4>
<p class="text-light">{{ .L.T "forms.redirectURLHelp" }}</p>
{{- if $.ConfigJS.PublicSubscription.RedirectURLs }}
- <ul class="no" data-cy="redirect-urls">
+ <ul class="no" data-testid="redirect-urls">
<li>
<label><input type="radio" name="redirect-url" value="" x-model="redirectURL" /> {{ .L.T "globals.terms.none" }}</label>
</li>
@@ -50,7 +50,7 @@
{{- end }}
</div>
- <div class="col-8 col-end" data-cy="form" aria-label="{{ .L.T "forms.formHTML" }}">
+ <div class="col-8 col-end" data-testid="form" aria-label="{{ .L.T "forms.formHTML" }}">
<p class="text-light">{{ .L.T "forms.formHTMLHelp" }}</p>
<div x-show="checked.length > 0" x-cloak>
diff --git a/static/admin/views/import.html b/static/admin/views/import.html
index 81393f91..c3c247de 100644
--- a/static/admin/views/import.html
+++ b/static/admin/views/import.html
@@ -25,11 +25,11 @@
<fieldset class="vstack gap-6">
<legend>{{ .L.T "import.mode" }}</legend>
<div class="hstack">
- <label><input type="radio" name="mode" value="subscribe" x-model="form.mode" data-cy="check-subscribe" /> {{ .L.T "import.subscribe" }}</label>
- <label><input type="radio" name="mode" value="blocklist" x-model="form.mode" data-cy="check-blocklist" /> {{ .L.T "import.blocklist" }}</label>
+ <label><input type="radio" name="mode" value="subscribe" x-model="form.mode" data-testid="check-subscribe" /> {{ .L.T "import.subscribe" }}</label>
+ <label><input type="radio" name="mode" value="blocklist" x-model="form.mode" data-testid="check-blocklist" /> {{ .L.T "import.blocklist" }}</label>
</div>
<label data-field>
- <input type="checkbox" role="switch" x-model="form.overwriteUserInfo" data-cy="overwrite-user-info" /> {{ .L.T "import.overwriteUserInfo" }}
+ <input type="checkbox" role="switch" x-model="form.overwriteUserInfo" data-testid="overwrite-user-info" /> {{ .L.T "import.overwriteUserInfo" }}
<small data-hint>{{ .L.T "import.overwriteUserInfoHelp" }}</small>
</label>
</fieldset>
@@ -39,16 +39,16 @@
<legend>{{ .L.T "globals.fields.status" }}</legend>
<template x-if="form.mode === 'subscribe'">
<div class="hstack">
- <label><input type="radio" name="subStatus" value="unconfirmed" x-model="form.subStatus" data-cy="check-unconfirmed" /> {{ .L.T "subscribers.status.unconfirmed" }}</label>
- <label><input type="radio" name="subStatus" value="confirmed" x-model="form.subStatus" data-cy="check-confirmed" /> {{ .L.T "subscribers.status.confirmed" }}</label>
+ <label><input type="radio" name="subStatus" value="unconfirmed" x-model="form.subStatus" data-testid="check-unconfirmed" /> {{ .L.T "subscribers.status.unconfirmed" }}</label>
+ <label><input type="radio" name="subStatus" value="confirmed" x-model="form.subStatus" data-testid="check-confirmed" /> {{ .L.T "subscribers.status.confirmed" }}</label>
</div>
</template>
<template x-if="form.mode === 'blocklist'">
- <label><input type="radio" name="subStatus" value="unsubscribed" x-model="form.subStatus" data-cy="check-unsubscribed" /> {{ .L.T "subscribers.status.unsubscribed" }}</label>
+ <label><input type="radio" name="subStatus" value="unsubscribed" x-model="form.subStatus" data-testid="check-unsubscribed" /> {{ .L.T "subscribers.status.unsubscribed" }}</label>
</template>
<template x-if="form.mode === 'subscribe'">
<label data-field>
- <input type="checkbox" role="switch" x-model="form.overwriteSubStatus" data-cy="overwrite-sub-status" /> {{ .L.T "import.overwriteSubStatus" }}
+ <input type="checkbox" role="switch" x-model="form.overwriteSubStatus" data-testid="overwrite-sub-status" /> {{ .L.T "import.overwriteSubStatus" }}
<small data-hint>{{ .L.T "import.overwriteSubStatusHelp" }}</small>
</label>
</template>
@@ -87,7 +87,7 @@
</div>
<div class="hstack">
- <button type="submit" data-variant="primary" data-cy="btn-upload" :disabled="!canUpload || isProcessing" :aria-busy="isProcessing">
+ <button type="submit" data-variant="primary" data-testid="btn-upload" :disabled="!canUpload || isProcessing" :aria-busy="isProcessing">
{{ Icon "upload" }} {{ .L.T "import.upload" }}
</button>
</div>
@@ -108,8 +108,8 @@
<div class="stat-value capitalize" :class="{ 'text-danger': status.status === 'failed' || status.status === 'stopped' }" x-text="status.status">{{ $d.Status.Status }}</div>
</header>
<div class="col-4 hstack justify-end items-center">
- <button type="button" data-variant="danger" data-cy="btn-stop" x-show="!isDone" @click="stopImport" :disabled="isProcessing || status.status === 'stopping'" :aria-busy="isProcessing">{{ .L.T "import.stopImport" }}</button>
- <button type="button" data-variant="primary" data-cy="btn-done" x-show="isDone" @click="stopImport" :disabled="isProcessing" :aria-busy="isProcessing">{{ .L.T "import.importDone" }}</button>
+ <button type="button" data-variant="danger" data-testid="btn-stop" x-show="!isDone" @click="stopImport" :disabled="isProcessing || status.status === 'stopping'" :aria-busy="isProcessing">{{ .L.T "import.stopImport" }}</button>
+ <button type="button" data-variant="primary" data-testid="btn-done" x-show="isDone" @click="stopImport" :disabled="isProcessing" :aria-busy="isProcessing">{{ .L.T "import.importDone" }}</button>
</div>
</div>
<footer>
diff --git a/tests/README.md b/tests/README.md
index a141a74a..55c7f7c0 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -1,6 +1,8 @@
# listmonk E2E tests
-Playwright UI E2E tests. Requires a built `./listmonk` binary in the repo root with a test Postgres instance (use docker-compose.yml) set in config.toml. Every test run wipes and reinstalls the DB schema.
+Playwright UI E2E tests. Requires a built `./listmonk` binary in the repo root with a test Postgres instance (use docker-compose.yml) set in config.toml. Every spec wipes and reinstalls the DB schema (see `resetDB` in `helpers.js`).
+
+`resetDB` also points listmonk's SMTP config at [MailHog](https://github.com/mailhog/MailHog) so mail is captured and testable via its API (`getMail`/`clearMail`). It needs `mailhog` and `psql` on `PATH`; DB credentials are read from the `LISTMONK_db__*` env (falling back to the config.toml defaults).
## Setup
diff --git a/tests/fixtures/subs.csv b/tests/fixtures/subs.csv
new file mode 100644
index 00000000..3d9b4d97
--- /dev/null
+++ b/tests/fixtures/subs.csv
@@ -0,0 +1,101 @@
+email,name,attributes
[email protected],First0 Last0,"{""age"": 29, ""city"": ""Bangalore"", ""clientId"": ""DAXX79""}"
[email protected],First1 Last1,"{""age"": 43, ""city"": ""Bangalore"", ""clientId"": ""DAXX71""}"
[email protected],First2 Last2,"{""age"": 47, ""city"": ""Bangalore"", ""clientId"": ""DAXX70""}"
[email protected],First3 Last3,"{""age"": 67, ""city"": ""Bangalore"", ""clientId"": ""DAXX32""}"
[email protected],First4 Last4,"{""age"": 63, ""city"": ""Bangalore"", ""clientId"": ""DAXX30""}"
[email protected],First5 Last5,"{""age"": 69, ""city"": ""Bangalore"", ""clientId"": ""DAXX64""}"
[email protected],First6 Last6,"{""age"": 68, ""city"": ""Bangalore"", ""clientId"": ""DAXX22""}"
[email protected],First7 Last7,"{""age"": 56, ""city"": ""Bangalore"", ""clientId"": ""DAXX54""}"
[email protected],First8 Last8,"{""age"": 58, ""city"": ""Bangalore"", ""clientId"": ""DAXX65""}"
[email protected],First9 Last9,"{""age"": 51, ""city"": ""Bangalore"", ""clientId"": ""DAXX66""}"
[email protected],First10 Last10,"{""age"": 53, ""city"": ""Bangalore"", ""clientId"": ""DAXX31""}"
[email protected],First11 Last11,"{""age"": 46, ""city"": ""Bangalore"", ""clientId"": ""DAXX59""}"
[email protected],First12 Last12,"{""age"": 41, ""city"": ""Bangalore"", ""clientId"": ""DAXX80""}"
[email protected],First13 Last13,"{""age"": 27, ""city"": ""Bangalore"", ""clientId"": ""DAXX96""}"
[email protected],First14 Last14,"{""age"": 51, ""city"": ""Bangalore"", ""clientId"": ""DAXX22""}"
[email protected],First15 Last15,"{""age"": 31, ""city"": ""Bangalore"", ""clientId"": ""DAXX97""}"
[email protected],First16 Last16,"{""age"": 59, ""city"": ""Bangalore"", ""clientId"": ""DAXX41""}"
[email protected],First17 Last17,"{""age"": 29, ""city"": ""Bangalore"", ""clientId"": ""DAXX93""}"
[email protected],First18 Last18,"{""age"": 39, ""city"": ""Bangalore"", ""clientId"": ""DAXX35""}"
[email protected],First19 Last19,"{""age"": 67, ""city"": ""Bangalore"", ""clientId"": ""DAXX21""}"
[email protected],First20 Last20,"{""age"": 66, ""city"": ""Bangalore"", ""clientId"": ""DAXX56""}"
[email protected],First21 Last21,"{""age"": 39, ""city"": ""Bangalore"", ""clientId"": ""DAXX26""}"
[email protected],First22 Last22,"{""age"": 44, ""city"": ""Bangalore"", ""clientId"": ""DAXX98""}"
[email protected],First23 Last23,"{""age"": 66, ""city"": ""Bangalore"", ""clientId"": ""DAXX64""}"
[email protected],First24 Last24,"{""age"": 48, ""city"": ""Bangalore"", ""clientId"": ""DAXX41""}"
[email protected],First25 Last25,"{""age"": 38, ""city"": ""Bangalore"", ""clientId"": ""DAXX80""}"
[email protected],First26 Last26,"{""age"": 27, ""city"": ""Bangalore"", ""clientId"": ""DAXX26""}"
[email protected],First27 Last27,"{""age"": 59, ""city"": ""Bangalore"", ""clientId"": ""DAXX55""}"
[email protected],First28 Last28,"{""age"": 49, ""city"": ""Bangalore"", ""clientId"": ""DAXX45""}"
[email protected],First29 Last29,"{""age"": 45, ""city"": ""Bangalore"", ""clientId"": ""DAXX74""}"
[email protected],First30 Last30,"{""age"": 47, ""city"": ""Bangalore"", ""clientId"": ""DAXX27""}"
[email protected],First31 Last31,"{""age"": 21, ""city"": ""Bangalore"", ""clientId"": ""DAXX37""}"
[email protected],First32 Last32,"{""age"": 21, ""city"": ""Bangalore"", ""clientId"": ""DAXX50""}"
[email protected],First33 Last33,"{""age"": 70, ""city"": ""Bangalore"", ""clientId"": ""DAXX29""}"
[email protected],First34 Last34,"{""age"": 59, ""city"": ""Bangalore"", ""clientId"": ""DAXX95""}"
[email protected],First35 Last35,"{""age"": 36, ""city"": ""Bangalore"", ""clientId"": ""DAXX79""}"
[email protected],First36 Last36,"{""age"": 47, ""city"": ""Bangalore"", ""clientId"": ""DAXX30""}"
[email protected],First37 Last37,"{""age"": 36, ""city"": ""Bangalore"", ""clientId"": ""DAXX92""}"
[email protected],First38 Last38,"{""age"": 29, ""city"": ""Bangalore"", ""clientId"": ""DAXX48""}"
[email protected],First39 Last39,"{""age"": 23, ""city"": ""Bangalore"", ""clientId"": ""DAXX12""}"
[email protected],First40 Last40,"{""age"": 39, ""city"": ""Bangalore"", ""clientId"": ""DAXX40""}"
[email protected],First41 Last41,"{""age"": 41, ""city"": ""Bangalore"", ""clientId"": ""DAXX51""}"
[email protected],First42 Last42,"{""age"": 22, ""city"": ""Bangalore"", ""clientId"": ""DAXX49""}"
[email protected],First43 Last43,"{""age"": 68, ""city"": ""Bangalore"", ""clientId"": ""DAXX58""}"
[email protected],First44 Last44,"{""age"": 45, ""city"": ""Bangalore"", ""clientId"": ""DAXX15""}"
[email protected],First45 Last45,"{""age"": 44, ""city"": ""Bangalore"", ""clientId"": ""DAXX75""}"
[email protected],First46 Last46,"{""age"": 42, ""city"": ""Bangalore"", ""clientId"": ""DAXX99""}"
[email protected],First47 Last47,"{""age"": 61, ""city"": ""Bangalore"", ""clientId"": ""DAXX39""}"
[email protected],First48 Last48,"{""age"": 57, ""city"": ""Bangalore"", ""clientId"": ""DAXX13""}"
[email protected],First49 Last49,"{""age"": 28, ""city"": ""Bangalore"", ""clientId"": ""DAXX97""}"
[email protected],First50 Last50,"{""age"": 61, ""city"": ""Bangalore"", ""clientId"": ""DAXX75""}"
[email protected],First51 Last51,"{""age"": 27, ""city"": ""Bangalore"", ""clientId"": ""DAXX55""}"
[email protected],First52 Last52,"{""age"": 62, ""city"": ""Bangalore"", ""clientId"": ""DAXX35""}"
[email protected],First53 Last53,"{""age"": 24, ""city"": ""Bangalore"", ""clientId"": ""DAXX67""}"
[email protected],First54 Last54,"{""age"": 25, ""city"": ""Bangalore"", ""clientId"": ""DAXX36""}"
[email protected],First55 Last55,"{""age"": 39, ""city"": ""Bangalore"", ""clientId"": ""DAXX74""}"
[email protected],First56 Last56,"{""age"": 53, ""city"": ""Bangalore"", ""clientId"": ""DAXX28""}"
[email protected],First57 Last57,"{""age"": 32, ""city"": ""Bangalore"", ""clientId"": ""DAXX36""}"
[email protected],First58 Last58,"{""age"": 64, ""city"": ""Bangalore"", ""clientId"": ""DAXX44""}"
[email protected],First59 Last59,"{""age"": 47, ""city"": ""Bangalore"", ""clientId"": ""DAXX65""}"
[email protected],First60 Last60,"{""age"": 62, ""city"": ""Bangalore"", ""clientId"": ""DAXX11""}"
[email protected],First61 Last61,"{""age"": 24, ""city"": ""Bangalore"", ""clientId"": ""DAXX55""}"
[email protected],First62 Last62,"{""age"": 61, ""city"": ""Bangalore"", ""clientId"": ""DAXX49""}"
[email protected],First63 Last63,"{""age"": 52, ""city"": ""Bangalore"", ""clientId"": ""DAXX83""}"
[email protected],First64 Last64,"{""age"": 38, ""city"": ""Bangalore"", ""clientId"": ""DAXX16""}"
[email protected],First65 Last65,"{""age"": 48, ""city"": ""Bangalore"", ""clientId"": ""DAXX54""}"
[email protected],First66 Last66,"{""age"": 35, ""city"": ""Bangalore"", ""clientId"": ""DAXX74""}"
[email protected],First67 Last67,"{""age"": 70, ""city"": ""Bangalore"", ""clientId"": ""DAXX22""}"
[email protected],First68 Last68,"{""age"": 21, ""city"": ""Bangalore"", ""clientId"": ""DAXX98""}"
[email protected],First69 Last69,"{""age"": 46, ""city"": ""Bangalore"", ""clientId"": ""DAXX24""}"
[email protected],First70 Last70,"{""age"": 58, ""city"": ""Bangalore"", ""clientId"": ""DAXX75""}"
[email protected],First71 Last71,"{""age"": 50, ""city"": ""Bangalore"", ""clientId"": ""DAXX57""}"
[email protected],First72 Last72,"{""age"": 63, ""city"": ""Bangalore"", ""clientId"": ""DAXX30""}"
[email protected],First73 Last73,"{""age"": 54, ""city"": ""Bangalore"", ""clientId"": ""DAXX77""}"
[email protected],First74 Last74,"{""age"": 67, ""city"": ""Bangalore"", ""clientId"": ""DAXX91""}"
[email protected],First75 Last75,"{""age"": 61, ""city"": ""Bangalore"", ""clientId"": ""DAXX30""}"
[email protected],First76 Last76,"{""age"": 50, ""city"": ""Bangalore"", ""clientId"": ""DAXX28""}"
[email protected],First77 Last77,"{""age"": 62, ""city"": ""Bangalore"", ""clientId"": ""DAXX41""}"
[email protected],First78 Last78,"{""age"": 66, ""city"": ""Bangalore"", ""clientId"": ""DAXX18""}"
[email protected],First79 Last79,"{""age"": 40, ""city"": ""Bangalore"", ""clientId"": ""DAXX89""}"
[email protected],First80 Last80,"{""age"": 21, ""city"": ""Bangalore"", ""clientId"": ""DAXX72""}"
[email protected],First81 Last81,"{""age"": 43, ""city"": ""Bangalore"", ""clientId"": ""DAXX31""}"
[email protected],First82 Last82,"{""age"": 33, ""city"": ""Bangalore"", ""clientId"": ""DAXX89""}"
[email protected],First83 Last83,"{""age"": 38, ""city"": ""Bangalore"", ""clientId"": ""DAXX88""}"
[email protected],First84 Last84,"{""age"": 24, ""city"": ""Bangalore"", ""clientId"": ""DAXX77""}"
[email protected],First85 Last85,"{""age"": 27, ""city"": ""Bangalore"", ""clientId"": ""DAXX40""}"
[email protected],First86 Last86,"{""age"": 67, ""city"": ""Bangalore"", ""clientId"": ""DAXX46""}"
[email protected],First87 Last87,"{""age"": 20, ""city"": ""Bangalore"", ""clientId"": ""DAXX53""}"
[email protected],First88 Last88,"{""age"": 45, ""city"": ""Bangalore"", ""clientId"": ""DAXX79""}"
[email protected],First89 Last89,"{""age"": 31, ""city"": ""Bangalore"", ""clientId"": ""DAXX11""}"
[email protected],First90 Last90,"{""age"": 51, ""city"": ""Bangalore"", ""clientId"": ""DAXX71""}"
[email protected],First91 Last91,"{""age"": 49, ""city"": ""Bangalore"", ""clientId"": ""DAXX20""}"
[email protected],First92 Last92,"{""age"": 26, ""city"": ""Bangalore"", ""clientId"": ""DAXX20""}"
[email protected],First93 Last93,"{""age"": 67, ""city"": ""Bangalore"", ""clientId"": ""DAXX64""}"
[email protected],First94 Last94,"{""age"": 60, ""city"": ""Bangalore"", ""clientId"": ""DAXX53""}"
[email protected],First95 Last95,"{""age"": 64, ""city"": ""Bangalore"", ""clientId"": ""DAXX91""}"
[email protected],First96 Last96,"{""age"": 27, ""city"": ""Bangalore"", ""clientId"": ""DAXX53""}"
[email protected],First97 Last97,"{""age"": 29, ""city"": ""Bangalore"", ""clientId"": ""DAXX46""}"
[email protected],First98 Last98,"{""age"": 26, ""city"": ""Bangalore"", ""clientId"": ""DAXX49""}"
[email protected],First99 Last99,"{""age"": 49, ""city"": ""Bangalore"", ""clientId"": ""DAXX26""}"
diff --git a/tests/global-setup.js b/tests/global-setup.js
index 780e5120..a3cf2ff8 100644
--- a/tests/global-setup.js
+++ b/tests/global-setup.js
@@ -1,20 +1,12 @@
import { chromium } from '@playwright/test';
import { mkdirSync } from 'node:fs';
-
-const BASE_URL = process.env.LISTMONK_URL || 'http://localhost:9000';
+import { login } from './helpers.js';
// Login and create a persistent session for tests.
export default async function globalSetup() {
- const browser = await chromium.launch();
- const page = await browser.newPage();
-
- await page.goto(`${BASE_URL}/admin/login?next=/admin/lists`);
- await page.getByLabel('Username').fill('admin');
- await page.getByLabel('Password').fill('listmonk');
- await page.getByRole('button', { name: 'Login' }).click();
- await page.waitForURL('**/admin/lists');
-
mkdirSync(new URL('.auth', import.meta.url).pathname, { recursive: true });
- await page.context().storageState({ path: new URL('.auth/admin.json', import.meta.url).pathname });
+
+ const browser = await chromium.launch();
+ await login(await browser.newContext());
await browser.close();
}
diff --git a/tests/global-teardown.js b/tests/global-teardown.js
new file mode 100644
index 00000000..6ba98c5c
--- /dev/null
+++ b/tests/global-teardown.js
@@ -0,0 +1,5 @@
+import { execSync } from 'node:child_process';
+
+export default function globalTeardown() {
+ try { execSync('pkill -9 listmonk', { stdio: 'ignore' }); } catch { /* none running */ }
+}
diff --git a/tests/helpers.js b/tests/helpers.js
index 66a2c933..77ee6e72 100644
--- a/tests/helpers.js
+++ b/tests/helpers.js
@@ -1,3 +1,20 @@
+import { execSync, execFileSync, spawn } from 'node:child_process';
+import { setTimeout as sleep } from 'node:timers/promises';
+
+const BASE_URL = process.env.LISTMONK_URL || 'http://localhost:9000';
+const MAILHOG_URL = process.env.MAILHOG_URL || 'http://localhost:8025';
+const ROOT = '..';
+const AUTH_FILE = new URL('.auth/admin.json', import.meta.url).pathname;
+const ENV = { ...process.env, LISTMONK_ADMIN_USER: 'admin', LISTMONK_ADMIN_PASSWORD: 'listmonk' };
+
+// A single SMTP server pointing at MailHog so tests capture (and can assert on) mail.
+const MAILHOG_SMTP = JSON.stringify([{
+ enabled: true, host: 'localhost', port: 1025, auth_protocol: 'none',
+ username: '', password: '', hello_hostname: '', max_conns: 10,
+ idle_timeout: '15s', wait_timeout: '5s', max_msg_retries: 2, msg_retry_delay: '10ms',
+ tls_type: 'none', tls_skip_verify: false, email_headers: [], from_addresses: [],
+}]);
+
// Open a table row's "More" actions dropdown.
export async function openMenu(row) {
await row.getByRole('button', { name: 'More' }).click();
@@ -7,3 +24,62 @@ export async function openMenu(row) {
export async function confirm(page) {
await page.locator('dialog[open] [data-confirm-ok]').click();
}
+
+// Log in and persist the session to the shared storage state file.
+export async function login(context) {
+ const page = await context.newPage();
+ await page.goto(`${BASE_URL}/admin/login?next=/admin/lists`);
+ await page.getByLabel('Username').fill('admin');
+ await page.getByLabel('Password').fill('listmonk');
+ await page.getByRole('button', { name: 'Login' }).click();
+ await page.waitForURL('**/admin/lists');
+ await context.storageState({ path: AUTH_FILE });
+ await page.close();
+}
+
+// Delete all mail captured by MailHog.
+export async function clearMail() {
+ await fetch(`${MAILHOG_URL}/api/v2/messages`, { method: 'DELETE' });
+}
+
+// All mail captured by MailHog, newest first.
+export async function getMail() {
+ return (await (await fetch(`${MAILHOG_URL}/api/v2/messages`)).json()).items;
+}
+
+// Point listmonk's SMTP config at MailHog on the freshly installed DB. DB
+// credentials come from the LISTMONK_db__* env, falling back to config.toml defaults.
+function configureSMTP() {
+ execFileSync('psql', [
+ '-h', process.env.LISTMONK_db__host || 'localhost',
+ '-p', process.env.LISTMONK_db__port || '5432',
+ '-U', process.env.LISTMONK_db__user || 'listmonk',
+ '-d', process.env.LISTMONK_db__database || 'listmonk',
+ '-c', `UPDATE settings SET value = '${MAILHOG_SMTP}' WHERE key = 'smtp';`,
+ ], { env: { ...process.env, PGPASSWORD: process.env.LISTMONK_db__password || 'listmonk' }, stdio: 'ignore' });
+}
+
+// Wipe and reinstall the DB by restarting listmonk, then refresh the login
+// session (a reinstall clears the DB-backed session). The schema can't be
+// reinstalled under a running server, so the process is killed and relaunched.
+export async function resetDB(browser) {
+ try { execSync('pkill -9 listmonk', { stdio: 'ignore' }); } catch { /* none running */ }
+ execSync('./listmonk --install --yes', { cwd: ROOT, env: ENV, stdio: 'ignore' });
+ configureSMTP();
+ spawn('./listmonk', ['--static-dir', 'static'], {
+ cwd: ROOT, env: ENV, detached: true, stdio: 'ignore',
+ }).unref();
+
+ for (let i = 0; i < 100; i += 1) {
+ try {
+ if ((await fetch(`${BASE_URL}/health`)).ok) break;
+ } catch { /* not up yet */ }
+ await sleep(200);
+ }
+
+ await clearMail();
+
+ const context = await browser.newContext();
+ await login(context);
+ await context.close();
+}
diff --git a/tests/package.json b/tests/package.json
index 9e15071c..10eb5608 100644
--- a/tests/package.json
+++ b/tests/package.json
@@ -2,7 +2,7 @@
"name": "listmonk-tests",
"private": true,
"type": "module",
- "description": "Playwright end-to-end tests for the listmonk SSR admin.",
+ "description": "E2E tests for listmonk admin.",
"scripts": {
"test": "playwright test",
"test:ui": "playwright test --ui"
diff --git a/tests/playwright.config.js b/tests/playwright.config.js
index 72b8088e..5298e079 100644
--- a/tests/playwright.config.js
+++ b/tests/playwright.config.js
@@ -2,6 +2,7 @@ import { defineConfig, devices } from '@playwright/test';
// listmonk root address (see ../config.toml).
const BASE_URL = process.env.LISTMONK_URL || 'http://localhost:9000';
+const MAILHOG_URL = process.env.MAILHOG_URL || 'http://localhost:8025';
export default defineConfig({
testDir: './specs',
@@ -11,14 +12,25 @@ export default defineConfig({
// Log in once and save the session before any tests run.
globalSetup: './global-setup.js',
- // Create a fresh listmonk DB instance for every test run.
- webServer: {
- command: './listmonk --install --yes && ./listmonk --static-dir static',
- cwd: '..',
- url: `${BASE_URL}/health`,
- reuseExistingServer: false,
- env: { LISTMONK_ADMIN_USER: 'admin', LISTMONK_ADMIN_PASSWORD: 'listmonk' },
- },
+ // Kill the listmonk instance resetDB().
+ globalTeardown: './global-teardown.js',
+
+ webServer: [
+ // Run mailhog local SMTP test server.
+ {
+ command: 'pkill -9 mailhog; mailhog',
+ url: MAILHOG_URL,
+ reuseExistingServer: false,
+ },
+ // Create a fresh listmonk DB instance for every test run.
+ {
+ command: 'pkill -9 listmonk; ./listmonk --install --yes && ./listmonk --static-dir static',
+ cwd: '..',
+ url: `${BASE_URL}/health`,
+ reuseExistingServer: false,
+ env: { LISTMONK_ADMIN_USER: 'admin', LISTMONK_ADMIN_PASSWORD: 'listmonk' },
+ },
+ ],
use: {
baseURL: BASE_URL,
diff --git a/tests/specs/forms.spec.js b/tests/specs/forms.spec.js
new file mode 100644
index 00000000..2468d614
--- /dev/null
+++ b/tests/specs/forms.spec.js
@@ -0,0 +1,237 @@
+import { test, expect } from '@playwright/test';
+import { resetDB, clearMail, getMail } from '../helpers.js';
+
+const FORMS = '/admin/lists/forms';
+const SUB_FORM = '/subscription/form';
+
+const REDIRECT_URLS = [
+ 'http://localhost:9000/thank-you',
+ 'https://example.com/welcome',
+];
+
+function publicLists(page) {
+ return page.locator('ul[data-testid=lists] li');
+}
+
+function formHTML(page) {
+ return page.locator('[data-testid=form] [role=textbox]');
+}
+
+function redirectURLs(page) {
+ return page.locator('[data-testid=redirect-urls] input[type=radio]');
+}
+
+// Saving trusted URLs SIGHUPs the process, which re-execs. Wait for the reload
+// to expose the new redirect URLs in the server config.
+async function setTrustedURLs(page, urls) {
+ const res = await page.request.put('/api/settings/security.trusted_urls', { data: urls });
+ expect(res.ok()).toBeTruthy();
+
+ await expect.poll(async () => {
+ try {
+ const { data } = await (await page.request.get('/api/config')).json();
+ return data.public_subscription?.redirect_urls ?? [];
+ } catch {
+ // The server is mid-reload and not accepting connections yet.
+ return [];
+ }
+ }, { timeout: 20000, intervals: [500] }).toEqual(urls);
+}
+
+async function publicListUUID(page) {
+ const { data } = await (await page.request.get('/api/lists')).json();
+ return data.results.find((l) => l.type === 'public').uuid;
+}
+
+async function subscribe(page, { email, name }) {
+ await page.goto(SUB_FORM);
+ await page.locator('input[name=email]').fill(email);
+ await page.locator('input[name=name]').fill(name);
+ await Promise.all([
+ page.waitForLoadState(),
+ page.locator('button[type=submit]').click(),
+ ]);
+ // A missing SMTP config surfaces a retry message instead of a success one.
+ await expect(page.locator('body')).toContainText(/has been sent|successfully|retry/i);
+}
+
+test.describe.configure({ mode: 'serial' });
+
+test.describe('Forms', () => {
+ test.beforeAll(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ test('lists only public lists and hides the form until one is picked', async ({ page }) => {
+ await page.goto(FORMS);
+
+ await expect(publicLists(page)).toHaveCount(1);
+ await expect(publicLists(page)).toContainText('Opt-in list');
+ await expect(formHTML(page)).toBeHidden();
+ });
+
+ test('hides the redirect URL options when none are whitelisted', async ({ page }) => {
+ await page.goto(FORMS);
+
+ // No trusted URLs on a fresh install, so the picker isn't rendered at all.
+ await expect(page.locator('[data-testid=redirect-urls]')).toHaveCount(0);
+ });
+
+ test('generates form HTML for the selected list', async ({ page }) => {
+ await page.goto(FORMS);
+
+ const checkbox = publicLists(page).locator('input');
+ const uuid = await checkbox.inputValue();
+
+ await checkbox.check();
+ await expect(formHTML(page)).toBeVisible();
+ await expect(formHTML(page)).toContainText(uuid);
+
+ await checkbox.uncheck();
+ await expect(formHTML(page)).toBeHidden();
+ });
+
+ test('subscribes new subscribers via the public form', async ({ page }) => {
+ // Add a second public list so the form offers a choice.
+ const res = await page.request.post('/api/lists', {
+ data: { name: 'test-list', type: 'public', optin: 'single' },
+ });
+ expect(res.ok()).toBeTruthy();
+
+ await clearMail();
+ await page.goto(SUB_FORM);
+ await expect(page.locator('ul.lists input[type=checkbox]')).toHaveCount(2);
+
+ for (let i = 0; i < 2; i += 1) {
+ await subscribe(page, { email: `test${i}@test.com`, name: `test${i}` });
+ }
+
+ const { data } = await (await page.request.get('/api/subscribers')).json();
+
+ // Two new + two seeded subscribers.
+ expect(data.total).toBe(4);
+ for (let i = 0; i < 2; i += 1) {
+ expect(data.results.find((s) => s.email === `test${i}@test.com`).lists.length).toBe(2);
+ }
+
+ // The double opt-in list sent each of them a confirmation email.
+ await expect.poll(async () => {
+ const tos = (await getMail()).map((m) => m.Content.Headers.To[0]);
+ return ['test0', 'test1'].every((n) => tos.some((t) => t.includes(`${n}@test.com`)));
+ }).toBe(true);
+ });
+
+ test('unsubscribes from a list and then blocklists', async ({ page }) => {
+ // Point the seeded campaign at the opt-in list the subscribers are on.
+ await page.request.put('/api/campaigns/1', { data: { lists: [2] } });
+
+ const subs = (await (await page.request.get('/api/subscribers')).json()).data.results;
+ const sub = subs.find((s) => s.email === '[email protected]');
+ const camp = (await (await page.request.get('/api/campaigns')).json()).data.results[0];
+ const url = `/subscription/${camp.uuid}/${sub.uuid}`;
+
+ // Unsubscribe from the campaign's list only.
+ await page.goto(url);
+ await page.locator('#btn-unsub').click();
+
+ let lists = (await (await page.request.get('/api/subscribers')).json())
+ .data.results.find((s) => s.email === '[email protected]').lists;
+ expect(lists.find((l) => l.id === 2).subscription_status).toBe('unsubscribed');
+ expect(lists.find((l) => l.id === 3).subscription_status).toBe('unconfirmed');
+
+ // Unsubscribe from everything and blocklist.
+ await page.goto(url);
+ await page.locator('#privacy-blocklist').check();
+ await page.locator('#btn-unsub').click();
+
+ const sub0 = (await (await page.request.get('/api/subscribers')).json())
+ .data.results.find((s) => s.email === '[email protected]');
+ expect(sub0.status).toBe('blocklisted');
+ expect(sub0.lists.find((l) => l.id === 2).subscription_status).toBe('unsubscribed');
+ expect(sub0.lists.find((l) => l.id === 3).subscription_status).toBe('unsubscribed');
+ });
+
+ test('manages subscription preferences', async ({ page }) => {
+ const subs = (await (await page.request.get('/api/subscribers')).json()).data.results;
+ const sub = subs.find((s) => s.email === '[email protected]');
+ const camp = (await (await page.request.get('/api/campaigns')).json()).data.results[0];
+
+ await page.goto(`/subscription/${camp.uuid}/${sub.uuid}?manage=true`);
+
+ await page.locator('input[name=name]').fill('new-name');
+ await page.locator('ul.lists input').first().uncheck();
+ await page.locator('#btn-unsub').click();
+
+ const sub1 = (await (await page.request.get('/api/subscribers')).json())
+ .data.results.find((s) => s.email === '[email protected]');
+ expect(sub1.name).toBe('new-name');
+ expect(sub1.lists.find((l) => l.id === 2).subscription_status).toBe('unsubscribed');
+ expect(sub1.lists.find((l) => l.id === 3).subscription_status).toBe('unconfirmed');
+ });
+});
+
+test.describe('Forms redirect URL whitelist', () => {
+ test.beforeAll(async ({ browser }) => {
+ await resetDB(browser);
+ // Reuse the logged-in session persisted by resetDB so the settings API is authorized.
+ const context = await browser.newContext({
+ storageState: new URL('../.auth/admin.json', import.meta.url).pathname,
+ });
+ const page = await context.newPage();
+ await setTrustedURLs(page, REDIRECT_URLS);
+ await context.close();
+ });
+
+ test('lists whitelisted URLs as redirect options', async ({ page }) => {
+ await page.goto(FORMS);
+
+ // A "None" radio plus one per whitelisted URL, with "None" selected by default.
+ await expect(redirectURLs(page)).toHaveCount(REDIRECT_URLS.length + 1);
+ await expect(page.locator('[data-testid=redirect-urls] input[value=""]')).toBeChecked();
+ for (const url of REDIRECT_URLS) {
+ await expect(page.locator(`[data-testid=redirect-urls] input[value="${url}"]`)).toHaveCount(1);
+ }
+ });
+
+ test('injects the selected redirect URL as a hidden "next" field', async ({ page }) => {
+ await page.goto(FORMS);
+
+ // A list has to be picked before any form HTML is generated.
+ await publicLists(page).locator('input').first().check();
+ await expect(formHTML(page)).toBeVisible();
+ await expect(formHTML(page)).not.toContainText('name="next"');
+
+ await page.locator(`[data-testid=redirect-urls] input[value="${REDIRECT_URLS[1]}"]`).check();
+ await expect(formHTML(page)).toContainText(`<input type="hidden" name="next" value="${REDIRECT_URLS[1]}" />`);
+
+ // Switching back to "None" removes it again.
+ await page.locator('[data-testid=redirect-urls] input[value=""]').check();
+ await expect(formHTML(page)).not.toContainText('name="next"');
+ });
+
+ test('redirects to a whitelisted "next" URL after submission', async ({ page }) => {
+ const uuid = await publicListUUID(page);
+
+ const res = await page.request.post(SUB_FORM, {
+ form: { email: '[email protected]', name: 'redirect', l: uuid, next: REDIRECT_URLS[0] },
+ maxRedirects: 0,
+ });
+
+ expect(res.status()).toBe(303);
+ expect(res.headers().location).toBe(REDIRECT_URLS[0]);
+ });
+
+ test('ignores a non-whitelisted "next" URL to prevent open redirects', async ({ page }) => {
+ const uuid = await publicListUUID(page);
+
+ const res = await page.request.post(SUB_FORM, {
+ form: { email: '[email protected]', name: 'noredirect', l: uuid, next: 'https://evil.example.com/phish' },
+ maxRedirects: 0,
+ });
+
+ // No redirect: the normal confirmation page is rendered instead.
+ expect(res.status()).toBe(200);
+ expect(res.headers().location).toBeUndefined();
+ expect(await res.text()).toMatch(/subscri|confirm/i);
+ });
+});
diff --git a/tests/specs/import.spec.js b/tests/specs/import.spec.js
new file mode 100644
index 00000000..7c826cf6
--- /dev/null
+++ b/tests/specs/import.spec.js
@@ -0,0 +1,81 @@
+import { test, expect } from '@playwright/test';
+import { confirm, resetDB } from '../helpers.js';
+
+const IMPORT = '/admin/subscribers/import';
+const SUBSCRIBERS = '/admin/subscribers';
+const CSV = new URL('../fixtures/subs.csv', import.meta.url).pathname;
+
+// Pick a list in the taginput selector. Filling the input matches a datalist
+// option, which the component turns into a tag carrying the list's ID.
+async function selectList(page, name) {
+ const input = page.locator('ot-taginput input');
+ await input.click();
+ await input.fill(name);
+ await expect(page.locator('ot-taginput .badge')).toContainText(name);
+}
+
+test.describe.configure({ mode: 'serial' });
+
+test.describe('Import', () => {
+ test.beforeAll(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ test('imports subscribers across modes and statuses', async ({ page }) => {
+ const cases = [
+ { mode: 'subscribe', subStatus: 'unconfirmed', overwrite: true },
+ { mode: 'subscribe', subStatus: 'confirmed', overwrite: true },
+ { mode: 'subscribe', subStatus: 'unconfirmed', overwrite: false },
+ { mode: 'blocklist', subStatus: 'unsubscribed', overwrite: false },
+ ];
+
+ for (const c of cases) {
+ await page.goto(IMPORT);
+ await page.getByTestId(`check-${c.mode}`).check();
+ await page.getByTestId(`check-${c.subStatus}`).check();
+
+ if (c.overwrite) {
+ await page.getByTestId('overwrite-user-info').check();
+ await page.getByTestId('overwrite-sub-status').check();
+ }
+
+ if (c.mode === 'subscribe') {
+ await selectList(page, 'Default list');
+ }
+
+ await page.locator('input[type=file]').setInputFiles(CSV);
+ await page.getByTestId('btn-upload').click();
+
+ // Overwriting the subscription status prompts for confirmation.
+ if (c.overwrite) {
+ await confirm(page);
+ }
+
+ // Wait for the import to finish, then clear it. Clearing reloads the
+ // page back to the upload form.
+ await expect(page.getByTestId('btn-done')).toBeVisible();
+ await page.getByTestId('btn-done').click();
+ await expect(page.getByTestId('btn-upload')).toBeVisible();
+
+ // 100 imported + 2 seeded subscribers.
+ await page.goto(SUBSCRIBERS);
+ await expect(page.locator('.page-title')).toContainText('(102)');
+ }
+ });
+
+ test.describe('rejects a malformed file', () => {
+ test.beforeAll(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
+ test('fails on a wrong delimiter', async ({ page }) => {
+ await page.goto(IMPORT);
+ await selectList(page, 'Default list');
+ await page.locator('input[name=delim]').fill('|');
+ await page.locator('input[type=file]').setInputFiles(CSV);
+ await page.getByTestId('btn-upload').click();
+
+ await expect(page.locator('.stat-value')).toContainText('failed');
+ });
+ });
+});
diff --git a/tests/specs/lists.spec.js b/tests/specs/lists.spec.js
index 724407c6..7c94d737 100644
--- a/tests/specs/lists.spec.js
+++ b/tests/specs/lists.spec.js
@@ -1,5 +1,5 @@
import { test, expect } from '@playwright/test';
-import { openMenu, confirm } from '../helpers.js';
+import { openMenu, confirm, resetDB } from '../helpers.js';
const LISTS = '/admin/lists';
@@ -39,6 +39,10 @@ async function withListAPI(page, method, action) {
test.describe.configure({ mode: 'serial' });
test.describe('Lists', () => {
+ test.beforeAll(async ({ browser }) => {
+ await resetDB(browser);
+ });
+
test('shows the default lists and their subscriber counts', async ({ page }) => {
await page.goto(LISTS);