Skip to content

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

20.6 KB
Raw Blame
import { test, expect } from '@playwright/test';
import { resetDB, getMail, confirm } from '../helpers.js';

const SETTINGS = '/admin/settings';
const smtpServers = (page) => page.locator('.mail-servers details');
const tab = (page, name) => page.getByRole('tab', { name, exact: true });

async function settings(page) {
  const res = await page.request.get('/api/settings');
  expect(res.ok()).toBeTruthy();
  return (await res.json()).data;
}

// Saving reloads the backend. Wait for the UI's health check before navigating.
async function save(page, status = 200) {
  const response = page.waitForResponse((r) => new URL(r.url()).pathname === '/api/settings'
    && r.request().method() === 'PUT');
  await page.getByRole('button', { name: 'Save', exact: true }).click();
  expect((await response).status()).toBe(status);
  if (status === 200) {
    await expect(page.getByRole('button', { name: 'Save', exact: true })).toBeDisabled();
    await expect(page.getByText('"Settings" updated', { exact: true })).toBeVisible();
  }
  await expect(page.getByRole('button', { name: 'Save', exact: true })).toBeEnabled();
}

async function remove(page, item, accept = true) {
  await item.getByRole('button', { name: 'Delete', exact: true }).click();
  await page.getByRole('dialog').getByRole('button', { name: accept ? 'Ok' : 'Cancel', exact: true }).click();
}

