1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { MockSmtpServer } from '@server/tests/shared'
4 import { HttpStatusCode } from '@shared/models'
12 } from '@shared/server-commands'
14 describe('Test contact form API validators', function () {
15 let server: PeerTubeServer
16 const emails: object[] = []
18 fromName: 'super name',
19 fromEmail: 'toto@example.com',
20 subject: 'my subject',
21 body: 'Hello, how are you?'
24 let command: ContactFormCommand
26 // ---------------------------------------------------------------
28 before(async function () {
31 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
34 server = await createSingleServer(1)
35 command = server.contactForm
38 it('Should not accept a contact form if emails are disabled', async function () {
39 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
42 it('Should not accept a contact form if it is disabled in the configuration', async function () {
45 await killallServers([ server ])
47 // Contact form is disabled
48 await server.run({ ...ConfigCommand.getEmailOverrideConfig(emailPort), contact_form: { enabled: false } })
49 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
52 it('Should not accept a contact form if from email is invalid', async function () {
55 await killallServers([ server ])
57 // Email & contact form enabled
58 await server.run(ConfigCommand.getEmailOverrideConfig(emailPort))
60 await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
61 await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
62 await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
65 it('Should not accept a contact form if from name is invalid', async function () {
66 await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
67 await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
68 await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
71 it('Should not accept a contact form if body is invalid', async function () {
72 await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
73 await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
74 await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
77 it('Should accept a contact form with the correct parameters', async function () {
78 await command.send(defaultBody)
81 after(async function () {
82 MockSmtpServer.Instance.kill()
84 await cleanupTests([ server ])