X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Ftests%2Fapi%2Fcheck-params%2Fcontact-form.ts;h=c7f9c1b476332aaa6e19b179e09c3bbe94758aec;hb=1896bca09e088b0da9d5e845407ecebae330618c;hp=c7e014b1ff04a90fc296e719522764656c928fd7;hpb=88108880bbdba473cfe36ecbebc1c3c4f972e102;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/tests/api/check-params/contact-form.ts b/server/tests/api/check-params/contact-form.ts index c7e014b1f..c7f9c1b47 100644 --- a/server/tests/api/check-params/contact-form.ts +++ b/server/tests/api/check-params/contact-form.ts @@ -1,24 +1,11 @@ -/* tslint:disable:no-unused-expression */ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ 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' +import { cleanupTests, flushAndRunServer, immutableAssign, killallServers, reRunServer, ServerInfo } from '../../../../shared/extra-utils' +import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form' +import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email' +import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes' describe('Test contact form API validators', function () { let server: ServerInfo @@ -26,23 +13,24 @@ describe('Test contact form API validators', function () { const defaultBody = { fromName: 'super name', fromEmail: 'toto@example.com', + subject: 'my subject', body: 'Hello, how are you?' } + let emailPort: number // --------------------------------------------------------------- before(async function () { this.timeout(60000) - await flushTests() - await MockSmtpServer.Instance.collectEmails(emails) + emailPort = await MockSmtpServer.Instance.collectEmails(emails) // Email is disabled - server = await runServer(1) + server = await flushAndRunServer(1) }) it('Should not accept a contact form if emails are disabled', async function () { - await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: 409 })) + await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) }) it('Should not accept a contact form if it is disabled in the configuration', async function () { @@ -51,8 +39,8 @@ describe('Test contact form API validators', 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 })) + await reRunServer(server, { smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } }) + await sendContactForm(immutableAssign(defaultBody, { url: server.url, expectedStatus: HttpStatusCode.CONFLICT_409 })) }) it('Should not accept a contact form if from email is invalid', async function () { @@ -61,23 +49,59 @@ describe('Test contact form API validators', 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 })) + 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 + })) }) 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 })) + 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 + })) }) 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 })) + 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 + })) }) it('Should accept a contact form with the correct parameters', async function () { @@ -86,11 +110,7 @@ describe('Test contact form API validators', function () { after(async function () { MockSmtpServer.Instance.kill() - killallServers([ server ]) - // Keep the logs if the test failed - if (this['ok']) { - await flushTests() - } + await cleanupTests([ server ]) }) })