test.describe('Settings', () => {
  // Reset before creating the page/context so it uses the refreshed login.
  test.beforeEach(async ({ browser }) => { await resetDB(browser); });
  test.beforeEach(async ({ page }) => { await page.goto(SETTINGS); });

  test('validates sender defaults and preserves configured and custom campaign senders', async ({ page }) => {
    const first = 'Default <[email protected]>';
    const second = 'News <[email protected]>';
    const custom = 'Custom <[email protected]>';
    const templated = '{{ .Subscriber.Attribs.sender }}';
    const current = await settings(page);
    const longSender = `${'a'.repeat(2000)} <[email protected]>`;
    for (const addresses of [null, [], [' '], ['invalid'], ['{{ .Subscriber.Email }}'],
      [' ', '{{ .Subscriber.Email }}'], ['{{ .Subscriber.Name }} <[email protected]>'], [first, 'invalid'],
      [longSender], [first, `${templated}${longSender}`], Array(251).fill(first)]) {
      const res = await page.request.put('/api/settings', {
        data: { ...current, 'app.from_email': addresses },
      });
      expect(res.status()).toBe(400);
    }

    const maxAddresses = Array.from({ length: 250 }, (_, i) => `sender${i}@example.com`);
    const res = await page.request.put('/api/settings', {
      data: { ...current, 'app.from_email': maxAddresses },
    });
    expect(res.status()).toBe(200);
    expect((await settings(page))['app.from_email']).toEqual(maxAddresses);

    await page.locator('[name="app.from_email"]').fill(`\n ${first} \n${second}\n${first}\n${templated}\n`);
    await save(page);
    expect((await settings(page))['app.from_email']).toEqual([first, second, templated]);
    await page.reload();
    await expect(page.locator('[name="app.from_email"]')).toHaveValue(`${first}\n${second}\n${templated}`);

    await page.goto('/admin/campaigns');
    await page.getByRole('link', { name: 'New', exact: true }).click();
    const input = page.locator('input[name="from_email"]');
    await expect(input).toHaveValue(first);
    await expect(input).toHaveAttribute('maxlength', '2000');
    await expect(input).toHaveAttribute('list', 'campaign-from-emails');
    const options = page.locator('#campaign-from-emails option');
    await expect(options).toHaveCount(3);
    await expect(options.nth(0)).toHaveAttribute('value', first);
    await expect(options.nth(1)).toHaveAttribute('value', second);
    await expect(options.nth(2)).toHaveAttribute('value', templated);

    await page.goto('/admin/campaigns/1');
    // Native datalist selections set the input value, just like typing it.
    for (const address of [second, custom]) {
      await input.fill(address);
      const response = page.waitForResponse((r) => new URL(r.url()).pathname === '/api/campaigns/1'
        && r.request().method() === 'PUT');
      await page.getByRole('button', { name: 'Save changes', exact: true }).click();
      expect((await response).ok()).toBeTruthy();
      await page.reload();
      await expect(input).toHaveValue(address);
    }
  });

  test('saves edits across tabs, enables SMTP and deletes a server', async ({ page }) => {
    const root = 'http://127.0.0.1:9000';
    const favicon = `${root}/public/static/logo.png`;
    await page.locator('[name="app.root_url"]').fill(root);
    await page.locator('[name="app.favicon_url"]').fill(favicon);
    await tab(page, 'Performance').click();
    await page.locator('[name="app.concurrency"]').fill('9');
    await tab(page, 'SMTP').click();
    await page.getByRole('button', { name: 'Add new' }).click();
    const servers = smtpServers(page);
    const added = servers.nth(1);
    await added.getByPlaceholder('smtp.yourmailserver.net').fill('localhost');
    await added.getByRole('spinbutton', { name: /^Port/ }).fill('1025');
    await added.getByRole('group', { name: 'TLS', exact: true }).getByRole('combobox').selectOption('none');
    await added.getByRole('switch', { name: 'Enabled', exact: true }).uncheck();
    await expect(added.getByPlaceholder('smtp.yourmailserver.net')).toBeDisabled();
    await expect(added.getByRole('button', { name: 'Test connection' })).toBeDisabled();
    await added.getByRole('switch', { name: 'Enabled', exact: true }).check();
    await remove(page, servers.first(), false);
    await expect(servers).toHaveCount(2);
    await remove(page, servers.first());
    await expect(servers).toHaveCount(1);
    await expect(servers.getByRole('button', { name: 'Delete' })).toBeHidden();
    await save(page);
    expect(await settings(page)).toMatchObject({
      'app.root_url': root, 'app.favicon_url': favicon, 'app.concurrency': 9,
      smtp: [{ enabled: true, host: 'localhost', port: 1025, tls_type: 'none' }],
    });
    await page.reload();
    await expect(servers.getByRole('switch', { name: 'Enabled', exact: true })).toBeChecked();
    await tab(page, 'General').click();
    await expect(page.locator('[name="app.root_url"]')).toHaveValue(root);
    await expect(page.locator('[name="app.favicon_url"]')).toHaveValue(favicon);
    await tab(page, 'Performance').click();
    await expect(page.locator('[name="app.concurrency"]')).toHaveValue('9');
  });

  test('searches across tabs using all terms', async ({ page }) => {
    const search = page.getByRole('searchbox', { name: 'Search' });
    await search.fill('  RATE   workers  ');
    await expect(page.locator('[name="app.concurrency"]')).toBeVisible();
    await expect(tab(page, 'General')).toBeHidden();
    await expect(tab(page, 'Performance')).toBeVisible();
    await search.fill('email');
    await expect(page.locator('[name="app.from_email"]')).toBeVisible();
    await expect(tab(page, 'SMTP')).toBeVisible();
    await tab(page, 'SMTP').click();
    await expect(search).toBeEmpty();
    await expect(smtpServers(page)).toBeVisible();
    await search.fill('no-such-setting');
    await expect(tab(page, 'General')).toBeVisible();
    await expect(smtpServers(page)).toBeVisible();
    await search.fill('concurrency');
    await search.fill('');
    await expect(smtpServers(page)).toBeVisible();
    await expect(page.locator('[name="app.concurrency"]')).toBeHidden();
  });

  test('SMTP presets reset credentials and preserve unrelated fields and accordion state', async ({ page }) => {
    await tab(page, 'SMTP').click();
    const server = smtpServers(page);
    await server.locator('summary').click();
    await server.getByPlaceholder('email-primary').fill('email-primary');
    await server.getByRole('group', { name: 'Auth protocol', exact: true }).getByRole('combobox').selectOption('login');
    await server.getByRole('textbox', { name: /^Username/ }).fill('old-user');
    await server.getByLabel(/Password/).fill('old-secret');
    for (const [provider, host, port, auth, tls] of [
      ['Gmail', 'smtp.gmail.com', '465', 'login', 'TLS'],
      ['Postmark', 'smtp.postmarkapp.com', '587', 'cram', 'STARTTLS'],
    ]) {
      await server.getByRole('link', { name: provider, exact: true }).click();
      await expect(server.getByPlaceholder('smtp.yourmailserver.net')).toHaveValue(host);
      await expect(server.getByRole('spinbutton', { name: /^Port/ })).toHaveValue(port);
      await expect(server.getByRole('group', { name: 'Auth protocol', exact: true }).getByRole('combobox')).toHaveValue(auth);
      await expect(server.getByRole('group', { name: 'TLS', exact: true }).getByRole('combobox')).toHaveValue(tls);
      await expect(server.getByRole('textbox', { name: /^Username/ })).toBeFocused();
      await expect(server.getByRole('textbox', { name: /^Username/ })).toBeEmpty();
      await expect(server.getByLabel(/Password/)).toBeEmpty();
      await expect(server.getByPlaceholder('email-primary')).toHaveValue('email-primary');
    }
    await server.getByRole('group', { name: 'Auth protocol', exact: true }).getByRole('combobox').selectOption('none');
    await expect(server.getByRole('textbox', { name: /^Username/ })).toBeDisabled();
    await expect(server.getByLabel(/Password/)).toBeDisabled();
    await page.reload();
    await expect(server.getByPlaceholder('smtp.yourmailserver.net')).toBeVisible();
    await server.locator('summary').click();
    await page.reload();
    await expect(server.getByPlaceholder('smtp.yourmailserver.net')).toBeHidden();
  });

  test('tests custom SMTP headers and masks saved secrets', async ({ page }) => {
    await tab(page, 'SMTP').click();
    const server = smtpServers(page);
    await server.locator('summary').click();
    await server.getByRole('group', { name: 'Auth protocol', exact: true }).getByRole('combobox').selectOption('login');
    await server.getByRole('textbox', { name: /^Username/ }).fill('test-user');
    await server.getByLabel(/Password/).fill('settings-test-secret');
    await server.getByRole('link', { name: 'Set custom headers' }).click();
    const headers = server.getByPlaceholder('[{"X-Custom": "value"}]');
    await headers.fill('[{"X-Settings-Test":"saved"}]');
    await save(page);
    await page.reload();
    await expect(server.getByLabel(/Password/)).toHaveValue(/^•+$/);
    expect(JSON.parse(await headers.inputValue())).toEqual([{ 'X-Settings-Test': 'saved' }]);
    await server.getByLabel(/Password/).fill('••partial');
    await page.getByRole('button', { name: 'Save', exact: true }).click();
    await expect(page.getByText(/Clear and re-enter the full password/)).toBeVisible();
    await server.getByLabel(/Password/).fill('replacement-secret');
    await save(page);
    await page.reload();
    await expect(server.getByLabel(/Password/)).toHaveValue(/^•+$/);
    // An unrelated save sends no replacement for the masked password.
    const request = page.waitForRequest((r) => r.url().endsWith('/api/settings') && r.method() === 'PUT');
    await save(page);
    expect((await request).postDataJSON().smtp[0].password).toBe('');
    await page.reload();
    await expect(server.getByLabel(/Password/)).toHaveValue(/^•+$/);
  });

  test('normalizes domain lists and retains tracking preferences while disabled', async ({ page }) => {
    await tab(page, 'Privacy').click();
    const individual = page.getByRole('switch', { name: /^Individual subscriber tracking/ });
    await individual.check();
    await page.getByRole('switch', { name: /^Disable tracking/ }).check();
    await expect(individual).toBeDisabled();
    await expect(individual).toBeChecked();
    const domainText = page.locator('textarea:visible');
    await domainText.fill('  EXAMPLE.COM  \n\n spam.test\n  ');
    await expect(page.getByRole('tab', { name: /Domain blocklist \(\s*2\s*\)/ })).toBeVisible();
    await page.getByRole('tab', { name: /^Domain allowlist/ }).click();
    await domainText.fill(' TRUSTED.TEST \n\n');
    await save(page);
    expect(await settings(page)).toMatchObject({
      'privacy.domain_blocklist': ['example.com', 'spam.test'],
      'privacy.domain_allowlist': ['trusted.test'],
      'privacy.disable_tracking': true, 'privacy.individual_tracking': true,
    });
    await page.reload();
    await expect(domainText).toHaveValue('example.com\nspam.test');
    await page.getByRole('switch', { name: /^Disable tracking/ }).uncheck();
    await expect(individual).toBeEnabled();
    await expect(individual).toBeChecked();
  });

  test('enables sliding window controls and persists their values', async ({ page }) => {
    await tab(page, 'Performance').click();
    const group = page.getByRole('group', { name: 'Enable sliding window limit', exact: true });
    await expect(group.getByRole('textbox', { name: /^Duration/ })).toBeDisabled();
    await group.getByRole('switch').check();
    await group.getByRole('spinbutton', { name: /^Max. messages/ }).fill('42');
    await group.getByRole('textbox', { name: /^Duration/ }).fill('2h');
    await save(page);
    await page.reload();
    await expect(group.getByRole('switch')).toBeChecked();
    await expect(group.getByRole('spinbutton', { name: /^Max. messages/ })).toHaveValue('42');
    await expect(group.getByRole('textbox', { name: /^Duration/ })).toHaveValue('2h');
    await group.getByRole('switch').uncheck();
    await expect(group.getByRole('textbox', { name: /^Duration/ })).toBeDisabled();
    await expect(group.getByRole('textbox', { name: /^Duration/ })).toHaveValue('2h');
  });

  test('adds, disables and deletes messengers with confirmation', async ({ page }) => {
    await tab(page, 'Messengers').click();
    await page.getByRole('button', { name: 'Add new' }).click();
    const messenger = page.locator('.messengers details');
    await expect(messenger.getByPlaceholder('mymessenger')).toBeFocused();
    await messenger.getByPlaceholder('mymessenger').fill('sms-test');
    await messenger.getByPlaceholder('https://postback.messenger.net/path').fill('http://localhost:9999/send');
    await messenger.getByRole('switch', { name: 'Enabled' }).uncheck();
    await save(page);
    await page.reload();
    await expect(messenger).toHaveCount(1);
    await expect(messenger.getByRole('switch', { name: 'Enabled' })).not.toBeChecked();
    expect((await settings(page)).messengers).toMatchObject([
      { name: 'sms-test', root_url: 'http://localhost:9999/send', enabled: false },
    ]);
    await remove(page, messenger, false);
    await expect(messenger).toHaveCount(1);
    await remove(page, messenger);
    await expect(messenger).toHaveCount(0);
    await save(page);
    await page.reload();
    await expect(messenger).toHaveCount(0);
    expect((await settings(page)).messengers).toEqual([]);
  });

  test('persists trusted URLs as separate entries', async ({ page }) => {
    await tab(page, 'Security').click();
    const urls = page.getByPlaceholder('https://example.com', { exact: true });
    await urls.fill('https://one.example.com\nhttps://two.example.com/thanks');
    await save(page);
    expect((await settings(page))['security.trusted_urls']).toEqual([
      'https://one.example.com', 'https://two.example.com/thanks',
    ]);
    await page.reload();
    await expect(urls).toHaveValue('https://one.example.com\nhttps://two.example.com/thanks');
    await urls.fill('');
    await save(page);
    await page.reload();
    await expect(urls).toBeEmpty();
  });

  test('persists bounce actions and gates mailbox and webhook controls', async ({ page }) => {
    await tab(page, 'Bounces').click();
    await page.getByRole('switch', { name: 'Enable bounce processing', exact: true }).check();
    const hard = page.getByRole('group', { name: 'Hard', exact: true });
    await hard.getByRole('spinbutton').fill('3');
    await hard.getByRole('combobox').selectOption('unsubscribe');
    const mailbox = page.getByRole('switch', { name: 'Enable bounce mailbox', exact: true });
    await mailbox.check();
    await expect(page.getByPlaceholder('bounce.yourmailserver.net')).toBeVisible();
    await mailbox.uncheck();
    await expect(page.getByPlaceholder('bounce.yourmailserver.net')).toHaveCount(0);
    const webhooks = page.getByRole('switch', { name: /^Enable bounce webhooks/ });
    await webhooks.check();
    await page.getByRole('group', { name: 'SES', exact: true }).getByRole('switch').check();
    await save(page);
    expect(await settings(page)).toMatchObject({
      'bounce.enabled': true, 'bounce.webhooks_enabled': true, 'bounce.ses': { enabled: true, topic_arns: [] },
      'bounce.actions': { hard: { count: 3, action: 'unsubscribe' } },
      'bounce.mailboxes': [{ enabled: false }],
    });
    await page.reload();
    await expect(hard.getByRole('spinbutton')).toHaveValue('3');
    await expect(hard.getByRole('combobox')).toHaveValue('unsubscribe');
    await webhooks.uncheck();
    await expect(page.getByRole('group', { name: 'SES', exact: true })).toHaveCount(0);
  });

  test('saves independent admin and public appearance editors', async ({ page }) => {
    await tab(page, 'Appearance').click();
    const css = page.getByRole('group', { name: 'Custom CSS', exact: true }).getByRole('textbox');
    const js = page.getByRole('group', { name: 'Custom JavaScript', exact: true }).getByRole('textbox');
    await css.fill('/* admin settings test */');
    await js.fill('// admin settings test');
    await tab(page, 'Public').click();
    await css.fill('/* public settings test */');
    await js.fill('// public settings test');
    await save(page);
    expect(await settings(page)).toMatchObject({
      'appearance.admin.custom_css': '/* admin settings test */',
      'appearance.admin.custom_js': '// admin settings test',
      'appearance.public.custom_css': '/* public settings test */',
      'appearance.public.custom_js': '// public settings test',
    });
    await page.reload();
    await expect(css).toHaveText('/* admin settings test */');
    await expect(js).toHaveText('// admin settings test');
    await tab(page, 'Public').click();
    await expect(css).toHaveText('/* public settings test */');
    await expect(js).toHaveText('// public settings test');
  });

});

