Skip to content

Commit

Permalink
fix register tests, and start login tests not working yet. Also impro…
Browse files Browse the repository at this point in the history
…ve test report
  • Loading branch information
MaximeDan committed Jun 20, 2024
1 parent 6a2891e commit ec09c3f
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 24 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts

.env
.env

# tests
test-report.html
9 changes: 9 additions & 0 deletions __test__/auth/apiClient/registerUserApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ it("returns error for bad request", async () => {
errors: expect.arrayContaining([
expect.objectContaining({
message: "Ce champ est requis",
path: "username",
}),
expect.objectContaining({
message: "Ce champ est requis",
path: "dateOfBirth",
}),
expect.objectContaining({
message: "Email invalide",
path: "email",
}),
]),
Expand All @@ -100,6 +108,7 @@ it("returns error for bad request", async () => {
});
});


it("returns error for invalid date of birth", async () => {
await testApiHandler({
appHandler: registerHandler,
Expand Down
85 changes: 85 additions & 0 deletions __test__/auth/loginUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* @jest-environment node
*/

import { testApiHandler } from "next-test-api-route-handler";
import * as loginHandler from "@/app/api/auth/[...nextauth]/route";

describe("NextAuth API", () => {
it("signs in with valid credentials", async () => {
await testApiHandler({
appHandler: loginHandler, // Use appHandler for Next.js API routes
params: { nextauth: ["callback", "credentials"] },
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
identifier: "[email protected]",
password: "alicePassword",
}),
});

console.log("Response status:", res.status);

if (res.status === 302) {
console.log("Redirect location:", res.headers.get("location"));
}

// Handle the 302 redirect or other responses as needed
expect(res.status).toBe(200); // Adjust this expectation based on your actual response

// Only parse JSON if status is not 302
if (res.status !== 302) {
const data = await res.json();
console.log("Response data:", data);

expect(data).toHaveProperty("user");
expect(data.user).toEqual(
expect.objectContaining({
email: "[email protected]",
}),
);
}
},
});
});

it("fails to sign in with invalid credentials", async () => {
await testApiHandler({
appHandler: loginHandler, // Use appHandler for Next.js API routes
params: { nextauth: ["callback", "credentials"] },
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
identifier: "[email protected]",
password: "invalid-password",
}),
});

console.log("Response status:", res.status);

if (res.status === 302) {
console.log("Redirect location:", res.headers.get("location"));
}

// Handle the 302 redirect or other responses as needed
expect(res.status).toBe(401); // Adjust this expectation based on your actual response

// Only parse JSON if status is not 302
if (res.status !== 302) {
const data = await res.json();
console.log("Response data:", data);

expect(data).toEqual(
expect.objectContaining({
error: "CredentialsSignin",
}),
);
}
},
});
});
});
39 changes: 19 additions & 20 deletions __test__/validators/registerSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
};

expect(() => registerSchema.parse(input)).not.toThrow();
});

it("fails validation with invalid email format", () => {
const input = {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: "1990-01-01",
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "invalid-email",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -34,7 +35,7 @@ describe("registerSchema", () => {
name: "J".repeat(256),
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -50,7 +51,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "password@123",
confirmPassword: "password@123",
Expand All @@ -66,7 +67,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@",
confirmPassword: "Password@",
Expand All @@ -82,7 +83,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password123",
confirmPassword: "Password123",
Expand All @@ -98,7 +99,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Pass@1",
confirmPassword: "Pass@1",
Expand All @@ -114,7 +115,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@124",
Expand All @@ -140,23 +141,21 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: "invalid-date", // This should be a string to trigger the invalid date error
dateOfBirth: "invalid-date",
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
};

expect(() => registerSchema.parse(input)).toThrow(
"Expected date, received string",
);
expect(() => registerSchema.parse(input)).toThrow("Date invalide");
});

it("fails validation with date of birth in the future", () => {
const input = {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date(Date.now() + 86400000), // Future date
dateOfBirth: new Date(Date.now() + 86400000).toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -172,7 +171,7 @@ describe("registerSchema", () => {
name: " ",
lastName: " ",
username: " ",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -186,7 +185,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Abc12345",
confirmPassword: "Abc12345",
Expand All @@ -202,7 +201,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password 123",
confirmPassword: "Password 123",
Expand All @@ -218,7 +217,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "JohnDoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -232,7 +231,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -246,7 +245,7 @@ describe("registerSchema", () => {
name: "John",
lastName: "Doe",
username: "johndoe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: " [email protected] ",
password: "Password@123",
confirmPassword: "Password@123",
Expand All @@ -262,7 +261,7 @@ describe("registerSchema", () => {
name: 123,
lastName: {},
username: [],
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
email: 456,
password: true,
confirmPassword: false,
Expand Down
14 changes: 13 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,19 @@ const config: Config = {
// projects: undefined,

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
reporters: [
"default",
[
"jest-html-reporter",
{
pageTitle: "Test Report",
outputPath: "test-report.html",
includeFailureMsg: true,
includeConsoleLog: true,
},
],
"jest-summary-reporter",
],

// Automatically reset mock state before every test
// resetMocks: false,
Expand Down
Loading

0 comments on commit ec09c3f

Please sign in to comment.