From a4101923e699e49ceb9ff36e971c75417fafc9f0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 9 Jan 2019 15:14:29 +0100 Subject: Implement contact form on server side --- server/tests/api/check-params/config.ts | 3 + server/tests/api/check-params/contact-form.ts | 92 +++++++++++++++++++++++++++ server/tests/api/check-params/index.ts | 2 +- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 server/tests/api/check-params/contact-form.ts (limited to 'server/tests/api/check-params') diff --git a/server/tests/api/check-params/config.ts b/server/tests/api/check-params/config.ts index b7bf41b58..4038ecbf0 100644 --- a/server/tests/api/check-params/config.ts +++ b/server/tests/api/check-params/config.ts @@ -48,6 +48,9 @@ describe('Test config API validators', function () { admin: { email: 'superadmin1@example.com' }, + contactForm: { + enabled: false + }, user: { videoQuota: 5242881, videoQuotaDaily: 318742 diff --git a/server/tests/api/check-params/contact-form.ts b/server/tests/api/check-params/contact-form.ts new file mode 100644 index 000000000..2407ac0b5 --- /dev/null +++ b/server/tests/api/check-params/contact-form.ts @@ -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() + } + }) +}) diff --git a/server/tests/api/check-params/index.ts b/server/tests/api/check-params/index.ts index 7a181d1d6..77c17036a 100644 --- a/server/tests/api/check-params/index.ts +++ b/server/tests/api/check-params/index.ts @@ -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' -- cgit v1.2.3