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