1
0
Fork 0
peertube/server/tests/api/check-params/contact-form.ts

81 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-01-31 10:56:52 -05:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-01-09 09:14:29 -05:00
import 'mocha'
2021-07-16 03:47:51 -04:00
import { cleanupTests, createSingleServer, killallServers, MockSmtpServer, PeerTubeServer } from '@shared/extra-utils'
2021-07-06 09:53:25 -04:00
import { ContactFormCommand } from '@shared/extra-utils/server'
2021-07-16 08:27:30 -04:00
import { HttpStatusCode } from '@shared/models'
2019-01-09 09:14:29 -05:00
describe('Test contact form API validators', function () {
2021-07-16 03:47:51 -04:00
let server: PeerTubeServer
2019-01-09 09:14:29 -05:00
const emails: object[] = []
const defaultBody = {
fromName: 'super name',
fromEmail: 'toto@example.com',
subject: 'my subject',
2019-01-09 09:14:29 -05:00
body: 'Hello, how are you?'
}
2019-04-24 09:10:37 -04:00
let emailPort: number
2021-07-06 09:53:25 -04:00
let command: ContactFormCommand
2019-01-09 09:14:29 -05:00
// ---------------------------------------------------------------
before(async function () {
this.timeout(60000)
2019-04-24 09:10:37 -04:00
emailPort = await MockSmtpServer.Instance.collectEmails(emails)
2019-01-09 09:14:29 -05:00
// Email is disabled
2021-07-16 03:47:51 -04:00
server = await createSingleServer(1)
2021-07-16 03:04:35 -04:00
command = server.contactForm
2019-01-09 09:14:29 -05:00
})
it('Should not accept a contact form if emails are disabled', async function () {
2021-07-06 09:53:25 -04:00
await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
2019-01-09 09:14:29 -05:00
})
it('Should not accept a contact form if it is disabled in the configuration', async function () {
2021-09-02 03:11:51 -04:00
this.timeout(25000)
2021-07-09 09:37:43 -04:00
await killallServers([ server ])
2019-01-09 09:14:29 -05:00
// Contact form is disabled
2021-07-16 03:47:51 -04:00
await server.run({ smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
2021-07-06 09:53:25 -04:00
await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
2019-01-09 09:14:29 -05:00
})
it('Should not accept a contact form if from email is invalid', async function () {
2021-09-02 03:11:51 -04:00
this.timeout(25000)
2021-07-09 09:37:43 -04:00
await killallServers([ server ])
2019-01-09 09:14:29 -05:00
// Email & contact form enabled
2021-07-16 03:47:51 -04:00
await server.run({ smtp: { hostname: 'localhost', port: emailPort } })
2019-01-09 09:14:29 -05:00
2021-07-06 09:53:25 -04:00
await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2019-01-09 09:14:29 -05:00
})
it('Should not accept a contact form if from name is invalid', async function () {
2021-07-06 09:53:25 -04:00
await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2019-01-09 09:14:29 -05:00
})
it('Should not accept a contact form if body is invalid', async function () {
2021-07-06 09:53:25 -04:00
await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
2019-01-09 09:14:29 -05:00
})
it('Should accept a contact form with the correct parameters', async function () {
2021-07-09 10:23:01 -04:00
await command.send(defaultBody)
2019-01-09 09:14:29 -05:00
})
2019-04-24 09:10:37 -04:00
after(async function () {
2019-01-09 09:14:29 -05:00
MockSmtpServer.Instance.kill()
2019-04-24 09:10:37 -04:00
2019-04-24 11:19:00 -04:00
await cleanupTests([ server ])
2019-01-09 09:14:29 -05:00
})
})