test account activation email is sent

This commit is contained in:
Moriz Wahl 2021-12-27 20:48:39 +01:00
parent fbac95f352
commit 6a666edc9f
3 changed files with 121 additions and 83 deletions

View File

@ -321,7 +321,5 @@ function isCreationValid(creations: number[], amount: number, creationDate: Date
async function hasActivatedEmail(email: string): Promise<boolean> {
const repository = getCustomRepository(LoginUserRepository)
const user = await repository.findByEmail(email)
let emailActivate = false
if (user) emailActivate = user.emailChecked
return user ? user.emailChecked : false
}

View File

@ -2,6 +2,8 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { createTestClient } from 'apollo-server-testing'
import gql from 'graphql-tag'
import { GraphQLError } from 'graphql'
import createServer from '../../server/createServer'
import { resetDB, initialize } from '@dbTools/helpers'
import { getRepository } from 'typeorm'
@ -9,6 +11,15 @@ import { LoginUser } from '@entity/LoginUser'
import { LoginUserBackup } from '@entity/LoginUserBackup'
import { LoginEmailOptIn } from '@entity/LoginEmailOptIn'
import { User } from '@entity/User'
import CONFIG from '../../config'
import { sendEMail } from '../../util/sendEMail'
jest.mock('../../util/sendEMail', () => {
return {
__esModule: true,
sendEMail: jest.fn(),
}
})
let mutate: any
let con: any
@ -31,25 +42,27 @@ describe('UserResolver', () => {
publisherId: 1234,
}
const mutation = `
mutation(
$email: String!
$firstName: String!
$lastName: String!
$language: String!
$publisherId: Int
) {
createUser(
email: $email
firstName: $firstName
lastName: $lastName
language: $language
publisherId: $publisherId
)
}
const mutation = gql`
mutation (
$email: String!
$firstName: String!
$lastName: String!
$language: String!
$publisherId: Int
) {
createUser(
email: $email
firstName: $firstName
lastName: $lastName
language: $language
publisherId: $publisherId
)
}
`
let result: any
let emailOptIn: string
let newUser: User
beforeAll(async () => {
result = await mutate({ mutation, variables })
@ -63,7 +76,7 @@ describe('UserResolver', () => {
expect(result).toEqual(expect.objectContaining({ data: { createUser: 'success' } }))
})
describe('different user tables', () => {
describe('valid input data', () => {
let loginUser: LoginUser[]
let user: User[]
let loginUserBackup: LoginUserBackup[]
@ -77,71 +90,99 @@ describe('UserResolver', () => {
loginEmailOptIn = await getRepository(LoginEmailOptIn)
.createQueryBuilder('login_email_optin')
.getMany()
newUser = user[0]
emailOptIn = loginEmailOptIn[0].verificationCode.toString()
})
it('saves the user in login_user table', () => {
expect(loginUser).toEqual([
{
id: expect.any(Number),
email: 'peter@lustig.de',
firstName: 'Peter',
lastName: 'Lustig',
username: '',
description: '',
password: '0',
pubKey: null,
privKey: null,
emailHash: expect.any(Buffer),
createdAt: expect.any(Date),
emailChecked: false,
passphraseShown: false,
language: 'de',
disabled: false,
groupId: 1,
publisherId: 1234,
},
])
})
describe('filling all tables', () => {
it('saves the user in login_user table', () => {
expect(loginUser).toEqual([
{
id: expect.any(Number),
email: 'peter@lustig.de',
firstName: 'Peter',
lastName: 'Lustig',
username: '',
description: '',
password: '0',
pubKey: null,
privKey: null,
emailHash: expect.any(Buffer),
createdAt: expect.any(Date),
emailChecked: false,
passphraseShown: false,
language: 'de',
disabled: false,
groupId: 1,
publisherId: 1234,
},
])
})
it('saves the user in state_user table', () => {
expect(user).toEqual([
{
id: expect.any(Number),
indexId: 0,
groupId: 0,
pubkey: expect.any(Buffer),
email: 'peter@lustig.de',
firstName: 'Peter',
lastName: 'Lustig',
username: '',
disabled: false,
},
])
})
it('saves the user in state_user table', () => {
expect(user).toEqual([
{
id: expect.any(Number),
indexId: 0,
groupId: 0,
pubkey: expect.any(Buffer),
email: 'peter@lustig.de',
firstName: 'Peter',
lastName: 'Lustig',
username: '',
disabled: false,
},
])
})
it('saves the user in login_user_backup table', () => {
expect(loginUserBackup).toEqual([
{
id: expect.any(Number),
passphrase: expect.any(String),
userId: loginUser[0].id,
mnemonicType: 2,
},
])
})
it('saves the user in login_user_backup table', () => {
expect(loginUserBackup).toEqual([
{
id: expect.any(Number),
passphrase: expect.any(String),
userId: loginUser[0].id,
mnemonicType: 2,
},
])
})
it('creates an email optin', () => {
expect(loginEmailOptIn).toEqual([
{
id: expect.any(Number),
userId: loginUser[0].id,
verificationCode: expect.any(String),
emailOptInTypeId: 1,
createdAt: expect.any(Date),
resendCount: 0,
updatedAt: expect.any(Date),
},
])
it('creates an email optin', () => {
expect(loginEmailOptIn).toEqual([
{
id: expect.any(Number),
userId: loginUser[0].id,
verificationCode: expect.any(String),
emailOptInTypeId: 1,
createdAt: expect.any(Date),
resendCount: 0,
updatedAt: expect.any(Date),
},
])
})
})
})
describe('account activation email', () => {
it('sends an account activation email', () => {
const activationLink = CONFIG.EMAIL_LINK_VERIFICATION.replace(/\$1/g, emailOptIn)
expect(sendEMail).toBeCalledWith({
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
to: `${newUser.firstName} ${newUser.lastName} <${newUser.email}>`,
subject: 'Gradido: E-Mail Überprüfung',
text:
expect.stringContaining(`Hallo ${newUser.firstName} ${newUser.lastName},`) &&
expect.stringContaining(activationLink),
})
})
})
describe('email already exists', () => {
it('throws an error', async () => {
await expect(mutate({ mutation, variables })).resolves.toEqual(
expect.objectContaining({
errors: [new GraphQLError('User already exists.')],
}),
)
})
})
})

View File

@ -471,13 +471,13 @@ export class UserResolver {
return 'success'
}
private async sendAccountActivationEmail(
private sendAccountActivationEmail(
activationLink: string,
firstName: string,
lastName: string,
email: string,
) {
const emailSent = await sendEMail({
): Promise<any> {
return sendEMail({
from: `Gradido (nicht antworten) <${CONFIG.EMAIL_SENDER}>`,
to: `${firstName} ${lastName} <${email}>`,
subject: 'Gradido: E-Mail Überprüfung',
@ -492,7 +492,6 @@ export class UserResolver {
Mit freundlichen Grüßen,
dein Gradido-Team`,
})
return emailSent
}
@Mutation(() => Boolean)