test deletePendingCreation and confirmPendingCreation mutations

This commit is contained in:
Moriz Wahl 2022-04-27 17:52:00 +02:00
parent 6f0c3c11a5
commit 76b1763bc0
2 changed files with 201 additions and 5 deletions

View File

@ -14,6 +14,8 @@ import {
createPendingCreation,
createPendingCreations,
updatePendingCreation,
deletePendingCreation,
confirmPendingCreation,
} from '@/seeds/graphql/mutations'
import { getPendingCreations, login } from '@/seeds/graphql/queries'
import { GraphQLError } from 'graphql'
@ -22,6 +24,7 @@ import { User } from '@entity/User'
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
import Decimal from 'decimal.js-light'
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
import { Transaction as DbTransaction } from '@entity/Transaction'
// mock account activation email to avoid console spam
jest.mock('@/mailer/sendAccountActivationEmail', () => {
@ -326,6 +329,40 @@ describe('AdminResolver', () => {
)
})
})
describe('deletePendingCreation', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
variables: {
id: 1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
)
})
})
describe('confirmPendingCreation', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
variables: {
id: 1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
)
})
})
})
describe('authenticated', () => {
@ -402,6 +439,40 @@ describe('AdminResolver', () => {
)
})
})
describe('deletePendingCreation', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
variables: {
id: 1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
)
})
})
describe('confirmPendingCreation', () => {
it('returns an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
variables: {
id: 1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('401 Unauthorized')],
}),
)
})
})
})
describe('with admin rights', () => {
@ -857,6 +928,126 @@ describe('AdminResolver', () => {
)
})
})
describe('deletePendingCreation', () => {
describe('creation id does not exist', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
variables: {
id: -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Creation not found to given id.')],
}),
)
})
})
describe('creation id does exist', () => {
it('returns true', async () => {
await expect(
mutate({
mutation: deletePendingCreation,
variables: {
id: creation ? creation.id : -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
data: { deletePendingCreation: true },
}),
)
})
})
})
describe('confirmPendingCreation', () => {
describe('creation does not exits', () => {
it('throws an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
variables: {
id: -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Creation not found to given id.')],
}),
)
})
})
describe('confirm own creation', () => {
beforeAll(async () => {
const now = new Date()
creation = await creationFactory(testEnv, {
email: 'peter@lustig.de',
amount: 400,
memo: 'Herzlich Willkommen bei Gradido!',
creationDate: new Date(now.getFullYear(), now.getMonth() - 1, 1).toISOString(),
})
})
it('thows an error', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
variables: {
id: creation ? creation.id : -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('Moderator can not confirm own pending creation')],
}),
)
})
})
describe('creation of other user', () => {
beforeAll(async () => {
const now = new Date()
creation = await creationFactory(testEnv, {
email: 'bibi@bloxberg.de',
amount: 450,
memo: 'Herzlich Willkommen bei Gradido liebe Bibi!',
creationDate: new Date(now.getFullYear(), now.getMonth() - 2, 1).toISOString(),
})
})
it('returns true', async () => {
await expect(
mutate({
mutation: confirmPendingCreation,
variables: {
id: creation ? creation.id : -1,
},
}),
).resolves.toEqual(
expect.objectContaining({
data: { confirmPendingCreation: true },
}),
)
})
it('creates a transaction', async () => {
const transaction = await DbTransaction.find()
expect(transaction[0].amount.toString()).toBe('450')
expect(transaction[0].memo).toBe('Herzlich Willkommen bei Gradido liebe Bibi!')
expect(transaction[0].linkedTransactionId).toEqual(null)
expect(transaction[0].transactionLinkId).toEqual(null)
expect(transaction[0].previous).toEqual(null)
expect(transaction[0].linkedUserId).toEqual(null)
expect(transaction[0].typeId).toEqual(1)
})
})
})
})
})
})

View File

@ -307,8 +307,11 @@ export class AdminResolver {
@Authorized([RIGHTS.DELETE_PENDING_CREATION])
@Mutation(() => Boolean)
async deletePendingCreation(@Arg('id', () => Int) id: number): Promise<boolean> {
const entity = await AdminPendingCreation.findOneOrFail(id)
const res = await AdminPendingCreation.delete(entity)
const pendingCreation = await AdminPendingCreation.findOne(id)
if (!pendingCreation) {
throw new Error('Creation not found to given id.')
}
const res = await AdminPendingCreation.delete(pendingCreation)
return !!res
}
@ -318,7 +321,10 @@ export class AdminResolver {
@Arg('id', () => Int) id: number,
@Ctx() context: Context,
): Promise<boolean> {
const pendingCreation = await AdminPendingCreation.findOneOrFail(id)
const pendingCreation = await AdminPendingCreation.findOne(id)
if (!pendingCreation) {
throw new Error('Creation not found to given id.')
}
const moderatorUser = getUser(context)
if (moderatorUser.id === pendingCreation.userId)
throw new Error('Moderator can not confirm own pending creation')
@ -349,8 +355,7 @@ export class AdminResolver {
transaction.memo = pendingCreation.memo
transaction.userId = pendingCreation.userId
transaction.previous = lastTransaction ? lastTransaction.id : null
// TODO pending creations decimal
transaction.amount = new Decimal(Number(pendingCreation.amount))
transaction.amount = pendingCreation.amount
transaction.creationDate = pendingCreation.date
transaction.balance = newBalance
transaction.balanceDate = receivedCallDate