adapt tests to work with User and UserContact

This commit is contained in:
Claus-Peter Hübner 2022-09-02 02:34:45 +02:00
parent a0fe5f7951
commit 97dee4d67c
4 changed files with 40 additions and 56 deletions

View File

@ -215,10 +215,12 @@ describe('UserResolver', () => {
mutation: createUser, mutation: createUser,
variables: { ...variables, email: 'bibi@bloxberg.de', language: 'it' }, 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.arrayContaining([
expect.objectContaining({ expect.objectContaining({
emailContact: expect.objectContaining({
email: 'bibi@bloxberg.de', email: 'bibi@bloxberg.de',
}),
language: 'de', language: 'de',
}), }),
]), ]),
@ -232,10 +234,12 @@ describe('UserResolver', () => {
mutation: createUser, mutation: createUser,
variables: { ...variables, email: 'raeuber@hotzenplotz.de', publisherId: undefined }, 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.arrayContaining([
expect.objectContaining({ expect.objectContaining({
emailContact: expect.objectContaining({
email: 'raeuber@hotzenplotz.de', email: 'raeuber@hotzenplotz.de',
}),
publisherId: null, publisherId: null,
}), }),
]), ]),
@ -276,8 +280,10 @@ describe('UserResolver', () => {
UserContact.findOne({ email: 'ein@besucher.de' }, { relations: ['user'] }), UserContact.findOne({ email: 'ein@besucher.de' }, { relations: ['user'] }),
).resolves.toEqual( ).resolves.toEqual(
expect.objectContaining({ expect.objectContaining({
user: expect.objectContaining({
contributionLinkId: link.id, contributionLinkId: link.id,
}), }),
}),
) )
}) })
}) })
@ -322,20 +328,20 @@ bei Gradidio sei dabei!`,
} }
let result: any let result: any
let emailOptIn: string let emailVerificationCode: string
describe('valid optin code and valid password', () => { describe('valid optin code and valid password', () => {
let newUser: any let newUser: User
beforeAll(async () => { beforeAll(async () => {
await mutate({ mutation: createUser, variables: createUserVariables }) await mutate({ mutation: createUser, variables: createUserVariables })
const loginEmailOptIn = await LoginEmailOptIn.find() const emailContact = await UserContact.findOneOrFail({ email: createUserVariables.email })
emailOptIn = loginEmailOptIn[0].verificationCode.toString() emailVerificationCode = emailContact.emailVerificationCode.toString()
result = await mutate({ result = await mutate({
mutation: setPassword, 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 () => { afterAll(async () => {
@ -343,11 +349,11 @@ bei Gradidio sei dabei!`,
}) })
it('sets email checked to true', () => { it('sets email checked to true', () => {
expect(newUser[0].emailChecked).toBeTruthy() expect(newUser.emailContact.emailChecked).toBeTruthy()
}) })
it('updates the password', () => { 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', () => { describe('no valid password', () => {
beforeAll(async () => { beforeAll(async () => {
await mutate({ mutation: createUser, variables: createUserVariables }) await mutate({ mutation: createUser, variables: createUserVariables })
const loginEmailOptIn = await LoginEmailOptIn.find() const emailContact = await UserContact.findOneOrFail({ email: createUserVariables.email })
emailOptIn = loginEmailOptIn[0].verificationCode.toString() emailVerificationCode = emailContact.emailVerificationCode.toString()
result = await mutate({ result = await mutate({
mutation: setPassword, mutation: setPassword,
variables: { code: emailOptIn, password: 'not-valid' }, variables: { code: emailVerificationCode, password: 'not-valid' },
}) })
}) })

View File

@ -324,25 +324,6 @@ export class UserResolver {
logger.info(`login with ${email}, ***, ${publisherId} ...`) logger.info(`login with ${email}, ***, ${publisherId} ...`)
email = email.trim().toLowerCase() email = email.trim().toLowerCase()
const dbUser = await findUserByEmail(email) 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) { if (dbUser.deletedAt) {
logger.error('The User was permanently deleted in database.') logger.error('The User was permanently deleted in database.')
throw new Error('This user was permanently deleted. Contact support for questions.') 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<boolean> { async forgotPassword(@Arg('email') email: string): Promise<boolean> {
logger.info(`forgotPassword(${email})...`) logger.info(`forgotPassword(${email})...`)
email = email.trim().toLowerCase() email = email.trim().toLowerCase()
const user = await findUserByEmail(email) const user = await findUserByEmail(email).catch(() => {
// const user = await DbUser.findOne({ email }) logger.warn(`fail on find UserContact per ${email}`)
})
if (!user) { if (!user) {
logger.warn(`no user found with ${email}`) logger.warn(`no user found with ${email}`)
return true return true
@ -650,12 +632,13 @@ export class UserResolver {
throw new Error('Could not login with emailVerificationCode') throw new Error('Could not login with emailVerificationCode')
}) })
*/ */
const userContact = await DbUserContact.findOneOrFail({ emailVerificationCode: code }).catch( const userContact = await DbUserContact.findOneOrFail(
() => { { emailVerificationCode: code },
{ relations: ['user'] },
).catch(() => {
logger.error('Could not login with emailVerificationCode') logger.error('Could not login with emailVerificationCode')
throw new Error('Could not login with emailVerificationCode') throw new Error('Could not login with emailVerificationCode')
}, })
)
logger.debug('userContact loaded...') logger.debug('userContact loaded...')
// Code is only valid for `CONFIG.EMAIL_CODE_VALID_TIME` minutes // Code is only valid for `CONFIG.EMAIL_CODE_VALID_TIME` minutes
if (!isEmailVerificationCodeValid(userContact.updatedAt)) { if (!isEmailVerificationCodeValid(userContact.updatedAt)) {
@ -669,10 +652,7 @@ export class UserResolver {
logger.debug('EmailVerificationCode is valid...') logger.debug('EmailVerificationCode is valid...')
// load user // load user
const user = await DbUser.findOneOrFail({ id: userContact.userId }).catch(() => { const user = userContact.user
logger.error('Could not find corresponding Login User')
throw new Error('Could not find corresponding Login User')
})
logger.debug('user with EmailVerificationCode found...') logger.debug('user with EmailVerificationCode found...')
// Generate Passphrase if needed // Generate Passphrase if needed
@ -902,13 +882,7 @@ async function findUserByEmail(email: string): Promise<DbUser> {
throw new Error('No user with this credentials') throw new Error('No user with this credentials')
}) })
const dbUser = dbUserContact.user 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 dbUser.emailContact = dbUserContact
*/
return dbUser return dbUser
} }

View File

@ -3,6 +3,7 @@ import { createContributionLink } from '@/seeds/graphql/mutations'
import { login } from '@/seeds/graphql/queries' import { login } from '@/seeds/graphql/queries'
import { ContributionLink } from '@model/ContributionLink' import { ContributionLink } from '@model/ContributionLink'
import { ContributionLinkInterface } from '@/seeds/contributionLink/ContributionLinkInterface' import { ContributionLinkInterface } from '@/seeds/contributionLink/ContributionLinkInterface'
import { User } from '@/graphql/model/User'
export const contributionLinkFactory = async ( export const contributionLinkFactory = async (
client: ApolloServerTestClient, client: ApolloServerTestClient,
@ -11,7 +12,10 @@ export const contributionLinkFactory = async (
const { mutate, query } = client const { mutate, query } = client
// login as admin // 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 = { const variables = {
amount: contributionLink.amount, amount: contributionLink.amount,

View File

@ -17,10 +17,10 @@ export const userFactory = async (
} = await mutate({ mutation: createUser, variables: user }) } = await mutate({ mutation: createUser, variables: user })
// console.log('creatUser:', { id }, { user }) // console.log('creatUser:', { id }, { user })
// get user from database // get user from database
let dbUser = await User.findOneOrFail({ id }) let dbUser = await User.findOneOrFail({ id }, { relations: ['emailContact']})
// console.log('dbUser:', dbUser) // console.log('dbUser:', dbUser)
const emailContact = await UserContact.findOneOrFail({ userId: id }) const emailContact = dbUser.emailContact
// console.log('emailContact:', emailContact) // console.log('emailContact:', emailContact)
if (user.emailChecked) { if (user.emailChecked) {