U1 mail-foundation (frontend en documentatie): Mail-kaart bij Instellingen
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { http, HttpResponse } from 'msw';
|
||||
import { renderApp, mockAuthenticated } from '@/test/utils';
|
||||
import { server } from '@/mocks/server';
|
||||
import { API_BASE, makeAuthResponse, mockUser } from '@/features/auth/mocks/fixtures';
|
||||
import { _resetSetupStatusCache } from '@/router';
|
||||
import type { MailSettings } from '@/features/settings/services/types';
|
||||
|
||||
beforeEach(() => {
|
||||
_resetSetupStatusCache();
|
||||
});
|
||||
|
||||
const baseSettings: MailSettings = {
|
||||
emailEnabled: true,
|
||||
fromAddress: 'no-reply@slpsoftware.nl',
|
||||
fromName: 'Andrea Slop Fotografie',
|
||||
defaultFromAddress: 'no-reply@slpsoftware.nl',
|
||||
defaultFromName: 'Andrea Slop Fotografie',
|
||||
allowedFromAddresses: ['no-reply@slpsoftware.nl', 'info@andreaslop.nl'],
|
||||
isCustomized: false,
|
||||
};
|
||||
|
||||
function mailEnabled(settings: MailSettings = baseSettings, put?: (body: unknown) => void) {
|
||||
server.use(
|
||||
http.get('*/System/capabilities', () =>
|
||||
HttpResponse.json({ modules: ['Availability', 'Identity'], emailEnabled: true, defaultLanguage: 'nl' }),
|
||||
),
|
||||
http.get(`${API_BASE}/api/v1/SiteSettings/mail`, () => HttpResponse.json(settings)),
|
||||
http.put(`${API_BASE}/api/v1/SiteSettings/mail`, async ({ request }) => {
|
||||
const body = await request.json();
|
||||
put?.(body);
|
||||
return HttpResponse.json({ ...settings, isCustomized: true });
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
function asAdministrator() {
|
||||
server.use(
|
||||
http.post(`${API_BASE}/api/v1/auth/refresh`, () =>
|
||||
HttpResponse.json(makeAuthResponse({ user: { ...mockUser, role: 'Administrator', name: 'Test Admin' } })),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
describe('MailSettingsCard', () => {
|
||||
it('is not shown when the site cannot send mail', async () => {
|
||||
mockAuthenticated();
|
||||
renderApp('/settings');
|
||||
|
||||
await screen.findByTestId('settings-title', {}, { timeout: 10000 });
|
||||
expect(screen.queryByTestId('mail-settings-card')).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('lets the Owner pick an allowed sender and name, and saves them', async () => {
|
||||
mockAuthenticated();
|
||||
let saved: unknown;
|
||||
mailEnabled(baseSettings, (b) => (saved = b));
|
||||
renderApp('/settings');
|
||||
|
||||
const select = await screen.findByTestId('mail-from-address', {}, { timeout: 10000 });
|
||||
expect(Array.from((select as HTMLSelectElement).options).map((o) => o.value)).toEqual([
|
||||
'no-reply@slpsoftware.nl',
|
||||
'info@andreaslop.nl',
|
||||
]);
|
||||
await userEvent.selectOptions(select, 'info@andreaslop.nl');
|
||||
const name = screen.getByTestId('mail-from-name');
|
||||
await userEvent.clear(name);
|
||||
await userEvent.type(name, 'Andrea');
|
||||
await userEvent.click(screen.getByTestId('mail-save-button'));
|
||||
|
||||
await vi.waitFor(() => expect(saved).toEqual({ fromAddress: 'info@andreaslop.nl', fromName: 'Andrea' }));
|
||||
});
|
||||
|
||||
it('disables save until something changes, and blocks an empty or too long name', async () => {
|
||||
mockAuthenticated();
|
||||
mailEnabled();
|
||||
renderApp('/settings');
|
||||
|
||||
const save = await screen.findByTestId('mail-save-button', {}, { timeout: 10000 });
|
||||
expect(save).toBeDisabled();
|
||||
|
||||
const name = screen.getByTestId('mail-from-name');
|
||||
await userEvent.clear(name);
|
||||
expect(save).toBeDisabled();
|
||||
|
||||
await userEvent.type(name, 'x'.repeat(81));
|
||||
expect(screen.getByTestId('mail-from-name-error')).toBeInTheDocument();
|
||||
expect(save).toBeDisabled();
|
||||
});
|
||||
|
||||
it('offers to restore the defaults only once the sender was customised', async () => {
|
||||
mockAuthenticated();
|
||||
mailEnabled({ ...baseSettings, isCustomized: true });
|
||||
renderApp('/settings');
|
||||
|
||||
expect(await screen.findByTestId('mail-reset-button', {}, { timeout: 10000 })).toBeEnabled();
|
||||
});
|
||||
|
||||
it('shows an Administrator the settings read-only', async () => {
|
||||
asAdministrator();
|
||||
mailEnabled();
|
||||
renderApp('/settings');
|
||||
|
||||
expect(await screen.findByTestId('mail-from-address', {}, { timeout: 10000 })).toBeDisabled();
|
||||
expect(screen.getByTestId('mail-from-name')).toBeDisabled();
|
||||
expect(screen.getByTestId('mail-readonly-note')).toBeInTheDocument();
|
||||
expect(screen.queryByTestId('mail-save-button')).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { toast } from 'sonner';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { useAuth } from '@/features/auth/context/auth-context';
|
||||
import { ProblemDetailsError } from '@/lib/api-client';
|
||||
import { useMailSettings, useUpdateMailSettings } from '@/features/settings/services/useMailSettings';
|
||||
import type { MailSettings } from '@/features/settings/services/types';
|
||||
|
||||
const MAX_NAME_LENGTH = 80;
|
||||
|
||||
/** Sender of the site's mails. An Administrator sees it, only an Owner changes it. */
|
||||
export function MailSettingsCard({ emailEnabled }: { emailEnabled: boolean }) {
|
||||
const { t } = useTranslation();
|
||||
const { user } = useAuth();
|
||||
const { data } = useMailSettings(emailEnabled);
|
||||
|
||||
// The card exists only where mail is configured: without it nothing here would do anything.
|
||||
if (!emailEnabled || !data) return null;
|
||||
|
||||
// Keyed by the stored values so a saved change re-initialises the form from the server's answer.
|
||||
return <MailSettingsForm key={`${data.fromAddress}|${data.fromName}`} data={data} canEdit={user?.role === 'Owner'} t={t} />;
|
||||
}
|
||||
|
||||
function MailSettingsForm({ data, canEdit, t }: { data: MailSettings; canEdit: boolean; t: (key: string) => string }) {
|
||||
const update = useUpdateMailSettings();
|
||||
const [address, setAddress] = useState(data.fromAddress);
|
||||
const [name, setName] = useState(data.fromName);
|
||||
|
||||
const nameTooLong = name.trim().length > MAX_NAME_LENGTH;
|
||||
const nameEmpty = name.trim().length === 0;
|
||||
const unchanged = address === data.fromAddress && name.trim() === data.fromName;
|
||||
|
||||
const save = async (payload: { fromAddress: string | null; fromName: string | null }, successKey: string) => {
|
||||
try {
|
||||
await update.mutateAsync(payload);
|
||||
toast.success(t(successKey));
|
||||
} catch (err) {
|
||||
if (err instanceof ProblemDetailsError || (err as { status?: number }).status === 400) {
|
||||
toast.error(t('settings.mail.invalid'));
|
||||
} else {
|
||||
toast.error(t('errors.generic'));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Card data-testid="mail-settings-card">
|
||||
<CardHeader>
|
||||
<CardTitle className="text-base">{t('settings.mail.title')}</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="space-y-4">
|
||||
<p className="text-sm text-muted-foreground">{t('settings.mail.description')}</p>
|
||||
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="mail-from-address">{t('settings.mail.fromAddress')}</Label>
|
||||
<select
|
||||
id="mail-from-address"
|
||||
value={address}
|
||||
onChange={(e) => setAddress(e.target.value)}
|
||||
disabled={!canEdit}
|
||||
data-testid="mail-from-address"
|
||||
className="h-10 w-full rounded-md border border-input bg-background px-3 text-sm disabled:cursor-not-allowed disabled:opacity-50"
|
||||
>
|
||||
{data.allowedFromAddresses.map((a) => (
|
||||
<option key={a} value={a}>
|
||||
{a}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<p className="text-xs text-muted-foreground">{t('settings.mail.fromAddressHelp')}</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1">
|
||||
<Label htmlFor="mail-from-name">{t('settings.mail.fromName')}</Label>
|
||||
<Input
|
||||
id="mail-from-name"
|
||||
value={name}
|
||||
maxLength={MAX_NAME_LENGTH + 20}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
disabled={!canEdit}
|
||||
data-testid="mail-from-name"
|
||||
/>
|
||||
{nameTooLong && (
|
||||
<p className="text-sm text-destructive" data-testid="mail-from-name-error">
|
||||
{t('settings.mail.nameTooLong')}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{canEdit ? (
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={() => save({ fromAddress: address, fromName: name.trim() }, 'settings.mail.saveSuccess')}
|
||||
disabled={update.isPending || unchanged || nameEmpty || nameTooLong}
|
||||
data-testid="mail-save-button"
|
||||
>
|
||||
{update.isPending ? t('settings.availability.saving') : t('settings.mail.save')}
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => save({ fromAddress: null, fromName: null }, 'settings.mail.resetSuccess')}
|
||||
disabled={update.isPending || !data.isCustomized}
|
||||
data-testid="mail-reset-button"
|
||||
>
|
||||
{t('settings.mail.reset')}
|
||||
</Button>
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-sm text-muted-foreground" data-testid="mail-readonly-note">
|
||||
{t('settings.mail.readOnly')}
|
||||
</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user