Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import * as nm from 'nodemailer'
- import config from '../config/environment'
- class Mailer {
- private mailConfig: nm.SentMessageInfo
- private mailOptions: nm.SendMailOptions
- public send = (
- to: string[],
- subject: string,
- template: string
- ): void | string[] => {
- const dest: string = to.join(',')
- const text: string = template
- this.sendDev(dest, subject, text)
- }
- private sendDev = (
- dest: string,
- subject: string,
- text: string
- ): void | string[] => {
- nm.createTestAccount((err: Error, account: nm.TestAccount) => {
- this.mailConfig = this.setMailConfig(account)
- const transporter: nm.Transporter =
- nm.createTransport(this.mailConfig)
- this.mailOptions = this.setMailOptions(dest, subject, text)
- transporter.sendMail(
- this.mailOptions,
- (error: Error, info: nm.SentMessageInfo
- ) => {
- if (config.env !== 'test') {
- // tslint:disable-next-line:no-console
- console.log('Preview URL: %s', nm.getTestMessageUrl(info))
- }
- })
- })
- }
- private setMailConfig = (
- account: nm.TestAccount = null
- ): nm.SentMessageInfo => {
- return {
- host: config.mail.host,
- port: 587,
- auth: {
- user: account.user
- pass: account.pass
- }
- }
- }
- private setMailOptions = (
- dest: string,
- subject: string,
- text: string
- ): nm.SendMailOptions => {
- return {
- from: '"Example" <contact@example.com>',
- to: dest,
- subject: subject,
- html: text
- }
- }
- }
- export default new Mailer()
- private saveTokenAndSendMail(user: IUser): Promise<object> {
- const token: IToken = new Token({
- userId: user.id,
- token: new Types.ObjectId()
- })
- return Token.create(token).then((newToken: IToken) => {
- const msg: string = 'Hello,nn' +
- 'Please verify your account by clicking the link: n'
- + config.domain
- + '/confirm-account/'
- + token.token + '.n'
- + ' Link expires in 12 hours'
- Mailer.send([user.mail], 'Account verification', msg)
- return {
- id: user.id,
- message: 'Verification link was sent to ' + user.mail
- }
- })
- }
- import { expect } from 'chai'
- import * as supertest from 'supertest'
- import app from '../../../app'
- import { User } from './../user.model'
- import config from '../../../config/environment'
- import { IMockUser } from '../../mock.interface'
- import * as nmMock from 'nodemailer-mock'
- import * as mockery from 'mockery'
- describe('GET USER ressource', () => {
- const request: supertest.SuperTest<supertest.Test> = supertest(app)
- before(async () => {
- await User.remove({})
- mockery.enable({warnOnUnregistered: false})
- mockery.registerMock('nodemailer', nmMock)
- })
- afterEach(async () => {
- await User.remove({})
- nmMock.mock.reset()
- })
- const mockUser1: IMockUser =
- Object.freeze({ login: 'user1', mail: 'user@mail.com', password: '123456789' })
- const mockUser2: IMockUser =
- Object.freeze({ login: 'user2', mail: 'user1@mail.com', password: '123456789' })
- describe('GET ' + config.apiVersion + 'user/confirm', () => {
- it('TODO : activate user', async () => {
- await request
- .post(config.apiVersion + 'user')
- .send(mockUser1)
- console.log(nmMock.mock.sentMail()) // Empty array
- let login: supertest.Response = await request
- .post(config.apiVersion + 'user/login')
- .send(mockUser1)
- await request.put(config.apiVersion + 'user/')
- .send({ isVerified: false})
- .set('Authorization', login.body.token)
- login = await request
- .post(config.apiVersion + 'user/login')
- .send(mockUser1)
- expect(login.status).to.equal(404)
- })
- })
- after(async () => {
- await User.remove({})
- })
- })
Add Comment
Please, Sign In to add comment