shift homecommunity creation for tests in seed directory-tree

This commit is contained in:
Claus-Peter Huebner 2023-11-07 21:34:04 +01:00
parent 8c898490fb
commit 63d7f754e8
5 changed files with 32 additions and 32 deletions

View File

@ -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 })

View File

@ -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 },

View File

@ -63,23 +63,3 @@ export async function getCommunity(communityUuid: string): Promise<DbCommunity |
where: [{ communityUuid }],
})
}
export async function createHomeCommunity(): Promise<DbCommunity> {
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
}
}

View File

@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid'
import { CONFIG } from '@/config'
export async function writeHomeCommunityEntry(): Promise<void> {
export async function writeHomeCommunityEntry(): Promise<DbCommunity> {
try {
// check for existing homeCommunity entry
let homeCom = await DbCommunity.findOne({ where: { foreign: false } })
@ -28,7 +28,30 @@ export async function writeHomeCommunityEntry(): Promise<void> {
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<DbCommunity> {
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
}
}

View File

@ -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<User> => {
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)