From 311aa023e47cb50b0cc49d85c4e3c1f7d47a53e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolfgang=20Hu=C3=9F?= Date: Mon, 23 May 2022 12:45:31 +0200 Subject: [PATCH] Test listTransactionLinksAdmin resolver, a start --- .../graphql/resolver/AdminResolver.test.ts | 244 ++++++++++++++++++ backend/src/graphql/resolver/AdminResolver.ts | 2 + 2 files changed, 246 insertions(+) diff --git a/backend/src/graphql/resolver/AdminResolver.test.ts b/backend/src/graphql/resolver/AdminResolver.test.ts index 40949810e..149fc2bcb 100644 --- a/backend/src/graphql/resolver/AdminResolver.test.ts +++ b/backend/src/graphql/resolver/AdminResolver.test.ts @@ -5,6 +5,9 @@ import { objectValuesToArray } from '@/util/utilities' import { testEnvironment, resetToken, cleanDB } from '@test/helpers' import { userFactory } from '@/seeds/factory/user' import { creationFactory } from '@/seeds/factory/creation' +import { creations } from '@/seeds/creation/index' +import { transactionLinkFactory } from '@/seeds/factory/transactionLink' +import { transactionLinks } from '@/seeds/transactionLink/index' import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' import { peterLustig } from '@/seeds/users/peter-lustig' import { stephenHawking } from '@/seeds/users/stephen-hawking' @@ -18,6 +21,7 @@ import { updatePendingCreation, deletePendingCreation, confirmPendingCreation, + listTransactionLinksAdmin, } from '@/seeds/graphql/mutations' import { getPendingCreations, login } from '@/seeds/graphql/queries' import { GraphQLError } from 'graphql' @@ -1324,4 +1328,244 @@ describe('AdminResolver', () => { }) }) }) + + describe('transaction links list', () => { + const variables = { + userId: 1, // dummy, may be replaced + filters: { + byDeleted: null, + byExpired: null, + byRedeemed: null, + }, + currentPage: 1, + pageSize: 5, + } + + describe('unauthenticated', () => { + it('returns an error', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) + + describe('authenticated', () => { + describe('without admin rights', () => { + beforeAll(async () => { + user = await userFactory(testEnv, bibiBloxberg) + await query({ + query: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + it('returns an error', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables, + }), + ).resolves.toEqual( + expect.objectContaining({ + errors: [new GraphQLError('401 Unauthorized')], + }), + ) + }) + }) + + describe('with admin rights', () => { + const expectNoDeletedOrRedeemed = expect.objectContaining({ + data: { + listTransactionLinksAdmin: { + linkCount: 6, + linkList: expect.not.arrayContaining([ + expect.objectContaining({ + memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', + createdAt: new Date(2022, 0, 1), + }), + expect.objectContaining({ + memo: 'Da habe ich mich wohl etwas übernommen.', + deletedAt: true, + }), + ]), + }, + }, + }) + + beforeAll(async () => { + // admin 'peter@lustig.de' has to exists for 'creationFactory' + admin = await userFactory(testEnv, peterLustig) + + user = await userFactory(testEnv, bibiBloxberg) + variables.userId = user.id + // bibi need GDDs + const bibisCreation = creations.find((creation) => creation.email === 'bibi@bloxberg.de') + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + await creationFactory(testEnv, bibisCreation!) + // bibis transaktion links + const bibisTransaktionLinks = transactionLinks.filter( + (transactionLink) => transactionLink.email === 'bibi@bloxberg.de', + ) + for (let i = 0; i < bibisTransaktionLinks.length; i++) { + await transactionLinkFactory(testEnv, bibisTransaktionLinks[i]) + } + + // admin: only now log in + await query({ + query: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) + }) + + afterAll(async () => { + await cleanDB() + resetToken() + }) + + // Wolle: make filters object nullable + describe.skip('without any filters', () => { + it('finds 6 open transaction links and no deleted or redeemed', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables, + }), + ).resolves.toEqual(expectNoDeletedOrRedeemed) + }) + }) + + describe('all filters are null', () => { + it('finds 6 open transaction links and no deleted or redeemed', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables, + }), + ).resolves.toEqual(expectNoDeletedOrRedeemed) + }) + }) + + describe('filter by deleted', () => { + it('finds 6 open transaction links and 1 deleted and no redeemed', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables: { + ...variables, + filters: { + byDeleted: true, // Wolle: rename to `withDeleted`? + byExpired: null, + byRedeemed: null, + }, + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + data: { + listTransactionLinksAdmin: { + linkCount: 7, + linkList: expect.arrayContaining([ + expect.not.objectContaining({ + memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', + // Wolle: createdAt: new Date(2022, 0, 1), + }), + expect.objectContaining({ + memo: 'Da habe ich mich wohl etwas übernommen.', + // Wolle: deletedAt: expect.any(String), + }), + ]), + }, + }, + }), + ) + }) + }) + + // Wolle: works not as expected, because of wrong 'linkCount' at least + describe.skip('filter by expired', () => { + it('finds 6 open transaction links and 1 deleted and no redeemed', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables: { + ...variables, + filters: { + byDeleted: null, + byExpired: true, + byRedeemed: null, + }, + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + data: { + listTransactionLinksAdmin: { + linkCount: 7, // Wolle: finds 5 but counts 7 + linkList: expect.arrayContaining([ + expect.objectContaining({ + memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', + // Wolle: createdAt: new Date(2022, 0, 1), + }), + expect.not.objectContaining({ + memo: 'Da habe ich mich wohl etwas übernommen.', + // Wolle: deletedAt: expect.any(String), + }), + ]), + }, + }, + }), + ) + }) + + // Wolle: not done jet + describe.skip('filter by redeemed', () => { + it('finds 6 open transaction links and 1 deleted and no redeemed', async () => { + await expect( + query({ + query: listTransactionLinksAdmin, + variables: { + ...variables, + filters: { + byDeleted: null, + byExpired: null, + byRedeemed: true, + }, + }, + }), + ).resolves.toEqual( + expect.objectContaining({ + data: { + listTransactionLinksAdmin: { + linkCount: 7, // Wolle: finds 5 but counts 7 + linkList: expect.arrayContaining([ + expect.objectContaining({ + memo: 'Leider wollte niemand meine Gradidos zum Neujahr haben :(', + // Wolle: createdAt: new Date(2022, 0, 1), + }), + expect.not.objectContaining({ + memo: 'Da habe ich mich wohl etwas übernommen.', + // Wolle: deletedAt: expect.any(String), + }), + ]), + }, + }, + }), + ) + }) + }) + }) + }) + }) }) diff --git a/backend/src/graphql/resolver/AdminResolver.ts b/backend/src/graphql/resolver/AdminResolver.ts index 7aa5cd079..8576d25cb 100644 --- a/backend/src/graphql/resolver/AdminResolver.ts +++ b/backend/src/graphql/resolver/AdminResolver.ts @@ -442,7 +442,9 @@ export class AdminResolver { if (!filters.byExpired) where.validUntil = MoreThan(new Date()) const [transactionLinks, count] = await dbTransactionLink.findAndCount({ where, + // Wolle: works not as expected withDeleted: filters.byDeleted, + // withDeleted: filters.byDeleted !== undefined ? !filters.byDeleted : undefined, order: { createdAt: order, },