From a9c58393d36d221197b48884a1960e6126ab31d7 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 6 Jul 2021 15:53:25 +0200 Subject: Introduce contact form command --- server/tests/api/check-params/contact-form.ts | 69 +++++++-------------------- server/tests/api/server/contact-form.ts | 23 ++++----- 2 files changed, 27 insertions(+), 65 deletions(-) (limited to 'server/tests') diff --git a/server/tests/api/check-params/contact-form.ts b/server/tests/api/check-params/contact-form.ts index 274562cbb..fb30766d9 100644 --- a/server/tests/api/check-params/contact-form.ts +++ b/server/tests/api/check-params/contact-form.ts @@ -1,10 +1,9 @@ /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ import 'mocha' -import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' -import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils' -import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email' -import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' +import { HttpStatusCode } from '@shared/core-utils' +import { cleanupTests, flushAndRunServer, killallServers, MockSmtpServer, reRunServer, ServerInfo } from '@shared/extra-utils' +import { ContactFormCommand } from '@shared/extra-utils/server' describe('Test contact form API validators', function () { let server: ServerInfo @@ -16,6 +15,7 @@ describe('Test contact form API validators', function () { body: 'Hello, how are you?' } let emailPort: number + let command: ContactFormCommand // --------------------------------------------------------------- @@ -26,10 +26,11 @@ describe('Test contact form API validators', function () { // Email is disabled server = await flushAndRunServer(1) + command = server.contactFormCommand }) 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 () { @@ -39,7 +40,7 @@ describe('Test contact form API validators', function () { // Contact form is disabled 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 () { @@ -50,61 +51,25 @@ describe('Test contact form API validators', function () { // Email & contact form enabled await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort } }) - 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: 'badEmail@' - })) - await sendContactForm(immutableAssign(defaultBody, { - url: server.url, - expectedStatus: HttpStatusCode.BAD_REQUEST_400, - fromEmail: undefined - })) + 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 }) }) it('Should not accept a contact form if from name is invalid', async function () { - await sendContactForm(immutableAssign(defaultBody, { - url: server.url, - 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 - })) + 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 }) }) it('Should not accept a contact form if body is invalid', async function () { - await sendContactForm(immutableAssign(defaultBody, { - url: server.url, - 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 - })) + 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 }) }) 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 () { diff --git a/server/tests/api/server/contact-form.ts b/server/tests/api/server/contact-form.ts index 71205723d..79c4c6748 100644 --- a/server/tests/api/server/contact-form.ts +++ b/server/tests/api/server/contact-form.ts @@ -2,17 +2,16 @@ import 'mocha' import * as chai from 'chai' -import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' -import { cleanupTests, flushAndRunServer, ServerInfo, setAccessTokensToServers, wait } from '../../../../shared/extra-utils' -import { MockSmtpServer } from '../../../../shared/extra-utils/mock-servers/mock-email' -import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' -import { waitJobs } from '../../../../shared/extra-utils/server/jobs' +import { HttpStatusCode } from '@shared/core-utils' +import { cleanupTests, flushAndRunServer, MockSmtpServer, ServerInfo, setAccessTokensToServers, wait, waitJobs } from '@shared/extra-utils' +import { ContactFormCommand } from '@shared/extra-utils/server' const expect = chai.expect describe('Test contact form', function () { let server: ServerInfo const emails: object[] = [] + let command: ContactFormCommand before(async function () { this.timeout(30000) @@ -27,13 +26,14 @@ describe('Test contact form', function () { } server = await flushAndRunServer(1, overrideConfig) await setAccessTokensToServers([ server ]) + + command = server.contactFormCommand }) it('Should send a contact form', async function () { this.timeout(10000) - await sendContactForm({ - url: server.url, + await command.send({ fromEmail: 'toto@example.com', body: 'my super message', subject: 'my subject', @@ -58,16 +58,14 @@ describe('Test contact form', function () { await wait(1000) - await sendContactForm({ - url: server.url, + await command.send({ fromEmail: 'toto@example.com', body: 'my super message', subject: 'my subject', fromName: 'Super toto' }) - await sendContactForm({ - url: server.url, + await command.send({ fromEmail: 'toto@example.com', body: 'my super message', 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 () { await wait(1000) - await sendContactForm({ - url: server.url, + await command.send({ fromEmail: 'toto@example.com', fromName: 'Super toto', subject: 'my subject', -- cgit v1.2.3