]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
a4101923 C |
2 | |
3 | import 'mocha' | |
4 | ||
a1587156 | 5 | import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils' |
94565d52 C |
6 | import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' |
7 | import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' | |
f2eb23cd | 8 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' |
a4101923 C |
9 | |
10 | describe('Test contact form API validators', function () { | |
11 | let server: ServerInfo | |
12 | const emails: object[] = [] | |
13 | const defaultBody = { | |
14 | fromName: 'super name', | |
15 | fromEmail: 'toto@example.com', | |
4e9fa5b7 | 16 | subject: 'my subject', |
a4101923 C |
17 | body: 'Hello, how are you?' |
18 | } | |
7c3b7976 | 19 | let emailPort: number |
a4101923 C |
20 | |
21 | // --------------------------------------------------------------- | |
22 | ||
23 | before(async function () { | |
24 | this.timeout(60000) | |
25 | ||
7c3b7976 | 26 | emailPort = await MockSmtpServer.Instance.collectEmails(emails) |
a4101923 C |
27 | |
28 | // Email is disabled | |
210feb6c | 29 | server = await flushAndRunServer(1) |
a4101923 C |
30 | }) |
31 | ||
32 | it('Should not accept a contact form if emails are disabled', async function () { | |
f2eb23cd | 33 | await sendContactForm(immutableAssign(defaultBody, { url: server.url, 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 } }) |
f2eb23cd | 43 | await sendContactForm(immutableAssign(defaultBody, { url: server.url, 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 | |
f2eb23cd RK |
54 | await sendContactForm(immutableAssign(defaultBody, { |
55 | url: server.url, | |
56 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
57 | fromEmail: 'badEmail' | |
58 | })) | |
59 | await sendContactForm(immutableAssign(defaultBody, { | |
60 | url: server.url, | |
61 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
62 | fromEmail: 'badEmail@' | |
63 | })) | |
64 | await sendContactForm(immutableAssign(defaultBody, { | |
65 | url: server.url, | |
66 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
67 | fromEmail: undefined | |
68 | })) | |
a4101923 C |
69 | }) |
70 | ||
71 | it('Should not accept a contact form if from name is invalid', async function () { | |
f2eb23cd RK |
72 | await sendContactForm(immutableAssign(defaultBody, { |
73 | url: server.url, | |
74 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
75 | fromName: 'name'.repeat(100) | |
76 | })) | |
77 | await sendContactForm(immutableAssign(defaultBody, { | |
78 | url: server.url, | |
79 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
80 | fromName: '' | |
81 | })) | |
82 | await sendContactForm(immutableAssign(defaultBody, { | |
83 | url: server.url, | |
84 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
85 | fromName: undefined | |
86 | })) | |
a4101923 C |
87 | }) |
88 | ||
89 | it('Should not accept a contact form if body is invalid', async function () { | |
f2eb23cd RK |
90 | await sendContactForm(immutableAssign(defaultBody, { |
91 | url: server.url, | |
92 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
93 | body: 'body'.repeat(5000) | |
94 | })) | |
95 | await sendContactForm(immutableAssign(defaultBody, { | |
96 | url: server.url, | |
97 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
98 | body: 'a' | |
99 | })) | |
100 | await sendContactForm(immutableAssign(defaultBody, { | |
101 | url: server.url, | |
102 | expectedStatus: HttpStatusCode.BAD_REQUEST_400, | |
103 | body: undefined | |
104 | })) | |
a4101923 C |
105 | }) |
106 | ||
107 | it('Should accept a contact form with the correct parameters', async function () { | |
108 | await sendContactForm(immutableAssign(defaultBody, { url: server.url })) | |
109 | }) | |
110 | ||
7c3b7976 | 111 | after(async function () { |
a4101923 | 112 | MockSmtpServer.Instance.kill() |
7c3b7976 | 113 | |
8519cc92 | 114 | await cleanupTests([ server ]) |
a4101923 C |
115 | }) |
116 | }) |