Implement contact form on server side

This commit is contained in:
Chocobozzz
2019-01-09 15:14:29 +01:00
parent 8d00889b60
commit a4101923e6
32 changed files with 541 additions and 49 deletions

View File

@@ -48,6 +48,9 @@ describe('Test config API validators', function () {
admin: {
email: 'superadmin1@example.com'
},
contactForm: {
enabled: false
},
user: {
videoQuota: 5242881,
videoQuotaDaily: 318742

View File

@@ -0,0 +1,92 @@
/* tslint:disable:no-unused-expression */
import 'mocha'
import {
flushTests,
immutableAssign,
killallServers,
reRunServer,
runServer,
ServerInfo,
setAccessTokensToServers
} from '../../../../shared/utils'
import {
checkBadCountPagination,
checkBadSortPagination,
checkBadStartPagination
} from '../../../../shared/utils/requests/check-api-params'
import { getAccount } from '../../../../shared/utils/users/accounts'
import { sendContactForm } from '../../../../shared/utils/server/contact-form'
import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
describe('Test contact form API validators', function () {
let server: ServerInfo
const emails: object[] = []
const defaultBody = {
fromName: 'super name',
fromEmail: 'toto@example.com',
body: 'Hello, how are you?'
}
// ---------------------------------------------------------------
before(async function () {
this.timeout(60000)
await flushTests()
await MockSmtpServer.Instance.collectEmails(emails)
// Email is disabled
server = await runServer(1)
})
it('Should not accept a contact form if emails are disabled', async function () {
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
})
it('Should not accept a contact form if it is disabled in the configuration', async function () {
killallServers([ server ])
// Contact form is disabled
await reRunServer(server, { smtp: { hostname: 'localhost' }, contact_form: { enabled: false } })
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 }))
})
it('Should not accept a contact form if from email is invalid', async function () {
killallServers([ server ])
// Email & contact form enabled
await reRunServer(server, { smtp: { hostname: 'localhost' } })
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail' }))
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: 'badEmail@' }))
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromEmail: undefined }))
})
it('Should not accept a contact form if from name is invalid', async function () {
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: 'name'.repeat(100) }))
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: '' }))
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, fromName: undefined }))
})
it('Should not accept a contact form if body is invalid', async function () {
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'body'.repeat(5000) }))
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: 'a' }))
await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 400, body: undefined }))
})
it('Should accept a contact form with the correct parameters', async function () {
await sendContactForm(immutableAssign(defaultBody, { url: server.url }))
})
after(async function () {
MockSmtpServer.Instance.kill()
killallServers([ server ])
// Keep the logs if the test failed
if (this['ok']) {
await flushTests()
}
})
})

View File

@@ -1,7 +1,7 @@
// Order of the tests we want to execute
import './accounts'
import './blocklist'
import './config'
import './contact-form'
import './follows'
import './jobs'
import './redundancy'

View File

