Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ export const UPDATE_TRACKING_CONSENT = gql`
`;

describe("mutation editProfile", () => {
it("should not allow name with less than 2 letters", async () => {
const user = await userFactory();
const { mutate } = makeClient({ ...user, auth: AuthType.Session });
const newName = "A";
const { errors } = await mutate(EDIT_PROFILE, {
variables: { name: newName }
});
const updatedUser = await prisma.user.findUniqueOrThrow({
where: { id: user.id }
});
expect(errors).toEqual([
expect.objectContaining({
message: `Le nom doit contenir au moins 2 lettres.`,
extensions: expect.objectContaining({ code: ErrorCode.BAD_USER_INPUT })
})
]);
expect(updatedUser.name).toEqual(user.name);
});

it("should not allow name with only special characters", async () => {
const user = await userFactory();
const { mutate } = makeClient({ ...user, auth: AuthType.Session });
const newName = ".-";
const { errors } = await mutate(EDIT_PROFILE, {
variables: { name: newName }
});
const updatedUser = await prisma.user.findUniqueOrThrow({
where: { id: user.id }
});
expect(errors).toEqual([
expect.objectContaining({
message: `Le nom doit contenir au moins 2 lettres.`,
extensions: expect.objectContaining({ code: ErrorCode.BAD_USER_INPUT })
})
]);
expect(updatedUser.name).toEqual(user.name);
});
afterAll(resetDatabase);
it("should edit user profile", async () => {
const user = await userFactory();
Expand Down Expand Up @@ -91,7 +128,7 @@ describe("mutation editProfile", () => {
// Then
expect(errors).toEqual([
expect.objectContaining({
message: `The name cannot be an empty string`,
message: `Le nom doit contenir au moins 2 lettres.`,
extensions: expect.objectContaining({
code: ErrorCode.BAD_USER_INPUT
})
Expand All @@ -118,7 +155,7 @@ describe("mutation editProfile", () => {
// Then
expect(errors).toEqual([
expect.objectContaining({
message: `The name cannot be an empty string`,
message: `Le nom doit contenir au moins 2 lettres.`,
extensions: expect.objectContaining({
code: ErrorCode.BAD_USER_INPUT
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,53 @@ const JOIN_WITH_INVITE = `
`;

describe("joinWithInvite mutation", () => {
it("should not allow name with less than 2 letters", async () => {
const company = await companyFactory();
const invitee = "[email protected]";
const invitation = await prisma.userAccountHash.create({
data: {
email: invitee,
companySiret: company.siret!,
role: "MEMBER",
hash: "hash-shortname"
}
});
const { errors } = await mutate(JOIN_WITH_INVITE, {
variables: {
inviteHash: invitation.hash,
name: "A",
password: "P4a$$woRd_1234"
}
});
expect(errors).not.toBeUndefined();
expect(errors?.[0].message).toBe(
"Le nom doit contenir au moins 2 lettres."
);
});

it("should not allow name with only special characters", async () => {
const company = await companyFactory();
const invitee = "[email protected]";
const invitation = await prisma.userAccountHash.create({
data: {
email: invitee,
companySiret: company.siret!,
role: "MEMBER",
hash: "hash-specialchars"
}
});
const { errors } = await mutate(JOIN_WITH_INVITE, {
variables: {
inviteHash: invitation.hash,
name: ".-",
password: "P4a$$woRd_1234"
}
});
expect(errors).not.toBeUndefined();
expect(errors?.[0].message).toBe(
"Le nom doit contenir au moins 2 lettres."
);
});
let mutate: ReturnType<typeof makeClient>["mutate"];
beforeAll(() => {
const testClient = makeClient();
Expand Down Expand Up @@ -146,7 +193,9 @@ describe("joinWithInvite mutation", () => {

// Then
expect(errors).toBeDefined();
expect(errors[0].message).toContain("Le champ ne peut pas être vide.");
expect(errors[0].message).toContain(
"Le nom doit contenir au moins 2 lettres."
);
});

it("should accept other pending invitations", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,49 @@ const SIGNUP = `
`;

describe("Mutation.signup", () => {
it("should not allow name with less than 2 letters", async () => {
const user = {
email: "[email protected]",
name: "A",
phone: "06 00 00 00 00"
};
const { errors } = await mutate<Pick<Mutation, "signup">>(SIGNUP, {
variables: {
userInfos: {
email: user.email,
password: viablePassword,
name: user.name,
phone: user.phone
}
}
});
expect(errors).not.toBeUndefined();
expect(errors?.[0].message).toBe(
"Le nom doit contenir au moins 2 lettres."
);
});

it("should not allow name with only special characters", async () => {
const user = {
email: "[email protected]",
name: ".-",
phone: "06 00 00 00 00"
};
const { errors } = await mutate<Pick<Mutation, "signup">>(SIGNUP, {
variables: {
userInfos: {
email: user.email,
password: viablePassword,
name: user.name,
phone: user.phone
}
}
});
expect(errors).not.toBeUndefined();
expect(errors?.[0].message).toBe(
"Le nom doit contenir au moins 2 lettres."
);
});
let mutate: ReturnType<typeof makeClient>["mutate"];
beforeAll(() => {
const testClient = makeClient();
Expand Down Expand Up @@ -91,7 +134,9 @@ describe("Mutation.signup", () => {

// Then
expect(errors).not.toBeUndefined();
expect(errors?.[0].message).toBe("Le champ ne peut pas être vide.");
expect(errors?.[0].message).toBe(
"Le nom doit contenir au moins 2 lettres."
);
});

it("should return the same result if email already exist", async () => {
Expand Down
12 changes: 10 additions & 2 deletions back/src/users/resolvers/mutations/editProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ export async function editProfileFn(
const editProfileSchema = yup.object({
name: yup
.string()
.test("empty", "The name cannot be an empty string", name =>
isDefined(name) ? !!name && name.trim().length > 0 : true
.test(
"at-least-2-letters",
"Le nom doit contenir au moins 2 lettres.",
name => {
if (!isDefined(name)) return true;
if (!name) return false;
const trimmed = name.trim();
const letterCount = (trimmed.match(/[\p{L}]/gu) || []).length;
return letterCount >= 2;
}
)
.isSafeSSTI(),
phone: yup.string(),
Expand Down
11 changes: 8 additions & 3 deletions back/src/users/resolvers/mutations/joinWithInvite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ const validationSchema = yup.object({
.required("Le nom est un champ requis")
.isSafeSSTI()
.test(
"not-empty-or-spaces",
"Le champ ne peut pas être vide.",
value => !!value && value.trim().length > 0
"at-least-2-letters",
"Le nom doit contenir au moins 2 lettres.",
value => {
if (!value) return false;
const trimmed = value.trim();
const letterCount = (trimmed.match(/[\p{L}]/gu) || []).length;
return letterCount >= 2;
}
),
password: yup
.string()
Expand Down
12 changes: 9 additions & 3 deletions back/src/users/resolvers/mutations/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ function validateArgs(args: MutationSignupArgs) {
.isSafeSSTI()
.required("Vous devez saisir nom et prénom.")
.test(
"not-empty-or-spaces",
"Le champ ne peut pas être vide.",
value => !!value && value.trim().length > 0
"at-least-2-letters",
"Le nom doit contenir au moins 2 lettres.",
value => {
if (!value) return false;
const trimmed = value.trim();
// Count letters (unicode)
const letterCount = (trimmed.match(/[\p{L}]/gu) || []).length;
return letterCount >= 2;
}
),
email: yup
.string()
Expand Down