Events and logs completed in User Resolver

This commit is contained in:
joseji 2022-09-15 11:42:03 +02:00
parent 4b226813aa
commit ded649f342
2 changed files with 64 additions and 9 deletions

View File

@ -19,6 +19,8 @@ import { contributionLinkFactory } from '@/seeds/factory/contributionLink'
import { ContributionLink } from '@model/ContributionLink'
// import { TransactionLink } from '@entity/TransactionLink'
import { EventProtocolType } from '@/event/EventProtocolType'
import { EventProtocol } from '@entity/EventProtocol'
import { logger } from '@test/testSetup'
import { validate as validateUUID, version as versionUUID } from 'uuid'
import { peterLustig } from '@/seeds/users/peter-lustig'
@ -169,6 +171,14 @@ describe('UserResolver', () => {
duration: expect.any(String),
})
})
it('stores the send confirmation event in the database', () => {
expect(EventProtocol.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventProtocolType.SEND_CONFIRMATION_EMAIL,
}),
)
})
})
describe('email already exists', () => {
@ -383,6 +393,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith('Password entered is lexically invalid')
})
})
describe('no valid optin code', () => {
@ -405,6 +419,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith('Could not login with emailVerificationCode')
})
})
})
@ -433,6 +451,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith('User with email=bibi@bloxberg.de does not exist')
})
})
describe('user is in database and correct login data', () => {
@ -475,6 +497,7 @@ bei Gradidio sei dabei!`,
describe('user is in database and wrong password', () => {
beforeAll(async () => {
await userFactory(testEnv, bibiBloxberg)
result = await query({ query: login, variables: { ...variables, password: 'wrong' } })
})
afterAll(async () => {
@ -482,14 +505,16 @@ bei Gradidio sei dabei!`,
})
it('returns an error', () => {
expect(
query({ query: login, variables: { ...variables, password: 'wrong' } }),
).resolves.toEqual(
expect(result).toEqual(
expect.objectContaining({
errors: [new GraphQLError('No user with this credentials')],
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith('The User has no valid credentials.')
})
})
})
@ -595,6 +620,14 @@ bei Gradidio sei dabei!`,
}),
)
})
it('stores the login event in the database', () => {
expect(EventProtocol.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventProtocolType.LOGIN,
}),
)
})
})
})
})
@ -649,13 +682,17 @@ bei Gradidio sei dabei!`,
})
describe('request reset password again', () => {
it('thows an error', async () => {
it('throws an error', async () => {
await expect(mutate({ mutation: forgotPassword, variables })).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('email already sent less than 10 minutes minutes ago')],
}),
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith(`email already sent less than 10 minutes minutes ago`)
})
})
})
})
@ -766,7 +803,7 @@ bei Gradidio sei dabei!`,
})
describe('language is not valid', () => {
it('thows an error', async () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: updateUserInfos,
@ -780,6 +817,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith(`"not-valid" isn't a valid language`)
})
})
describe('password', () => {
@ -799,6 +840,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith(`Old password is invalid`)
})
})
describe('invalid new password', () => {
@ -821,6 +866,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith('newPassword does not fullfil the rules')
})
})
describe('correct old and new password', () => {
@ -840,7 +889,7 @@ bei Gradidio sei dabei!`,
)
})
it('can login wtih new password', async () => {
it('can login with new password', async () => {
await expect(
query({
query: login,
@ -860,7 +909,7 @@ bei Gradidio sei dabei!`,
)
})
it('cannot login wtih old password', async () => {
it('cannot login with old password', async () => {
await expect(
query({
query: login,
@ -875,6 +924,10 @@ bei Gradidio sei dabei!`,
}),
)
})
it('logs the error thrown', () => {
expect(logger.error).toBeCalledWith('The User has no valid credentials.')
})
})
})
})

View File

@ -273,7 +273,7 @@ export class UserResolver {
logger.info(`login with ${email}, ***, ${publisherId} ...`)
email = email.trim().toLowerCase()
const dbUser = await DbUser.findOneOrFail({ email }, { withDeleted: true }).catch(() => {
logger.error(`User with email=${email} does not exists`)
logger.error(`User with email=${email} does not exist`)
throw new Error('No user with this credentials')
})
if (dbUser.deletedAt) {
@ -389,7 +389,7 @@ export class UserResolver {
/* uncomment this, when you need the activation link on the console */
// In case EMails are disabled log the activation link for the user
if (!emailSent) {
logger.debug(`Email not send!`)
logger.debug(`Email not sent!`)
}
logger.info('createUser() faked and send multi registration mail...')
@ -548,6 +548,7 @@ export class UserResolver {
logger.info(`setPassword(${code}, ***)...`)
// Validate Password
if (!isPassword(password)) {
logger.error('Password entered is lexically invalid')
throw new Error(
'Please enter a valid password with at least 8 characters, upper and lower case letters, at least one number and one special character!',
)
@ -727,6 +728,7 @@ export class UserResolver {
try {
await queryRunner.manager.save(userEntity).catch((error) => {
logger.error('error saving user: ' + error)
throw new Error('error saving user: ' + error)
})