adapt UserResolverTest to work with Users and UserContacts

This commit is contained in:
Claus-Peter Hübner 2022-09-01 01:07:25 +02:00
parent eedaf9e6e3
commit a0fe5f7951
2 changed files with 39 additions and 35 deletions

View File

@ -22,6 +22,9 @@ import { ContributionLink } from '@model/ContributionLink'
import { logger } from '@test/testSetup' import { logger } from '@test/testSetup'
import { validate as validateUUID, version as versionUUID } from 'uuid' import { validate as validateUUID, version as versionUUID } from 'uuid'
import { peterLustig } from '@/seeds/users/peter-lustig' import { peterLustig } from '@/seeds/users/peter-lustig'
import { UserContact } from '@entity/UserContact'
import { OptInType } from '../enum/OptInType'
import { UserContactType } from '../enum/UserContactType'
// import { klicktippSignIn } from '@/apis/KlicktippController' // import { klicktippSignIn } from '@/apis/KlicktippController'
@ -82,7 +85,7 @@ describe('UserResolver', () => {
} }
let result: any let result: any
let emailOptIn: string let emailVerificationCode: string
let user: User[] let user: User[]
beforeAll(async () => { beforeAll(async () => {
@ -101,11 +104,11 @@ describe('UserResolver', () => {
}) })
describe('valid input data', () => { describe('valid input data', () => {
let loginEmailOptIn: LoginEmailOptIn[] // let loginEmailOptIn: LoginEmailOptIn[]
beforeAll(async () => { beforeAll(async () => {
user = await User.find() user = await User.find({ relations: ['emailContact'] })
loginEmailOptIn = await LoginEmailOptIn.find() // loginEmailOptIn = await LoginEmailOptIn.find()
emailOptIn = loginEmailOptIn[0].verificationCode.toString() emailVerificationCode = user[0].emailContact.emailVerificationCode.toString()
}) })
describe('filling all tables', () => { describe('filling all tables', () => {
@ -115,8 +118,8 @@ describe('UserResolver', () => {
id: expect.any(Number), id: expect.any(Number),
gradidoID: expect.any(String), gradidoID: expect.any(String),
alias: null, alias: null,
email: 'peter@lustig.de', emailContact: expect.any(UserContact), // 'peter@lustig.de',
emailId: null, emailId: expect.any(Number),
firstName: 'Peter', firstName: 'Peter',
lastName: 'Lustig', lastName: 'Lustig',
password: '0', password: '0',
@ -124,7 +127,7 @@ describe('UserResolver', () => {
privKey: null, privKey: null,
// emailHash: expect.any(Buffer), // emailHash: expect.any(Buffer),
createdAt: expect.any(Date), createdAt: expect.any(Date),
emailChecked: false, // emailChecked: false,
passphrase: expect.any(String), passphrase: expect.any(String),
language: 'de', language: 'de',
isAdmin: null, isAdmin: null,
@ -141,17 +144,20 @@ describe('UserResolver', () => {
}) })
it('creates an email optin', () => { it('creates an email optin', () => {
expect(loginEmailOptIn).toEqual([ expect(user[0].emailContact).toEqual({
{
id: expect.any(Number), id: expect.any(Number),
type: UserContactType.USER_CONTACT_EMAIL,
userId: user[0].id, userId: user[0].id,
verificationCode: expect.any(String), email: 'peter@lustig.de',
emailOptInTypeId: 1, emailChecked: false,
emailVerificationCode: expect.any(String),
emailOptInTypeId: OptInType.EMAIL_OPT_IN_REGISTER,
emailResendCount: 0,
phone: null,
createdAt: expect.any(Date), createdAt: expect.any(Date),
resendCount: 0, deletedAt: null,
updatedAt: expect.any(Date), updatedAt: null,
}, })
])
}) })
}) })
}) })
@ -160,7 +166,7 @@ describe('UserResolver', () => {
it('sends an account activation email', () => { it('sends an account activation email', () => {
const activationLink = CONFIG.EMAIL_LINK_VERIFICATION.replace( const activationLink = CONFIG.EMAIL_LINK_VERIFICATION.replace(
/{optin}/g, /{optin}/g,
emailOptIn, emailVerificationCode,
).replace(/{code}/g, '') ).replace(/{code}/g, '')
expect(sendAccountActivationEmail).toBeCalledWith({ expect(sendAccountActivationEmail).toBeCalledWith({
link: activationLink, link: activationLink,
@ -244,7 +250,7 @@ describe('UserResolver', () => {
// activate account of admin Peter Lustig // activate account of admin Peter Lustig
await mutate({ await mutate({
mutation: setPassword, mutation: setPassword,
variables: { code: emailOptIn, password: 'Aa12345_' }, variables: { code: emailVerificationCode, password: 'Aa12345_' },
}) })
// make Peter Lustig Admin // make Peter Lustig Admin
const peter = await User.findOneOrFail({ id: user[0].id }) const peter = await User.findOneOrFail({ id: user[0].id })
@ -266,7 +272,9 @@ describe('UserResolver', () => {
}) })
it('sets the contribution link id', async () => { it('sets the contribution link id', async () => {
await expect(User.findOne({ email: 'ein@besucher.de' })).resolves.toEqual( await expect(
UserContact.findOne({ email: 'ein@besucher.de' }, { relations: ['user'] }),
).resolves.toEqual(
expect.objectContaining({ expect.objectContaining({
contributionLinkId: link.id, contributionLinkId: link.id,
}), }),
@ -616,13 +624,13 @@ bei Gradidio sei dabei!`,
describe('user exists in DB', () => { describe('user exists in DB', () => {
let result: any let result: any
let loginEmailOptIn: LoginEmailOptIn[] let emailContact: UserContact
beforeAll(async () => { beforeAll(async () => {
await userFactory(testEnv, bibiBloxberg) await userFactory(testEnv, bibiBloxberg)
await resetEntity(LoginEmailOptIn) // await resetEntity(LoginEmailOptIn)
result = await mutate({ mutation: forgotPassword, variables }) result = await mutate({ mutation: forgotPassword, variables })
loginEmailOptIn = await LoginEmailOptIn.find() emailContact = await UserContact.findOneOrFail(variables)
}) })
afterAll(async () => { afterAll(async () => {
@ -630,18 +638,12 @@ bei Gradidio sei dabei!`,
}) })
it('returns true', async () => { it('returns true', async () => {
await expect(result).toEqual( expect(result).toEqual(expect.objectContaining({ data: { forgotPassword: true } }))
expect.objectContaining({
data: {
forgotPassword: true,
},
}),
)
}) })
it('sends reset password email', () => { it('sends reset password email', () => {
expect(sendResetPasswordEmail).toBeCalledWith({ expect(sendResetPasswordEmail).toBeCalledWith({
link: activationLink(loginEmailOptIn[0]), link: activationLink(emailContact.emailVerificationCode),
firstName: 'Bibi', firstName: 'Bibi',
lastName: 'Bloxberg', lastName: 'Bloxberg',
email: 'bibi@bloxberg.de', email: 'bibi@bloxberg.de',

View File

@ -896,17 +896,19 @@ export class UserResolver {
async function findUserByEmail(email: string): Promise<DbUser> { async function findUserByEmail(email: string): Promise<DbUser> {
const dbUserContact = await DbUserContact.findOneOrFail( const dbUserContact = await DbUserContact.findOneOrFail(
{ email: email }, { email: email },
{ withDeleted: true }, { withDeleted: true, relations: ['user'] },
).catch(() => { ).catch(() => {
logger.error(`UserContact with email=${email} does not exists`) logger.error(`UserContact with email=${email} does not exists`)
throw new Error('No user with this credentials') throw new Error('No user with this credentials')
}) })
const userId = dbUserContact.userId const dbUser = dbUserContact.user
/*
const dbUser = await DbUser.findOneOrFail({ id: userId }, { withDeleted: true }).catch(() => { const dbUser = await DbUser.findOneOrFail({ id: userId }, { withDeleted: true }).catch(() => {
logger.error(`User with emailContact=${email} connected per userId=${userId} does not exist`) logger.error(`User with emailContact=${email} connected per userId=${userId} does not exist`)
throw new Error('No user with this credentials') throw new Error('No user with this credentials')
}) })
dbUser.emailContact = dbUserContact dbUser.emailContact = dbUserContact
*/
return dbUser return dbUser
} }