Refactor module middleware initialization logic.

Replaced `getMiddleware` with `onServerInit` for streamlined middleware registration. Updated `AppAuth` to automatically register its authentication middleware. Added a test case to verify middleware registration. Removed redundant cookie renewal logic from `AdminController` and made related adjustments across modules.
This commit is contained in:
dswbx
2025-01-09 10:59:48 +01:00
parent 7d3d1e811f
commit 47f48be514
9 changed files with 58 additions and 27 deletions

View File

@@ -1,4 +1,5 @@
import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test";
import { afterAll, beforeAll, beforeEach, describe, expect, spyOn, test } from "bun:test";
import { createApp } from "../../src";
import { AuthController } from "../../src/auth/api/AuthController";
import { AppAuth, type ModuleBuildContext } from "../../src/modules";
import { disableConsoleLog, enableConsoleLog } from "../helper";
@@ -76,4 +77,29 @@ describe("AppAuth", () => {
expect(users[0].email).toBe("some@body.com");
}
});
test("registers auth middleware automatically", async () => {
const app = createApp({
initialConfig: {
auth: {
enabled: true,
jwt: {
secret: "123456"
}
}
}
});
await app.build();
const spy = spyOn(app.module.auth.authenticator, "requestCookieRefresh");
// register custom route
app.server.get("/test", async (c) => c.text("test"));
// call a system api and then the custom route
await app.server.request("/api/system/ping");
await app.server.request("/test");
expect(spy.mock.calls.length).toBe(2);
});
});