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'
5 import { cleanupTests, ContactFormCommand, createSingleServer, killallServers, PeerTubeServer } from '@shared/server-commands'
7 describe('Test contact form API validators', function () {
8 let server: PeerTubeServer
9 const emails: object[] = []
11 fromName: 'super name',
12 fromEmail: 'toto@example.com',
13 subject: 'my subject',
14 body: 'Hello, how are you?'
17 let command: ContactFormCommand
19 // ---------------------------------------------------------------
21 before(async function () {
24 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
27 server = await createSingleServer(1)
28 command = server.contactForm
31 it('Should not accept a contact form if emails are disabled', async function () {
32 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
35 it('Should not accept a contact form if it is disabled in the configuration', async function () {
38 await killallServers([ server ])
40 // Contact form is disabled
41 await server.run({ smtp: { hostname: '127.0.0.1', port: emailPort }, contact_form: { enabled: false } })
42 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
45 it('Should not accept a contact form if from email is invalid', async function () {
48 await killallServers([ server ])
50 // Email & contact form enabled
51 await server.run({ smtp: { hostname: '127.0.0.1', port: emailPort } })
53 await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
54 await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
55 await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
58 it('Should not accept a contact form if from name is invalid', async function () {
59 await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
60 await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
61 await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
64 it('Should not accept a contact form if body is invalid', async function () {
65 await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
66 await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
67 await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
70 it('Should accept a contact form with the correct parameters', async function () {
71 await command.send(defaultBody)
74 after(async function () {
75 MockSmtpServer.Instance.kill()
77 await cleanupTests([ server ])