added tests for events

This commit is contained in:
joseji 2022-09-28 11:57:49 +02:00
parent 33eeab344f
commit 0f1f9baa8d
2 changed files with 40 additions and 15 deletions

View File

@ -41,6 +41,8 @@ import { Contribution } from '@entity/Contribution'
import { Transaction as DbTransaction } from '@entity/Transaction'
import { ContributionLink as DbContributionLink } from '@entity/ContributionLink'
import { sendContributionConfirmedEmail } from '@/mailer/sendContributionConfirmedEmail'
import { EventProtocol } from '@entity/EventProtocol'
import { EventProtocolType } from '@/event/EventProtocolType'
// mock account activation email to avoid console spam
jest.mock('@/mailer/sendAccountActivationEmail', () => {
@ -1037,6 +1039,15 @@ describe('AdminResolver', () => {
}),
)
})
it('stores the create contribution event in the database', async () => {
await expect(EventProtocol.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventProtocolType.CONTRIBUTION_CREATE,
userId: admin.id,
}),
)
})
})
describe('second creation surpasses the available amount ', () => {
@ -1451,6 +1462,14 @@ describe('AdminResolver', () => {
)
})
it('stores the contribution confirm event in the database', async () => {
await expect(EventProtocol.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventProtocolType.CONTRIBUTION_CONFIRM,
}),
)
})
it('creates a transaction', async () => {
const transaction = await DbTransaction.find()
expect(transaction[0].amount.toString()).toBe('450')
@ -1475,6 +1494,14 @@ describe('AdminResolver', () => {
}),
)
})
it('stores the send confirmation email event in the database', async () => {
await expect(EventProtocol.find()).resolves.toContainEqual(
expect.objectContaining({
type: EventProtocolType.SEND_CONFIRMATION_EMAIL,
}),
)
})
})
describe('confirm two creations one after the other quickly', () => {

View File

@ -67,7 +67,12 @@ import { ContributionMessage } from '@model/ContributionMessage'
import { sendContributionConfirmedEmail } from '@/mailer/sendContributionConfirmedEmail'
import { sendAddedContributionMessageEmail } from '@/mailer/sendAddedContributionMessageEmail'
import { eventProtocol } from '@/event/EventProtocolEmitter'
import { Event, EventContributionConfirm, EventContributionCreate, EventContributionLinkDefine, EventSendConfirmationEmail } from '@/event/Event'
import {
Event,
EventContributionConfirm,
EventContributionCreate,
EventSendConfirmationEmail,
} from '@/event/Event'
// const EMAIL_OPT_IN_REGISTER = 1
// const EMAIL_OPT_UNKNOWN = 3 // elopage?
@ -514,13 +519,6 @@ export class AdminResolver {
contributionAmount: contribution.amount,
overviewURL: CONFIG.EMAIL_LINK_OVERVIEW,
})
const event = new Event()
const eventContributionConfirm = new EventContributionConfirm()
eventContributionConfirm.xUserId = user.id
eventContributionConfirm.amount = contribution.amount
eventContributionConfirm.contributionId = contribution.id
await eventProtocol.writeEvent(event.setEventContributionConfirm(eventContributionConfirm))
} catch (e) {
await queryRunner.rollbackTransaction()
logger.error(`Creation was not successful: ${e}`)
@ -528,6 +526,13 @@ export class AdminResolver {
} finally {
await queryRunner.release()
}
const event = new Event()
const eventContributionConfirm = new EventContributionConfirm()
eventContributionConfirm.userId = user.id
eventContributionConfirm.amount = contribution.amount
eventContributionConfirm.contributionId = contribution.id
await eventProtocol.writeEvent(event.setEventContributionConfirm(eventContributionConfirm))
return true
}
@ -693,13 +698,6 @@ export class AdminResolver {
dbContributionLink.maxAmountPerMonth = maxAmountPerMonth
dbContributionLink.maxPerCycle = maxPerCycle
await dbContributionLink.save()
const event = new Event()
const eventContributionLinkDefine = new EventContributionLinkDefine()
await eventProtocol.writeEvent(
event.setEventContributionLinkDefine(eventContributionLinkDefine),
)
logger.debug(`createContributionLink successful!`)
return new ContributionLink(dbContributionLink)
}