From 180e4ef27dc3477fa624b679cddaa4060153bf40 Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 25 Jul 2022 11:17:10 +0200 Subject: [PATCH 1/4] Add test to createContribution that error is thrown when memo length smaller than 5 and greater than 255 --- .../resolver/ContributionResolver.test.ts | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/backend/src/graphql/resolver/ContributionResolver.test.ts b/backend/src/graphql/resolver/ContributionResolver.test.ts index b584624c2..1aa468ed7 100644 --- a/backend/src/graphql/resolver/ContributionResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionResolver.test.ts @@ -66,6 +66,42 @@ describe('ContributionResolver', () => { }) describe('input not valid', () => { + it('throws error when memo length smaller than 5 chars', async () => { + const date = new Date() + await expect( + mutate({ + mutation: createContribution, + variables: { + amount: 100.0, + memo: 'Test', + creationDate: date.toString(), + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('memo text is too short (5 characters minimum)')], + }), + ) + }) + + it('throws error when memo length greater than 255 chars', async () => { + const date = new Date() + await expect( + mutate({ + mutation: createContribution, + variables: { + amount: 100.0, + memo: 'Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test', + creationDate: date.toString(), + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('memo text is too long (255 characters maximum)')], + }), + ) + }) + it('throws error when creationDate not-valid', async () => { await expect( mutate({ From bcb0882a06476f9c89799ccaebbdbc1cdc06fe14 Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 25 Jul 2022 11:23:28 +0200 Subject: [PATCH 2/4] Add test to updateContribution check if error is thrown when memo length is smaller than 5 or greater than 255 characters. --- .../resolver/ContributionResolver.test.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/backend/src/graphql/resolver/ContributionResolver.test.ts b/backend/src/graphql/resolver/ContributionResolver.test.ts index 1aa468ed7..20f11ff9a 100644 --- a/backend/src/graphql/resolver/ContributionResolver.test.ts +++ b/backend/src/graphql/resolver/ContributionResolver.test.ts @@ -349,6 +349,48 @@ describe('ContributionResolver', () => { }) }) + describe('Memo length smaller than 5 chars', () => { + it('throws error', async () => { + const date = new Date() + await expect( + mutate({ + mutation: updateContribution, + variables: { + contributionId: result.data.createContribution.id, + amount: 100.0, + memo: 'Test', + creationDate: date.toString(), + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('memo text is too short (5 characters minimum)')], + }), + ) + }) + }) + + describe('Memo length greater than 255 chars', () => { + it('throws error', async () => { + const date = new Date() + await expect( + mutate({ + mutation: updateContribution, + variables: { + contributionId: result.data.createContribution.id, + amount: 100.0, + memo: 'Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test', + creationDate: date.toString(), + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('memo text is too long (255 characters maximum)')], + }), + ) + }) + }) + describe('wrong user tries to update the contribution', () => { beforeAll(async () => { await query({ From 6f5945f99173a1ca43c70f68c2769bfac7fd1dbc Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 25 Jul 2022 11:24:05 +0200 Subject: [PATCH 3/4] createContribution & updateContribution checks if memo length is correct. --- .../graphql/resolver/ContributionResolver.ts | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index ef4467a71..c6b129b5f 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -12,6 +12,9 @@ import { UnconfirmedContribution } from '@model/UnconfirmedContribution' import { User } from '@model/User' import { validateContribution, getUserCreation, updateCreations } from './util/creations' +const MEMO_MAX_CHARS = 255 +const MEMO_MIN_CHARS = 5 + @Resolver() export class ContributionResolver { @Authorized([RIGHTS.CREATE_CONTRIBUTION]) @@ -20,6 +23,16 @@ export class ContributionResolver { @Args() { amount, memo, creationDate }: ContributionArgs, @Ctx() context: Context, ): Promise { + if (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}`) + throw new Error(`memo text is too short (${MEMO_MIN_CHARS} characters minimum)`) + } + const user = getUser(context) const creations = await getUserCreation(user.id) logger.trace('creations', creations) @@ -119,6 +132,16 @@ export class ContributionResolver { @Args() { amount, memo, creationDate }: ContributionArgs, @Ctx() context: Context, ): Promise { + if (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}`) + throw new Error(`memo text is too short (${MEMO_MIN_CHARS} characters minimum)`) + } + const user = getUser(context) const contributionToUpdate = await dbContribution.findOne({ From 8aa6bc540ec5ce1d8a9134967ed1357e3d247dd9 Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 25 Jul 2022 12:41:11 +0200 Subject: [PATCH 4/4] Change CONTRIBUTIONLINK_MEMO_..._CHARS to MEMO_..._CHARS and using it on ContributionResolver, TransactionResolver and AdminResolver. --- backend/src/graphql/resolver/AdminResolver.ts | 11 ++++------- backend/src/graphql/resolver/ContributionResolver.ts | 4 +--- backend/src/graphql/resolver/TransactionResolver.ts | 4 +--- backend/src/graphql/resolver/const/const.ts | 4 ++-- 4 files changed, 8 insertions(+), 15 deletions(-) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 12cad529c..84ae09cf8 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -54,11 +54,11 @@ import { updateCreations, } from './util/creations' import { - CONTRIBUTIONLINK_MEMO_MAX_CHARS, - CONTRIBUTIONLINK_MEMO_MIN_CHARS, CONTRIBUTIONLINK_NAME_MAX_CHARS, CONTRIBUTIONLINK_NAME_MIN_CHARS, FULL_CREATION_AVAILABLE, + MEMO_MAX_CHARS, + MEMO_MIN_CHARS, } from './const/const' // const EMAIL_OPT_IN_REGISTER = 1 @@ -595,11 +595,8 @@ export class AdminResolver { logger.error(`The memo must be initialized!`) throw new Error(`The memo must be initialized!`) } - if ( - memo.length < CONTRIBUTIONLINK_MEMO_MIN_CHARS || - memo.length > CONTRIBUTIONLINK_MEMO_MAX_CHARS - ) { - const msg = `The value of 'memo' with a length of ${memo.length} did not fulfill the requested bounderies min=${CONTRIBUTIONLINK_MEMO_MIN_CHARS} and max=${CONTRIBUTIONLINK_MEMO_MAX_CHARS}` + if (memo.length < MEMO_MIN_CHARS || memo.length > MEMO_MAX_CHARS) { + const msg = `The value of 'memo' with a length of ${memo.length} did not fulfill the requested bounderies min=${MEMO_MIN_CHARS} and max=${MEMO_MAX_CHARS}` logger.error(`${msg}`) throw new Error(`${msg}`) } diff --git a/backend/src/graphql/resolver/ContributionResolver.ts b/backend/src/graphql/resolver/ContributionResolver.ts index c6b129b5f..a22715fb4 100644 --- a/backend/src/graphql/resolver/ContributionResolver.ts +++ b/backend/src/graphql/resolver/ContributionResolver.ts @@ -11,9 +11,7 @@ import { Contribution, ContributionListResult } from '@model/Contribution' import { UnconfirmedContribution } from '@model/UnconfirmedContribution' import { User } from '@model/User' import { validateContribution, getUserCreation, updateCreations } from './util/creations' - -const MEMO_MAX_CHARS = 255 -const MEMO_MIN_CHARS = 5 +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' @Resolver() export class ContributionResolver { diff --git a/backend/src/graphql/resolver/TransactionResolver.ts b/backend/src/graphql/resolver/TransactionResolver.ts index 023e5b2ff..bc062a1f4 100644 --- a/backend/src/graphql/resolver/TransactionResolver.ts +++ b/backend/src/graphql/resolver/TransactionResolver.ts @@ -34,9 +34,7 @@ import { virtualLinkTransaction, virtualDecayTransaction } from '@/util/virtualT import Decimal from 'decimal.js-light' import { BalanceResolver } from './BalanceResolver' - -const MEMO_MAX_CHARS = 255 -const MEMO_MIN_CHARS = 5 +import { MEMO_MAX_CHARS, MEMO_MIN_CHARS } from './const/const' export const executeTransaction = async ( amount: Decimal, diff --git a/backend/src/graphql/resolver/const/const.ts b/backend/src/graphql/resolver/const/const.ts index d5ba08784..e4eb9a13b 100644 --- a/backend/src/graphql/resolver/const/const.ts +++ b/backend/src/graphql/resolver/const/const.ts @@ -8,5 +8,5 @@ export const FULL_CREATION_AVAILABLE = [ ] export const CONTRIBUTIONLINK_NAME_MAX_CHARS = 100 export const CONTRIBUTIONLINK_NAME_MIN_CHARS = 5 -export const CONTRIBUTIONLINK_MEMO_MAX_CHARS = 255 -export const CONTRIBUTIONLINK_MEMO_MIN_CHARS = 5 +export const MEMO_MAX_CHARS = 255 +export const MEMO_MIN_CHARS = 5