mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
test deletePendingCreation and confirmPendingCreation mutations
This commit is contained in:
parent
6f0c3c11a5
commit
76b1763bc0
@ -14,6 +14,8 @@ import {
|
|||||||
createPendingCreation,
|
createPendingCreation,
|
||||||
createPendingCreations,
|
createPendingCreations,
|
||||||
updatePendingCreation,
|
updatePendingCreation,
|
||||||
|
deletePendingCreation,
|
||||||
|
confirmPendingCreation,
|
||||||
} from '@/seeds/graphql/mutations'
|
} from '@/seeds/graphql/mutations'
|
||||||
import { getPendingCreations, login } from '@/seeds/graphql/queries'
|
import { getPendingCreations, login } from '@/seeds/graphql/queries'
|
||||||
import { GraphQLError } from 'graphql'
|
import { GraphQLError } from 'graphql'
|
||||||
@ -22,6 +24,7 @@ import { User } from '@entity/User'
|
|||||||
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
|
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
|
||||||
import Decimal from 'decimal.js-light'
|
import Decimal from 'decimal.js-light'
|
||||||
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
|
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
|
||||||
|
import { Transaction as DbTransaction } from '@entity/Transaction'
|
||||||
|
|
||||||
// mock account activation email to avoid console spam
|
// mock account activation email to avoid console spam
|
||||||
jest.mock('@/mailer/sendAccountActivationEmail', () => {
|
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', () => {
|
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', () => {
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@ -307,8 +307,11 @@ export class AdminResolver {
|
|||||||
@Authorized([RIGHTS.DELETE_PENDING_CREATION])
|
@Authorized([RIGHTS.DELETE_PENDING_CREATION])
|
||||||
@Mutation(() => Boolean)
|
@Mutation(() => Boolean)
|
||||||
async deletePendingCreation(@Arg('id', () => Int) id: number): Promise<boolean> {
|
async deletePendingCreation(@Arg('id', () => Int) id: number): Promise<boolean> {
|
||||||
const entity = await AdminPendingCreation.findOneOrFail(id)
|
const pendingCreation = await AdminPendingCreation.findOne(id)
|
||||||
const res = await AdminPendingCreation.delete(entity)
|
if (!pendingCreation) {
|
||||||
|
throw new Error('Creation not found to given id.')
|
||||||
|
}
|
||||||
|
const res = await AdminPendingCreation.delete(pendingCreation)
|
||||||
return !!res
|
return !!res
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -318,7 +321,10 @@ export class AdminResolver {
|
|||||||
@Arg('id', () => Int) id: number,
|
@Arg('id', () => Int) id: number,
|
||||||
@Ctx() context: Context,
|
@Ctx() context: Context,
|
||||||
): Promise<boolean> {
|
): 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)
|
const moderatorUser = getUser(context)
|
||||||
if (moderatorUser.id === pendingCreation.userId)
|
if (moderatorUser.id === pendingCreation.userId)
|
||||||
throw new Error('Moderator can not confirm own pending creation')
|
throw new Error('Moderator can not confirm own pending creation')
|
||||||
@ -349,8 +355,7 @@ export class AdminResolver {
|
|||||||
transaction.memo = pendingCreation.memo
|
transaction.memo = pendingCreation.memo
|
||||||
transaction.userId = pendingCreation.userId
|
transaction.userId = pendingCreation.userId
|
||||||
transaction.previous = lastTransaction ? lastTransaction.id : null
|
transaction.previous = lastTransaction ? lastTransaction.id : null
|
||||||
// TODO pending creations decimal
|
transaction.amount = pendingCreation.amount
|
||||||
transaction.amount = new Decimal(Number(pendingCreation.amount))
|
|
||||||
transaction.creationDate = pendingCreation.date
|
transaction.creationDate = pendingCreation.date
|
||||||
transaction.balance = newBalance
|
transaction.balance = newBalance
|
||||||
transaction.balanceDate = receivedCallDate
|
transaction.balanceDate = receivedCallDate
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user