156 lines
4.5 KiB
JavaScript
156 lines
4.5 KiB
JavaScript
"use strict";
|
|
|
|
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const { createBetterAuthAdapter } = require("../src/integrations/better-auth");
|
|
|
|
function createMockAuthHandler() {
|
|
return async function handler(request) {
|
|
const url = new URL(request.url);
|
|
const path = url.pathname;
|
|
|
|
if (path.endsWith("/sign-in/email")) {
|
|
const body = await request.json();
|
|
const userId = String(body.email);
|
|
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("/sign-up/email")) {
|
|
const body = await request.json();
|
|
const userId = String(body.email);
|
|
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("/sign-in/social")) {
|
|
return new Response(JSON.stringify({ url: "https://x.com/i/oauth2/authorize?state=test" }), {
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "application/json",
|
|
"set-cookie": "xartaudio_oauth_state=test; 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("signInWithEmail returns session cookie", async () => {
|
|
const adapter = createBetterAuthAdapter({
|
|
appBaseUrl: "http://localhost:3000",
|
|
secret: "test-secret",
|
|
authHandler: createMockAuthHandler(),
|
|
});
|
|
|
|
const result = await adapter.signInWithEmail({
|
|
email: "alice@example.com",
|
|
password: "password123",
|
|
});
|
|
assert.match(String(result.setCookie), /xartaudio_better_auth=alice@example\.com/);
|
|
});
|
|
|
|
test("signUpWithEmail returns session cookie", async () => {
|
|
const adapter = createBetterAuthAdapter({
|
|
appBaseUrl: "http://localhost:3000",
|
|
secret: "test-secret",
|
|
authHandler: createMockAuthHandler(),
|
|
});
|
|
|
|
const result = await adapter.signUpWithEmail({
|
|
name: "Alice",
|
|
email: "alice@example.com",
|
|
password: "password123",
|
|
});
|
|
assert.match(String(result.setCookie), /xartaudio_better_auth=alice@example\.com/);
|
|
});
|
|
|
|
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("getXAuthorizationUrl returns provider url when x oauth is configured", async () => {
|
|
const adapter = createBetterAuthAdapter({
|
|
appBaseUrl: "http://localhost:3000",
|
|
secret: "test-secret",
|
|
xOAuthClientId: "x-client-id",
|
|
xOAuthClientSecret: "x-client-secret",
|
|
authHandler: createMockAuthHandler(),
|
|
});
|
|
|
|
const result = await adapter.getXAuthorizationUrl({
|
|
callbackURL: "http://localhost:3000/app",
|
|
});
|
|
|
|
assert.match(result.url, /^https:\/\/x\.com\/i\/oauth2\/authorize/);
|
|
assert.match(String(result.setCookie), /xartaudio_oauth_state=test/);
|
|
});
|
|
|
|
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/);
|
|
});
|