]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/check-params/contact-form.ts
b2126b9b09981b7d80771d409a1ea743d86a2f83
[github/Chocobozzz/PeerTube.git] / server / tests / api / check-params / contact-form.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import 'mocha'
4
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
9 describe('Test contact form API validators', function () {
10 let server: ServerInfo
11 const emails: object[] = []
12 const defaultBody = {
13 fromName: 'super name',
14 fromEmail: 'toto@example.com',
15 subject: 'my subject',
16 body: 'Hello, how are you?'
17 }
18 let emailPort: number
19
20 // ---------------------------------------------------------------
21
22 before(async function () {
23 this.timeout(60000)
24
25 emailPort = await MockSmtpServer.Instance.collectEmails(emails)
26
27 // Email is disabled
28 server = await flushAndRunServer(1)
29 })
30
31 it('Should not accept a contact form if emails are disabled', async function () {
32 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
33 })
34
35 it('Should not accept a contact form if it is disabled in the configuration', async function () {
36 this.timeout(10000)
37
38 killallServers([ server ])
39
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: 409 }))
43 })
44
45 it('Should not accept a contact form if from email is invalid', async function () {
46 this.timeout(10000)
47
48 killallServers([ server ])
49
50 // Email & contact form enabled
51 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
52
53 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail' }))
54 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail@' }))
55 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: undefined }))
56 })
57
58 it('Should not accept a contact form if from name is invalid', async function () {
59 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: 'name'.repeat(100) }))
60 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: '' }))
61 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: undefined }))
62 })
63
64 it('Should not accept a contact form if body is invalid', async function () {
65 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'body'.repeat(5000) }))
66 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'a' }))
67 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: undefined }))
68 })
69
70 it('Should accept a contact form with the correct parameters', async function () {
71 await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
72 })
73
74 after(async function () {
75 MockSmtpServer.Instance.kill()
76
77 await cleanupTests([ server ])
78 })
79 })