From 20fbaa276f510ef716c236226144e7c8427bb0e7 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Thu, 15 Dec 2022 18:35:11 +0100 Subject: [PATCH 1/4] feat(backend): test semaphore --- .../src/graphql/resolver/semaphore.test.ts | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 backend/src/graphql/resolver/semaphore.test.ts diff --git a/backend/src/graphql/resolver/semaphore.test.ts b/backend/src/graphql/resolver/semaphore.test.ts new file mode 100644 index 000000000..e334910f1 --- /dev/null +++ b/backend/src/graphql/resolver/semaphore.test.ts @@ -0,0 +1,190 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +import Decimal from 'decimal.js-light' +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import { logger } from '@test/testSetup' +import { userFactory } from '@/seeds/factory/user' +import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg' +import { bobBaumeister } from '@/seeds/users/bob-baumeister' +import { peterLustig } from '@/seeds/users/peter-lustig' +import { creationFactory, nMonthsBefore } from '@/seeds/factory/creation' +import { cleanDB, testEnvironment, contributionDateFormatter } from '@test/helpers' +import { + confirmContribution, + createContribution, + createTransactionLink, + redeemTransactionLink, + login, + createContributionLink, + sendCoins, +} from '@/seeds/graphql/mutations' + +let mutate: any, con: any +let testEnv: any + +beforeAll(async () => { + testEnv = await testEnvironment() + mutate = testEnv.mutate + con = testEnv.con + await cleanDB() +}) + +afterAll(async () => { + await cleanDB() + await con.close() +}) + +describe('semaphore', () => { + let contributionLinkCode = '' + let bobsTransactionLinkCode = '' + let bibisTransactionLinkCode = '' + let bibisOpenContributionId = -1 + let bobsOpenContributionId = -1 + + beforeAll(async () => { + const now = new Date() + await userFactory(testEnv, bibiBloxberg) + await userFactory(testEnv, peterLustig) + await userFactory(testEnv, bobBaumeister) + await creationFactory(testEnv, { + email: 'bibi@bloxberg.de', + amount: 1000, + memo: 'Herzlich Willkommen bei Gradido!', + creationDate: nMonthsBefore(new Date()), + confirmed: true, + }) + await creationFactory(testEnv, { + email: 'bob@baumeister.de', + amount: 1000, + memo: 'Herzlich Willkommen bei Gradido!', + creationDate: nMonthsBefore(new Date()), + confirmed: true, + }) + await mutate({ + mutation: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) + const { + data: { createContributionLink: contributionLink }, + } = await mutate({ + mutation: createContributionLink, + variables: { + amount: new Decimal(200), + name: 'Test Contribution Link', + memo: 'Danke für deine Teilnahme an dem Test der Contribution Links', + cycle: 'ONCE', + validFrom: new Date(2022, 5, 18).toISOString(), + validTo: new Date(now.getFullYear() + 1, 7, 14).toISOString(), + maxAmountPerMonth: new Decimal(200), + maxPerCycle: 1, + }, + }) + contributionLinkCode = 'CL-' + contributionLink.code + await mutate({ + mutation: login, + variables: { email: 'bob@baumeister.de', password: 'Aa12345_' }, + }) + const { + data: { createTransactionLink: bobsLink }, + } = await mutate({ + mutation: createTransactionLink, + variables: { + email: 'bob@baumeister.de', + amount: 20, + memo: 'Bobs Link', + }, + }) + const { + data: { createContribution: bobsContribution }, + } = await mutate({ + mutation: createContribution, + variables: { + creationDate: contributionDateFormatter(new Date()), + amount: 200, + memo: 'Bobs Contribution', + }, + }) + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + const { + data: { createTransactionLink: bibisLink }, + } = await mutate({ + mutation: createTransactionLink, + variables: { + amount: 20, + memo: 'Bibis Link', + }, + }) + const { + data: { createContribution: bibisContribution }, + } = await mutate({ + mutation: createContribution, + variables: { + creationDate: contributionDateFormatter(new Date()), + amount: 200, + memo: 'Bibis Contribution', + }, + }) + bobsTransactionLinkCode = bobsLink.code + bibisTransactionLinkCode = bibisLink.code + bibisOpenContributionId = bibisContribution.id + bobsOpenContributionId = bobsContribution.id + }) + + it('creates a lot of transactions without errors', async () => { + await mutate({ + mutation: login, + variables: { email: 'bibi@bloxberg.de', password: 'Aa12345_' }, + }) + const bibiRedeemContributionLink = mutate({ + mutation: redeemTransactionLink, + variables: { code: contributionLinkCode }, + }) + const redeemBobsLink = mutate({ + mutation: redeemTransactionLink, + variables: { code: bobsTransactionLinkCode }, + }) + const bibisTransaction = mutate({ + mutation: sendCoins, + variables: { email: 'bob@baumeister.de', amount: '50', memo: 'Das ist für dich, Bob' }, + }) + await mutate({ + mutation: login, + variables: { email: 'bob@baumeister.de', password: 'Aa12345_' }, + }) + const bobRedeemContributionLink = mutate({ + mutation: redeemTransactionLink, + variables: { code: contributionLinkCode }, + }) + const redeemBibisLink = mutate({ + mutation: redeemTransactionLink, + variables: { code: bibisTransactionLinkCode }, + }) + const bobsTransaction = mutate({ + mutation: sendCoins, + variables: { email: 'bibi@bloxberg.de', amount: '50', memo: 'Das ist für dich, Bibi' }, + }) + await mutate({ + mutation: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) + const confirmBibisContribution = mutate({ + mutation: confirmContribution, + variables: { id: bibisOpenContributionId }, + }) + const confirmBobsContribution = mutate({ + mutation: confirmContribution, + variables: { id: bobsOpenContributionId }, + }) + await expect(bibiRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) + await expect(redeemBobsLink).resolves.toMatchObject({ errors: undefined }) + await expect(bibisTransaction).resolves.toMatchObject({ errors: undefined }) + await expect(bobRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) + await expect(redeemBibisLink).resolves.toMatchObject({ errors: undefined }) + await expect(bobsTransaction).resolves.toMatchObject({ errors: undefined }) + await expect(confirmBibisContribution).resolves.toMatchObject({ errors: undefined }) + await expect(confirmBobsContribution).resolves.toMatchObject({ errors: undefined }) + }) +}) From 5a925c0526a8c5a2e9677e9829ef543493077616 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Thu, 15 Dec 2022 23:08:19 +0100 Subject: [PATCH 2/4] "fixed" semaphore tests --- .../src/graphql/resolver/semaphore.test.ts | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/backend/src/graphql/resolver/semaphore.test.ts b/backend/src/graphql/resolver/semaphore.test.ts index e334910f1..d3cee5bb6 100644 --- a/backend/src/graphql/resolver/semaphore.test.ts +++ b/backend/src/graphql/resolver/semaphore.test.ts @@ -154,10 +154,16 @@ describe('semaphore', () => { mutation: login, variables: { email: 'bob@baumeister.de', password: 'Aa12345_' }, }) - const bobRedeemContributionLink = mutate({ + /* + - "errors": undefined, + + "errors": Array [ + + [GraphQLError: Creation from contribution link was not successful. Error: to < from, reverse decay calculation is invalid], + + ], + */ + /* const bobRedeemContributionLink = mutate({ mutation: redeemTransactionLink, variables: { code: contributionLinkCode }, - }) + }) */ const redeemBibisLink = mutate({ mutation: redeemTransactionLink, variables: { code: bibisTransactionLinkCode }, @@ -170,21 +176,23 @@ describe('semaphore', () => { mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, }) - const confirmBibisContribution = mutate({ + // Creation was not successful: Error: to < from, reverse decay calculation is invalid + /* const confirmBibisContribution = mutate({ mutation: confirmContribution, variables: { id: bibisOpenContributionId }, - }) - const confirmBobsContribution = mutate({ + }) */ + // Creation was not successful: Error: to < from, reverse decay calculation is invalid + /* const confirmBobsContribution = mutate({ mutation: confirmContribution, variables: { id: bobsOpenContributionId }, - }) + }) */ await expect(bibiRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) await expect(redeemBobsLink).resolves.toMatchObject({ errors: undefined }) await expect(bibisTransaction).resolves.toMatchObject({ errors: undefined }) - await expect(bobRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) + // await expect(bobRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) await expect(redeemBibisLink).resolves.toMatchObject({ errors: undefined }) await expect(bobsTransaction).resolves.toMatchObject({ errors: undefined }) - await expect(confirmBibisContribution).resolves.toMatchObject({ errors: undefined }) - await expect(confirmBobsContribution).resolves.toMatchObject({ errors: undefined }) + // await expect(confirmBibisContribution).resolves.toMatchObject({ errors: undefined }) + // await expect(confirmBobsContribution).resolves.toMatchObject({ errors: undefined }) }) }) From 2f17ec565e850dd7dd7c6e649c2e52a299754e42 Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 21 Dec 2022 00:05:50 +0100 Subject: [PATCH 3/4] all tests are running --- .../src/graphql/resolver/semaphore.test.ts | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/backend/src/graphql/resolver/semaphore.test.ts b/backend/src/graphql/resolver/semaphore.test.ts index d3cee5bb6..e334910f1 100644 --- a/backend/src/graphql/resolver/semaphore.test.ts +++ b/backend/src/graphql/resolver/semaphore.test.ts @@ -154,16 +154,10 @@ describe('semaphore', () => { mutation: login, variables: { email: 'bob@baumeister.de', password: 'Aa12345_' }, }) - /* - - "errors": undefined, - + "errors": Array [ - + [GraphQLError: Creation from contribution link was not successful. Error: to < from, reverse decay calculation is invalid], - + ], - */ - /* const bobRedeemContributionLink = mutate({ + const bobRedeemContributionLink = mutate({ mutation: redeemTransactionLink, variables: { code: contributionLinkCode }, - }) */ + }) const redeemBibisLink = mutate({ mutation: redeemTransactionLink, variables: { code: bibisTransactionLinkCode }, @@ -176,23 +170,21 @@ describe('semaphore', () => { mutation: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, }) - // Creation was not successful: Error: to < from, reverse decay calculation is invalid - /* const confirmBibisContribution = mutate({ + const confirmBibisContribution = mutate({ mutation: confirmContribution, variables: { id: bibisOpenContributionId }, - }) */ - // Creation was not successful: Error: to < from, reverse decay calculation is invalid - /* const confirmBobsContribution = mutate({ + }) + const confirmBobsContribution = mutate({ mutation: confirmContribution, variables: { id: bobsOpenContributionId }, - }) */ + }) await expect(bibiRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) await expect(redeemBobsLink).resolves.toMatchObject({ errors: undefined }) await expect(bibisTransaction).resolves.toMatchObject({ errors: undefined }) - // await expect(bobRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) + await expect(bobRedeemContributionLink).resolves.toMatchObject({ errors: undefined }) await expect(redeemBibisLink).resolves.toMatchObject({ errors: undefined }) await expect(bobsTransaction).resolves.toMatchObject({ errors: undefined }) - // await expect(confirmBibisContribution).resolves.toMatchObject({ errors: undefined }) - // await expect(confirmBobsContribution).resolves.toMatchObject({ errors: undefined }) + await expect(confirmBibisContribution).resolves.toMatchObject({ errors: undefined }) + await expect(confirmBobsContribution).resolves.toMatchObject({ errors: undefined }) }) }) From fb7c61f3b2d5107e99932ce60a53c0a5ff21704f Mon Sep 17 00:00:00 2001 From: Ulf Gebhardt Date: Wed, 21 Dec 2022 00:16:28 +0100 Subject: [PATCH 4/4] require 77% coverage on backend --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c3238507a..12891851a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -528,7 +528,7 @@ jobs: report_name: Coverage Backend type: lcov result_path: ./backend/coverage/lcov.info - min_coverage: 74 + min_coverage: 77 token: ${{ github.token }} ##########################################################################