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