contact-form.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* tslint:disable:no-unused-expression */
  2. import 'mocha'
  3. import {
  4. flushTests,
  5. immutableAssign,
  6. killallServers,
  7. reRunServer,
  8. flushAndRunServer,
  9. ServerInfo,
  10. setAccessTokensToServers, cleanupTests
  11. } from '../../../../shared/extra-utils'
  12. import {
  13. checkBadCountPagination,
  14. checkBadSortPagination,
  15. checkBadStartPagination
  16. } from '../../../../shared/extra-utils/requests/check-api-params'
  17. import { getAccount } from '../../../../shared/extra-utils/users/accounts'
  18. import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
  19. import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
  20. describe('Test contact form API validators', function () {
  21. let server: ServerInfo
  22. const emails: object[] = []
  23. const defaultBody = {
  24. fromName: 'super name',
  25. fromEmail: 'toto@example.com',
  26. body: 'Hello, how are you?'
  27. }
  28. let emailPort: number
  29. // ---------------------------------------------------------------
  30. before(async function () {
  31. this.timeout(60000)
  32. emailPort = await MockSmtpServer.Instance.collectEmails(emails)
  33. // Email is disabled
  34. server = await flushAndRunServer(1)
  35. })
  36. it('Should not accept a contact form if emails are disabled', async function () {
  37. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
  38. })
  39. it('Should not accept a contact form if it is disabled in the configuration', async function () {
  40. this.timeout(10000)
  41. killallServers([ server ])
  42. // Contact form is disabled
  43. await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
  44. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
  45. })
  46. it('Should not accept a contact form if from email is invalid', async function () {
  47. this.timeout(10000)
  48. killallServers([ server ])
  49. // Email & contact form enabled
  50. await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
  51. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail' }))
  52. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail@' }))
  53. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: undefined }))
  54. })
  55. it('Should not accept a contact form if from name is invalid', async function () {
  56. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: 'name'.repeat(100) }))
  57. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: '' }))
  58. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: undefined }))
  59. })
  60. it('Should not accept a contact form if body is invalid', async function () {
  61. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'body'.repeat(5000) }))
  62. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'a' }))
  63. await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: undefined }))
  64. })
  65. it('Should accept a contact form with the correct parameters', async function () {
  66. await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
  67. })
  68. after(async function () {
  69. MockSmtpServer.Instance.kill()
  70. await cleanupTests([ server ])
  71. })
  72. })