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