From ec87bea83658e2f269455f9602bad46f84c1823a Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Tue, 7 Mar 2023 14:31:24 +0100 Subject: [PATCH] feat(backend): test transaction links --- .../resolver/TransactionLinkResolver.test.ts | 30 ++++++++++++++++++- .../resolver/TransactionLinkResolver.ts | 12 ++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts index 60b4551be..b4e49cfe0 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts @@ -306,7 +306,6 @@ describe('TransactionLinkResolver', () => { }) }) - // TODO: have this test separated into a transactionLink and a contributionLink part describe('redeem daily Contribution Link', () => { const now = new Date() let contributionLink: DbContributionLink | undefined @@ -508,6 +507,35 @@ describe('TransactionLinkResolver', () => { }) }) }) + + describe('transaction link', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + describe('link does not exits', () => { + beforeAll(async () => { + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + }) + + it('throws and logs the error', async () => { + await expect( + mutate({ + mutation: redeemTransactionLink, + variables: { + code: 'not-valid', + }, + }), + ).resolves.toMatchObject({ + errors: [new GraphQLError('Transaction link not found')], + }) + expect(logger.error).toBeCalledWith('Transaction link not found', 'not-valid') + }) + }) + }) }) }) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index ab5b52bad..ef55475db 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -285,12 +285,20 @@ export class TransactionLinkResolver { return true } else { const now = new Date() - const transactionLink = await DbTransactionLink.findOneOrFail({ code }) - const linkedUser = await DbUser.findOneOrFail( + const transactionLink = await DbTransactionLink.findOne({ code }) + if (!transactionLink) { + throw new LogError('Transaction link not found', code) + } + + const linkedUser = await DbUser.findOne( { id: transactionLink.userId }, { relations: ['emailContact'] }, ) + if (!linkedUser) { + throw new LogError('Linked user not found for given link', transactionLink.userId) + } + if (user.id === linkedUser.id) { throw new LogError('Cannot redeem own transaction link', user.id) }