Guest User

Untitled

a guest
Oct 16th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import * as nm from 'nodemailer'
  2. import config from '../config/environment'
  3.  
  4. class Mailer {
  5.  
  6. private mailConfig: nm.SentMessageInfo
  7. private mailOptions: nm.SendMailOptions
  8.  
  9. public send = (
  10. to: string[],
  11. subject: string,
  12. template: string
  13. ): void | string[] => {
  14. const dest: string = to.join(',')
  15. const text: string = template
  16. this.sendDev(dest, subject, text)
  17. }
  18.  
  19. private sendDev = (
  20. dest: string,
  21. subject: string,
  22. text: string
  23. ): void | string[] => {
  24. nm.createTestAccount((err: Error, account: nm.TestAccount) => {
  25. this.mailConfig = this.setMailConfig(account)
  26. const transporter: nm.Transporter =
  27. nm.createTransport(this.mailConfig)
  28. this.mailOptions = this.setMailOptions(dest, subject, text)
  29. transporter.sendMail(
  30. this.mailOptions,
  31. (error: Error, info: nm.SentMessageInfo
  32. ) => {
  33. if (config.env !== 'test') {
  34. // tslint:disable-next-line:no-console
  35. console.log('Preview URL: %s', nm.getTestMessageUrl(info))
  36. }
  37. })
  38. })
  39. }
  40.  
  41. private setMailConfig = (
  42. account: nm.TestAccount = null
  43. ): nm.SentMessageInfo => {
  44. return {
  45. host: config.mail.host,
  46. port: 587,
  47. auth: {
  48. user: account.user
  49. pass: account.pass
  50. }
  51. }
  52. }
  53.  
  54. private setMailOptions = (
  55. dest: string,
  56. subject: string,
  57. text: string
  58. ): nm.SendMailOptions => {
  59. return {
  60. from: '"Example" <contact@example.com>',
  61. to: dest,
  62. subject: subject,
  63. html: text
  64. }
  65. }
  66. }
  67.  
  68. export default new Mailer()
  69.  
  70. private saveTokenAndSendMail(user: IUser): Promise<object> {
  71. const token: IToken = new Token({
  72. userId: user.id,
  73. token: new Types.ObjectId()
  74. })
  75. return Token.create(token).then((newToken: IToken) => {
  76. const msg: string = 'Hello,nn' +
  77. 'Please verify your account by clicking the link: n'
  78. + config.domain
  79. + '/confirm-account/'
  80. + token.token + '.n'
  81. + ' Link expires in 12 hours'
  82. Mailer.send([user.mail], 'Account verification', msg)
  83. return {
  84. id: user.id,
  85. message: 'Verification link was sent to ' + user.mail
  86. }
  87. })
  88. }
  89.  
  90. import { expect } from 'chai'
  91. import * as supertest from 'supertest'
  92. import app from '../../../app'
  93. import { User } from './../user.model'
  94. import config from '../../../config/environment'
  95. import { IMockUser } from '../../mock.interface'
  96. import * as nmMock from 'nodemailer-mock'
  97. import * as mockery from 'mockery'
  98.  
  99. describe('GET USER ressource', () => {
  100.  
  101. const request: supertest.SuperTest<supertest.Test> = supertest(app)
  102.  
  103. before(async () => {
  104. await User.remove({})
  105. mockery.enable({warnOnUnregistered: false})
  106. mockery.registerMock('nodemailer', nmMock)
  107. })
  108.  
  109. afterEach(async () => {
  110. await User.remove({})
  111. nmMock.mock.reset()
  112. })
  113.  
  114. const mockUser1: IMockUser =
  115. Object.freeze({ login: 'user1', mail: 'user@mail.com', password: '123456789' })
  116. const mockUser2: IMockUser =
  117. Object.freeze({ login: 'user2', mail: 'user1@mail.com', password: '123456789' })
  118.  
  119. describe('GET ' + config.apiVersion + 'user/confirm', () => {
  120.  
  121. it('TODO : activate user', async () => {
  122. await request
  123. .post(config.apiVersion + 'user')
  124. .send(mockUser1)
  125. console.log(nmMock.mock.sentMail()) // Empty array
  126. let login: supertest.Response = await request
  127. .post(config.apiVersion + 'user/login')
  128. .send(mockUser1)
  129. await request.put(config.apiVersion + 'user/')
  130. .send({ isVerified: false})
  131. .set('Authorization', login.body.token)
  132. login = await request
  133. .post(config.apiVersion + 'user/login')
  134. .send(mockUser1)
  135. expect(login.status).to.equal(404)
  136. })
  137. })
  138.  
  139. after(async () => {
  140. await User.remove({})
  141. })
  142. })
Add Comment
Please, Sign In to add comment