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