From 63d7f754e84d244677d994734f5e1f05fb35b910 Mon Sep 17 00:00:00 2001 From: Claus-Peter Huebner Date: Tue, 7 Nov 2023 21:34:04 +0100 Subject: [PATCH] shift homecommunity creation for tests in seed directory-tree --- .../graphql/resolver/EmailOptinCodes.test.ts | 5 ++-- .../src/graphql/resolver/UserResolver.test.ts | 9 +++---- .../src/graphql/resolver/util/communities.ts | 20 --------------- backend/src/seeds/community/index.ts | 25 ++++++++++++++++++- backend/src/seeds/factory/user.ts | 5 ++-- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/backend/src/graphql/resolver/EmailOptinCodes.test.ts b/backend/src/graphql/resolver/EmailOptinCodes.test.ts index 731d4a395..09ad743fe 100644 --- a/backend/src/graphql/resolver/EmailOptinCodes.test.ts +++ b/backend/src/graphql/resolver/EmailOptinCodes.test.ts @@ -8,11 +8,10 @@ import { GraphQLError } from 'graphql' import { testEnvironment, cleanDB } from '@test/helpers' import { CONFIG } from '@/config' +import { writeHomeCommunityEntry } from '@/seeds/community' import { createUser, setPassword, forgotPassword } from '@/seeds/graphql/mutations' import { queryOptIn } from '@/seeds/graphql/queries' -import { createHomeCommunity } from './util/communities' - let mutate: ApolloServerTestClient['mutate'], query: ApolloServerTestClient['query'], con: Connection @@ -48,7 +47,7 @@ describe('EmailOptinCodes', () => { lastName: 'Lustig', language: 'de', } - await createHomeCommunity() + await writeHomeCommunityEntry() const { data: { createUser: user }, } = await mutate({ mutation: createUser, variables }) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index fd9a85790..e16e0f0fc 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -34,6 +34,7 @@ import { import { EventType } from '@/event/Events' import { SecretKeyCryptographyCreateKey } from '@/password/EncryptorUtils' import { encryptPassword } from '@/password/PasswordEncryptor' +import { writeHomeCommunityEntry } from '@/seeds/community' import { contributionLinkFactory } from '@/seeds/factory/contributionLink' import { transactionLinkFactory } from '@/seeds/factory/transactionLink' import { userFactory } from '@/seeds/factory/user' @@ -67,8 +68,6 @@ import { stephenHawking } from '@/seeds/users/stephen-hawking' import { printTimeDuration } from '@/util/time' import { objectValuesToArray } from '@/util/utilities' -import { createHomeCommunity } from './util/communities' - jest.mock('@/emails/sendEmailVariants', () => { const originalModule = jest.requireActual('@/emails/sendEmailVariants') return { @@ -131,7 +130,7 @@ describe('UserResolver', () => { beforeAll(async () => { jest.clearAllMocks() - homeCom = await createHomeCommunity() + homeCom = await writeHomeCommunityEntry() result = await mutate({ mutation: createUser, variables }) }) @@ -546,7 +545,7 @@ describe('UserResolver', () => { let newUser: User beforeAll(async () => { - await createHomeCommunity() + await writeHomeCommunityEntry() await mutate({ mutation: createUser, variables: createUserVariables }) const emailContact = await UserContact.findOneOrFail({ where: { email: createUserVariables.email }, @@ -591,7 +590,7 @@ describe('UserResolver', () => { describe('no valid password', () => { beforeAll(async () => { - await createHomeCommunity() + await writeHomeCommunityEntry() await mutate({ mutation: createUser, variables: createUserVariables }) const emailContact = await UserContact.findOneOrFail({ where: { email: createUserVariables.email }, diff --git a/backend/src/graphql/resolver/util/communities.ts b/backend/src/graphql/resolver/util/communities.ts index 9a271066d..0c0023a19 100644 --- a/backend/src/graphql/resolver/util/communities.ts +++ b/backend/src/graphql/resolver/util/communities.ts @@ -63,23 +63,3 @@ export async function getCommunity(communityUuid: string): Promise { - let homeCom: DbCommunity - try { - return await getHomeCommunity() - } catch (err) { - homeCom = DbCommunity.create() - homeCom.foreign = false - homeCom.url = 'http://localhost/api' - homeCom.publicKey = Buffer.from('publicKey-HomeCommunity') - homeCom.privateKey = Buffer.from('privateKey-HomeCommunity') - homeCom.communityUuid = 'HomeCom-UUID' - homeCom.authenticatedAt = new Date() - homeCom.name = 'HomeCommunity-name' - homeCom.description = 'HomeCommunity-description' - homeCom.creationDate = new Date() - await DbCommunity.insert(homeCom) - return homeCom - } -} diff --git a/backend/src/seeds/community/index.ts b/backend/src/seeds/community/index.ts index 6a639ee44..e3b420d00 100644 --- a/backend/src/seeds/community/index.ts +++ b/backend/src/seeds/community/index.ts @@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid' import { CONFIG } from '@/config' -export async function writeHomeCommunityEntry(): Promise { +export async function writeHomeCommunityEntry(): Promise { try { // check for existing homeCommunity entry let homeCom = await DbCommunity.findOne({ where: { foreign: false } }) @@ -28,7 +28,30 @@ export async function writeHomeCommunityEntry(): Promise { homeCom.creationDate = new Date() await DbCommunity.insert(homeCom) } + return homeCom } catch (err) { throw new Error(`Seeding: Error writing HomeCommunity-Entry`) // : ${err}`) } } + +export async function createHomeCommunity(): Promise { + let homeCom: DbCommunity + try { + return await DbCommunity.findOneOrFail({ + where: [{ foreign: false }], + }) + } catch (err) { + homeCom = DbCommunity.create() + homeCom.foreign = false + homeCom.url = 'http://localhost/api' + homeCom.publicKey = Buffer.from('publicKey-HomeCommunity') + homeCom.privateKey = Buffer.from('privateKey-HomeCommunity') + homeCom.communityUuid = 'HomeCom-UUID' + homeCom.authenticatedAt = new Date() + homeCom.name = 'HomeCommunity-name' + homeCom.description = 'HomeCommunity-description' + homeCom.creationDate = new Date() + await DbCommunity.insert(homeCom) + return homeCom + } +} diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index 321e9db17..3ddddf336 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -5,8 +5,8 @@ import { ApolloServerTestClient } from 'apollo-server-testing' import { RoleNames } from '@enum/RoleNames' -import { createHomeCommunity, getHomeCommunity } from '@/graphql/resolver/util/communities' import { setUserRole } from '@/graphql/resolver/util/modifyUserRole' +import { writeHomeCommunityEntry } from '@/seeds/community' import { createUser, setPassword } from '@/seeds/graphql/mutations' import { UserInterface } from '@/seeds/users/UserInterface' @@ -16,7 +16,7 @@ export const userFactory = async ( ): Promise => { const { mutate } = client - await createHomeCommunity() + const homeCom = await writeHomeCommunityEntry() const { data: { @@ -47,7 +47,6 @@ export const userFactory = async ( await dbUser.save() } try { - const homeCom = await getHomeCommunity() if (homeCom.communityUuid) { dbUser.communityUuid = homeCom.communityUuid await User.save(dbUser)