Skip to content

Commit

Permalink
Added tests for authentication errors
Browse files Browse the repository at this point in the history
  • Loading branch information
teclator committed May 31, 2024
1 parent 0d6a9fd commit 0f61f78
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion web/src/components/core/LoginPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,28 @@ import React from "react";
import { screen, within } from "@testing-library/react";
import { plainRender } from "~/test-utils";
import { LoginPage } from "~/components/core";
import { AuthErrors } from "~/context/auth";

let mockIsAuthenticated;
const mockLoginFn = jest.fn();
let mockLoginError;

jest.mock("~/context/auth", () => ({
...jest.requireActual("~/context/auth"),
useAuth: () => {
return {
isAuthenticated: mockIsAuthenticated,
login: mockLoginFn
login: mockLoginFn,
error: mockLoginError
};
}
}));

describe("LoginPage", () => {
beforeAll(() => {
mockIsAuthenticated = false;
mockLoginError = null;
mockLoginFn.mockResolvedValue({ status: 200 });
jest.spyOn(console, "error").mockImplementation();
});

Expand All @@ -65,6 +70,50 @@ describe("LoginPage", () => {
expect(mockLoginFn).toHaveBeenCalledWith("s3cr3t");
});

describe("and the entered password is wrong", () => {
beforeAll(() => {
mockLoginFn.mockResolvedValue({ status: 400 });
mockLoginError = AuthErrors.AUTH;
});

it("renders an authentication error", async () => {
const { user } = plainRender(<LoginPage />);
const form = screen.getByRole("form", { name: "Login form" });
const passwordInput = within(form).getByLabelText("Password input");
const loginButton = within(form).getByRole("button", { name: "Log in" });

await user.type(passwordInput, "s3cr3t");
await user.click(loginButton);

expect(mockLoginFn).toHaveBeenCalledWith("s3cr3t");
const form_error = screen.getByRole("form", { name: "Login form" });
within(form_error).getByText(/Could not log in/);
});
});

describe("and the server is down", () => {
beforeAll(() => {
mockLoginFn.mockResolvedValue({ status: 504 });
mockLoginError = AuthErrors.SERVER;
});

it("renders a server error text", async () => {
const { user } = plainRender(<LoginPage />);
const form = screen.getByRole("form", { name: "Login form" });
const passwordInput = within(form).getByLabelText("Password input");
const loginButton = within(form).getByRole("button", { name: "Log in" });

await user.type(passwordInput, "s3cr3t");
await user.click(loginButton);

expect(mockLoginFn).toHaveBeenCalledWith("s3cr3t");
const form_error = screen.getByRole("form", { name: "Login form" });
within(form_error).getByText(/Could not authenticate/);
});
});


Check failure on line 115 in web/src/components/core/LoginPage.test.jsx

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

More than 1 blank line not allowed

Check failure on line 115 in web/src/components/core/LoginPage.test.jsx

View workflow job for this annotation

GitHub Actions / frontend_build (18.x)

More than 1 blank line not allowed

it("renders a button to know more about the project", async () => {
const { user } = plainRender(<LoginPage />);
const button = screen.getByRole("button", { name: "What is this?" });
Expand Down

0 comments on commit 0f61f78

Please sign in to comment.