fix: unable to update profile with missing values (@fehmer) (#5859)

This commit is contained in:
Christian Fehmer 2024-09-07 13:05:44 +02:00 committed by GitHub
parent 6de9ad6e1c
commit 9c083f2397
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 49 additions and 5 deletions

View file

@ -2879,6 +2879,50 @@ describe("user controller test", () => {
}
);
});
it("should update with empty strings", async () => {
//GIVEN
const newProfile = {
bio: "",
keyboard: "",
socialProfiles: {
github: "",
twitter: "",
website: "",
},
};
//WHEN
const { body } = await mockApp
.patch("/users/profile")
.set("Authorization", `Uid ${uid}`)
.send({
...newProfile,
selectedBadgeId: -1,
})
.expect(200);
//THEN
expect(body).toEqual({
message: "Profile updated",
data: newProfile,
});
expect(updateProfileMock).toHaveBeenCalledWith(
uid,
{
bio: "",
keyboard: "",
socialProfiles: {
github: "",
twitter: "",
website: "",
},
},
{
badges: [{ id: 4 }, { id: 2 }, { id: 3 }],
}
);
});
it("should fail with unknown properties", async () => {
//WHEN
const { body } = await mockApp

View file

@ -109,8 +109,8 @@ function profileDetailsBase(
export const UserProfileDetailsSchema = z
.object({
bio: profileDetailsBase(z.string().max(250)),
keyboard: profileDetailsBase(z.string().max(75)),
bio: profileDetailsBase(z.string().max(250)).or(z.literal("")),
keyboard: profileDetailsBase(z.string().max(75)).or(z.literal("")),
socialProfiles: z
.object({
twitter: profileDetailsBase(
@ -118,16 +118,16 @@ export const UserProfileDetailsSchema = z
.string()
.max(20)
.regex(/^[0-9a-zA-Z_.-]+$/)
),
).or(z.literal("")),
github: profileDetailsBase(
z
.string()
.max(39)
.regex(/^[0-9a-zA-Z_.-]+$/)
),
).or(z.literal("")),
website: profileDetailsBase(
z.string().url().max(200).startsWith("https://")
),
).or(z.literal("")),
})
.strict()
.optional(),