diff --git a/cypress/e2e/authorization.cy.ts b/cypress/e2e/authorization.cy.ts new file mode 100644 index 00000000..e4911691 --- /dev/null +++ b/cypress/e2e/authorization.cy.ts @@ -0,0 +1,53 @@ +import { testUsers } from "#utils/testing/test-users" + +describe("Authorization", () => { + it("logs in successfully", () => { + cy.visit("/auth") + + expect(localStorage.authToken).to.be.equal(undefined) + cy.contains("Welcome").should("be.visible") + cy.contains("Log in").should("be.disabled") + cy.get('input[name="username"]').type("john-doe") + cy.get('input[name="password"]').type("john-doe-password") + cy.contains("Log in").should("be.enabled").click() + cy.contains("Welcome").should("not.exist") + expect(localStorage.authToken).to.match(/.+/) + cy.contains("Log in").should("not.exist") + cy.contains("Log out").should("be.visible") + cy.contains("You are logged in as john-doe.").should("be.visible") + }) + + it("case: user enters unexisting username", () => { + cy.visit("/auth") + + cy.get('input[name="username"]').type("john-doe-WITH-A-TYPO") + cy.get('input[name="password"]').type("john-doe-password") + cy.contains("Log in").click() + cy.contains("User not found.").should("be.visible") + cy.get('input[name="username"]').clear().type("john-doe") + cy.contains("Log in").click() + cy.contains("You are logged in as john-doe.").should("be.visible") + }) + + it("case: user enters invalid password", () => { + cy.visit("/auth") + + cy.get('input[name="username"]').type("john-doe") + cy.get('input[name="password"]').type("john-doe-password-WITH-A-TYPO") + cy.contains("Log in").click() + cy.contains("Invalid password.").should("be.visible") + cy.get('input[name="password"]').clear().type("john-doe-password") + cy.contains("Log in").click() + cy.contains("You are logged in as john-doe.").should("be.visible") + }) + + it("logs out successfully", () => { + cy.authorize(testUsers.johnDoe.id) + cy.visit("/auth") + + expect(localStorage.authToken).to.match(/.+/) + cy.contains("Log out").click() + cy.contains("Log in").should("be.visible") + expect(localStorage.authToken).to.be.equal(undefined) + }) +}) diff --git a/src/views/auth/index.test.tsx b/src/views/auth/index.test.tsx deleted file mode 100644 index e9cf0cbb..00000000 --- a/src/views/auth/index.test.tsx +++ /dev/null @@ -1,74 +0,0 @@ -import { screen, waitFor, waitForElementToBeRemoved } from "@testing-library/react" -import userEvent from "@testing-library/user-event" - -import { render } from "#utils/testing/render" -import { testUsers } from "#utils/testing/test-users" - -import { Auth } from "./index" - -describe("Auth service.", () => { - test("Login works correctly.", async () => { - await render(, { iAm: "guest" }) - - expect(localStorage.authToken).toBeUndefined() - expect(screen.getByText("Welcome")).toBeInTheDocument() - expect(screen.getByText("Log in")).toBeDisabled() - await waitFor(() => userEvent.type(screen.getByLabelText("Username"), "john-doe")) - await waitFor(() => userEvent.type(screen.getByLabelText("Password"), "john-doe-password")) - expect(screen.getByText("Log in")).toBeEnabled() - await waitFor(() => userEvent.click(screen.getByText("Log in"))) - await waitForElementToBeRemoved(() => screen.getByText("Welcome")) - expect(localStorage.authToken).toEqual(expect.stringMatching(".+")) - expect(screen.queryByText("Log in")).not.toBeInTheDocument() - expect(screen.getByText("Log out")).toBeInTheDocument() - await waitFor(() => { - expect(screen.getByText(/You are logged in/)).toHaveTextContent("You are logged in as john-doe.") - }) - }) - - test("Case: User enters unexisting username.", async () => { - await render(, { iAm: "guest" }) - - expect(localStorage.authToken).toBeUndefined() - await waitFor(() => userEvent.type(screen.getByLabelText("Username"), "john-doe-INCORRECT-USERNAME")) - await waitFor(() => userEvent.type(screen.getByLabelText("Password"), "john-doe-password")) - await waitFor(() => userEvent.click(screen.getByText("Log in"))) - await waitFor(() => expect(screen.getByText("User not found.")).toBeInTheDocument()) - - userEvent.clear(screen.getByLabelText("Username")) - await waitFor(() => expect(screen.queryByText("User not found.")).not.toBeInTheDocument()) - await waitFor(() => userEvent.type(screen.getByLabelText("Username"), "john-doe")) - await waitFor(() => userEvent.click(screen.getByText("Log in"))) - await waitFor(() => { - expect(screen.getByText(/You are logged in/)).toHaveTextContent("You are logged in as john-doe.") - }) - }) - - test("Case: User enters invalid password.", async () => { - await render(, { iAm: "guest" }) - - expect(localStorage.authToken).toBeUndefined() - await waitFor(() => userEvent.type(screen.getByLabelText("Username"), "john-doe")) - await waitFor(() => userEvent.type(screen.getByLabelText("Password"), "john-doe-INVALID-password")) - userEvent.click(screen.getByText("Log in")) - await waitFor(() => expect(screen.getByText("Invalid password.")).toBeInTheDocument()) - - userEvent.clear(screen.getByLabelText("Password")) - await waitFor(() => expect(screen.queryByText("Invalid password.")).not.toBeInTheDocument()) - await waitFor(() => userEvent.type(screen.getByLabelText("Password"), "john-doe-password")) - await waitFor(() => userEvent.click(screen.getByText("Log in"))) - await waitFor(() => { - expect(screen.getByText(/You are logged in/)).toHaveTextContent("You are logged in as john-doe.") - }) - }) - - test("Logout works correctly.", async () => { - await render(, { iAm: testUsers.johnDoe.id }) - - expect(localStorage.authToken).toEqual(expect.stringMatching(".+")) - userEvent.click(screen.getByText("Log out")) - await waitFor(() => expect(localStorage.authToken).toBeUndefined()) - expect(screen.queryByText("Log out")).not.toBeInTheDocument() - expect(screen.getByText("Log in")).toBeInTheDocument() - }) -})