]>
Commit | Line | Data |
---|---|---|
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 | import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' | |
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', | |
16 | subject: 'my subject', | |
17 | body: 'Hello, how are you?' | |
18 | } | |
19 | let emailPort: number | |
20 | ||
21 | // --------------------------------------------------------------- | |
22 | ||
23 | before(async function () { | |
24 | this.timeout(60000) | |
25 | ||
26 | emailPort = await MockSmtpServer.Instance.collectEmails(emails) | |
27 | ||
28 | // Email is disabled | |
29 | server = await flushAndRunServer(1) | |
30 | }) | |
31 | ||
32 | it('Should not accept a contact form if emails are disabled', async function () { | |
33 | await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) | |
34 | }) | |
35 | ||
36 | it('Should not accept a contact form if it is disabled in the configuration', async function () { | |
37 | this.timeout(10000) | |
38 | ||
39 | killallServers([ server ]) | |
40 | ||
41 | // Contact form is disabled | |
42 | await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) | |
43 | await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) | |
44 | }) | |
45 | ||
46 | it('Should not accept a contact form if from email is invalid', async function () { | |
47 | this.timeout(10000) | |
48 | ||
49 | killallServers([ server ]) | |
50 | ||
51 | // Email & contact form enabled | |
52 | await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } }) | |
53 | ||
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 | })) | |
69 | }) | |
70 | ||
71 | it('Should not accept a contact form if from name is invalid', async function () { | |
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 | })) | |
87 | }) | |
88 | ||
89 | it('Should not accept a contact form if body is invalid', async function () { | |
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 | })) | |
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 | ||
111 | after(async function () { | |
112 | MockSmtpServer.Instance.kill() | |
113 | ||
114 | await cleanupTests([ server ]) | |
115 | }) | |
116 | }) |