Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174import { beforeEach, expect, it, vi } from 'vitest';import { secureFetch } from '../../lib/client-cert';import { loginToComail } from '../../lib/comail-login';import { authorizeComailService } from '../comail-service-access';import type { ComailConnection } from '../comail';
vi.mock('../../lib/client-cert', () => ({ secureFetch: vi.fn() }));vi.mock('../../lib/comail-login', () => ({ loginToComail: vi.fn() }));const fetch = vi.mocked(secureFetch);const login = vi.mocked(loginToComail);const client = (): ComailConnection => ({ accountId: 'mail', authHeader: 'Basic synthetic', comailAccount: { inboxUrl: 'https://inbox.comail.at', did: 'did:plc:owner', groupwareEnabled: true, sessionToken: 'synthetic-session',} });const consent = (profile = 'owner-data') => new Response(JSON.stringify({ error: 'authorization_required', authorizationProfile: profile }), { status: 409 });const result = () => ({ serverUrl: 'https://inbox.comail.at', username: 'owner@example.test', password: 'synthetic-password', comail: client().comailAccount! });beforeEach(() => { vi.clearAllMocks(); login.mockResolvedValue(result()); });
it('upgrades mail and calendar through Login with Atmosphere and rechecks service access', async () => { const connection = client(); fetch.mockResolvedValueOnce(consent()).mockResolvedValueOnce(new Response('{}')); const complete = vi.fn(async (value: Awaited<ReturnType<typeof loginToComail>>) => { connection.comailAccount = value.comail; connection.authHeader = 'Basic refreshed'; }); await authorizeComailService(connection, 'calendar', complete); expect(login).toHaveBeenCalledWith(); expect(complete).toHaveBeenCalledWith(result()); expect(fetch.mock.calls.map(call => call[0])).toEqual([ 'https://inbox.comail.at/webmail/native/calendar/calendars', 'https://inbox.comail.at/webmail/native/calendar/calendars', ]); expect(fetch.mock.calls[1][1]!.headers).toMatchObject({ Authorization: 'Basic refreshed' });});
it('reconnects the same mailbox through native login before checking its PDS grant', async () => { const connection = client(); fetch.mockResolvedValueOnce(Response.json({ did: 'did:plc:owner', mailbox: 'owner@example.test', identities: ['owner@example.test'], capabilities: { messageCreate: true, draftReplace: true, messageRead: true, inventory: true, submission: false } })); const complete = vi.fn(async (value: Awaited<ReturnType<typeof loginToComail>>) => { connection.comailAccount = value.comail; connection.authHeader = 'Basic refreshed'; }); await authorizeComailService(connection, 'mail', complete); expect(login).toHaveBeenCalledOnce(); expect(complete).toHaveBeenCalledOnce(); expect(fetch.mock.calls.map(call => call[0])).toEqual(['https://inbox.comail.at/webmail/native/mail/bootstrap']); expect(fetch.mock.calls[0][1]).toMatchObject({ method: 'POST', headers: { Authorization: 'Basic refreshed', 'X-Comail-Native-Session': 'synthetic-session' } });});
it('reports when renewed sign-in still lacks the PDS send grant', async () => { const connection = client(); fetch.mockResolvedValueOnce(Response.json({ did: 'did:plc:owner', mailbox: 'owner@example.test', identities: ['owner@example.test'], capabilities: { messageCreate: true, draftReplace: true, messageRead: true, inventory: true, submission: false, submissionReauthorizationRequired: true } })); await expect(authorizeComailService(connection, 'mail', async value => { connection.comailAccount = value.comail; })).rejects.toThrow('mailbox grant');});
it.each(['calendar', 'contacts', 'files'] as const)('uses existing %s access without another sign-in', async service => { fetch.mockResolvedValueOnce(new Response('{}')); const complete = vi.fn(); await authorizeComailService(client(), service, complete); expect(login).not.toHaveBeenCalled(); expect(complete).not.toHaveBeenCalled();});
it('keeps the current account when the browser returns another account', async () => { fetch.mockResolvedValueOnce(consent()); const other = result(); other.comail.did = 'did:plc:other'; login.mockResolvedValueOnce(other); const complete = vi.fn(); await expect(authorizeComailService(client(), 'calendar', complete)).rejects.toThrow('same Comail account'); expect(complete).not.toHaveBeenCalled(); expect(fetch).toHaveBeenCalledTimes(1);});
it('discards a sign-in result after an account switch', async () => { const connection = client(); fetch.mockResolvedValueOnce(consent()); login.mockImplementationOnce(async () => { connection.accountId = 'other'; return result(); }); const complete = vi.fn(); await expect(authorizeComailService(connection, 'calendar', complete)).rejects.toThrow('account changed'); expect(complete).not.toHaveBeenCalled();});
it('does not claim authorization when sign-in is cancelled or access is still denied', async () => { fetch.mockResolvedValueOnce(consent()); login.mockRejectedValueOnce(new Error('cancelled')); const complete = vi.fn(); await expect(authorizeComailService(client(), 'calendar', complete)).rejects.toThrow('cancelled'); expect(complete).not.toHaveBeenCalled(); fetch.mockResolvedValueOnce(consent()).mockResolvedValueOnce(consent()); await expect(authorizeComailService(client(), 'calendar', complete)).rejects.toMatchObject({ status: 409 });});
it.each(['owner-membership', 'member-calendar', 'member-personal', 'unknown'])('does not add %s permissions to account login', async profile => { fetch.mockResolvedValueOnce(consent(profile)); await expect(authorizeComailService(client(), 'calendar', vi.fn())).rejects.toThrow('not supported'); expect(login).not.toHaveBeenCalled();});
it.each([409, 503])('does not launch sign-in for a generic %s failure', async status => { fetch.mockResolvedValueOnce(new Response(JSON.stringify({ error: 'conflict' }), { status })); await expect(authorizeComailService(client(), 'calendar', vi.fn())).rejects.toThrow(); expect(login).not.toHaveBeenCalled();});
it.each([ ['calendar', '/calendar/calendars', 'owner-personal'], ['calendar', '/calendar/calendars', 'owner-source'], ['contacts', '/contacts/book', undefined], ['files', '/files/inventory', undefined],] as const)('renews %s access using the shared Inbox login', async (service, path, profile) => { const connection = client(); fetch.mockResolvedValueOnce(new Response(JSON.stringify({ error: 'authorization_required', authorizationProfile: profile, }), { status: 409 })).mockResolvedValueOnce(new Response('{}')); const complete = vi.fn(async (value: Awaited<ReturnType<typeof loginToComail>>) => { connection.comailAccount = value.comail; }); await authorizeComailService(connection, service, complete); expect(login).toHaveBeenCalledOnce(); expect(complete).toHaveBeenCalledOnce(); expect(fetch.mock.calls.map(call => call[0])).toEqual([ `https://inbox.comail.at/webmail/native${path}`, `https://inbox.comail.at/webmail/native${path}`, ]);});
it.each(['calendar', 'contacts', 'files'] as const)('renews an expired native session for %s', async service => { fetch.mockResolvedValueOnce(new Response('{}', { status: 401 })).mockResolvedValueOnce(new Response('{}')); await authorizeComailService(client(), service, vi.fn()); expect(login).toHaveBeenCalledOnce(); expect(fetch).toHaveBeenCalledTimes(2);});
it('lets an older account without a native session sign in again', async () => { const connection = client(); connection.comailAccount!.sessionToken = ''; fetch.mockResolvedValueOnce(new Response('{}')); const complete = vi.fn(async (value: Awaited<ReturnType<typeof loginToComail>>) => { connection.comailAccount = value.comail; }); await authorizeComailService(connection, 'contacts', complete); expect(login).toHaveBeenCalledOnce(); expect(fetch).toHaveBeenCalledOnce();});
it.each(['calendar', 'contacts', 'files'] as const)('renews %s when the saved groupware flag predates account enablement', async service => { const connection = client(); connection.comailAccount!.groupwareEnabled = false; fetch.mockResolvedValueOnce(new Response('{}')); const complete = vi.fn(async (value: Awaited<ReturnType<typeof loginToComail>>) => { connection.comailAccount = value.comail; }); await authorizeComailService(connection, service, complete); expect(login).toHaveBeenCalledOnce(); expect(complete).toHaveBeenCalledOnce(); expect(fetch).toHaveBeenCalledOnce();});
it('keeps disabled groupware denied after a completed sign-in', async () => { const connection = client(); connection.comailAccount!.groupwareEnabled = false; const disabled = result(); disabled.comail.groupwareEnabled = false; login.mockResolvedValueOnce(disabled); await expect(authorizeComailService(connection, 'calendar', async value => { connection.comailAccount = value.comail; })).rejects.toThrow('not enabled'); expect(fetch).not.toHaveBeenCalled();});