Fix express validator
[oweals/peertube.git] / server / tests / api / check-params / contact-form.ts
1 /* tslint:disable:no-unused-expression */
2
3 import 'mocha'
4
5 import {
6   flushTests,
7   immutableAssign,
8   killallServers,
9   reRunServer,
10   flushAndRunServer,
11   ServerInfo,
12   setAccessTokensToServers, cleanupTests
13 } from '../../../../shared/extra-utils'
14 import {
15   checkBadCountPagination,
16   checkBadSortPagination,
17   checkBadStartPagination
18 } from '../../../../shared/extra-utils/requests/check-api-params'
19 import { getAccount } from '../../../../shared/extra-utils/users/accounts'
20 import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
21 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
22
23 describe('Test contact form API validators', function () {
24   let server: ServerInfo
25   const emails: object[] = []
26   const defaultBody = {
27     fromName: 'super name',
28     fromEmail: 'toto@example.com',
29     subject: 'my subject',
30     body: 'Hello, how are you?'
31   }
32   let emailPort: number
33
34   // ---------------------------------------------------------------
35
36   before(async function () {
37     this.timeout(60000)
38
39     emailPort = await MockSmtpServer.Instance.collectEmails(emails)
40
41     // Email is disabled
42     server = await flushAndRunServer(1)
43   })
44
45   it('Should not accept a contact form if emails are disabled', async function () {
46     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
47   })
48
49   it('Should not accept a contact form if it is disabled in the configuration', async function () {
50     this.timeout(10000)
51
52     killallServers([ server ])
53
54     // Contact form is disabled
55     await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
56     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
57   })
58
59   it('Should not accept a contact form if from email is invalid', async function () {
60     this.timeout(10000)
61
62     killallServers([ server ])
63
64     // Email & contact form enabled
65     await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
66
67     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail' }))
68     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail@' }))
69     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: undefined }))
70   })
71
72   it('Should not accept a contact form if from name is invalid', async function () {
73     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: 'name'.repeat(100) }))
74     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: '' }))
75     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: undefined }))
76   })
77
78   it('Should not accept a contact form if body is invalid', async function () {
79     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'body'.repeat(5000) }))
80     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'a' }))
81     await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: undefined }))
82   })
83
84   it('Should accept a contact form with the correct parameters', async function () {
85     await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
86   })
87
88   after(async function () {
89     MockSmtpServer.Instance.kill()
90
91     await cleanupTests([ server ])
92   })
93 })