80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
// The Lucia project.
|
|
// Copyright 2026-2026 DSP, inc. All rights reserved.
|
|
// Authors:
|
|
// imacat.yang@dsp.im (imacat), 2026/03/06
|
|
|
|
import { describe, it, expect, beforeEach } from "vitest";
|
|
|
|
describe("router beforeEach guard logic", () => {
|
|
beforeEach(() => {
|
|
// Clear cookies
|
|
document.cookie.split(";").forEach((c) => {
|
|
const name = c.split("=")[0].trim();
|
|
if (name) {
|
|
document.cookie = name + "=; Max-Age=-99999999; path=/";
|
|
}
|
|
});
|
|
});
|
|
|
|
// Simulate the guard logic from router/index.ts
|
|
function runGuard(to) {
|
|
const hasLoginMarker = document.cookie
|
|
.split(";")
|
|
.some((c) => c.trim().startsWith("isLuciaLoggedIn="));
|
|
const hasAccessToken = document.cookie
|
|
.split(";")
|
|
.some((c) => c.trim().startsWith("luciaToken="));
|
|
const isAuthenticated = hasLoginMarker && hasAccessToken;
|
|
|
|
if (to.name === "Login") {
|
|
if (isAuthenticated) return { name: "Files" };
|
|
}
|
|
|
|
const requiresAuth = (to.matched || []).some((r) => r.meta?.requiresAuth);
|
|
if (requiresAuth && !isAuthenticated) {
|
|
return {
|
|
path: "/login",
|
|
query: {
|
|
"return-to": btoa(to.fullPath || to.path || "/"),
|
|
},
|
|
};
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
it("redirects logged-in user from Login to Files", () => {
|
|
document.cookie = "isLuciaLoggedIn=true";
|
|
document.cookie = "luciaToken=token";
|
|
expect(runGuard({ name: "Login" })).toEqual({ name: "Files" });
|
|
});
|
|
|
|
it("allows unauthenticated user to visit Login", () => {
|
|
expect(runGuard({ name: "Login" })).toBeUndefined();
|
|
});
|
|
|
|
it("redirects unauthenticated user when route requiresAuth", () => {
|
|
const result = runGuard({
|
|
name: "Files",
|
|
path: "/files",
|
|
fullPath: "/files",
|
|
matched: [{ meta: { requiresAuth: true } }],
|
|
});
|
|
expect(result.path).toBe("/login");
|
|
expect(atob(result.query["return-to"])).toBe("/files");
|
|
});
|
|
|
|
it("does not interfere with non-Login routes", () => {
|
|
document.cookie = "isLuciaLoggedIn=true";
|
|
document.cookie = "luciaToken=token";
|
|
expect(
|
|
runGuard({
|
|
name: "Files",
|
|
path: "/files",
|
|
fullPath: "/files",
|
|
matched: [{ meta: { requiresAuth: true } }],
|
|
}),
|
|
).toBeUndefined();
|
|
});
|
|
});
|