]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/check-params/contact-form.ts
Introduce abuse command
[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'
8ef9457f 4import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
a1587156 5import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils'
8ef9457f 6import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
94565d52 7import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
a4101923
C
8
9describe('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',
4e9fa5b7 15 subject: 'my subject',
a4101923
C
16 body: 'Hello, how are you?'
17 }
7c3b7976 18 let emailPort: number
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)
a4101923
C
29 })
30
31 it('Should not accept a contact form if emails are disabled', async function () {
f2eb23cd 32 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
a4101923
C
33 })
34
35 it('Should not accept a contact form if it is disabled in the configuration', async function () {
848f499d
C
36 this.timeout(10000)
37
a4101923
C
38 killallServers([ server ])
39
40 // Contact form is disabled
7c3b7976 41 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
f2eb23cd 42 await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
a4101923
C
43 })
44
45 it('Should not accept a contact form if from email is invalid', async function () {
848f499d
C
46 this.timeout(10000)
47
a4101923
C
48 killallServers([ server ])
49
50 // Email & contact form enabled
7c3b7976 51 await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
a4101923 52
f2eb23cd
RK
53 await sendContactForm(immutableAssign(defaultBody, {
54 url: server.url,
55 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
56 fromEmail: 'badEmail'
57 }))
58 await sendContactForm(immutableAssign(defaultBody, {
59 url: server.url,
60 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
61 fromEmail: 'badEmail@'
62 }))
63 await sendContactForm(immutableAssign(defaultBody, {
64 url: server.url,
65 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
66 fromEmail: undefined
67 }))
a4101923
C
68 })
69
70 it('Should not accept a contact form if from name is invalid', async function () {
f2eb23cd
RK
71 await sendContactForm(immutableAssign(defaultBody, {
72 url: server.url,
73 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
74 fromName: 'name'.repeat(100)
75 }))
76 await sendContactForm(immutableAssign(defaultBody, {
77 url: server.url,
78 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
79 fromName: ''
80 }))
81 await sendContactForm(immutableAssign(defaultBody, {
82 url: server.url,
83 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
84 fromName: undefined
85 }))
a4101923
C
86 })
87
88 it('Should not accept a contact form if body is invalid', async function () {
f2eb23cd
RK
89 await sendContactForm(immutableAssign(defaultBody, {
90 url: server.url,
91 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
92 body: 'body'.repeat(5000)
93 }))
94 await sendContactForm(immutableAssign(defaultBody, {
95 url: server.url,
96 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
97 body: 'a'
98 }))
99 await sendContactForm(immutableAssign(defaultBody, {
100 url: server.url,
101 expectedStatus: HttpStatusCode.BAD_REQUEST_400,
102 body: undefined
103 }))
a4101923
C
104 })
105
106 it('Should accept a contact form with the correct parameters', async function () {
107 await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
108 })
109
7c3b7976 110 after(async function () {
a4101923 111 MockSmtpServer.Instance.kill()
7c3b7976 112
8519cc92 113 await cleanupTests([ server ])
a4101923
C
114 })
115})