contact-form.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* tslint:disable:no-unused-expression */
  2. import * as chai from 'chai'
  3. import 'mocha'
  4. import {
  5. flushTests,
  6. killallServers,
  7. flushAndRunServer,
  8. ServerInfo,
  9. setAccessTokensToServers,
  10. wait,
  11. cleanupTests
  12. } from '../../../../shared/extra-utils'
  13. import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
  14. import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
  15. import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
  16. const expect = chai.expect
  17. describe('Test contact form', function () {
  18. let server: ServerInfo
  19. const emails: object[] = []
  20. before(async function () {
  21. this.timeout(30000)
  22. const port = await MockSmtpServer.Instance.collectEmails(emails)
  23. const overrideConfig = {
  24. smtp: {
  25. hostname: 'localhost',
  26. port
  27. }
  28. }
  29. server = await flushAndRunServer(1, overrideConfig)
  30. await setAccessTokensToServers([ server ])
  31. })
  32. it('Should send a contact form', async function () {
  33. this.timeout(10000)
  34. await sendContactForm({
  35. url: server.url,
  36. fromEmail: 'toto@example.com',
  37. body: 'my super message',
  38. subject: 'my subject',
  39. fromName: 'Super toto'
  40. })
  41. await waitJobs(server)
  42. expect(emails).to.have.lengthOf(1)
  43. const email = emails[0]
  44. expect(email['from'][0]['address']).equal('test-admin@localhost')
  45. expect(email['from'][0]['name']).equal('toto@example.com')
  46. expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
  47. expect(email['subject']).contains('my subject')
  48. expect(email['text']).contains('my super message')
  49. })
  50. it('Should not be able to send another contact form because of the anti spam checker', async function () {
  51. await sendContactForm({
  52. url: server.url,
  53. fromEmail: 'toto@example.com',
  54. body: 'my super message',
  55. subject: 'my subject',
  56. fromName: 'Super toto'
  57. })
  58. await sendContactForm({
  59. url: server.url,
  60. fromEmail: 'toto@example.com',
  61. body: 'my super message',
  62. fromName: 'Super toto',
  63. subject: 'my subject',
  64. expectedStatus: 403
  65. })
  66. })
  67. it('Should be able to send another contact form after a while', async function () {
  68. await wait(1000)
  69. await sendContactForm({
  70. url: server.url,
  71. fromEmail: 'toto@example.com',
  72. fromName: 'Super toto',
  73. subject: 'my subject',
  74. body: 'my super message'
  75. })
  76. })
  77. after(async function () {
  78. MockSmtpServer.Instance.kill()
  79. await cleanupTests([ server ])
  80. })
  81. })