describe("Authentication", () => { beforeEach(() => { cy.visit("/login"); }); it("should display login form", () => { cy.get('input[name="username"]').should("be.visible"); cy.get('input[name="password"]').should("be.visible"); cy.get('button[type="submit"]').should("be.visible"); cy.contains("ورود به پنل مدیریت").should("be.visible"); cy.contains("لطفا اطلاعات خود را وارد کنید").should("be.visible"); }); it("should show validation errors for empty fields", () => { // Type something then clear to trigger validation cy.get('input[name="username"]').type("a").clear(); cy.get('input[name="password"]').type("a").clear(); // Click outside to trigger validation cy.get("body").click(); cy.contains("نام کاربری الزامی است").should("be.visible"); cy.contains("رمز عبور الزامی است").should("be.visible"); }); it("should show error for invalid credentials", () => { cy.get('input[name="username"]').type("invaliduser"); cy.get('input[name="password"]').type("wrongpass"); cy.get('button[type="submit"]').click(); cy.contains("نام کاربری یا رمز عبور اشتباه است", { timeout: 10000 }).should( "be.visible" ); }); it("should successfully login with valid credentials", () => { cy.get('input[name="username"]').type("admin"); cy.get('input[name="password"]').type("admin123"); cy.get('button[type="submit"]').click(); // Should redirect to dashboard - handle trailing slash cy.url().should("not.include", "/login"); cy.url().should("satisfy", (url) => { return ( url === Cypress.config().baseUrl || url === Cypress.config().baseUrl + "/" ); }); // Should see dashboard content cy.contains("داشبورد").should("be.visible"); }); it("should logout successfully", () => { // First login cy.get('input[name="username"]').type("admin"); cy.get('input[name="password"]').type("admin123"); cy.get('button[type="submit"]').click(); cy.url().should("not.include", "/login"); // Clear session to simulate logout cy.clearLocalStorage(); cy.visit("/login"); // Should redirect to login cy.url().should("include", "/login"); cy.contains("ورود به پنل مدیریت").should("be.visible"); }); it("should redirect to login when accessing protected routes without authentication", () => { cy.visit("/products"); cy.url().should("include", "/login"); cy.visit("/admin-users"); cy.url().should("include", "/login"); cy.visit("/roles"); cy.url().should("include", "/login"); }); it("should remember login state after page refresh", () => { // Login first cy.get('input[name="username"]').type("admin"); cy.get('input[name="password"]').type("admin123"); cy.get('button[type="submit"]').click(); cy.url().should("not.include", "/login"); cy.reload(); // Should still be logged in cy.url().should("not.include", "/login"); cy.contains("داشبورد").should("be.visible"); }); it("should toggle password visibility", () => { cy.get('input[name="password"]').should("have.attr", "type", "password"); // Click the eye button to show password cy.get(".absolute.inset-y-0.left-0").click(); cy.get('input[name="password"]').should("have.attr", "type", "text"); // Click again to hide password cy.get(".absolute.inset-y-0.left-0").click(); cy.get('input[name="password"]').should("have.attr", "type", "password"); }); });