Files
token-circles/worker/test/google-resolve.test.ts
Komediruzecki 3352878cba feat(mail): sign-up sends Confirm, and the Welcome goes once the address is confirmed
A password sign-up gets Confirm (renderConfirmEmail): the link that opens
the account and nothing of the welcome. Send the link again gets the same
body as a fresh link (renderEmailVerification). Both are on the new mail
layout and say the link asks for a password sign-in wherever it is opened.

The Welcome (renderWelcome) goes to an account once: when its address is
first confirmed, by its link, Google sign-in, an emailed sign-in code or a
reset link, and at once for a Google sign-up. The record is a
reminder_dedup row (user_id, 'welcome'), claimed in the D1 batch that
confirms the address and only while it is unconfirmed (worker/src/
welcome.ts). The mail goes after the answer, best effort, and a failed send
is not retried.

Migration 0033 is data only: it marks every existing account as welcomed,
so an account still waiting for its link gets no second welcome.
2026-10-11 17:30:25 +02:00

130 lines
5.2 KiB
TypeScript

/**
* resolveGoogleUser contract — the function was reshaped in the branded-emails
* change (returns { userId, created, email } so the OAuth callback can send a
* welcome mail to brand-new accounts). These tests pin all three resolution
* paths against the real D1 schema so a Google sign-in regression can't hide.
*/
import { env } from 'cloudflare:test';
import { beforeEach, describe, expect, it } from 'vitest';
import { resolveGoogleUser } from '../src/auth';
const CLAIMS = {
sub: 'google-sub-123',
email: 'gina@example.com',
email_verified: 'true',
} as Parameters<typeof resolveGoogleUser>[1];
describe('resolveGoogleUser', () => {
beforeEach(async () => {
for (const t of ['transactions', 'accounts', 'categories', 'profiles', 'users']) {
await env.DB.prepare(`DELETE FROM ${t}`).run();
}
});
it('creates a brand-new google user with a default profile and reports created', async () => {
const res = await resolveGoogleUser(env.DB, CLAIMS);
expect(res.created).toBe(true);
expect(res.email).toBe('gina@example.com');
expect(res.userId).toBeGreaterThan(0);
const user = await env.DB.prepare('SELECT * FROM users WHERE id = ?')
.bind(res.userId)
.first<{ auth_provider: string; provider_id: string; email: string }>();
expect(user?.auth_provider).toBe('google');
expect(user?.provider_id).toBe('google-sub-123');
const profile = await env.DB.prepare('SELECT name FROM profiles WHERE user_id = ?')
.bind(res.userId)
.first<{ name: string }>();
expect(profile?.name).toBe('Personal Profile');
});
it('returns the SAME user (created=false) on a repeat sign-in', async () => {
const first = await resolveGoogleUser(env.DB, CLAIMS);
const again = await resolveGoogleUser(env.DB, CLAIMS);
expect(again.userId).toBe(first.userId);
expect(again.created).toBe(false);
});
it('links a verified google identity to an existing email account without creating', async () => {
await env.DB.prepare(
"INSERT INTO users (id, email, password_hash, auth_provider, token_version) VALUES (900, 'gina@example.com', 'pbkdf2$100000$x$y', 'password', 1)"
).run();
const res = await resolveGoogleUser(env.DB, CLAIMS);
expect(res.userId).toBe(900);
expect(res.created).toBe(false);
// Its address was never confirmed, so its password went, and the caller is told.
expect(res.cleared).toEqual(['password']);
const user = await env.DB.prepare(
'SELECT auth_provider, provider_id FROM users WHERE id = 900'
).first<{ auth_provider: string; provider_id: string }>();
expect(user?.auth_provider).toBe('google');
expect(user?.provider_id).toBe('google-sub-123');
});
it('does not store an unverified email and still creates the account', async () => {
const res = await resolveGoogleUser(env.DB, {
...CLAIMS,
email_verified: 'false',
} as typeof CLAIMS);
expect(res.created).toBe(true);
expect(res.email).toBeNull();
// No confirmed address, nowhere to send a welcome.
expect(res.welcome).toBe(false);
const user = await env.DB.prepare('SELECT email FROM users WHERE id = ?')
.bind(res.userId)
.first<{ email: string | null }>();
expect(user?.email).toBeNull();
});
it('joins the account whose address differs from the claim only in case', async () => {
await env.DB.prepare(
"INSERT INTO users (id, email, password_hash, email_verified, auth_provider, token_version) VALUES (901, 'gina@example.com', 'pbkdf2$100000$x$y', 1, 'password', 1)"
).run();
const res = await resolveGoogleUser(env.DB, { ...CLAIMS, email: 'Gina@Example.COM' });
expect(res).toEqual({
userId: 901,
created: false,
email: null,
cleared: [],
tokenVersion: 1,
welcome: false,
});
const user = await env.DB.prepare(
'SELECT email, provider_id FROM users WHERE id = 901'
).first();
expect(user).toEqual({ email: 'gina@example.com', provider_id: 'google-sub-123' });
});
it('stores a new account address in lower case', async () => {
const res = await resolveGoogleUser(env.DB, { ...CLAIMS, email: 'New.Person@Example.COM' });
expect(res.created).toBe(true);
expect(res.email).toBe('new.person@example.com');
// Google has confirmed the address, so the account's welcome is claimed now (welcome.ts).
expect(res.welcome).toBe(true);
const user = await env.DB.prepare('SELECT email FROM users WHERE id = ?')
.bind(res.userId)
.first<{ email: string }>();
expect(user?.email).toBe('new.person@example.com');
});
it('finds an address stored earlier with capitals', async () => {
// A row kept with capitals: the look-up ignores case.
await env.DB.prepare(
"INSERT INTO users (id, email, email_verified, auth_provider, provider_id, token_version) VALUES (902, 'Gina@Example.com', 1, 'google', 'google-sub-older', 1)"
).run();
const res = await resolveGoogleUser(env.DB, CLAIMS);
expect(res).toEqual({
userId: 902,
created: false,
email: null,
cleared: [],
tokenVersion: 1,
welcome: false,
});
const { results } = await env.DB.prepare(
"SELECT id FROM users WHERE lower(email) = 'gina@example.com'"
).all();
expect(results).toEqual([{ id: 902 }]);
});
});