Open History to see the last commit
1import { test } from 'node:test';
2import assert from 'node:assert/strict';
3import { allowedEmail, createAuthGateway } from '../src/auth.ts';
4
5const webUrl = 'http://localhost:3000';
6function request(path: string, body: unknown, origin = webUrl) {
7 return new Request(`${webUrl}/api/auth/${path}`, { method: 'POST', headers: { Origin: origin, 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
8}
9function gateway(proxy: (request: Request, path: string) => Promise<Response>) {
10 return createAuthGateway({ baseUrl: 'https://auth.example.com/auth', webUrl, domains: ['databricks.com'], emails: ['invited@example.com'], proxy,
11 resolveUser: async user => ({ id: 'existing-glow-user', email: user.email, name: 'Existing creator' }),
12 });
13}
14
15test('work-domain access is exact and normalized; explicit invitations remain supported', () => {
16 for (const email of ['person@databricks.com', ' Person@DATABRICKS.COM ', 'invited@example.com']) assert.equal(allowedEmail(email, ['databricks.com'], ['invited@example.com']), true);
17 for (const email of ['person@databricks.com.example.com', 'person@sub.databricks.com', 'person@example.com', 'person@databricksXcom', 'person@@databricks.com']) assert.equal(allowedEmail(email, ['databricks.com'], []), false);
18});
19
20test('unsupported routes, foreign origins, and disallowed addresses cannot request codes', async () => {
21 const auth = gateway(async () => { throw new Error('A rejected request must not reach Neon'); });
22 assert.equal((await auth.handler(request('email-otp/send-verification-otp', { email: 'person@example.com', type: 'sign-in' }))).status, 403);
23 assert.equal((await auth.handler(request('email-otp/send-verification-otp', { email: 'person@databricks.com', type: 'sign-in' }, 'https://other.example.com'))).status, 403);
24 assert.equal((await auth.handler(request('sign-up/email', { email: 'person@databricks.com' }))).status, 404);
25 assert.equal((await auth.handler(request('sign-in/email', { email: 'person@databricks.com', password: 'unused' }))).status, 404);
26});
27
28test('code requests use normalized email and managed sign-in with no extra user fields', async () => {
29 const auth = gateway(async (forwarded, path) => {
30 assert.equal(path, 'email-otp/send-verification-otp');
31 assert.deepEqual(await forwarded.json(), { email: 'person@databricks.com', type: 'sign-in' });
32 return Response.json({ success: true });
33 });
34 assert.equal((await auth.handler(request('email-otp/send-verification-otp', { email: ' Person@DATABRICKS.COM ', type: 'sign-in', name: 'ignored' }))).status, 200);
35});
36
37test('only verified managed identities receive the existing Glow ownership ID and secure cookies', async () => {
38 const identity = { id: 'neon-user', name: '', email: 'person@databricks.com', emailVerified: true };
39 const auth = gateway(async (_request, path) => {
40 assert.equal(path, 'get-session');
41 return Response.json({ user: identity, session: { userId: identity.id } }, { headers: { 'Set-Cookie': '__Secure-neon-auth.session_token=opaque; Domain=auth.example.com; Path=/auth; Secure; HttpOnly; SameSite=None; Partitioned' } });
42 });
43 const response = await auth.handler(new Request(`${webUrl}/api/auth/get-session`));
44 const result = await response.json();
45 assert.equal(result.user.id, 'existing-glow-user');
46 assert.equal(result.session.userId, 'existing-glow-user');
47 const cookie = response.headers.get('set-cookie')!;
48 assert.match(cookie, /HttpOnly/); assert.match(cookie, /Secure/); assert.match(cookie, /SameSite=Lax/);
49 assert.doesNotMatch(cookie, /Domain=|Partitioned/);
50 assert.equal((await auth.api.getSession({ headers: new Headers() }))?.user.id, 'existing-glow-user');
51 const unverified = gateway(async () => Response.json({ user: { ...identity, emailVerified: false }, session: {} }));
52 await assert.rejects(unverified.api.getSession({ headers: new Headers() }), /Verify your email/);
53 const outsider = gateway(async () => Response.json({ user: { ...identity, email: 'person@example.com' }, session: {} }));
54 await assert.rejects(outsider.api.getSession({ headers: new Headers() }), /Databricks email/);
55});
56
57test('incorrect or expired codes remain failures without receiving session cookies', async () => {
58 const auth = gateway(async () => Response.json({ message: 'Invalid OTP' }, { status: 400 }));
59 const response = await auth.handler(request('sign-in/email-otp', { email: 'person@databricks.com', otp: '000000' }));
60 assert.equal(response.status, 400);
61 assert.equal(response.headers.get('set-cookie'), null);
62});