1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
4 import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
5 import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils'
6 import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
7 import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
9 describe('Test contact form API validators', function () {
10 let server: ServerInfo
11 const emails: object[] = []
13 fromName: 'super name',
14 fromEmail: 'toto@example.com',
15 subject: 'my subject',
16 body: 'Hello, how are you?'
20 // ---------------------------------------------------------------
22 before(async function () {
25 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
28 server = await flushAndRunServer(1)
31 it('Should not accept a contact form if emails are disabled', async function () {
32 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
35 it('Should not accept a contact form if it is disabled in the configuration', async function () {
38 killallServers([ server ])
40 // Contact form is disabled
41 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
42 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
45 it('Should not accept a contact form if from email is invalid', async function () {
48 killallServers([ server ])
50 // Email & contact form enabled
51 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
53 await sendContactForm(immutableAssign(defaultBody, {
55 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
58 await sendContactForm(immutableAssign(defaultBody, {
60 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
61 fromEmail: 'badEmail@'
63 await sendContactForm(immutableAssign(defaultBody, {
65 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
70 it('Should not accept a contact form if from name is invalid', async function () {
71 await sendContactForm(immutableAssign(defaultBody, {
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
74 fromName: 'name'.repeat(100)
76 await sendContactForm(immutableAssign(defaultBody, {
78 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
81 await sendContactForm(immutableAssign(defaultBody, {
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
88 it('Should not accept a contact form if body is invalid', async function () {
89 await sendContactForm(immutableAssign(defaultBody, {
91 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
92 body: 'body'.repeat(5000)
94 await sendContactForm(immutableAssign(defaultBody, {
96 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
99 await sendContactForm(immutableAssign(defaultBody, {
101 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
106 it('Should accept a contact form with the correct parameters', async function () {
107 await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
110 after(async function () {
111 MockSmtpServer.Instance.kill()
113 await cleanupTests([ server ])