]>
Commit | Line | Data |
---|---|---|
a1587156 | 1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
a4101923 | 2 | |
c55e3d72 | 3 | import { MockSmtpServer } from '@server/tests/shared' |
4c7e60bc | 4 | import { HttpStatusCode } from '@shared/models' |
c55e3d72 | 5 | import { cleanupTests, ContactFormCommand, createSingleServer, killallServers, PeerTubeServer } from '@shared/server-commands' |
a4101923 C |
6 | |
7 | describe('Test contact form API validators', function () { | |
254d3579 | 8 | let server: PeerTubeServer |
a4101923 C |
9 | const emails: object[] = [] |
10 | const defaultBody = { | |
11 | fromName: 'super name', | |
12 | fromEmail: 'toto@example.com', | |
4e9fa5b7 | 13 | subject: 'my subject', |
a4101923 C |
14 | body: 'Hello, how are you?' |
15 | } | |
7c3b7976 | 16 | let emailPort: number |
a9c58393 | 17 | let command: ContactFormCommand |
a4101923 C |
18 | |
19 | // --------------------------------------------------------------- | |
20 | ||
21 | before(async function () { | |
22 | this.timeout(60000) | |
23 | ||
7c3b7976 | 24 | emailPort = await MockSmtpServer.Instance.collectEmails(emails) |
a4101923 C |
25 | |
26 | // Email is disabled | |
254d3579 | 27 | server = await createSingleServer(1) |
89d241a7 | 28 | command = server.contactForm |
a4101923 C |
29 | }) |
30 | ||
31 | it('Should not accept a contact form if emails are disabled', async function () { | |
a9c58393 | 32 | await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 }) |
a4101923 C |
33 | }) |
34 | ||
35 | it('Should not accept a contact form if it is disabled in the configuration', async function () { | |
f4e75a6f | 36 | this.timeout(25000) |
848f499d | 37 | |
9293139f | 38 | await killallServers([ server ]) |
a4101923 C |
39 | |
40 | // Contact form is disabled | |
254d3579 | 41 | await server.run({ smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) |
a9c58393 | 42 | await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 }) |
a4101923 C |
43 | }) |
44 | ||
45 | it('Should not accept a contact form if from email is invalid', async function () { | |
f4e75a6f | 46 | this.timeout(25000) |
848f499d | 47 | |
9293139f | 48 | await killallServers([ server ]) |
a4101923 C |
49 | |
50 | // Email & contact form enabled | |
254d3579 | 51 | await server.run({ smtp: { hostname: 'localhost', port: emailPort } }) |
a4101923 | 52 | |
a9c58393 C |
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 }) | |
a4101923 C |
56 | }) |
57 | ||
58 | it('Should not accept a contact form if from name is invalid', async function () { | |
a9c58393 C |
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 }) | |
a4101923 C |
62 | }) |
63 | ||
64 | it('Should not accept a contact form if body is invalid', async function () { | |
a9c58393 C |
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 }) | |
a4101923 C |
68 | }) |
69 | ||
70 | it('Should accept a contact form with the correct parameters', async function () { | |
dd0ebb71 | 71 | await command.send(defaultBody) |
a4101923 C |
72 | }) |
73 | ||
7c3b7976 | 74 | after(async function () { |
a4101923 | 75 | MockSmtpServer.Instance.kill() |
7c3b7976 | 76 | |
8519cc92 | 77 | await cleanupTests([ server ]) |
a4101923 C |
78 | }) |
79 | }) |