1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
5 import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils'
6 import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
7 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
8 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
10 describe('Test contact form API validators', function () {
11 let server: ServerInfo
12 const emails: object[] = []
14 fromName: 'super name',
15 fromEmail: 'toto@example.com',
16 subject: 'my subject',
17 body: 'Hello, how are you?'
21 // ---------------------------------------------------------------
23 before(async function () {
26 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
29 server = await flushAndRunServer(1)
32 it('Should not accept a contact form if emails are disabled', async function () {
33 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
36 it('Should not accept a contact form if it is disabled in the configuration', async function () {
39 killallServers([ server ])
41 // Contact form is disabled
42 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
43 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
46 it('Should not accept a contact form if from email is invalid', async function () {
49 killallServers([ server ])
51 // Email & contact form enabled
52 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
54 await sendContactForm(immutableAssign(defaultBody, {
56 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
59 await sendContactForm(immutableAssign(defaultBody, {
61 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
62 fromEmail: 'badEmail@'
64 await sendContactForm(immutableAssign(defaultBody, {
66 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
71 it('Should not accept a contact form if from name is invalid', async function () {
72 await sendContactForm(immutableAssign(defaultBody, {
74 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
75 fromName: 'name'.repeat(100)
77 await sendContactForm(immutableAssign(defaultBody, {
79 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
82 await sendContactForm(immutableAssign(defaultBody, {
84 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
89 it('Should not accept a contact form if body is invalid', async function () {
90 await sendContactForm(immutableAssign(defaultBody, {
92 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
93 body: 'body'.repeat(5000)
95 await sendContactForm(immutableAssign(defaultBody, {
97 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
100 await sendContactForm(immutableAssign(defaultBody, {
102 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
107 it('Should accept a contact form with the correct parameters', async function () {
108 await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
111 after(async function () {
112 MockSmtpServer.Instance.kill()
114 await cleanupTests([ server ])