feat(backend): alias in update user info

This commit is contained in:
Moriz Wahl 2023-02-21 17:14:09 +01:00
parent bf9dff7d59
commit 55ee3a0b6b
4 changed files with 148 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import { ArgsType, Field } from 'type-graphql'
// import { Length } from 'class-validator'
@ArgsType()
export default class UpdateUserInfosArgs {
@ -8,6 +9,10 @@ export default class UpdateUserInfosArgs {
@Field({ nullable: true })
lastName?: string
@Field({ nullable: true })
// @Length(5, 20)
alias?: string
@Field({ nullable: true })
language?: string

View File

@ -1134,6 +1134,129 @@ describe('UserResolver', () => {
})
})
describe('alias', () => {
beforeEach(() => {
jest.clearAllMocks()
})
describe('too short', () => {
it('throws and logs an error', async () => {
await expect(
mutate({
mutation: updateUserInfos,
variables: {
alias: 'bibi',
},
}),
).resolves.toMatchObject({
errors: [new GraphQLError('Given alias is too short')],
data: null,
})
expect(logger.error).toBeCalledWith('Given alias is too short', 'bibi')
})
})
describe('too long', () => {
it('throws and logs an error', async () => {
await expect(
mutate({
mutation: updateUserInfos,
variables: {
alias: 'bibis_alias_far_too_long',
},
}),
).resolves.toMatchObject({
errors: [new GraphQLError('Given alias is too long')],
data: null,
})
expect(logger.error).toBeCalledWith(
'Given alias is too long',
'bibis_alias_far_too_long',
)
})
})
describe('invalid characters', () => {
it('throws and logs an error', async () => {
await expect(
mutate({
mutation: updateUserInfos,
variables: {
alias: 'no_underscore',
},
}),
).resolves.toMatchObject({
errors: [new GraphQLError('Invalid characters in alias')],
data: null,
})
expect(logger.error).toBeCalledWith('Invalid characters in alias', 'no_underscore')
})
})
describe('alias exists', () => {
let peter: User
beforeAll(async () => {
peter = await userFactory(testEnv, peterLustig)
await mutate({
mutation: login,
variables: {
email: 'peter@lustig.de',
password: 'Aa12345_',
},
})
await mutate({
mutation: updateUserInfos,
variables: {
alias: 'bibiBloxberg',
},
})
await mutate({
mutation: login,
variables: {
email: 'bibi@bloxberg.de',
password: 'Aa12345_',
},
})
})
afterAll(async () => {
const [user] = await User.find({ id: peter.id })
await user.remove()
})
it('throws and logs an error', async () => {
await expect(
mutate({
mutation: updateUserInfos,
variables: {
alias: 'bibiBloxberg',
},
}),
).resolves.toMatchObject({
errors: [new GraphQLError('Alias already in use')],
data: null,
})
expect(logger.error).toBeCalledWith('Alias already in use', 'bibiBloxberg')
})
})
describe('valid alias', () => {
it('updates the user in DB', async () => {
await mutate({
mutation: updateUserInfos,
variables: {
alias: 'bibiBloxberg',
},
})
await expect(User.findOne()).resolves.toEqual(
expect.objectContaining({
alias: 'bibiBloxberg',
}),
)
})
})
})
describe('language is not valid', () => {
it('throws an error', async () => {
jest.clearAllMocks()

View File

@ -517,6 +517,7 @@ export class UserResolver {
{
firstName,
lastName,
alias,
language,
password,
passwordNew,
@ -536,6 +537,23 @@ export class UserResolver {
userEntity.lastName = lastName
}
if (alias) {
if (alias.length < 5) {
throw new LogError('Given alias is too short', alias)
}
if (alias.length > 20) {
throw new LogError('Given alias is too long', alias)
}
if (!alias.match(/^[0-9A-Za-z]+$/)) {
throw new LogError('Invalid characters in alias', alias)
}
const aliasInUse = await DbUser.find({ alias })
if (aliasInUse.length !== 0) {
throw new LogError('Alias already in use', alias)
}
userEntity.alias = alias
}
if (language) {
if (!isLanguage(language)) {
throw new LogError('Given language is not a valid language', language)

View File

@ -28,6 +28,7 @@ export const updateUserInfos = gql`
mutation (
$firstName: String
$lastName: String
$alias: String
$password: String
$passwordNew: String
$locale: String
@ -37,6 +38,7 @@ export const updateUserInfos = gql`
updateUserInfos(
firstName: $firstName
lastName: $lastName
alias: $alias
password: $password
passwordNew: $passwordNew
language: $locale