106 lines
3.0 KiB
JavaScript
106 lines
3.0 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { createBetterAuthAdapter } = require("../src/integrations/better-auth");
|
|
|
|
function createMockAuthHandler() {
|
|
const signedIn = new Set();
|
|
|
|
return async function handler(request) {
|
|
const url = new URL(request.url);
|
|
const path = url.pathname;
|
|
|
|
if (path.endsWith("/sign-in/email")) {
|
|
return new Response(JSON.stringify({ ok: false }), { status: 401 });
|
|
}
|
|
|
|
if (path.endsWith("/sign-up/email")) {
|
|
const body = await request.json();
|
|
const userId = String(body.name);
|
|
signedIn.add(userId);
|
|
return new Response(JSON.stringify({ ok: true }), {
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"set-cookie": `xartaudio_better_auth=${userId}; Path=/; HttpOnly`,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (path.endsWith("/get-session")) {
|
|
const cookie = request.headers.get("cookie") || "";
|
|
const match = cookie.match(/xartaudio_better_auth=([^;]+)/);
|
|
if (!match) {
|
|
return new Response(JSON.stringify(null), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
return new Response(JSON.stringify({
|
|
user: {
|
|
id: match[1],
|
|
name: match[1],
|
|
},
|
|
}), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}
|
|
|
|
if (path.endsWith("/sign-out")) {
|
|
return new Response(JSON.stringify({ ok: true }), {
|
|
status: 200,
|
|
headers: {
|
|
"set-cookie": "xartaudio_better_auth=; Path=/; Max-Age=0",
|
|
},
|
|
});
|
|
}
|
|
|
|
return new Response("not_found", { status: 404 });
|
|
};
|
|
}
|
|
|
|
test("signInDevUser signs up and returns cookie when user does not exist", async () => {
|
|
const adapter = createBetterAuthAdapter({
|
|
appBaseUrl: "http://localhost:3000",
|
|
secret: "test-secret",
|
|
authHandler: createMockAuthHandler(),
|
|
});
|
|
|
|
const result = await adapter.signInDevUser("alice");
|
|
assert.match(String(result.setCookie), /xartaudio_better_auth=alice/);
|
|
});
|
|
|
|
test("getAuthenticatedUserId resolves session user from better-auth endpoint", async () => {
|
|
const adapter = createBetterAuthAdapter({
|
|
appBaseUrl: "http://localhost:3000",
|
|
secret: "test-secret",
|
|
authHandler: createMockAuthHandler(),
|
|
});
|
|
|
|
const userId = await adapter.getAuthenticatedUserId({
|
|
cookie: "xartaudio_better_auth=bob",
|
|
});
|
|
assert.equal(userId, "bob");
|
|
});
|
|
|
|
test("handleRoute proxies requests into better-auth handler", async () => {
|
|
const adapter = createBetterAuthAdapter({
|
|
appBaseUrl: "http://localhost:3000",
|
|
secret: "test-secret",
|
|
authHandler: createMockAuthHandler(),
|
|
});
|
|
|
|
const response = await adapter.handleRoute({
|
|
method: "POST",
|
|
path: "/api/auth/sign-out",
|
|
headers: { cookie: "xartaudio_better_auth=alice" },
|
|
rawBody: "",
|
|
});
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.match(String(response.headers["set-cookie"]), /Max-Age=0/);
|
|
});
|