]>
Commit | Line | Data |
---|---|---|
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | |
2 | ||
3 | import 'mocha' | |
4 | import { MockSmtpServer } from '@server/tests/shared' | |
5 | import { HttpStatusCode } from '@shared/models' | |
6 | import { cleanupTests, ContactFormCommand, createSingleServer, killallServers, PeerTubeServer } from '@shared/server-commands' | |
7 | ||
8 | describe('Test contact form API validators', function () { | |
9 | let server: PeerTubeServer | |
10 | const emails: object[] = [] | |
11 | const defaultBody = { | |
12 | fromName: 'super name', | |
13 | fromEmail: 'toto@example.com', | |
14 | subject: 'my subject', | |
15 | body: 'Hello, how are you?' | |
16 | } | |
17 | let emailPort: number | |
18 | let command: ContactFormCommand | |
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 createSingleServer(1) | |
29 | command = server.contactForm | |
30 | }) | |
31 | ||
32 | it('Should not accept a contact form if emails are disabled', async function () { | |
33 | await command.send({ ...defaultBody, 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(25000) | |
38 | ||
39 | await killallServers([ server ]) | |
40 | ||
41 | // Contact form is disabled | |
42 | await server.run({ smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) | |
43 | await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 }) | |
44 | }) | |
45 | ||
46 | it('Should not accept a contact form if from email is invalid', async function () { | |
47 | this.timeout(25000) | |
48 | ||
49 | await killallServers([ server ]) | |
50 | ||
51 | // Email & contact form enabled | |
52 | await server.run({ smtp: { hostname: 'localhost', port: emailPort } }) | |
53 | ||
54 | await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
55 | await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
56 | await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
57 | }) | |
58 | ||
59 | it('Should not accept a contact form if from name is invalid', async function () { | |
60 | await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
61 | await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
62 | await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
63 | }) | |
64 | ||
65 | it('Should not accept a contact form if body is invalid', async function () { | |
66 | await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
67 | await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
68 | await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) | |
69 | }) | |
70 | ||
71 | it('Should accept a contact form with the correct parameters', async function () { | |
72 | await command.send(defaultBody) | |
73 | }) | |
74 | ||
75 | after(async function () { | |
76 | MockSmtpServer.Instance.kill() | |
77 | ||
78 | await cleanupTests([ server ]) | |
79 | }) | |
80 | }) |