test.describe('Maintenance: bulk deletion', () => {
  test.beforeEach(async ({ browser }) => { await resetDB(browser); });

  test('limits subscriber cleanup to its selected type and subscription cleanup to its cutoff', async ({ page }) => {
    const data = async (res) => {
      expect(res.ok(), await res.text()).toBeTruthy();
      return (await res.json()).data;
    };
    const original = (await data(await page.request.get('/api/subscribers'))).results;
    const blocked = await data(await page.request.post('/api/subscribers', { data: {
      email: '[email protected]', name: 'Blocked', status: 'blocklisted', lists: [1],
    } }));
    const orphan = await data(await page.request.post('/api/subscribers', { data: {
      email: '[email protected]', name: 'Orphan', status: 'enabled', lists: [],
    } }));
    await page.goto('/admin/settings/maintenance');
    const subscribers = page.getByRole('group', { name: 'Subscribers', exact: true });
    for (const type of ['blocklisted', 'orphan']) {
      await subscribers.getByRole('combobox', { name: 'Data', exact: true }).selectOption(type);
      await subscribers.getByRole('button', { name: 'Delete', exact: true }).click();
      const [res] = await Promise.all([
        page.waitForResponse((r) => r.url().endsWith(`/api/maintenance/subscribers/${type}`)), confirm(page),
      ]);
      expect(res.ok()).toBeTruthy();
      const remaining = (await data(await page.request.get('/api/subscribers'))).results;
      expect(remaining.some((s) => s.id === blocked.id)).toBe(false);
      expect(remaining.some((s) => s.id === orphan.id)).toBe(type !== 'orphan');
      expect(remaining.filter((s) => original.some((o) => o.id === s.id))).toEqual(original);
    }
    expect((await page.request.delete('/api/maintenance/subscriptions/unconfirmed?before_date=invalid')).status()).toBe(400);
    await data(await page.request.delete('/api/maintenance/subscriptions/unconfirmed?before_date=1970-01-01T00:00:00Z'));
    expect((await data(await page.request.get('/api/subscribers'))).results).toEqual(original);
    const future = new Date(Date.now() + 86400000).toISOString();
    await data(await page.request.delete(`/api/maintenance/subscriptions/unconfirmed?before_date=${encodeURIComponent(future)}`));
    const remaining = (await data(await page.request.get('/api/subscribers'))).results;
    expect(remaining.map((s) => s.id)).toEqual(original.map((s) => s.id));
    for (const sub of remaining) {
      const before = original.find((s) => s.id === sub.id);
      expect(sub.lists.map((l) => l.id)).toEqual(before.lists.filter((l) => !(l.optin === 'double' && l.subscription_status === 'unconfirmed')).map((l) => l.id));
    }
  });
});