mirror of https://github.com/Chocobozzz/PeerTube
Introduce contact form command
parent
2d1ad5b960
commit
a9c58393d3
|
@ -1,10 +1,9 @@
|
||||||
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
|
||||||
|
|
||||||
import 'mocha'
|
import 'mocha'
|
||||||
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
import { HttpStatusCode } from '@shared/core-utils'
|
||||||
import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils'
|
import { cleanupTests, flushAndRunServer, killallServers, MockSmtpServer, reRunServer, ServerInfo } from '@shared/extra-utils'
|
||||||
import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
|
import { ContactFormCommand } from '@shared/extra-utils/server'
|
||||||
import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
|
|
||||||
|
|
||||||
describe('Test contact form API validators', function () {
|
describe('Test contact form API validators', function () {
|
||||||
let server: ServerInfo
|
let server: ServerInfo
|
||||||
|
@ -16,6 +15,7 @@ describe('Test contact form API validators', function () {
|
||||||
body: 'Hello, how are you?'
|
body: 'Hello, how are you?'
|
||||||
}
|
}
|
||||||
let emailPort: number
|
let emailPort: number
|
||||||
|
let command: ContactFormCommand
|
||||||
|
|
||||||
// ---------------------------------------------------------------
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -26,10 +26,11 @@ describe('Test contact form API validators', function () {
|
||||||
|
|
||||||
// Email is disabled
|
// Email is disabled
|
||||||
server = await flushAndRunServer(1)
|
server = await flushAndRunServer(1)
|
||||||
|
command = server.contactFormCommand
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should not accept a contact form if emails are disabled', async function () {
|
it('Should not accept a contact form if emails are disabled', async function () {
|
||||||
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
|
await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should not accept a contact form if it is disabled in the configuration', async function () {
|
it('Should not accept a contact form if it is disabled in the configuration', async function () {
|
||||||
|
@ -39,7 +40,7 @@ describe('Test contact form API validators', function () {
|
||||||
|
|
||||||
// Contact form is disabled
|
// Contact form is disabled
|
||||||
await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
|
await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
|
||||||
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 }))
|
await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should not accept a contact form if from email is invalid', async function () {
|
it('Should not accept a contact form if from email is invalid', async function () {
|
||||||
|
@ -50,61 +51,25 @@ describe('Test contact form API validators', function () {
|
||||||
// Email & contact form enabled
|
// Email & contact form enabled
|
||||||
await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
|
await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } })
|
||||||
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
await command.send({ ...defaultBody, fromEmail: 'badEmail', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
url: server.url,
|
await command.send({ ...defaultBody, fromEmail: 'badEmail@', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
await command.send({ ...defaultBody, fromEmail: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
fromEmail: 'badEmail'
|
|
||||||
}))
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
|
||||||
url: server.url,
|
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
|
||||||
fromEmail: 'badEmail@'
|
|
||||||
}))
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
|
||||||
url: server.url,
|
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
|
||||||
fromEmail: undefined
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should not accept a contact form if from name is invalid', async function () {
|
it('Should not accept a contact form if from name is invalid', async function () {
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
await command.send({ ...defaultBody, fromName: 'name'.repeat(100), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
url: server.url,
|
await command.send({ ...defaultBody, fromName: '', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
await command.send({ ...defaultBody, fromName: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
fromName: 'name'.repeat(100)
|
|
||||||
}))
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
|
||||||
url: server.url,
|
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
|
||||||
fromName: ''
|
|
||||||
}))
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
|
||||||
url: server.url,
|
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
|
||||||
fromName: undefined
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should not accept a contact form if body is invalid', async function () {
|
it('Should not accept a contact form if body is invalid', async function () {
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
await command.send({ ...defaultBody, body: 'body'.repeat(5000), expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
url: server.url,
|
await command.send({ ...defaultBody, body: 'a', expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
await command.send({ ...defaultBody, body: undefined, expectedStatus: HttpStatusCode.BAD_REQUEST_400 })
|
||||||
body: 'body'.repeat(5000)
|
|
||||||
}))
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
|
||||||
url: server.url,
|
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
|
||||||
body: 'a'
|
|
||||||
}))
|
|
||||||
await sendContactForm(immutableAssign(defaultBody, {
|
|
||||||
url: server.url,
|
|
||||||
expectedStatus: HttpStatusCode.BAD_REQUEST_400,
|
|
||||||
body: undefined
|
|
||||||
}))
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should accept a contact form with the correct parameters', async function () {
|
it('Should accept a contact form with the correct parameters', async function () {
|
||||||
await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
|
await command.send({ ...defaultBody })
|
||||||
})
|
})
|
||||||
|
|
||||||
after(async function () {
|
after(async function () {
|
||||||
|
|
|
@ -2,17 +2,16 @@
|
||||||
|
|
||||||
import 'mocha'
|
import 'mocha'
|
||||||
import * as chai from 'chai'
|
import * as chai from 'chai'
|
||||||
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
import { HttpStatusCode } from '@shared/core-utils'
|
||||||
import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers, wait } from '../../../../shared/extra-utils'
|
import { cleanupTests, flushAndRunServer, MockSmtpServer, ServerInfo, setAccessTokensToServers, wait, waitJobs } from '@shared/extra-utils'
|
||||||
import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email'
|
import { ContactFormCommand } from '@shared/extra-utils/server'
|
||||||
import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
|
|
||||||
import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
|
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
|
|
||||||
describe('Test contact form', function () {
|
describe('Test contact form', function () {
|
||||||
let server: ServerInfo
|
let server: ServerInfo
|
||||||
const emails: object[] = []
|
const emails: object[] = []
|
||||||
|
let command: ContactFormCommand
|
||||||
|
|
||||||
before(async function () {
|
before(async function () {
|
||||||
this.timeout(30000)
|
this.timeout(30000)
|
||||||
|
@ -27,13 +26,14 @@ describe('Test contact form', function () {
|
||||||
}
|
}
|
||||||
server = await flushAndRunServer(1, overrideConfig)
|
server = await flushAndRunServer(1, overrideConfig)
|
||||||
await setAccessTokensToServers([ server ])
|
await setAccessTokensToServers([ server ])
|
||||||
|
|
||||||
|
command = server.contactFormCommand
|
||||||
})
|
})
|
||||||
|
|
||||||
it('Should send a contact form', async function () {
|
it('Should send a contact form', async function () {
|
||||||
this.timeout(10000)
|
this.timeout(10000)
|
||||||
|
|
||||||
await sendContactForm({
|
await command.send({
|
||||||
url: server.url,
|
|
||||||
fromEmail: 'toto@example.com',
|
fromEmail: 'toto@example.com',
|
||||||
body: 'my super message',
|
body: 'my super message',
|
||||||
subject: 'my subject',
|
subject: 'my subject',
|
||||||
|
@ -58,16 +58,14 @@ describe('Test contact form', function () {
|
||||||
|
|
||||||
await wait(1000)
|
await wait(1000)
|
||||||
|
|
||||||
await sendContactForm({
|
await command.send({
|
||||||
url: server.url,
|
|
||||||
fromEmail: 'toto@example.com',
|
fromEmail: 'toto@example.com',
|
||||||
body: 'my super message',
|
body: 'my super message',
|
||||||
subject: 'my subject',
|
subject: 'my subject',
|
||||||
fromName: 'Super toto'
|
fromName: 'Super toto'
|
||||||
})
|
})
|
||||||
|
|
||||||
await sendContactForm({
|
await command.send({
|
||||||
url: server.url,
|
|
||||||
fromEmail: 'toto@example.com',
|
fromEmail: 'toto@example.com',
|
||||||
body: 'my super message',
|
body: 'my super message',
|
||||||
fromName: 'Super toto',
|
fromName: 'Super toto',
|
||||||
|
@ -79,8 +77,7 @@ describe('Test contact form', function () {
|
||||||
it('Should be able to send another contact form after a while', async function () {
|
it('Should be able to send another contact form after a while', async function () {
|
||||||
await wait(1000)
|
await wait(1000)
|
||||||
|
|
||||||
await sendContactForm({
|
await command.send({
|
||||||
url: server.url,
|
|
||||||
fromEmail: 'toto@example.com',
|
fromEmail: 'toto@example.com',
|
||||||
fromName: 'Super toto',
|
fromName: 'Super toto',
|
||||||
subject: 'my subject',
|
subject: 'my subject',
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
|
||||||
|
import { ContactForm } from '../../models/server'
|
||||||
|
import { AbstractCommand, OverrideCommandOptions } from '../shared'
|
||||||
|
|
||||||
|
export class ContactFormCommand extends AbstractCommand {
|
||||||
|
|
||||||
|
send (options: OverrideCommandOptions & {
|
||||||
|
fromEmail: string
|
||||||
|
fromName: string
|
||||||
|
subject: string
|
||||||
|
body: string
|
||||||
|
}) {
|
||||||
|
const path = '/api/v1/server/contact'
|
||||||
|
|
||||||
|
const body: ContactForm = {
|
||||||
|
fromEmail: options.fromEmail,
|
||||||
|
fromName: options.fromName,
|
||||||
|
subject: options.subject,
|
||||||
|
body: options.body
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.postBodyRequest({
|
||||||
|
...options,
|
||||||
|
|
||||||
|
path,
|
||||||
|
token: null,
|
||||||
|
fields: body,
|
||||||
|
defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,31 +0,0 @@
|
||||||
import * as request from 'supertest'
|
|
||||||
import { ContactForm } from '../../models/server'
|
|
||||||
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
|
||||||
|
|
||||||
function sendContactForm (options: {
|
|
||||||
url: string
|
|
||||||
fromEmail: string
|
|
||||||
fromName: string
|
|
||||||
subject: string
|
|
||||||
body: string
|
|
||||||
expectedStatus?: number
|
|
||||||
}) {
|
|
||||||
const path = '/api/v1/server/contact'
|
|
||||||
|
|
||||||
const body: ContactForm = {
|
|
||||||
fromEmail: options.fromEmail,
|
|
||||||
fromName: options.fromName,
|
|
||||||
subject: options.subject,
|
|
||||||
body: options.body
|
|
||||||
}
|
|
||||||
return request(options.url)
|
|
||||||
.post(path)
|
|
||||||
.send(body)
|
|
||||||
.expect(options.expectedStatus || HttpStatusCode.NO_CONTENT_204)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
export {
|
|
||||||
sendContactForm
|
|
||||||
}
|
|
|
@ -0,0 +1 @@
|
||||||
|
export * from './contact-form-command'
|
|
@ -16,6 +16,7 @@ import { AbusesCommand } from '../moderation'
|
||||||
import { OverviewsCommand } from '../overviews'
|
import { OverviewsCommand } from '../overviews'
|
||||||
import { makeGetRequest } from '../requests/requests'
|
import { makeGetRequest } from '../requests/requests'
|
||||||
import { SearchCommand } from '../search'
|
import { SearchCommand } from '../search'
|
||||||
|
import { ContactFormCommand } from './contact-form-command'
|
||||||
|
|
||||||
interface ServerInfo {
|
interface ServerInfo {
|
||||||
app: ChildProcess
|
app: ChildProcess
|
||||||
|
@ -77,6 +78,7 @@ interface ServerInfo {
|
||||||
abusesCommand?: AbusesCommand
|
abusesCommand?: AbusesCommand
|
||||||
overviewsCommand?: OverviewsCommand
|
overviewsCommand?: OverviewsCommand
|
||||||
searchCommand?: SearchCommand
|
searchCommand?: SearchCommand
|
||||||
|
contactFormCommand?: ContactFormCommand
|
||||||
}
|
}
|
||||||
|
|
||||||
function parallelTests () {
|
function parallelTests () {
|
||||||
|
@ -290,6 +292,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
|
||||||
server.abusesCommand = new AbusesCommand(server)
|
server.abusesCommand = new AbusesCommand(server)
|
||||||
server.overviewsCommand = new OverviewsCommand(server)
|
server.overviewsCommand = new OverviewsCommand(server)
|
||||||
server.searchCommand = new SearchCommand(server)
|
server.searchCommand = new SearchCommand(server)
|
||||||
|
server.contactFormCommand = new ContactFormCommand(server)
|
||||||
|
|
||||||
res(server)
|
res(server)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue