From 5f8702c3c4c7010bc3d94ab7642b219cd9d2743c Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Mon, 27 Feb 2023 15:44:14 +0100 Subject: [PATCH] catch error for not existing users and test this --- .../resolver/TransactionLinkResolver.test.ts | 20 +++++++++++++++++++ .../resolver/TransactionLinkResolver.ts | 6 +++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts index 2bbca26d3..60b4551be 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts @@ -600,6 +600,26 @@ describe('TransactionLinkResolver', () => { resetToken() }) + describe('', () => { + it('throws error when user does not exists', async () => { + jest.clearAllMocks() + await expect( + mutate({ + mutation: listTransactionLinksAdmin, + variables: { + userId: -1, + }, + }), + ).resolves.toMatchObject({ + errors: [new GraphQLError('Could not find requested User')], + }) + }) + + it('logs the error thrown', () => { + expect(logger.error).toBeCalledWith('Could not find requested User', -1) + }) + }) + describe('without any filters', () => { it('finds 6 open transaction links and no deleted or redeemed', async () => { await expect( diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index 2c8450bd6..ab5b52bad 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -346,6 +346,10 @@ export class TransactionLinkResolver { @Arg('userId', () => Int) userId: number, ): Promise { - return transactionLinkList(paginated, filters, await DbUser.findOneOrFail({ id: userId })) + const user = await DbUser.findOne({ id: userId }) + if (!user) { + throw new LogError('Could not find requested User', userId) + } + return transactionLinkList(paginated, filters, user) } }