Migrate Cypress E2E from cy.intercept to MSW service worker

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-22 09:30:22 +08:00
parent b978071f94
commit 3d1de913f8
27 changed files with 184 additions and 307 deletions

View File

@@ -20,7 +20,6 @@ describe("Paste URL login redirect", () => {
cy.get("#account").type("testadmin");
cy.get("#password").type("password123");
cy.get("form").submit();
cy.wait("@postToken");
// After login, the app should attempt to redirect to the return-to URL.
// Since window.location.href is used (not router.push), we verify the
@@ -35,18 +34,25 @@ describe("Paste URL login redirect", () => {
cy.get("#account").type("testadmin");
cy.get("#password").type("password123");
cy.get("form").submit();
cy.wait("@postToken");
cy.url().should("include", "/files");
});
it("Unauthenticated user cannot access inner pages", () => {
setupApiIntercepts();
// Override my-account to return 401 (simulate logged-out state)
cy.intercept("GET", "/api/my-account", {
statusCode: 401,
body: { detail: "Not authenticated" },
}).as("getMyAccountUnauth");
// Visit login first to load the app + MSW
cy.visit("/login");
cy.get("#login_btn_main_btn").should("exist");
// Override my-account to return 401 (simulate logged-out state) via MSW
cy.window().then((win) => {
const { http, HttpResponse } = win.__msw__;
win.__mswWorker__.use(
http.get("/api/my-account", () =>
HttpResponse.json(
{ detail: "Not authenticated" },
{ status: 401 },
)),
);
});
cy.visit("/files");