mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 01:46:07 +00:00
further tests adapted
This commit is contained in:
parent
9c9a05e64f
commit
ddee5707f5
@ -17,17 +17,10 @@ export class UserAdmin {
|
||||
this.deletedAt = user.deletedAt
|
||||
this.emailConfirmationSend = emailConfirmationSend
|
||||
if (user.userRoles) {
|
||||
switch (user.userRoles[0].role) {
|
||||
case ROLE_NAMES.ROLE_NAME_ADMIN:
|
||||
this.isAdmin = user.userRoles[0].createdAt
|
||||
break
|
||||
case ROLE_NAMES.ROLE_NAME_MODERATOR:
|
||||
this.isModerator = user.userRoles[0].createdAt
|
||||
break
|
||||
default:
|
||||
this.isAdmin = null
|
||||
this.isModerator = null
|
||||
}
|
||||
this.roles = [] as string[]
|
||||
user.userRoles.forEach((userRole) => {
|
||||
this.roles?.push(userRole.role)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,11 +51,30 @@ export class UserAdmin {
|
||||
@Field(() => String, { nullable: true })
|
||||
emailConfirmationSend: string | null
|
||||
|
||||
@Field(() => Date, { nullable: true })
|
||||
isAdmin: Date | null
|
||||
@Field(() => [String], { nullable: true })
|
||||
roles: string[] | null
|
||||
}
|
||||
|
||||
@Field(() => Date, { nullable: true })
|
||||
isModerator: Date | null
|
||||
export function isAdmin(user: UserAdmin): boolean {
|
||||
if (user.roles) {
|
||||
for (const role of user.roles) {
|
||||
if (role === ROLE_NAMES.ROLE_NAME_ADMIN) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export function isModerator(user: UserAdmin): boolean {
|
||||
if (user.roles) {
|
||||
for (const role of user.roles) {
|
||||
if (role === ROLE_NAMES.ROLE_NAME_MODERATOR) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ObjectType()
|
||||
|
||||
@ -106,7 +106,7 @@ beforeAll(async () => {
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanDB()
|
||||
// await cleanDB()
|
||||
await con.close()
|
||||
})
|
||||
|
||||
@ -130,7 +130,7 @@ describe('UserResolver', () => {
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await cleanDB()
|
||||
// await cleanDB()
|
||||
})
|
||||
|
||||
it('returns success', () => {
|
||||
@ -164,7 +164,7 @@ describe('UserResolver', () => {
|
||||
createdAt: expect.any(Date),
|
||||
// emailChecked: false,
|
||||
language: 'de',
|
||||
userRoles: null,
|
||||
userRoles: expect.any(Array),
|
||||
deletedAt: null,
|
||||
publisherId: 1234,
|
||||
referrerId: null,
|
||||
@ -340,28 +340,17 @@ describe('UserResolver', () => {
|
||||
where: { id: user[0].id },
|
||||
relations: ['userRoles'],
|
||||
})
|
||||
console.log('vorher peter=', peter)
|
||||
await mutate({
|
||||
mutation: setUserRole,
|
||||
variables: { userId: user[0].id, role: ROLE_NAMES.ROLE_NAME_ADMIN },
|
||||
})
|
||||
peter.userRoles = [] as UserRole[]
|
||||
peter.userRoles[0] = UserRole.create()
|
||||
peter.userRoles[0].createdAt = new Date()
|
||||
peter.userRoles[0].role = ROLE_NAMES.ROLE_NAME_ADMIN
|
||||
peter.userRoles[0].userId = peter.id
|
||||
await peter.userRoles[0].save()
|
||||
|
||||
peter = await User.findOneOrFail({
|
||||
where: { id: user[0].id },
|
||||
relations: ['userRoles'],
|
||||
})
|
||||
console.log('nachher peter=', peter)
|
||||
|
||||
/*
|
||||
if (peter.userRole == null) {
|
||||
peter.userRole = UserRole.create()
|
||||
}
|
||||
peter.userRole.createdAt = new Date()
|
||||
peter.userRole.role = ROLE_NAMES.ROLE_NAME_ADMIN
|
||||
peter.userRole.userId = peter.id
|
||||
await peter.userRole.save()
|
||||
// await peter.save()
|
||||
console.log('user peter=', peter)
|
||||
*/
|
||||
|
||||
// date statement
|
||||
const actualDate = new Date()
|
||||
@ -443,13 +432,15 @@ describe('UserResolver', () => {
|
||||
beforeAll(async () => {
|
||||
await userFactory(testEnv, peterLustig)
|
||||
await userFactory(testEnv, bobBaumeister)
|
||||
await mutate({ mutation: login, variables: bobData })
|
||||
const loginResult = await mutate({ mutation: login, variables: bobData })
|
||||
console.log('login result=', loginResult)
|
||||
|
||||
// create contribution as user bob
|
||||
contribution = await mutate({
|
||||
mutation: createContribution,
|
||||
variables: { amount: 1000, memo: 'testing', creationDate: new Date().toISOString() },
|
||||
})
|
||||
console.log('createContribution contribution=', contribution)
|
||||
|
||||
// login as admin
|
||||
await mutate({ mutation: login, variables: peterData })
|
||||
@ -459,6 +450,7 @@ describe('UserResolver', () => {
|
||||
mutation: confirmContribution,
|
||||
variables: { id: contribution.data.createContribution.id },
|
||||
})
|
||||
console.log('confirmContribution contribution=', contribution)
|
||||
|
||||
// login as user bob
|
||||
bob = await mutate({ mutation: login, variables: bobData })
|
||||
@ -471,6 +463,7 @@ describe('UserResolver', () => {
|
||||
})
|
||||
|
||||
transactionLink = await TransactionLink.findOneOrFail()
|
||||
console.log('transactionLink=', transactionLink)
|
||||
|
||||
resetToken()
|
||||
|
||||
@ -483,6 +476,7 @@ describe('UserResolver', () => {
|
||||
redeemCode: transactionLink.code,
|
||||
},
|
||||
})
|
||||
console.log('newUser=', newUser)
|
||||
})
|
||||
|
||||
it('sets the referrer id to bob baumeister id', async () => {
|
||||
@ -704,7 +698,7 @@ describe('UserResolver', () => {
|
||||
firstName: 'Bibi',
|
||||
hasElopage: false,
|
||||
id: expect.any(Number),
|
||||
userRoles: null,
|
||||
roles: expect.any(Array),
|
||||
klickTipp: {
|
||||
newsletterState: false,
|
||||
},
|
||||
@ -981,7 +975,7 @@ describe('UserResolver', () => {
|
||||
},
|
||||
hasElopage: false,
|
||||
publisherId: 1234,
|
||||
userRoles: null,
|
||||
roles: expect.any(Array),
|
||||
},
|
||||
},
|
||||
}),
|
||||
@ -1501,7 +1495,7 @@ describe('UserResolver', () => {
|
||||
firstName: 'Bibi',
|
||||
hasElopage: false,
|
||||
id: expect.any(Number),
|
||||
userRoles: null,
|
||||
roles: expect.any(Array),
|
||||
klickTipp: {
|
||||
newsletterState: false,
|
||||
},
|
||||
|
||||
@ -619,7 +619,7 @@ export class UserResolver {
|
||||
|
||||
const [users, count] = await userRepository.findAndCount({
|
||||
where: {
|
||||
isAdmin: Not(IsNull()),
|
||||
userRoles: Not(IsNull()),
|
||||
},
|
||||
order: {
|
||||
createdAt: order,
|
||||
@ -655,7 +655,7 @@ export class UserResolver {
|
||||
'emailId',
|
||||
'emailContact',
|
||||
'deletedAt',
|
||||
'isAdmin',
|
||||
'userRoles',
|
||||
]
|
||||
const [users, count] = await userRepository.findBySearchCriteriaPagedFiltered(
|
||||
userFields.map((fieldName) => {
|
||||
@ -728,6 +728,7 @@ export class UserResolver {
|
||||
where: { id: userId },
|
||||
relations: ['userRoles'],
|
||||
})
|
||||
console.log('setUserRole user=', user)
|
||||
// user exists ?
|
||||
if (!user) {
|
||||
throw new LogError('Could not find user with given ID', userId)
|
||||
@ -759,9 +760,11 @@ export class UserResolver {
|
||||
throw new LogError('User already is in role=', role)
|
||||
}
|
||||
}
|
||||
console.log('setUserRole before save user=', user)
|
||||
await user.save()
|
||||
await EVENT_ADMIN_USER_ROLE_SET(user, moderator)
|
||||
const newUser = await DbUser.findOne({ id: userId }, { relations: ['userRoles'] })
|
||||
console.log('setUserRole newUser=', user)
|
||||
return newUser?.userRoles ? newUser.userRoles[0].role : null
|
||||
}
|
||||
|
||||
|
||||
@ -21,7 +21,7 @@ export const userFactory = async (
|
||||
} = await mutate({ mutation: createUser, variables: user })
|
||||
// console.log('creatUser:', { id }, { user })
|
||||
// get user from database
|
||||
let dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact', 'userRole'] })
|
||||
let dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact', 'userRoles'] })
|
||||
// console.log('dbUser:', dbUser)
|
||||
|
||||
const emailContact = dbUser.emailContact
|
||||
@ -35,7 +35,7 @@ export const userFactory = async (
|
||||
}
|
||||
|
||||
// get last changes of user from database
|
||||
dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact', 'userRole'] })
|
||||
dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact', 'userRoles'] })
|
||||
|
||||
if (user.createdAt || user.deletedAt || user.role) {
|
||||
if (user.createdAt) dbUser.createdAt = user.createdAt
|
||||
|
||||
@ -117,8 +117,8 @@ export const confirmContribution = gql`
|
||||
`
|
||||
|
||||
export const setUserRole = gql`
|
||||
mutation ($userId: Int!, $isAdmin: Boolean!) {
|
||||
setUserRole(userId: $userId, isAdmin: $isAdmin)
|
||||
mutation ($userId: Int!, $role: String!) {
|
||||
setUserRole(userId: $userId, role: $role)
|
||||
}
|
||||
`
|
||||
|
||||
@ -315,7 +315,7 @@ export const login = gql`
|
||||
}
|
||||
hasElopage
|
||||
publisherId
|
||||
isAdmin
|
||||
roles
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
@ -11,7 +11,7 @@ export const verifyLogin = gql`
|
||||
}
|
||||
hasElopage
|
||||
publisherId
|
||||
isAdmin
|
||||
roles
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -87,7 +87,7 @@ export const searchUsers = gql`
|
||||
hasElopage
|
||||
emailConfirmationSend
|
||||
deletedAt
|
||||
isAdmin
|
||||
roles
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,8 +87,8 @@ export class User extends BaseEntity {
|
||||
@Column({ type: 'bool', default: false })
|
||||
hideAmountGDT: boolean
|
||||
|
||||
@OneToMany(() => UserRole, (userRole: UserRole) => userRole.userId)
|
||||
@JoinColumn({ name: 'id' })
|
||||
@OneToMany(() => UserRole, (userRole) => userRole.user)
|
||||
@JoinColumn({ name: 'user_id' })
|
||||
userRoles?: UserRole[]
|
||||
|
||||
@Column({ name: 'referrer_id', type: 'int', unsigned: true, nullable: true, default: null })
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
|
||||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm'
|
||||
import { User } from '../User'
|
||||
|
||||
@Entity('user_roles', { engine: 'InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci' })
|
||||
export class UserRole extends BaseEntity {
|
||||
@ -16,4 +17,8 @@ export class UserRole extends BaseEntity {
|
||||
|
||||
@Column({ name: 'updated_at', nullable: true, default: null, type: 'datetime' })
|
||||
updatedAt: Date | null
|
||||
|
||||
@ManyToOne(() => User, (user) => user.userRoles)
|
||||
@JoinColumn({ name: 'user_id' })
|
||||
user: User
|
||||
}
|
||||
|
||||
@ -68,7 +68,7 @@ export default defineConfig({
|
||||
}
|
||||
hasElopage
|
||||
publisherId
|
||||
isAdmin
|
||||
roles
|
||||
hideAmountGDD
|
||||
hideAmountGDT
|
||||
__typename
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user