Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148import { AbortController as NativeAbortController, AbortSignal as NativeAbortSignal } from 'abort-controller';import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import * as WebBrowser from 'expo-web-browser';
vi.mock('../client-cert', () => ({ secureFetch: vi.fn() }));import { secureFetch } from '../client-cert';let loginToComail: typeof import('../comail-login').loginToComail;let prepareComailLogin: typeof import('../comail-login').prepareComailLogin;
const browser = vi.mocked(WebBrowser.openAuthSessionAsync);const request = vi.mocked(secureFetch);
describe('Comail native sign-in', () => { beforeEach(async () => { vi.resetModules(); ({ loginToComail, prepareComailLogin } = await import('../comail-login')); vi.resetAllMocks(); request.mockResolvedValueOnce( new Response('{"protocol":"comail-native-v1"}'), ); });
it('shares and reuses the public sign-in preflight before opening the browser', async () => { await Promise.all([prepareComailLogin(), prepareComailLogin()]); await prepareComailLogin(); expect(request).toHaveBeenCalledTimes(1); expect(browser).not.toHaveBeenCalled(); });
it('retries an unsuccessful preflight instead of caching it', async () => { request.mockReset().mockResolvedValueOnce(new Response('{}', { status: 503 })) .mockResolvedValueOnce(new Response('{"protocol":"comail-native-v1"}')); await expect(prepareComailLogin()).rejects.toThrow('not available'); await prepareComailLogin(); expect(request).toHaveBeenCalledTimes(2); });
it.each([undefined, 'owned-device'])('exchanges a state-bound code with PKCE and optional credential ID: %s', async (credentialId) => { browser.mockImplementation(async (url, redirect) => { const query = new URL(url).searchParams; expect(query.get('handle')).toBe('owner.example.test'); expect(query.get('code_challenge')).toHaveLength(43); return { type: 'success', url: `${redirect}#code=${'a'.repeat(64)}&state=${query.get('state')}`, }; }); request.mockResolvedValue( new Response( JSON.stringify({ flow: 'password', server_url: 'https://inbox.comail.at', username: 'owner@comail.at', password: 'synthetic', comail: { inboxUrl: 'https://inbox.comail.at', did: 'did:plc:test', groupwareEnabled: true, sessionToken: 'synthetic-native-session', mailMode: 'pds', ...(credentialId ? { credentialId } : {}), }, }), { headers: { 'Content-Type': 'application/json' } }, ), ); const result = await loginToComail('@owner.example.test'); expect(result.comail.did).toBe('did:plc:test'); expect(result.comail.credentialId).toBe(credentialId); expect(result.comail.mailMode).toBe('pds'); expect(request).toHaveBeenCalledWith( 'https://inbox.comail.at/webmail/native/auth/redeem', expect.objectContaining({ method: 'POST', redirect: 'error' }), ); const body = JSON.parse(String(request.mock.calls[1][1]?.body)); expect(body.code_verifier).toHaveLength(43); expect(JSON.stringify(browser.mock.calls)).not.toContain('synthetic'); });
it.each(['../other-device', '', 42])('rejects an unsafe credential ID: %s', async (credentialId) => { browser.mockImplementation(async (url, redirect) => ({ type: 'success', url: `${redirect}#code=${'a'.repeat(64)}&state=${new URL(url).searchParams.get('state')}` })); request.mockResolvedValue(Response.json({ flow: 'password', server_url: 'https://inbox.comail.at', username: 'owner@comail.at', password: 'synthetic', comail: { inboxUrl: 'https://inbox.comail.at', did: 'did:plc:test', groupwareEnabled: false, sessionToken: 'native-session', credentialId }, })); await expect(loginToComail()).rejects.toThrow('Invalid Comail sign-in response'); });
it('rejects an unknown mail mode in the native sign-in response', async () => { browser.mockImplementation(async (url, redirect) => ({ type: 'success', url: `${redirect}#code=${'a'.repeat(64)}&state=${new URL(url).searchParams.get('state')}` })); request.mockResolvedValue(Response.json({ flow: 'password', server_url: 'https://inbox.comail.at', username: 'owner@comail.at', password: 'synthetic', comail: { inboxUrl: 'https://inbox.comail.at', did: 'did:plc:test', groupwareEnabled: false, sessionToken: 'native-session', mailMode: 'unknown' } })); await expect(loginToComail()).rejects.toThrow('Invalid Comail sign-in response'); });
it.each(['credential_capacity', 'rate_limited'])('distinguishes exhausted credentials from other limits: %s', async (error) => { browser.mockImplementation(async (url, redirect) => ({ type: 'success', url: `${redirect}#code=${'a'.repeat(64)}&state=${new URL(url).searchParams.get('state')}` })); request.mockResolvedValue(Response.json({ error }, { status: 429 })); await expect(loginToComail()).rejects.toThrow(error === 'credential_capacity' ? 'revoke an unused' : 'Comail sign-in expired'); });
it('requires a fresh browser session without a required handle, including add-account mode', async () => { browser.mockRejectedValue(new Error('Browser closed')); await expect(loginToComail('', true)).rejects.toThrow(); expect(browser).toHaveBeenCalledTimes(1); const [start, redirect, options] = browser.mock.calls[0]; expect(new URL(start).searchParams.has('handle')).toBe(false); expect(new URL(start).searchParams.get('select_account')).toBe('1'); expect(redirect).toBe('comailnative://auth/callback'); expect(options?.preferEphemeralSession).toBe(true); });
it('reports an unavailable native API before opening the browser', async () => { request.mockReset(); request.mockResolvedValue(new Response('{}', { status: 404 })); await expect(loginToComail('owner.example.test')).rejects.toThrow( 'not available', ); expect(browser).not.toHaveBeenCalled(); });
it('rejects a forged callback before sending a credential request', async () => { browser.mockResolvedValue({ type: 'success', url: `comailnative://auth/callback#code=${'a'.repeat(64)}&state=foreign`, }); await expect(loginToComail('owner.example.test')).rejects.toThrow('state'); expect(request).toHaveBeenCalledTimes(1); });
it('does not follow a callback to another app or trust another service origin', async () => { browser.mockImplementation(async (url) => ({ type: 'success', url: `evil://auth/callback#code=${'a'.repeat(64)}&state=${new URL(url).searchParams.get('state')}`, })); await expect(loginToComail('owner.example.test')).rejects.toThrow(); expect(request).toHaveBeenCalledTimes(1); });});
// Use the same abort implementation as React Native, rather than Node's richer API.beforeEach(() => { vi.stubGlobal('AbortController', NativeAbortController); vi.stubGlobal('AbortSignal', NativeAbortSignal);});
afterEach(() => vi.unstubAllGlobals());