From ddee5707f51a46d82219c85d6c1d787e80ec4eee Mon Sep 17 00:00:00 2001 From: Claus-Peter Huebner Date: Thu, 29 Jun 2023 00:00:12 +0200 Subject: [PATCH] further tests adapted --- backend/src/graphql/model/UserAdmin.ts | 42 +++++++++++------- .../src/graphql/resolver/UserResolver.test.ts | 44 ++++++++----------- backend/src/graphql/resolver/UserResolver.ts | 7 ++- backend/src/seeds/factory/user.ts | 4 +- backend/src/seeds/graphql/mutations.ts | 6 +-- backend/src/seeds/graphql/queries.ts | 4 +- .../entity/0067-add_user_roles_table/User.ts | 4 +- .../0067-add_user_roles_table/UserRole.ts | 7 ++- e2e-tests/cypress.config.ts | 2 +- 9 files changed, 67 insertions(+), 53 deletions(-) diff --git a/backend/src/graphql/model/UserAdmin.ts b/backend/src/graphql/model/UserAdmin.ts index d12b629fa..8e7526a61 100644 --- a/backend/src/graphql/model/UserAdmin.ts +++ b/backend/src/graphql/model/UserAdmin.ts @@ -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() diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index c37e0f48e..ac71afbf3 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -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, }, diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 274b3fa1d..b100eb785 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -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 } diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index fb84787df..e55f8e8d6 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -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 diff --git a/backend/src/seeds/graphql/mutations.ts b/backend/src/seeds/graphql/mutations.ts index 22e0b1b09..06b2caef5 100644 --- a/backend/src/seeds/graphql/mutations.ts +++ b/backend/src/seeds/graphql/mutations.ts @@ -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 } } ` diff --git a/backend/src/seeds/graphql/queries.ts b/backend/src/seeds/graphql/queries.ts index ce7efbfc3..6a9dcc601 100644 --- a/backend/src/seeds/graphql/queries.ts +++ b/backend/src/seeds/graphql/queries.ts @@ -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 } } } diff --git a/database/entity/0067-add_user_roles_table/User.ts b/database/entity/0067-add_user_roles_table/User.ts index ba75084ec..666d9dacc 100644 --- a/database/entity/0067-add_user_roles_table/User.ts +++ b/database/entity/0067-add_user_roles_table/User.ts @@ -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 }) diff --git a/database/entity/0067-add_user_roles_table/UserRole.ts b/database/entity/0067-add_user_roles_table/UserRole.ts index eb68b6e85..4734de8d9 100644 --- a/database/entity/0067-add_user_roles_table/UserRole.ts +++ b/database/entity/0067-add_user_roles_table/UserRole.ts @@ -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 } diff --git a/e2e-tests/cypress.config.ts b/e2e-tests/cypress.config.ts index e26259626..3c1485a90 100644 --- a/e2e-tests/cypress.config.ts +++ b/e2e-tests/cypress.config.ts @@ -68,7 +68,7 @@ export default defineConfig({ } hasElopage publisherId - isAdmin + roles hideAmountGDD hideAmountGDT __typename