mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge pull request #1621 from gradido/seed-transactions-in-backend
feat: Seed Creation Transactions in Backend
This commit is contained in:
commit
89c6501318
@ -2,7 +2,7 @@
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
import { testEnvironment, headerPushMock, resetToken, cleanDB } from '@test/helpers'
|
||||
import { createUserFactory } from '@/seeds/factory/user'
|
||||
import { userFactory } from '@/seeds/factory/user'
|
||||
import { bibiBloxberg } from '@/seeds/users/bibi-bloxberg'
|
||||
import { createUser, setPassword } from '@/seeds/graphql/mutations'
|
||||
import { login, logout } from '@/seeds/graphql/queries'
|
||||
@ -11,6 +11,7 @@ import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
||||
import { User } from '@entity/User'
|
||||
import CONFIG from '@/config'
|
||||
import { sendAccountActivationEmail } from '@/mailer/sendAccountActivationEmail'
|
||||
|
||||
// import { klicktippSignIn } from '@/apis/KlicktippController'
|
||||
|
||||
jest.mock('@/mailer/sendAccountActivationEmail', () => {
|
||||
@ -30,9 +31,10 @@ jest.mock('@/apis/KlicktippController', () => {
|
||||
*/
|
||||
|
||||
let mutate: any, query: any, con: any
|
||||
let testEnv: any
|
||||
|
||||
beforeAll(async () => {
|
||||
const testEnv = await testEnvironment()
|
||||
testEnv = await testEnvironment()
|
||||
mutate = testEnv.mutate
|
||||
query = testEnv.query
|
||||
con = testEnv.con
|
||||
@ -312,7 +314,7 @@ describe('UserResolver', () => {
|
||||
|
||||
describe('user is in database and correct login data', () => {
|
||||
beforeAll(async () => {
|
||||
await createUserFactory(mutate, bibiBloxberg)
|
||||
await userFactory(testEnv, bibiBloxberg)
|
||||
result = await query({ query: login, variables })
|
||||
})
|
||||
|
||||
@ -349,7 +351,7 @@ describe('UserResolver', () => {
|
||||
|
||||
describe('user is in database and wrong password', () => {
|
||||
beforeAll(async () => {
|
||||
await createUserFactory(mutate, bibiBloxberg)
|
||||
await userFactory(testEnv, bibiBloxberg)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
@ -387,7 +389,7 @@ describe('UserResolver', () => {
|
||||
}
|
||||
|
||||
beforeAll(async () => {
|
||||
await createUserFactory(mutate, bibiBloxberg)
|
||||
await userFactory(testEnv, bibiBloxberg)
|
||||
await query({ query: login, variables })
|
||||
})
|
||||
|
||||
|
||||
7
backend/src/seeds/creation/CreationInterface.ts
Normal file
7
backend/src/seeds/creation/CreationInterface.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export interface CreationInterface {
|
||||
email: string
|
||||
amount: number
|
||||
memo: string
|
||||
creationDate: string
|
||||
confirmed?: boolean
|
||||
}
|
||||
29
backend/src/seeds/creation/index.ts
Normal file
29
backend/src/seeds/creation/index.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { CreationInterface } from './CreationInterface'
|
||||
|
||||
const lastMonth = (date: Date): string => {
|
||||
return new Date(date.getFullYear(), date.getMonth() - 1, 1).toISOString()
|
||||
}
|
||||
|
||||
export const creations: CreationInterface[] = [
|
||||
{
|
||||
email: 'bibi@bloxberg.de',
|
||||
amount: 1000,
|
||||
memo: 'Herzlich Willkommen bei Gradido!',
|
||||
creationDate: lastMonth(new Date()),
|
||||
confirmed: true,
|
||||
},
|
||||
{
|
||||
email: 'bob@baumeister.de',
|
||||
amount: 1000,
|
||||
memo: 'Herzlich Willkommen bei Gradido!',
|
||||
creationDate: lastMonth(new Date()),
|
||||
confirmed: true,
|
||||
},
|
||||
{
|
||||
email: 'raeuber@hotzenplotz.de',
|
||||
amount: 1000,
|
||||
memo: 'Herzlich Willkommen bei Gradido!',
|
||||
creationDate: lastMonth(new Date()),
|
||||
confirmed: true,
|
||||
},
|
||||
]
|
||||
38
backend/src/seeds/factory/creation.ts
Normal file
38
backend/src/seeds/factory/creation.ts
Normal file
@ -0,0 +1,38 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
import { createPendingCreation, confirmPendingCreation } from '@/seeds/graphql/mutations'
|
||||
import { login } from '@/seeds/graphql/queries'
|
||||
import { CreationInterface } from '@/seeds/creation/CreationInterface'
|
||||
import { ApolloServerTestClient } from 'apollo-server-testing'
|
||||
import { User } from '@entity/User'
|
||||
import { AdminPendingCreation } from '@entity/AdminPendingCreation'
|
||||
// import CONFIG from '@/config/index'
|
||||
|
||||
export const creationFactory = async (
|
||||
client: ApolloServerTestClient,
|
||||
creation: CreationInterface,
|
||||
): Promise<void> => {
|
||||
const { mutate, query } = client
|
||||
|
||||
// login as Peter Lustig (admin)
|
||||
await query({ query: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } })
|
||||
|
||||
// get Peter Lustig's user id
|
||||
const peterLustig = await User.findOneOrFail({ where: { email: 'peter@lustig.de' } })
|
||||
const variables = { ...creation, moderator: peterLustig.id }
|
||||
|
||||
await mutate({ mutation: createPendingCreation, variables })
|
||||
|
||||
// get User
|
||||
const user = await User.findOneOrFail({ where: { email: creation.email } })
|
||||
|
||||
if (creation.confirmed) {
|
||||
const pendingCreation = await AdminPendingCreation.findOneOrFail({
|
||||
where: { userId: user.id },
|
||||
order: { created: 'DESC' },
|
||||
})
|
||||
|
||||
await mutate({ mutation: confirmPendingCreation, variables: { id: pendingCreation.id } })
|
||||
}
|
||||
}
|
||||
@ -1,13 +1,16 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||
|
||||
import { createUser, setPassword } from '@/seeds/graphql/mutations'
|
||||
import { User } from '@entity/User'
|
||||
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
|
||||
import { ServerUser } from '@entity/ServerUser'
|
||||
import { UserInterface } from '@/seeds/users/UserInterface'
|
||||
import { ApolloServerTestClient } from 'apollo-server-testing'
|
||||
|
||||
export const userFactory = async (
|
||||
client: ApolloServerTestClient,
|
||||
user: UserInterface,
|
||||
): Promise<void> => {
|
||||
const { mutate } = client
|
||||
|
||||
export const createUserFactory = async (mutate: any, user: UserInterface): Promise<void> => {
|
||||
await mutate({ mutation: createUser, variables: user })
|
||||
let dbUser = await User.findOneOrFail({ where: { email: user.email } })
|
||||
|
||||
|
||||
@ -69,3 +69,29 @@ export const createTransactionLink = gql`
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
// from admin interface
|
||||
|
||||
export const createPendingCreation = gql`
|
||||
mutation (
|
||||
$email: String!
|
||||
$amount: Float!
|
||||
$memo: String!
|
||||
$creationDate: String!
|
||||
$moderator: Int!
|
||||
) {
|
||||
createPendingCreation(
|
||||
email: $email
|
||||
amount: $amount
|
||||
memo: $memo
|
||||
creationDate: $creationDate
|
||||
moderator: $moderator
|
||||
)
|
||||
}
|
||||
`
|
||||
|
||||
export const confirmPendingCreation = gql`
|
||||
mutation ($id: Float!) {
|
||||
confirmPendingCreation(id: $id)
|
||||
}
|
||||
`
|
||||
|
||||
@ -7,14 +7,16 @@ import { createTestClient } from 'apollo-server-testing'
|
||||
import { name, internet, random } from 'faker'
|
||||
|
||||
import { users } from './users/index'
|
||||
import { createUserFactory } from './factory/user'
|
||||
import { creations } from './creation/index'
|
||||
import { userFactory } from './factory/user'
|
||||
import { creationFactory } from './factory/creation'
|
||||
import { entities } from '@entity/index'
|
||||
|
||||
const context = {
|
||||
token: '',
|
||||
setHeaders: {
|
||||
push: (value: string): void => {
|
||||
context.token = value
|
||||
push: (value: { key: string; value: string }): void => {
|
||||
context.token = value.value
|
||||
},
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
forEach: (): void => {},
|
||||
@ -38,19 +40,18 @@ const resetEntity = async (entity: any) => {
|
||||
|
||||
const run = async () => {
|
||||
const server = await createServer(context)
|
||||
const testClient = createTestClient(server.apollo)
|
||||
const { mutate } = testClient
|
||||
const seedClient = createTestClient(server.apollo)
|
||||
const { con } = server
|
||||
await cleanDB()
|
||||
|
||||
// seed the standard users
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
await createUserFactory(mutate, users[i])
|
||||
await userFactory(seedClient, users[i])
|
||||
}
|
||||
|
||||
// seed 100 random users
|
||||
for (let i = 0; i < 100; i++) {
|
||||
await createUserFactory(mutate, {
|
||||
await userFactory(seedClient, {
|
||||
firstName: name.firstName(),
|
||||
lastName: name.lastName(),
|
||||
email: internet.email(),
|
||||
@ -58,6 +59,11 @@ const run = async () => {
|
||||
})
|
||||
}
|
||||
|
||||
// create GDD
|
||||
for (let i = 0; i < creations.length; i++) {
|
||||
await creationFactory(seedClient, creations[i])
|
||||
}
|
||||
|
||||
await con.close()
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user