Files
token-circles/worker/test/google-resolve.test.ts
KomediruzeckiandGitHub 0b4302f58b fix: mail logo crop + env-aware mail assets; resolveGoogleUser contract tests (#348)
* test(worker): pin the resolveGoogleUser contract

The branded-emails change reshaped resolveGoogleUser to return
{ userId, created, email } so the OAuth callback can welcome brand-new
accounts. These tests pin all three resolution paths (create with default
profile, repeat sign-in, verified-email linking) plus the unverified-email
case against the real D1 schema, so a Google sign-in regression can't hide
behind the mocked-out OAuth dance.

* fix(email,pwa): regenerate the cropped app icon; mail assets follow the sending env

The mail masthead showed only the top-left corner of the logo — because
frontend/public/icon-192.png IS a top-left crop: the last commit to touch it
(30771a8, itself titled 'regenerate from full logo instead of top-left crop')
produced another cropped file. Regenerated properly from icon-192.svg (full
mark: rounded night tile, glow, dashed orbit ring, core, warm satellite) —
this also fixes the PWA icon wherever it was consumed at face value.

Mail assets (logo + orbit GIF) now load from the SENDING deployment's app
origin (CORS_ORIGIN/APP_ORIGINS, defaulting to prod): the templates
hardcoded https://tokencircles.com, so dev-sent mails pulled prod assets —
where /email/orbit.gif 404s until the next prod release and the icon was the
stale crop. assetOrigin threads through the shell and every renderer/call
site; footer links stay canonical. +1 renderer test (152 green).
2026-07-16 22:58:10 +02:00

75 lines
3.0 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);
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();
const user = await env.DB.prepare('SELECT email FROM users WHERE id = ?')
.bind(res.userId)
.first<{ email: string | null }>();
expect(user?.email).toBeNull();
});
});