aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/api/check-params/contact-form.ts
blob: c7e014b1ff04a90fc296e719522764656c928fd7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* tslint:disable:no-unused-expression */

import 'mocha'

import {
  flushTests,
  immutableAssign,
  killallServers,
  reRunServer,
  runServer,
  ServerInfo,
  setAccessTokensToServers
} from '../../../../shared/utils'
import {
  checkBadCountPagination,
  checkBadSortPagination,
  checkBadStartPagination
} from '../../../../shared/utils/requests/check-api-params'
import { getAccount } from '../../../../shared/utils/users/accounts'
import { sendContactForm } from '../../../../shared/utils/server/contact-form'
import { MockSmtpServer } from '../../../../shared/utils/miscs/email'

describe('Test contact form API validators', function () {
  let server: ServerInfo
  const emails: object[] = []
  const defaultBody = {
    fromName: 'super name',
    fromEmail: 'toto@example.com',
    body: 'Hello, how are you?'
  }

  // ---------------------------------------------------------------

  before(async function () {
    this.timeout(60000)

    await flushTests()
    await MockSmtpServer.Instance.collectEmails(emails)

    // Email is disabled
    server = await runServer(1)
  })

  it('Should not accept a contact form if emails are disabled', async function () {
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
  })

  it('Should not accept a contact form if it is disabled in the configuration', async function () {
    this.timeout(10000)

    killallServers([ server ])

    // Contact form is disabled
    await reRunServer(server, { smtp: { hostname: 'localhost' }, contact_form: { enabled: false } })
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
  })

  it('Should not accept a contact form if from email is invalid', async function () {
    this.timeout(10000)

    killallServers([ server ])

    // Email & contact form enabled
    await reRunServer(server, { smtp: { hostname: 'localhost' } })

    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail' }))
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail@' }))
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: undefined }))
  })

  it('Should not accept a contact form if from name is invalid', async function () {
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: 'name'.repeat(100) }))
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: '' }))
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: undefined }))
  })

  it('Should not accept a contact form if body is invalid', async function () {
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'body'.repeat(5000) }))
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'a' }))
    await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: undefined }))
  })

  it('Should accept a contact form with the correct parameters', async function () {
    await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
  })

  after(async function () {
    MockSmtpServer.Instance.kill()
    killallServers([ server ])

    // Keep the logs if the test failed
    if (this['ok']) {
      await flushTests()
    }
  })
})