* Migrate authentication from Clerk to BetterAuth (#322) Replaces @clerk/react-router with self-hosted better-auth to eliminate the external Clerk dependency and keep all user/session data in our own PostgreSQL database. **What changed** - New: auth.server.ts (BetterAuth config w/ Drizzle adapter, bcrypt, Resend), auth-client.ts, api.auth.$.ts handler - New: /login and /register pages with email+password and Google/Discord OAuth; open-redirect guard on redirectTo param - New: UserMenu component replacing Clerk's UserButton - Schema: sessions, accounts, verifications tables; emailVerified column; clerkId made nullable - Migrations 0081 (BetterAuth tables) and 0082 (accounts extra columns for v1.6.9) - All ~30 route files: getAuth → auth.api.getSession, isUserAdminByClerkId → isUserAdmin - root.tsx: isAdmin read directly from session.user.isAdmin (no extra DB query) - useDraftAuthRecovery: removed Clerk JWT refresh logic; replaced with cookie-session check - models/user.ts: removed findUserByClerkId, findOrCreateUser, updateUserByClerkId (webhook pattern) - Deleted: app/routes/api/webhooks/clerk.ts; uninstalled @clerk/react-router, @clerk/themes, svix - scripts/migrate.mjs: extended with idempotent Clerk → BetterAuth data migration (FK conversion, email_verified, OAuth accounts) - scripts/migrate-clerk-passwords.mjs: one-time script to import bcrypt hashes from Clerk CSV export - BETTERAUTH_MIGRATION.md: dev and production runbooks - All test mocks updated: vi.mock('~/lib/auth.server') instead of @clerk/react-router/server - Test fixtures: added emailVerified field **Follow-up (post-stable)** - Rename actor_clerk_id column → actor_user_id in commissioner_audit_log - Drop clerk_id column from users once migration confirmed Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Add .npmrc with legacy-peer-deps for better-auth/drizzle peer dep conflict better-auth@1.6.9 declares peerOptional deps on drizzle-orm ^0.45.2 and drizzle-kit >=0.31.4, but we run drizzle-orm ~0.36.3 / drizzle-kit ~0.28.1. The adapter works correctly at runtime with our versions — the peer dep is only for stricter type checking. This unblocks npm ci in CI without a risky drizzle major-version upgrade. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
437 lines
13 KiB
TypeScript
437 lines
13 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import type { RouterContextProvider } from "react-router";
|
|
import { action } from "~/routes/api/autodraft.update";
|
|
|
|
const ctx = {} as unknown as RouterContextProvider;
|
|
|
|
// Mock dependencies
|
|
vi.mock("~/database/context");
|
|
vi.mock("~/server/socket", () => ({
|
|
getSocketIO: vi.fn(),
|
|
}));
|
|
vi.mock("~/lib/auth.server", () => ({
|
|
auth: { api: { getSession: vi.fn() } },
|
|
}));
|
|
vi.mock("~/models/user", () => ({
|
|
isUserAdmin: vi.fn().mockResolvedValue(false),
|
|
}));
|
|
|
|
describe("Autodraft Settings API", () => {
|
|
let mockDb: any;
|
|
let mockSocketIO: any;
|
|
|
|
beforeEach(async () => {
|
|
vi.clearAllMocks();
|
|
|
|
// Mock auth session
|
|
const { auth } = await import("~/lib/auth.server");
|
|
vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: "user-789" } } as any);
|
|
|
|
// Mock Socket.IO
|
|
mockSocketIO = {
|
|
to: vi.fn().mockReturnThis(),
|
|
emit: vi.fn(),
|
|
};
|
|
|
|
const socketModule = await import("~/server/socket");
|
|
vi.mocked(socketModule.getSocketIO).mockReturnValue(mockSocketIO);
|
|
|
|
// Mock database
|
|
mockDb = {
|
|
query: {
|
|
teams: {
|
|
findFirst: vi.fn(),
|
|
},
|
|
seasons: {
|
|
findFirst: vi.fn().mockResolvedValue({ id: "season-123", leagueId: "league-1" }),
|
|
},
|
|
commissioners: {
|
|
findFirst: vi.fn().mockResolvedValue(null),
|
|
},
|
|
autodraftSettings: {
|
|
findFirst: vi.fn(),
|
|
},
|
|
},
|
|
update: vi.fn().mockReturnThis(),
|
|
set: vi.fn().mockReturnThis(),
|
|
where: vi.fn().mockReturnThis(),
|
|
returning: vi.fn(),
|
|
insert: vi.fn().mockReturnThis(),
|
|
values: vi.fn().mockReturnThis(),
|
|
};
|
|
|
|
const { database } = await import("~/database/context");
|
|
vi.mocked(database).mockReturnValue(mockDb);
|
|
});
|
|
|
|
describe("Update Autodraft Settings", () => {
|
|
it("should create new autodraft settings when none exist", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
// Mock team ownership verification
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
// Mock no existing settings
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
// Mock insert returning new settings
|
|
const newSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
queueOnly: false,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValue([newSettings]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
formData.append("queueOnly", "false");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.settings.id).toBe(newSettings.id);
|
|
expect(data.settings.isEnabled).toBe(true);
|
|
expect(data.settings.mode).toBe("next_pick");
|
|
expect(mockDb.insert).toHaveBeenCalled();
|
|
expect(mockSocketIO.to).toHaveBeenCalledWith(`draft-${seasonId}`);
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
queueOnly: false,
|
|
source: "user",
|
|
});
|
|
});
|
|
|
|
it("should update existing autodraft settings", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
const existingSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: false,
|
|
mode: "next_pick",
|
|
queueOnly: false,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(existingSettings);
|
|
|
|
const updatedSettings = {
|
|
...existingSettings,
|
|
isEnabled: true,
|
|
mode: "while_on",
|
|
queueOnly: false,
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValue([updatedSettings]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "while_on");
|
|
formData.append("queueOnly", "false");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.settings.isEnabled).toBe(true);
|
|
expect(data.settings.mode).toBe("while_on");
|
|
expect(mockDb.update).toHaveBeenCalled();
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "while_on",
|
|
queueOnly: false,
|
|
source: "user",
|
|
});
|
|
});
|
|
|
|
it("should save queueOnly flag correctly", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
const newSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
queueOnly: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValue([newSettings]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
formData.append("queueOnly", "true");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
expect(data.success).toBe(true);
|
|
expect(data.settings.queueOnly).toBe(true);
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
queueOnly: true,
|
|
source: "user",
|
|
});
|
|
});
|
|
|
|
it("should reject unauthorized users", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const differentUserId = "user-999";
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: differentUserId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
|
|
expect(response.status).toBe(403);
|
|
const data = await response.json();
|
|
expect(data.error).toBe("Unauthorized");
|
|
});
|
|
|
|
it("should require all fields", async () => {
|
|
const formData = new FormData();
|
|
formData.append("seasonId", "season-123");
|
|
// Missing teamId and mode
|
|
|
|
const request = new Request("http://localhost/api/autodraft/update", {
|
|
method: "POST",
|
|
body: formData,
|
|
});
|
|
|
|
const response = await action({
|
|
request,
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
|
|
expect(response.status).toBe(400);
|
|
const data = await response.json();
|
|
expect(data.error).toBe("Missing required fields");
|
|
});
|
|
|
|
it("should handle all three autodraft states (Off, Next Pick, All Picks)", async () => {
|
|
const seasonId = "season-123";
|
|
const teamId = "team-456";
|
|
const userId = "user-789";
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
// State 1: Off (isEnabled=false)
|
|
const offSettings = {
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: false,
|
|
mode: "next_pick",
|
|
queueOnly: false,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
mockDb.returning.mockResolvedValueOnce([offSettings]);
|
|
|
|
const formData1 = new FormData();
|
|
formData1.append("seasonId", seasonId);
|
|
formData1.append("teamId", teamId);
|
|
formData1.append("isEnabled", "false");
|
|
formData1.append("mode", "next_pick");
|
|
formData1.append("queueOnly", "false");
|
|
|
|
const response1 = await action({
|
|
request: new Request("http://localhost/api/autodraft/update", { method: "POST", body: formData1 }),
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
const data1 = await response1.json();
|
|
expect(data1.settings.isEnabled).toBe(false);
|
|
|
|
// State 2: Next Pick (isEnabled=true, mode=next_pick)
|
|
const nextPickSettings = { ...offSettings, id: "settings-2", isEnabled: true, mode: "next_pick" };
|
|
mockDb.returning.mockResolvedValueOnce([nextPickSettings]);
|
|
|
|
const formData2 = new FormData();
|
|
formData2.append("seasonId", seasonId);
|
|
formData2.append("teamId", teamId);
|
|
formData2.append("isEnabled", "true");
|
|
formData2.append("mode", "next_pick");
|
|
formData2.append("queueOnly", "false");
|
|
|
|
const response2 = await action({
|
|
request: new Request("http://localhost/api/autodraft/update", { method: "POST", body: formData2 }),
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
const data2 = await response2.json();
|
|
expect(data2.settings.isEnabled).toBe(true);
|
|
expect(data2.settings.mode).toBe("next_pick");
|
|
|
|
// State 3: All Picks (isEnabled=true, mode=while_on)
|
|
const allPicksSettings = { ...offSettings, id: "settings-3", isEnabled: true, mode: "while_on" };
|
|
mockDb.returning.mockResolvedValueOnce([allPicksSettings]);
|
|
|
|
const formData3 = new FormData();
|
|
formData3.append("seasonId", seasonId);
|
|
formData3.append("teamId", teamId);
|
|
formData3.append("isEnabled", "true");
|
|
formData3.append("mode", "while_on");
|
|
formData3.append("queueOnly", "false");
|
|
|
|
const response3 = await action({
|
|
request: new Request("http://localhost/api/autodraft/update", { method: "POST", body: formData3 }),
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
const data3 = await response3.json();
|
|
expect(data3.settings.isEnabled).toBe(true);
|
|
expect(data3.settings.mode).toBe("while_on");
|
|
});
|
|
|
|
it("should emit socket event to correct room with queueOnly included", async () => {
|
|
const seasonId = "season-abc";
|
|
const teamId = "team-xyz";
|
|
const userId = "user-123";
|
|
|
|
const { auth } = await import("~/lib/auth.server");
|
|
vi.mocked(auth.api.getSession).mockResolvedValue({ user: { id: userId } } as any);
|
|
|
|
mockDb.query.teams.findFirst.mockResolvedValue({
|
|
id: teamId,
|
|
ownerId: userId,
|
|
name: "Test Team",
|
|
});
|
|
|
|
mockDb.query.autodraftSettings.findFirst.mockResolvedValue(null);
|
|
|
|
mockDb.returning.mockResolvedValue([{
|
|
id: "settings-1",
|
|
seasonId,
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
queueOnly: true,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}]);
|
|
|
|
const formData = new FormData();
|
|
formData.append("seasonId", seasonId);
|
|
formData.append("teamId", teamId);
|
|
formData.append("isEnabled", "true");
|
|
formData.append("mode", "next_pick");
|
|
formData.append("queueOnly", "true");
|
|
|
|
await action({
|
|
request: new Request("http://localhost/api/autodraft/update", { method: "POST", body: formData }),
|
|
params: {},
|
|
context: ctx,
|
|
});
|
|
|
|
expect(mockSocketIO.to).toHaveBeenCalledWith(`draft-${seasonId}`);
|
|
expect(mockSocketIO.emit).toHaveBeenCalledWith("autodraft-updated", {
|
|
teamId,
|
|
isEnabled: true,
|
|
mode: "next_pick",
|
|
queueOnly: true,
|
|
source: "user",
|
|
});
|
|
});
|
|
});
|
|
});
|