@@ -33,14 +33,20 @@ function checkInitialConfig (data: CustomConfig) {
expect(data.instance.defaultNSFWPolicy).to.equal('display')
expect(data.instance.customizations.css).to.be.empty
expect(data.instance.customizations.javascript).to.be.empty
expect(data.services.twitter.username).to.equal('@Chocobozzz')
expect(data.services.twitter.whitelisted).to.be.false
expect(data.cache.previews.size).to.equal(1)
expect(data.cache.captions.size).to.equal(1)
expect(data.signup.enabled).to.be.true
expect(data.signup.limit).to.equal(4)
expect(data.signup.requiresEmailVerification).to.be.false
expect(data.admin.email).to.equal('admin1@example.com')
expect(data.contactForm.enabled).to.be.true
expect(data.user.videoQuota).to.equal(5242880)
expect(data.user.videoQuotaDaily).to.equal(-1)
expect(data.transcoding.enabled).to.be.false
@@ -64,16 +70,23 @@ function checkUpdatedConfig (data: CustomConfig) {
expect(data.instance.defaultNSFWPolicy).to.equal('blur')
expect(data.instance.customizations.javascript).to.equal('alert("coucou")')
expect(data.instance.customizations.css).to.equal('body { background-color: red; }')
expect(data.services.twitter.username).to.equal('@Kuja')
expect(data.services.twitter.whitelisted).to.be.true
expect(data.cache.previews.size).to.equal(2)
expect(data.cache.captions.size).to.equal(3)
expect(data.signup.enabled).to.be.false
expect(data.signup.limit).to.equal(5)
expect(data.signup.requiresEmailVerification).to.be.true
expect(data.admin.email).to.equal('superadmin1@example.com')
expect(data.contactForm.enabled).to.be.false
expect(data.user.videoQuota).to.equal(5242881)
expect(data.user.videoQuotaDaily).to.equal(318742)
expect(data.transcoding.enabled).to.be.true
expect(data.transcoding.threads).to.equal(1)
expect(data.transcoding.allowAdditionalExtensions).to.be.true
@@ -82,6 +95,7 @@ function checkUpdatedConfig (data: CustomConfig) {
expect(data.transcoding.resolutions['480p']).to.be.true
expect(data.transcoding.resolutions['720p']).to.be.false
expect(data.transcoding.resolutions['1080p']).to.be.false
expect(data.import.videos.http.enabled).to.be.false
expect(data.import.videos.torrent.enabled).to.be.false
}
@@ -127,6 +141,8 @@ describe('Test config', function () {
expect(data.video.file.extensions).to.contain('.mp4')
expect(data.video.file.extensions).to.contain('.webm')
expect(data.video.file.extensions).to.contain('.ogv')
expect(data.contactForm.enabled).to.be.true
})
it('Should get the customized configuration', async function () {
@@ -172,6 +188,9 @@ describe('Test config', function () {
admin: {
email: 'superadmin1@example.com'
},
contactForm: {
enabled: false
},
user: {
videoQuota: 5242881,
videoQuotaDaily: 318742

View File

@@ -0,0 +1,84 @@
/* tslint:disable:no-unused-expression */
import * as chai from 'chai'
import 'mocha'
import { flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, wait } from '../../../../shared/utils'
import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
import { waitJobs } from '../../../../shared/utils/server/jobs'
import { sendContactForm } from '../../../../shared/utils/server/contact-form'
const expect = chai.expect
describe('Test contact form', function () {
let server: ServerInfo
const emails: object[] = []
before(async function () {
this.timeout(30000)
await MockSmtpServer.Instance.collectEmails(emails)
await flushTests()
const overrideConfig = {
smtp: {
hostname: 'localhost'
}
}
server = await runServer(1, overrideConfig)
await setAccessTokensToServers([ server ])
})
it('Should send a contact form', async function () {
await sendContactForm({
url: server.url,
fromEmail: 'toto@example.com',
body: 'my super message',
fromName: 'Super toto'
})
await waitJobs(server)
expect(emails).to.have.lengthOf(1)
const email = emails[0]
expect(email['from'][0]['address']).equal('toto@example.com')
expect(email['to'][0]['address']).equal('admin1@example.com')
expect(email['subject']).contains('Contact form')
expect(email['text']).contains('my super message')
})
it('Should not be able to send another contact form because of the anti spam checker', async function () {
await sendContactForm({
url: server.url,
fromEmail: 'toto@example.com',
body: 'my super message',
fromName: 'Super toto'
})
await sendContactForm({
url: server.url,
fromEmail: 'toto@example.com',
body: 'my super message',
fromName: 'Super toto',
expectedStatus: 403
})
})
it('Should be able to send another contact form after a while', async function () {
await wait(1000)
await sendContactForm({
url: server.url,
fromEmail: 'toto@example.com',
body: 'my super message',
fromName: 'Super toto'
})
})
after(async function () {
MockSmtpServer.Instance.kill()
killallServers([ server ])
})
})

View File

@@ -8,18 +8,17 @@ import { VideoCommentThreadTree } from '../../../../shared/models/videos/video-c
import {
completeVideoCheck,
getVideo,
immutableAssign,
reRunServer,
unfollow,
viewVideo,
flushAndRunMultipleServers,
getVideo,
getVideosList,
immutableAssign,
killallServers,
reRunServer,
ServerInfo,
setAccessTokensToServers,
uploadVideo,
unfollow,
updateVideo,
uploadVideo,
wait
} from '../../../../shared/utils'
import { follow, getFollowersListPaginationAndSort } from '../../../../shared/utils/server/follows'

View File

@@ -1,4 +1,5 @@
import './config'
import './contact-form'
import './email'
import './follow-constraints'
import './follows'