<commit_before>/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { cleanupTests, createSingleServer, killallServers, MockSmtpServer, PeerTubeServer } from '@shared/extra-utils'
import { ContactFormCommand } from '@shared/extra-utils/server'
import { HttpStatusCode } from '@shared/models'

describe('Test contact form API validators', function () {
  let server: PeerTubeServer
  const emails: object[] = []
  const defaultBody = {
    fromName: '<NAME>',
    fromEmail: '<EMAIL>',
    subject: 'my subject',
    body: 'Hello, how are you?'
  }
  let emailPort: number
  let command: ContactFormCommand

  // ---------------------------------------------------------------

  before(async function () {
    this.timeout(60000)

    emailPort = await MockSmtpServer.Instance.collectEmails(emails)

    // Email is disabled
    server = await createSingleServer(1)
    command = server.contactForm
  })

  it('Should not accept a contact form if emails are disabled', async function () {
    await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
  })

  it('Should not accept a contact form if it is disabled in the configuration', async function () {
    this.timeout(10000)

    await killallServers([ server ])

    // Contact form is disabled
    await server.run({ smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
    await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
  })

  it('Should not accept a contact form if from email is invalid', async function () {
    this.timeout(10000)

    await killallServers([ server ])

    // Email & contact form enabled
    await server.run({ smtp: { hostname: 'localhost', port: emailPort } })

    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 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 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 command.send(defaultBody)
  })

  after(async function () {
    MockSmtpServer.Instance.kill()

    await cleanupTests([ server ])
  })
})
<commit_msg>Increase contact form tests timeout<commit_after>/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import 'mocha'
import { cleanupTests, createSingleServer, killallServers, MockSmtpServer, PeerTubeServer } from '@shared/extra-utils'
import { ContactFormCommand } from '@shared/extra-utils/server'
import { HttpStatusCode } from '@shared/models'

describe('Test contact form API validators', function () {
  let server: PeerTubeServer
  const emails: object[] = []
  const defaultBody = {
    fromName: '<NAME>',
    fromEmail: '<EMAIL>',
    subject: 'my subject',
    body: 'Hello, how are you?'
  }
  let emailPort: number
  let command: ContactFormCommand

  // ---------------------------------------------------------------

  before(async function () {
    this.timeout(60000)

    emailPort = await MockSmtpServer.Instance.collectEmails(emails)

    // Email is disabled
    server = await createSingleServer(1)
    command = server.contactForm
  })

  it('Should not accept a contact form if emails are disabled', async function () {
    await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
  })

  it('Should not accept a contact form if it is disabled in the configuration', async function () {
    this.timeout(25000)

    await killallServers([ server ])

    // Contact form is disabled
    await server.run({ smtp: { hostname: 'localhost', port: emailPort }, contact_form: { enabled: false } })
    await command.send({ ...defaultBody, expectedStatus: HttpStatusCode.CONFLICT_409 })
  })

  it('Should not accept a contact form if from email is invalid', async function () {
    this.timeout(25000)

    await killallServers([ server ])

    // Email & contact form enabled
    await server.run({ smtp: { hostname: 'localhost', port: emailPort } })

    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 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 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 command.send(defaultBody)
  })

  after(async function () {
    MockSmtpServer.Instance.kill()

    await cleanupTests([ server ])
  })
})

{'fraction_non_alphanumeric': 0.13886328725038402, 'fraction_numerical': 0.017204301075268817, 'mean_word_length': 3.833704528582034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4033639', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1809, 'n_words': 516}