Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prevent users from setting an already used username #2651

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
21 changes: 21 additions & 0 deletions graphql/resolvers/userDataCrud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('User Data CRUD resolvers', () => {
}

prismaMock.user.update.mockResolvedValueOnce(userMock)
prismaMock.user.findFirst.mockResolvedValueOnce(null)

expect(
updateUserNames(
Expand All @@ -27,4 +28,24 @@ describe('User Data CRUD resolvers', () => {
)
).resolves.toStrictEqual(userMock)
})

it('Should not update the user username and name if username already used', async () => {
expect.assertions(1)

const userMock = {
username: mockUsername,
name: mockName
}

prismaMock.user.update.mockResolvedValueOnce(userMock)
prismaMock.user.findFirst.mockResolvedValueOnce(userMock)

expect(
updateUserNames(
null,
{ username: mockUsername, mockName },
{ req: { user: { id: 1 } } }
)
).rejects.toEqual(Error('Username is already used'))
})
})
10 changes: 10 additions & 0 deletions graphql/resolvers/userDataCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ export const updateUserNames = withUserContainer<
>(async (_, args, { req }) => {
const { username, name } = args

const usernameAlreadyUsed = await prisma.user.findFirst({
where: {
username
}
})

if (usernameAlreadyUsed) {
throw new Error('Username is already used')
}

const user = await prisma.user.update({
where: {
id: req.user!.id
Expand Down
3 changes: 2 additions & 1 deletion pages/settings/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const BasicSettings = ({
error={error?.message || ''}
texts={{
loading: 'Updating your names...',
data: `Updated your names successfully!`
data: `Updated your names successfully!`,
error: error?.message
}}
dismiss={{
onDismissData: () => {},
Expand Down