As Cross Events' own sign-in sends: when, the site, rough location and device, with what to do if it wasn't them. WhatsApp too for a verified number. Admins can turn it off (signin_notices). Fire-and-forget: never holds up or fails a sign-in. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
51 lines
2.7 KiB
TypeScript
51 lines
2.7 KiB
TypeScript
// 1.6.0: a "New sign-in" email (and WhatsApp) after each sign-in, as Cross Events sends.
|
|
import assert from 'node:assert/strict';
|
|
import { after, before, describe, test } from 'node:test';
|
|
import { startHarness, type Harness } from './support/harness.js';
|
|
import { describeDevice, sendSignInNotice } from '../src/accounts/signInNotice.js';
|
|
import type { MemoryMessenger } from '../src/lib/messages.js';
|
|
|
|
let h: Harness;
|
|
before(async () => { h = await startHarness(); });
|
|
after(async () => { await h.close(); });
|
|
|
|
const CHROME_WIN = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36';
|
|
|
|
describe('sign-in notices', () => {
|
|
test('an email with when, where, the device and the site; WhatsApp too for a verified number', async () => {
|
|
const m = h.ctx.messenger as MemoryMessenger;
|
|
const a = await h.makeAccount({ email: 'notice.person@hope.test' });
|
|
assert.equal(await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: '127.0.0.1', userAgent: CHROME_WIN, clientId: 'accounts-self' }), 'sent');
|
|
const mail = m.emails.filter((e) => e.to === 'notice.person@hope.test').at(-1)!;
|
|
assert.match(mail.subject, /^New sign-in to your .+ account$/);
|
|
assert.match(mail.text, /Device: Chrome on Windows/);
|
|
assert.match(mail.text, /Signed in to: Your account page/);
|
|
assert.match(mail.html ?? '', /Not you\?/);
|
|
|
|
await h.db.account.update({ where: { id: a.id as string }, data: { phone: '+27821234567', phoneVerified: true } });
|
|
m.whatsappOn = true;
|
|
const before = m.whatsapps.length;
|
|
await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: null, userAgent: null, clientId: null });
|
|
assert.equal(m.whatsapps.length, before + 1);
|
|
assert.match(m.whatsapps.at(-1)!.text, /New sign-in/);
|
|
m.whatsappOn = false;
|
|
});
|
|
|
|
test('admins can switch them off; a failing email never throws', async () => {
|
|
const m = h.ctx.messenger as MemoryMessenger;
|
|
const a = await h.makeAccount({ email: 'notice.off@hope.test' });
|
|
await h.ctx.settings.set('signin_notices', 'false');
|
|
assert.equal(await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: null, userAgent: null, clientId: null }), 'off');
|
|
await h.ctx.settings.set('signin_notices', null);
|
|
m.emailOn = false;
|
|
assert.equal(await sendSignInNotice(h.ctx, { accountId: a.id as string, ip: null, userAgent: null, clientId: null }), 'failed');
|
|
m.emailOn = true;
|
|
});
|
|
|
|
test('device names', () => {
|
|
assert.equal(describeDevice(CHROME_WIN), 'Chrome on Windows');
|
|
assert.equal(describeDevice('Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 Version/17.0 Mobile/15E148 Safari/604.1'), 'Safari on iPhone or iPad');
|
|
assert.equal(describeDevice(null), 'Unknown device');
|
|
});
|
|
});
|