test: cover better auth migration config
This commit is contained in:
61
src/auth.migration.ts
Normal file
61
src/auth.migration.ts
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import { DatabaseSync } from "node:sqlite";
|
||||||
|
import { passkey } from "@better-auth/passkey";
|
||||||
|
import { betterAuth } from "better-auth";
|
||||||
|
import type { BetterAuthPlugin } from "better-auth";
|
||||||
|
import {
|
||||||
|
admin,
|
||||||
|
emailOTP,
|
||||||
|
haveIBeenPwned,
|
||||||
|
lastLoginMethod,
|
||||||
|
multiSession,
|
||||||
|
openAPI,
|
||||||
|
twoFactor,
|
||||||
|
} from "better-auth/plugins";
|
||||||
|
import { passwordPolicy } from "./password";
|
||||||
|
|
||||||
|
const migrationBaseURL = "http://localhost:8788";
|
||||||
|
|
||||||
|
export const migrationPlugins: BetterAuthPlugin[] = [
|
||||||
|
openAPI(),
|
||||||
|
haveIBeenPwned({
|
||||||
|
customPasswordCompromisedMessage: "This password has appeared in a data breach.",
|
||||||
|
}),
|
||||||
|
emailOTP({
|
||||||
|
otpLength: 6,
|
||||||
|
expiresIn: 300,
|
||||||
|
allowedAttempts: 3,
|
||||||
|
storeOTP: "hashed",
|
||||||
|
sendVerificationOnSignUp: true,
|
||||||
|
overrideDefaultEmailVerification: false,
|
||||||
|
sendVerificationOTP: async () => {},
|
||||||
|
}),
|
||||||
|
twoFactor(),
|
||||||
|
multiSession({ maximumSessions: 10 }),
|
||||||
|
lastLoginMethod({ storeInDatabase: true }),
|
||||||
|
admin(),
|
||||||
|
passkey({
|
||||||
|
rpID: "localhost",
|
||||||
|
rpName: "cfw-auth",
|
||||||
|
origin: migrationBaseURL,
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
export const auth = betterAuth({
|
||||||
|
database: new DatabaseSync(":memory:"),
|
||||||
|
baseURL: migrationBaseURL,
|
||||||
|
secret: "migration-schema-generation-only",
|
||||||
|
emailAndPassword: {
|
||||||
|
enabled: true,
|
||||||
|
...passwordPolicy,
|
||||||
|
requireEmailVerification: true,
|
||||||
|
revokeSessionsOnPasswordReset: true,
|
||||||
|
},
|
||||||
|
emailVerification: {
|
||||||
|
sendOnSignUp: true,
|
||||||
|
sendOnSignIn: true,
|
||||||
|
autoSignInAfterVerification: true,
|
||||||
|
},
|
||||||
|
plugins: migrationPlugins,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default auth;
|
||||||
@@ -174,3 +174,29 @@ describe("passkey config", () => {
|
|||||||
expect(plugins.map((plugin) => plugin.id)).toContain("passkey");
|
expect(plugins.map((plugin) => plugin.id)).toContain("passkey");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("Better Auth migration config", () => {
|
||||||
|
it("exports a static auth instance for the Better Auth CLI", async () => {
|
||||||
|
const module = await import("../src/auth.migration");
|
||||||
|
expect(module.auth).toBeDefined();
|
||||||
|
expect(module.default).toBe(module.auth);
|
||||||
|
expect(typeof module.auth.handler).toBe("function");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps schema-affecting plugins enabled for generated migrations", async () => {
|
||||||
|
const { migrationPlugins } = await import("../src/auth.migration");
|
||||||
|
const pluginIds = migrationPlugins.map((plugin) => plugin.id);
|
||||||
|
|
||||||
|
expect(pluginIds).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
"open-api",
|
||||||
|
"email-otp",
|
||||||
|
"two-factor",
|
||||||
|
"multi-session",
|
||||||
|
"last-login-method",
|
||||||
|
"admin",
|
||||||
|
"passkey",
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user