Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximeDan committed Jun 20, 2024
1 parent 3df7169 commit 6a2891e
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 60 deletions.
254 changes: 250 additions & 4 deletions __test__/auth/apiClient/registerUserApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ it("registers a new user successfully", async () => {
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01"),
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();
Expand Down Expand Up @@ -50,8 +51,9 @@ it("returns error for already used email", async () => {
lastName: "Prisma",
email: "[email protected]",
username: "aliceUserName",
password: "alicePassword",
dateOfBirth: new Date("1990-01-01"),
password: "Password@123",
confirmPassword: "Password@123",
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();
Expand All @@ -77,14 +79,258 @@ it("returns error for bad request", async () => {
name: "Alice",
lastName: "Prisma",
email: "",
password: "Password@123",
confirmPassword: "Password@123",
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "Ce champ est requis",
path: "email",
}),
]),
}),
);
},
});
});

it("returns error for invalid date of birth", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: "invalid-date",
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "Date invalide",
path: "dateOfBirth",
}),
]),
}),
);
},
});
});

it("returns error for future date of birth", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date(Date.now() + 86400000).toISOString(), // Future date
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "La date ne peut pas être dans le futur",
path: "dateOfBirth",
}),
]),
}),
);
},
});
});

it("returns error for missing name", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
username: "johndoe",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "Ce champ est requis",
path: "name",
}),
]),
}),
);
},
});
});

it("returns error for invalid email format", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "invalid-email",
password: "Password@123",
confirmPassword: "Password@123",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "Email invalide",
path: "email",
}),
]),
}),
);
},
});
});

it("returns error for weak password", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "password",
confirmPassword: "password",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "Veuillez renseigner au moins une lettre majuscule",
path: "password",
}),
]),
}),
);
},
});
});

it("returns error for whitespace in required fields", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@123",
username: " ",
name: " ",
lastName: " ",
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
errors: expect.arrayContaining([
expect.objectContaining({
message: "Ce champ est requis",
path: expect.stringMatching(/^(name|lastName|username)$/),
}),
]),
}),
);
},
});
});

it("returns error for mismatched passwords", async () => {
await testApiHandler({
appHandler: registerHandler,
test: async ({ fetch }) => {
const res = await fetch({
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "[email protected]",
password: "Password@123",
confirmPassword: "Password@124",
username: "johndoe",
name: "John",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01").toISOString(),
}),
});
const data = await res.json();

expect(res.status).toBe(400);
expect(data).toEqual(
expect.objectContaining({
message: "email is required",
errors: expect.arrayContaining([
expect.objectContaining({
message: "Les mots de passe ne correspondent pas",
path: "confirmPassword",
}),
]),
}),
);
},
Expand Down
Loading

0 comments on commit 6a2891e

Please sign in to comment.