]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/contact-form.ts
Fix CLI tools
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / contact-form.ts
CommitLineData
a1587156 1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
a4101923
C
2
3import 'mocha'
a9c58393
C
4import { HttpStatusCode } from '@shared/core-utils'
5import { cleanupTests, flushAndRunServer, killallServers, MockSmtpServer, reRunServer, ServerInfo } from '@shared/extra-utils'
6import { ContactFormCommand } from '@shared/extra-utils/server'
a4101923
C
7
8describe('Test contact form API validators', function () {
9 let server: ServerInfo
10 const emails: object[] = []
11 const defaultBody = {
12 fromName: 'super name',
13 fromEmail: 'toto@example.com',
4e9fa5b7 14 subject: 'my subject',
a4101923
C
15 body: 'Hello, how are you?'
16 }
7c3b7976 17 let emailPort: number
a9c58393 18 let command: ContactFormCommand
a4101923
C
19
20 // ---------------------------------------------------------------
21
22 before(async function () {
23 this.timeout(60000)
24
7c3b7976 25 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
a4101923
C
26
27 // Email is disabled
210feb6c 28 server = await flushAndRunServer(1)
a9c58393 29 command = server.contactFormCommand
a4101923
C
30 })
31
32 it('Should not accept a contact form if emails are disabled', async function () {
a9c58393 33 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
a4101923
C
34 })
35
36 it('Should not accept a contact form if it is disabled in the configuration', async function () {
848f499d
C
37 this.timeout(10000)
38
a4101923
C
39 killallServers([ server ])
40
41 // Contact form is disabled
7c3b7976 42 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
a9c58393 43 await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
a4101923
C
44 })
45
46 it('Should not accept a contact form if from email is invalid', async function () {
848f499d
C
47 this.timeout(10000)
48
a4101923
C
49 killallServers([ server ])
50
51 // Email & contact form enabled
7c3b7976 52 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
a4101923 53
a9c58393
C
54 await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
55 await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
56 await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
a4101923
C
57 })
58
59 it('Should not accept a contact form if from name is invalid', async function () {
a9c58393
C
60 await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
61 await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
62 await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
a4101923
C
63 })
64
65 it('Should not accept a contact form if body is invalid', async function () {
a9c58393
C
66 await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
67 await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
68 await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
a4101923
C
69 })
70
71 it('Should accept a contact form with the correct parameters', async function () {
a9c58393 72 await command.send({ ...defaultBody })
a4101923
C
73 })
74
7c3b7976 75 after(async function () {
a4101923 76 MockSmtpServer.Instance.kill()
7c3b7976 77
8519cc92 78 await cleanupTests([ server ])
a4101923
C
79 })
80})