fix(auth): restore BetterAuth OIDC flow across api/web/compose
This commit is contained in:
@@ -102,11 +102,46 @@ describe("AuthController", () => {
|
||||
expect(err).toBeInstanceOf(HttpException);
|
||||
expect((err as HttpException).getStatus()).toBe(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
expect((err as HttpException).getResponse()).toBe(
|
||||
"Unable to complete authentication. Please try again in a moment.",
|
||||
"Unable to complete authentication. Please try again in a moment."
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("should preserve better-call status and body for handler APIError", async () => {
|
||||
const apiError = {
|
||||
statusCode: HttpStatus.BAD_REQUEST,
|
||||
message: "Invalid OAuth configuration",
|
||||
body: {
|
||||
message: "Invalid OAuth configuration",
|
||||
code: "INVALID_OAUTH_CONFIGURATION",
|
||||
},
|
||||
};
|
||||
mockNodeHandler.mockRejectedValueOnce(apiError);
|
||||
|
||||
const mockRequest = {
|
||||
method: "POST",
|
||||
url: "/auth/sign-in/oauth2",
|
||||
headers: {},
|
||||
ip: "192.168.1.10",
|
||||
socket: { remoteAddress: "192.168.1.10" },
|
||||
} as unknown as ExpressRequest;
|
||||
|
||||
const mockResponse = {
|
||||
headersSent: false,
|
||||
} as unknown as ExpressResponse;
|
||||
|
||||
try {
|
||||
await controller.handleAuth(mockRequest, mockResponse);
|
||||
expect.unreachable("Expected HttpException to be thrown");
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(HttpException);
|
||||
expect((err as HttpException).getStatus()).toBe(HttpStatus.BAD_REQUEST);
|
||||
expect((err as HttpException).getResponse()).toMatchObject({
|
||||
message: "Invalid OAuth configuration",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("should log warning and not throw when handler throws after headers sent", async () => {
|
||||
const handlerError = new Error("Stream interrupted");
|
||||
mockNodeHandler.mockRejectedValueOnce(handlerError);
|
||||
@@ -142,9 +177,7 @@ describe("AuthController", () => {
|
||||
headersSent: false,
|
||||
} as unknown as ExpressResponse;
|
||||
|
||||
await expect(controller.handleAuth(mockRequest, mockResponse)).rejects.toThrow(
|
||||
HttpException,
|
||||
);
|
||||
await expect(controller.handleAuth(mockRequest, mockResponse)).rejects.toThrow(HttpException);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -187,7 +220,7 @@ describe("AuthController", () => {
|
||||
OIDC_CLIENT_SECRET: "test-client-secret",
|
||||
OIDC_CLIENT_ID: "test-client-id",
|
||||
OIDC_ISSUER: "https://auth.test.com/",
|
||||
OIDC_REDIRECT_URI: "https://app.test.com/auth/callback/authentik",
|
||||
OIDC_REDIRECT_URI: "https://app.test.com/auth/oauth2/callback/authentik",
|
||||
BETTER_AUTH_SECRET: "test-better-auth-secret",
|
||||
JWT_SECRET: "test-jwt-secret",
|
||||
CSRF_SECRET: "test-csrf-secret",
|
||||
@@ -296,11 +329,9 @@ describe("AuthController", () => {
|
||||
},
|
||||
};
|
||||
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(UnauthorizedException);
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(
|
||||
UnauthorizedException,
|
||||
);
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(
|
||||
"Missing authentication context",
|
||||
"Missing authentication context"
|
||||
);
|
||||
});
|
||||
|
||||
@@ -313,22 +344,18 @@ describe("AuthController", () => {
|
||||
},
|
||||
};
|
||||
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(UnauthorizedException);
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(
|
||||
UnauthorizedException,
|
||||
);
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(
|
||||
"Missing authentication context",
|
||||
"Missing authentication context"
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw UnauthorizedException when both req.user and req.session are undefined", () => {
|
||||
const mockRequest = {};
|
||||
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(UnauthorizedException);
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(
|
||||
UnauthorizedException,
|
||||
);
|
||||
expect(() => controller.getSession(mockRequest as never)).toThrow(
|
||||
"Missing authentication context",
|
||||
"Missing authentication context"
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -401,9 +428,7 @@ describe("AuthController", () => {
|
||||
|
||||
await controller.handleAuth(mockRequest, mockResponse);
|
||||
|
||||
expect(debugSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining("203.0.113.50"),
|
||||
);
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining("203.0.113.50"));
|
||||
});
|
||||
|
||||
it("should extract first IP from X-Forwarded-For with comma-separated IPs", async () => {
|
||||
@@ -423,13 +448,9 @@ describe("AuthController", () => {
|
||||
|
||||
await controller.handleAuth(mockRequest, mockResponse);
|
||||
|
||||
expect(debugSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining("203.0.113.50"),
|
||||
);
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining("203.0.113.50"));
|
||||
// Ensure it does NOT contain the second IP in the extracted position
|
||||
expect(debugSpy).toHaveBeenCalledWith(
|
||||
expect.not.stringContaining("70.41.3.18"),
|
||||
);
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.not.stringContaining("70.41.3.18"));
|
||||
});
|
||||
|
||||
it("should extract first IP from X-Forwarded-For as array", async () => {
|
||||
@@ -449,9 +470,7 @@ describe("AuthController", () => {
|
||||
|
||||
await controller.handleAuth(mockRequest, mockResponse);
|
||||
|
||||
expect(debugSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining("203.0.113.50"),
|
||||
);
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining("203.0.113.50"));
|
||||
});
|
||||
|
||||
it("should fallback to req.ip when no X-Forwarded-For header", async () => {
|
||||
@@ -471,9 +490,7 @@ describe("AuthController", () => {
|
||||
|
||||
await controller.handleAuth(mockRequest, mockResponse);
|
||||
|
||||
expect(debugSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining("192.168.1.100"),
|
||||
);
|
||||
expect(debugSpy).toHaveBeenCalledWith(expect.stringContaining("192.168.1.100"));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user