events and half log tests

This commit is contained in:
joseji 2022-09-22 11:44:30 +02:00
parent c4b4661a5c
commit c08aac58a9
2 changed files with 39 additions and 3 deletions

View File

@ -16,6 +16,9 @@ import { userFactory } from '@/seeds/factory/user'
import { creationFactory } from '@/seeds/factory/creation'
import { creations } from '@/seeds/creation/index'
import { peterLustig } from '@/seeds/users/peter-lustig'
import { EventProtocol } from '@entity/EventProtocol'
import { EventProtocolType } from '@/event/EventProtocolType'
import { logger } from '@test/testSetup'
let mutate: any, query: any, con: any
let testEnv: any
@ -35,6 +38,8 @@ afterAll(async () => {
})
describe('ContributionResolver', () => {
let bibi: any
describe('createContribution', () => {
describe('unauthenticated', () => {
it('returns an error', async () => {
@ -54,7 +59,7 @@ describe('ContributionResolver', () => {
describe('authenticated with valid user', () => {
beforeAll(async () => {
await userFactory(testEnv, bibiBloxberg)
await query({
bibi = await query({
query: login,
variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' },
})
@ -84,6 +89,10 @@ describe('ContributionResolver', () => {
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith(`memo text is too short: memo.length=4 < (5)`)
})
it('throws error when memo length greater than 255 chars', async () => {
const date = new Date()
await expect(
@ -102,6 +111,10 @@ describe('ContributionResolver', () => {
)
})
it('logs the error found', () => {
expect(logger.error).toBeCalledWith(`memo text is too long: memo.length=259 > (255)`)
})
it('throws error when creationDate not-valid', async () => {
await expect(
mutate({
@ -165,6 +178,15 @@ describe('ContributionResolver', () => {
}),
)
})
it('stores the create contribution event in the database', async () => {
await expect(EventProtocol.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventProtocolType.CONTRIBUTION_CREATE,
userId: bibi.data.login.id,
}),
)
})
})
})
})

View File

@ -13,6 +13,8 @@ import { Contribution, ContributionListResult } from '@model/Contribution'
import { UnconfirmedContribution } from '@model/UnconfirmedContribution'
import { validateContribution, getUserCreation, updateCreations } from './util/creations'
import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const'
import { Event, EventConfirmationEmail, EventContributionCreate } from '@/event/Event'
import { eventProtocol } from '@/event/EventProtocolEmitter'
@Resolver()
export class ContributionResolver {
@ -23,15 +25,17 @@ export class ContributionResolver {
@Ctx() context: Context,
): Promise<UnconfirmedContribution> {
if (memo.length > MEMO_MAX_CHARS) {
logger.error(`memo text is too long: memo.length=${memo.length} > (${MEMO_MAX_CHARS}`)
logger.error(`memo text is too long: memo.length=${memo.length} > (${MEMO_MAX_CHARS})`)
throw new Error(`memo text is too long (${MEMO_MAX_CHARS} characters maximum)`)
}
if (memo.length < MEMO_MIN_CHARS) {
logger.error(`memo text is too short: memo.length=${memo.length} < (${MEMO_MIN_CHARS}`)
logger.error(`memo text is too short: memo.length=${memo.length} < (${MEMO_MIN_CHARS})`)
throw new Error(`memo text is too short (${MEMO_MIN_CHARS} characters minimum)`)
}
const event = new Event()
const user = getUser(context)
const creations = await getUserCreation(user.id)
logger.trace('creations', creations)
@ -49,6 +53,11 @@ export class ContributionResolver {
logger.trace('contribution to save', contribution)
await dbContribution.save(contribution)
const eventCreateContribution = new EventContributionCreate()
eventCreateContribution.userId = user.id
await eventProtocol.writeEvent(event.setEventContributionCreate(eventCreateContribution))
return new UnconfirmedContribution(contribution, user, creations)
}
@ -61,12 +70,15 @@ export class ContributionResolver {
const user = getUser(context)
const contribution = await dbContribution.findOne(id)
if (!contribution) {
logger.error('Contribution not found for given id')
throw new Error('Contribution not found for given id.')
}
if (contribution.userId !== user.id) {
logger.error('Can not delete contribution of another user')
throw new Error('Can not delete contribution of another user')
}
if (contribution.confirmedAt) {
logger.error('A confirmed contribution can not be deleted')
throw new Error('A confirmed contribution can not be deleted')
}
contribution.contributionStatus = ContributionStatus.DELETED
@ -154,9 +166,11 @@ export class ContributionResolver {
where: { id: contributionId, confirmedAt: IsNull() },
})
if (!contributionToUpdate) {
logger.error('No contribution found for given id')
throw new Error('No contribution found to given id.')
}
if (contributionToUpdate.userId !== user.id) {
logger.error('user of the pending contribution and send user does not correspond')
throw new Error('user of the pending contribution and send user does not correspond')
}