Files
native/src/api/__tests__/sieve.test.ts
T
Linus Rath a86cd1d0eb feat: manage filters and vacation of shared and group accounts
The phone only ever read and wrote Sieve scripts and the vacation
responder of the user's own account, and the "Shared with me" list in
Account settings said to use the webmail for the rest. Port the
webmail's scoped settings (4c6c1aab) for these two panes:

- `api/sieve.ts` and `api/vacation.ts` take an optional account id and
  gate on the account's own `accountCapabilities` for
  `urn:ietf:params:jmap:sieve` and `urn:ietf:params:jmap:vacationresponse`,
  treating non-personal accounts as capable because Stalwart doesn't
  always advertise capabilities on them. Script blobs are uploaded and
  downloaded against the same account.
- Tapping a shared account in Account settings scopes Settings to it:
  only the Filters and Vacation Responder panes it supports are listed,
  behind a "Managing: <name>" banner that leads back to the own account.
  The scope is dropped when the signed-in account changes.
- The filter and vacation stores remember which account they hold,
  reset when it changes and drop replies for an account the user has
  already left, so one account's rules are never saved into another.
- The rule editor offers the managed account's folders as targets, and
  the "vacation active" banner only shows for the own account.
2026-09-24 19:01:24 +02:00

135 lines
5.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('../jmap-client', () => ({
jmapClient: {
accountId: 'own',
authHeader: 'Basic x',
request: vi.fn(),
currentSession: null as unknown,
},
}));
vi.mock('../../lib/client-cert', () => ({ secureFetch: vi.fn() }));
import { jmapClient } from '../jmap-client';
import { secureFetch } from '../../lib/client-cert';
import { CAPABILITIES } from '../types';
import {
accountSupportsSieve,
getSieveCapabilities,
getSieveScriptContent,
getSieveScripts,
isSieveSupported,
updateSieveScript,
validateSieveScript,
} from '../sieve';
const mockRequest = jmapClient.request as ReturnType<typeof vi.fn>;
const mockFetch = secureFetch as unknown as ReturnType<typeof vi.fn>;
const SIEVE_CAPS = { sieveExtensions: ['fileinto'] };
function setSession(session: unknown) {
(jmapClient as { currentSession: unknown }).currentSession = session;
}
beforeEach(() => {
vi.clearAllMocks();
setSession({
downloadUrl: 'https://mail/download/{accountId}/{blobId}/{name}?type={type}',
uploadUrl: 'https://mail/upload/{accountId}/',
primaryAccounts: { [CAPABILITIES.SIEVE]: 'own' },
capabilities: { [CAPABILITIES.SIEVE]: {} },
accounts: {
own: { name: 'me', isPersonal: true, isReadOnly: false, accountCapabilities: { [CAPABILITIES.SIEVE]: SIEVE_CAPS } },
team: { name: 'Team', isPersonal: false, isReadOnly: false, accountCapabilities: { [CAPABILITIES.MAIL]: {} } },
other: { name: 'Other', isPersonal: true, isReadOnly: false, accountCapabilities: { [CAPABILITIES.MAIL]: {} } },
},
});
});
describe('accountSupportsSieve', () => {
const caps = { [CAPABILITIES.SIEVE]: {} };
it('needs the Sieve capability in a personal account\'s own capabilities', () => {
expect(accountSupportsSieve(
{ name: 'me', isPersonal: true, isReadOnly: false, accountCapabilities: { [CAPABILITIES.SIEVE]: {} } },
caps,
)).toBe(true);
expect(accountSupportsSieve(
{ name: 'me', isPersonal: true, isReadOnly: false, accountCapabilities: { [CAPABILITIES.MAIL]: {} } },
caps,
)).toBe(false);
});
it('treats shared/group accounts as capable, but only when the server has Sieve', () => {
expect(accountSupportsSieve({ name: 'grp', isPersonal: false, isReadOnly: false }, caps)).toBe(true);
expect(accountSupportsSieve({ name: 'grp', isPersonal: false, isReadOnly: false }, {})).toBe(false);
expect(accountSupportsSieve(undefined, caps)).toBe(false);
});
it('keeps the session answer for servers without accountCapabilities', () => {
expect(accountSupportsSieve({ name: 'me', isPersonal: true, isReadOnly: false }, caps)).toBe(true);
});
});
describe('isSieveSupported', () => {
it('checks the own Sieve account by default and a named account on request', () => {
expect(isSieveSupported()).toBe(true);
expect(isSieveSupported('team')).toBe(true);
expect(isSieveSupported('other')).toBe(false);
expect(isSieveSupported('missing')).toBe(false);
});
it('is false without a session', () => {
setSession(null);
expect(isSieveSupported()).toBe(false);
});
});
describe('account scoping', () => {
it('reads the capabilities of the requested account', () => {
expect(getSieveCapabilities()).toEqual(SIEVE_CAPS);
expect(getSieveCapabilities('team')).toBeNull();
});
it('lists scripts of the requested account', async () => {
mockRequest.mockResolvedValue({ methodResponses: [['SieveScript/get', { list: [] }, '0']] });
await getSieveScripts('team');
expect(mockRequest.mock.calls[0][0][0][1]).toEqual({ accountId: 'team' });
await getSieveScripts();
expect(mockRequest.mock.calls[1][0][0][1]).toEqual({ accountId: 'own' });
});
it('downloads a shared account\'s script against that account', async () => {
mockFetch.mockResolvedValue({ ok: true, text: async () => 'keep;' });
await getSieveScriptContent('blob-1', 'team');
expect(mockFetch.mock.calls[0][0]).toContain('/download/team/blob-1/');
});
it('uploads and saves into the requested account', async () => {
mockFetch.mockResolvedValue({ ok: true, json: async () => ({ team: { blobId: 'b-new' } }) });
mockRequest.mockResolvedValue({ methodResponses: [['SieveScript/set', { updated: { s1: null } }, '0']] });
await updateSieveScript('s1', 'keep;', true, 'team');
expect(mockFetch.mock.calls[0][0]).toBe('https://mail/upload/team/');
const [method, args] = mockRequest.mock.calls[0][0][0];
expect(method).toBe('SieveScript/set');
expect(args).toEqual({
accountId: 'team',
update: { s1: { blobId: 'b-new' } },
onSuccessActivateScript: 's1',
});
});
it('validates against the requested account', async () => {
mockFetch.mockResolvedValue({ ok: true, json: async () => ({ blobId: 'b-val' }) });
mockRequest.mockResolvedValue({ methodResponses: [['SieveScript/validate', {}, '0']] });
await expect(validateSieveScript('keep;', 'team')).resolves.toEqual({ isValid: true });
expect(mockFetch.mock.calls[0][0]).toBe('https://mail/upload/team/');
expect(mockRequest.mock.calls[0][0][0][1]).toEqual({ accountId: 'team', blobId: 'b-val' });
});
});