From 97dee4d67cf7318f957037b8bf5fdf1205861434 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Fri, 2 Sep 2022 02:34:45 +0200 Subject: [PATCH] adapt tests to work with User and UserContact --- .../src/graphql/resolver/UserResolver.test.ts | 38 ++++++++------- backend/src/graphql/resolver/UserResolver.ts | 48 +++++-------------- backend/src/seeds/factory/contributionLink.ts | 6 ++- backend/src/seeds/factory/user.ts | 4 +- 4 files changed, 40 insertions(+), 56 deletions(-) diff --git a/backend/src/graphql/resolver/UserResolver.test.ts b/backend/src/graphql/resolver/UserResolver.test.ts index 14edc7343..b8d2a1d37 100644 --- a/backend/src/graphql/resolver/UserResolver.test.ts +++ b/backend/src/graphql/resolver/UserResolver.test.ts @@ -215,10 +215,12 @@ describe('UserResolver', () => { mutation: createUser, variables: { ...variables, email: 'bibi@bloxberg.de', language: 'it' }, }) - await expect(User.find()).resolves.toEqual( + await expect(User.find({ relations: ['emailContact'] }, )).resolves.toEqual( expect.arrayContaining([ expect.objectContaining({ - email: 'bibi@bloxberg.de', + emailContact: expect.objectContaining({ + email: 'bibi@bloxberg.de', + }), language: 'de', }), ]), @@ -232,10 +234,12 @@ describe('UserResolver', () => { mutation: createUser, variables: { ...variables, email: 'raeuber@hotzenplotz.de', publisherId: undefined }, }) - await expect(User.find()).resolves.toEqual( + await expect(User.find({ relations: ['emailContact'] }, )).resolves.toEqual( expect.arrayContaining([ expect.objectContaining({ - email: 'raeuber@hotzenplotz.de', + emailContact: expect.objectContaining({ + email: 'raeuber@hotzenplotz.de', + }), publisherId: null, }), ]), @@ -276,7 +280,9 @@ describe('UserResolver', () => { UserContact.findOne({ email: 'ein@besucher.de' }, { relations: ['user'] }), ).resolves.toEqual( expect.objectContaining({ - contributionLinkId: link.id, + user: expect.objectContaining({ + contributionLinkId: link.id, + }), }), ) }) @@ -322,20 +328,20 @@ bei Gradidio sei dabei!`, } let result: any - let emailOptIn: string + let emailVerificationCode: string describe('valid optin code and valid password', () => { - let newUser: any + let newUser: User beforeAll(async () => { await mutate({ mutation: createUser, variables: createUserVariables }) - const loginEmailOptIn = await LoginEmailOptIn.find() - emailOptIn = loginEmailOptIn[0].verificationCode.toString() + const emailContact = await UserContact.findOneOrFail({ email: createUserVariables.email }) + emailVerificationCode = emailContact.emailVerificationCode.toString() result = await mutate({ mutation: setPassword, - variables: { code: emailOptIn, password: 'Aa12345_' }, + variables: { code: emailVerificationCode, password: 'Aa12345_' }, }) - newUser = await User.find() + newUser = await User.findOneOrFail({ id: emailContact.userId }, { relations: ['emailContact'] }) }) afterAll(async () => { @@ -343,11 +349,11 @@ bei Gradidio sei dabei!`, }) it('sets email checked to true', () => { - expect(newUser[0].emailChecked).toBeTruthy() + expect(newUser.emailContact.emailChecked).toBeTruthy() }) it('updates the password', () => { - expect(newUser[0].password).toEqual('3917921995996627700') + expect(newUser.password).toEqual('3917921995996627700') }) /* @@ -369,11 +375,11 @@ bei Gradidio sei dabei!`, describe('no valid password', () => { beforeAll(async () => { await mutate({ mutation: createUser, variables: createUserVariables }) - const loginEmailOptIn = await LoginEmailOptIn.find() - emailOptIn = loginEmailOptIn[0].verificationCode.toString() + const emailContact = await UserContact.findOneOrFail({ email: createUserVariables.email }) + emailVerificationCode = emailContact.emailVerificationCode.toString() result = await mutate({ mutation: setPassword, - variables: { code: emailOptIn, password: 'not-valid' }, + variables: { code: emailVerificationCode, password: 'not-valid' }, }) }) diff --git a/backend/src/graphql/resolver/UserResolver.ts b/backend/src/graphql/resolver/UserResolver.ts index 0cde3c73a..64a21cef1 100644 --- a/backend/src/graphql/resolver/UserResolver.ts +++ b/backend/src/graphql/resolver/UserResolver.ts @@ -324,25 +324,6 @@ export class UserResolver { logger.info(`login with ${email}, ***, ${publisherId} ...`) email = email.trim().toLowerCase() const dbUser = await findUserByEmail(email) - /* - const dbUserContact = await DbUserContact.findOneOrFail({ email }, { withDeleted: true }).catch( - () => { - logger.error(`UserContact with email=${email} does not exists`) - throw new Error('No user with this credentials') - }, - ) - const userId = dbUserContact.userId - const dbUser = await DbUser.findOneOrFail(userId).catch(() => { - logger.error(`User with emeilContact=${email} connected per userId=${userId} does not exist`) - throw new Error('No user with this credentials') - }) - */ - /* - const dbUser = await DbUser.findOneOrFail({ email }, { withDeleted: true }).catch(() => { - logger.error(`User with email=${email} does not exists`) - throw new Error('No user with this credentials') - }) - */ if (dbUser.deletedAt) { logger.error('The User was permanently deleted in database.') throw new Error('This user was permanently deleted. Contact support for questions.') @@ -591,8 +572,9 @@ export class UserResolver { async forgotPassword(@Arg('email') email: string): Promise { logger.info(`forgotPassword(${email})...`) email = email.trim().toLowerCase() - const user = await findUserByEmail(email) - // const user = await DbUser.findOne({ email }) + const user = await findUserByEmail(email).catch(() => { + logger.warn(`fail on find UserContact per ${email}`) + }) if (!user) { logger.warn(`no user found with ${email}`) return true @@ -650,12 +632,13 @@ export class UserResolver { throw new Error('Could not login with emailVerificationCode') }) */ - const userContact = await DbUserContact.findOneOrFail({ emailVerificationCode: code }).catch( - () => { - logger.error('Could not login with emailVerificationCode') - throw new Error('Could not login with emailVerificationCode') - }, - ) + const userContact = await DbUserContact.findOneOrFail( + { emailVerificationCode: code }, + { relations: ['user'] }, + ).catch(() => { + logger.error('Could not login with emailVerificationCode') + throw new Error('Could not login with emailVerificationCode') + }) logger.debug('userContact loaded...') // Code is only valid for `CONFIG.EMAIL_CODE_VALID_TIME` minutes if (!isEmailVerificationCodeValid(userContact.updatedAt)) { @@ -669,10 +652,7 @@ export class UserResolver { logger.debug('EmailVerificationCode is valid...') // load user - const user = await DbUser.findOneOrFail({ id: userContact.userId }).catch(() => { - logger.error('Could not find corresponding Login User') - throw new Error('Could not find corresponding Login User') - }) + const user = userContact.user logger.debug('user with EmailVerificationCode found...') // Generate Passphrase if needed @@ -902,13 +882,7 @@ async function findUserByEmail(email: string): Promise { throw new Error('No user with this credentials') }) const dbUser = dbUserContact.user - /* - const dbUser = await DbUser.findOneOrFail({ id: userId }, { withDeleted: true }).catch(() => { - logger.error(`User with emailContact=${email} connected per userId=${userId} does not exist`) - throw new Error('No user with this credentials') - }) dbUser.emailContact = dbUserContact - */ return dbUser } diff --git a/backend/src/seeds/factory/contributionLink.ts b/backend/src/seeds/factory/contributionLink.ts index 5c83b6ad3..b422993e1 100644 --- a/backend/src/seeds/factory/contributionLink.ts +++ b/backend/src/seeds/factory/contributionLink.ts @@ -3,6 +3,7 @@ import { createContributionLink } from '@/seeds/graphql/mutations' import { login } from '@/seeds/graphql/queries' import { ContributionLink } from '@model/ContributionLink' import { ContributionLinkInterface } from '@/seeds/contributionLink/ContributionLinkInterface' +import { User } from '@/graphql/model/User' export const contributionLinkFactory = async ( client: ApolloServerTestClient, @@ -11,7 +12,10 @@ export const contributionLinkFactory = async ( const { mutate, query } = client // login as admin - await query({ query: login, variables: { email: 'peter@lustig.de', password: 'Aa12345_' } }) + const user = await query({ + query: login, + variables: { email: 'peter@lustig.de', password: 'Aa12345_' }, + }) const variables = { amount: contributionLink.amount, diff --git a/backend/src/seeds/factory/user.ts b/backend/src/seeds/factory/user.ts index c2eb20bc4..df6e1ef6b 100644 --- a/backend/src/seeds/factory/user.ts +++ b/backend/src/seeds/factory/user.ts @@ -17,10 +17,10 @@ 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 }) + let dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact']}) // console.log('dbUser:', dbUser) - const emailContact = await UserContact.findOneOrFail({ userId: id }) + const emailContact = dbUser.emailContact // console.log('emailContact:', emailContact) if (user.emailChecked) {