Skip to content

Commit

Permalink
Merge pull request #2651 from flacial/2650-prevent-users-from-setting…
Browse files Browse the repository at this point in the history
…-an-already-used-username

fix: Prevent users from setting an already used username
  • Loading branch information
flacial authored Dec 27, 2022
2 parents 9065dce + 295ef64 commit 58432a1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
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

1 comment on commit 58432a1

@vercel
Copy link

@vercel vercel bot commented on 58432a1 Dec 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.