104 lines
4.5 KiB
JavaScript
104 lines
4.5 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { normalizeAccount, publicAccount } = require('../desktop/mail-model.cjs');
|
|
const { registerMailIpcHandlers } = require('../desktop/ipc/mail.cjs');
|
|
|
|
function registerOAuthMailHandlers(overrides = {}) {
|
|
const handlers = new Map();
|
|
registerMailIpcHandlers({
|
|
registerIpcHandler(channel, handler) { handlers.set(channel, handler); },
|
|
normalizeAccount,
|
|
publicAccount,
|
|
async connectMail() { return { mailbox: 'INBOX', messages: 0 }; },
|
|
async authorizeOAuthAccount(account) {
|
|
return {
|
|
...account,
|
|
oauthAccessToken: 'private-access-token',
|
|
oauthRefreshToken: 'private-refresh-token',
|
|
oauthAccessTokenExpiresAt: Date.now() + 3600_000
|
|
};
|
|
},
|
|
listOAuthProviders() { return [{ id: 'google', label: 'Google', configured: true }, { id: 'microsoft', label: 'Microsoft', configured: false }]; },
|
|
async saveAccount(account) { return { ...account, id: 'oauth-account' }; },
|
|
requestBackgroundSync() {},
|
|
randomUUID: () => 'generated-account-id',
|
|
...overrides
|
|
});
|
|
return handlers;
|
|
}
|
|
|
|
test('OAuth account setup authorizes in the main process and never returns tokens to the renderer', async () => {
|
|
const calls = [];
|
|
const handlers = registerOAuthMailHandlers({
|
|
async authorizeOAuthAccount(account) {
|
|
calls.push(['authorize', account.oauthProvider]);
|
|
return { ...account, oauthAccessToken: 'private-access-token', oauthRefreshToken: 'private-refresh-token', oauthAccessTokenExpiresAt: Date.now() + 3600_000 };
|
|
},
|
|
async connectMail(account) {
|
|
calls.push(['connect', account.authMethod, account.oauthRefreshToken]);
|
|
return { mailbox: 'INBOX', messages: 0 };
|
|
},
|
|
async saveAccount(account) {
|
|
calls.push(['save', account.oauthRefreshToken]);
|
|
return { ...account, id: 'oauth-account' };
|
|
}
|
|
});
|
|
|
|
const result = await handlers.get('mail:connect')({}, {
|
|
email: 'user@gmail.com', authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User'
|
|
});
|
|
|
|
assert.deepEqual(calls.map(call => call[0]), ['authorize', 'connect', 'save']);
|
|
assert.deepEqual(calls[1], ['connect', 'oauth2', 'private-refresh-token']);
|
|
assert.equal(result.authMethod, 'oauth2');
|
|
assert.equal(result.oauthProvider, 'google');
|
|
assert.equal('oauthAccessToken' in result, false);
|
|
assert.equal('oauthRefreshToken' in result, false);
|
|
assert.equal('password' in result, false);
|
|
});
|
|
|
|
test('OAuth provider discovery reports configured status without exposing client credentials', async () => {
|
|
const handlers = registerOAuthMailHandlers();
|
|
assert.deepEqual(await handlers.get('mail:list-oauth-providers')(), [
|
|
{ id: 'google', label: 'Google', configured: true },
|
|
{ id: 'microsoft', label: 'Microsoft', configured: false }
|
|
]);
|
|
});
|
|
|
|
test('editing an OAuth account preserves its token unless the user requests reauthorization', async () => {
|
|
const existing = {
|
|
...normalizeAccount({ email: 'user@gmail.com', displayName: 'User', authMethod: 'oauth2', oauthProvider: 'google' }),
|
|
id: 'oauth-account', oauthAccessToken: 'old-access', oauthRefreshToken: 'old-refresh', oauthAccessTokenExpiresAt: 10
|
|
};
|
|
const store = { accounts: [existing] };
|
|
let authorizations = 0;
|
|
const handlers = new Map();
|
|
registerMailIpcHandlers({
|
|
registerIpcHandler(channel, handler) { handlers.set(channel, handler); },
|
|
normalizeAccount,
|
|
publicAccount,
|
|
async readAccount(id) { return store.accounts.find(account => account.id === id); },
|
|
async updateAccountStore(update) { return update(store); },
|
|
async connectMail() { return { mailbox: 'INBOX', messages: 0 }; },
|
|
async authorizeOAuthAccount(account) {
|
|
authorizations += 1;
|
|
return { ...account, oauthAccessToken: 'new-access', oauthRefreshToken: 'new-refresh', oauthAccessTokenExpiresAt: Date.now() + 3600_000 };
|
|
},
|
|
requestBackgroundSync() {}
|
|
});
|
|
const event = {};
|
|
|
|
await handlers.get('mail:update-account')(event, 'oauth-account', {
|
|
authMethod: 'oauth2', oauthProvider: 'google', displayName: 'User'
|
|
});
|
|
assert.equal(authorizations, 0);
|
|
assert.equal(store.accounts[0].oauthRefreshToken, 'old-refresh');
|
|
|
|
const result = await handlers.get('mail:update-account')(event, 'oauth-account', {
|
|
authMethod: 'oauth2', oauthProvider: 'google', reauthorizeOAuth: true, displayName: 'User'
|
|
});
|
|
assert.equal(authorizations, 1);
|
|
assert.equal(store.accounts[0].oauthRefreshToken, 'new-refresh');
|
|
assert.equal('oauthRefreshToken' in result, false);
|
|
});
|