fix(#410): use toNodeHandler for BetterAuth Express compatibility
Some checks failed
ci/woodpecker/push/api Pipeline failed
Some checks failed
ci/woodpecker/push/api Pipeline failed
BetterAuth expects Web API Request objects (Fetch API standard) with headers.get(), but NestJS/Express passes IncomingMessage objects with headers[] property access. Use better-auth/node's toNodeHandler to properly convert between Express req/res and BetterAuth's Web API handler. Also fixes vitest SWC config to read the correct tsconfig for NestJS decorator metadata emission, which was causing DI injection failures in tests. Fixes #410 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import type { AuthUser, AuthSession } from "@mosaic/shared";
|
||||
import type { Request as ExpressRequest, Response as ExpressResponse } from "express";
|
||||
import { AuthController } from "./auth.controller";
|
||||
import { AuthService } from "./auth.service";
|
||||
|
||||
describe("AuthController", () => {
|
||||
let controller: AuthController;
|
||||
let authService: AuthService;
|
||||
|
||||
const mockNodeHandler = vi.fn().mockResolvedValue(undefined);
|
||||
|
||||
const mockAuthService = {
|
||||
getAuth: vi.fn(),
|
||||
getNodeHandler: vi.fn().mockReturnValue(mockNodeHandler),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -24,25 +27,30 @@ describe("AuthController", () => {
|
||||
}).compile();
|
||||
|
||||
controller = module.get<AuthController>(AuthController);
|
||||
authService = module.get<AuthService>(AuthService);
|
||||
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Restore mock implementations after clearAllMocks
|
||||
mockAuthService.getNodeHandler.mockReturnValue(mockNodeHandler);
|
||||
mockNodeHandler.mockResolvedValue(undefined);
|
||||
});
|
||||
|
||||
describe("handleAuth", () => {
|
||||
it("should call BetterAuth handler", async () => {
|
||||
const mockHandler = vi.fn().mockResolvedValue({ status: 200 });
|
||||
mockAuthService.getAuth.mockReturnValue({ handler: mockHandler });
|
||||
|
||||
it("should delegate to BetterAuth node handler with Express req/res", async () => {
|
||||
const mockRequest = {
|
||||
method: "GET",
|
||||
url: "/auth/session",
|
||||
};
|
||||
headers: {},
|
||||
ip: "127.0.0.1",
|
||||
socket: { remoteAddress: "127.0.0.1" },
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
await controller.handleAuth(mockRequest as unknown as Request);
|
||||
const mockResponse = {} as unknown as ExpressResponse;
|
||||
|
||||
expect(mockAuthService.getAuth).toHaveBeenCalled();
|
||||
expect(mockHandler).toHaveBeenCalledWith(mockRequest);
|
||||
await controller.handleAuth(mockRequest, mockResponse);
|
||||
|
||||
expect(mockAuthService.getNodeHandler).toHaveBeenCalled();
|
||||
expect(mockNodeHandler).toHaveBeenCalledWith(mockRequest, mockResponse);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user