PeerTube/server/tests/api/server/contact-form.ts

114 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-01-31 09:56:52 -06:00
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2019-01-09 08:14:29 -06:00
import 'mocha'
2021-07-06 03:36:54 -05:00
import * as chai from 'chai'
import { MockSmtpServer } from '@server/tests/shared'
import { wait } from '@shared/core-utils'
import { HttpStatusCode } from '@shared/models'
2021-07-16 02:47:51 -05:00
import {
cleanupTests,
ContactFormCommand,
createSingleServer,
PeerTubeServer,
setAccessTokensToServers,
waitJobs
} from '@shared/server-commands'
2019-01-09 08:14:29 -06:00
const expect = chai.expect
describe('Test contact form', function () {
2021-07-16 02:47:51 -05:00
let server: PeerTubeServer
2019-01-09 08:14:29 -06:00
const emails: object[] = []
2021-07-06 08:53:25 -05:00
let command: ContactFormCommand
2019-01-09 08:14:29 -06:00
before(async function () {
this.timeout(30000)
const port = await MockSmtpServer.Instance.collectEmails(emails)
2019-01-09 08:14:29 -06:00
const overrideConfig = {
smtp: {
hostname: 'localhost',
port
2019-01-09 08:14:29 -06:00
}
}
2021-07-16 02:47:51 -05:00
server = await createSingleServer(1, overrideConfig)
2019-01-09 08:14:29 -06:00
await setAccessTokensToServers([ server ])
2021-07-06 08:53:25 -05:00
2021-07-16 02:04:35 -05:00
command = server.contactForm
2019-01-09 08:14:29 -06:00
})
it('Should send a contact form', async function () {
2019-01-10 04:51:25 -06:00
this.timeout(10000)
2021-07-06 08:53:25 -05:00
await command.send({
2019-01-09 08:14:29 -06:00
fromEmail: 'toto@example.com',
body: 'my super message',
subject: 'my subject',
2019-01-09 08:14:29 -06:00
fromName: 'Super toto'
})
await waitJobs(server)
expect(emails).to.have.lengthOf(1)
const email = emails[0]
2019-02-14 04:56:23 -06:00
expect(email['from'][0]['address']).equal('test-admin@localhost')
expect(email['replyTo'][0]['address']).equal('toto@example.com')
2019-04-26 01:50:52 -05:00
expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
expect(email['subject']).contains('my subject')
2019-01-09 08:14:29 -06:00
expect(email['text']).contains('my super message')
})
it('Should not have duplicated email address in text message', async function () {
const text = emails[0]['text'] as string
const matches = text.match(/toto@example.com/g)
expect(matches).to.have.lengthOf(1)
})
2019-01-09 08:14:29 -06:00
it('Should not be able to send another contact form because of the anti spam checker', async function () {
2021-06-14 09:52:22 -05:00
this.timeout(10000)
await wait(1000)
2021-07-06 08:53:25 -05:00
await command.send({
2019-01-09 08:14:29 -06:00
fromEmail: 'toto@example.com',
body: 'my super message',
subject: 'my subject',
2019-01-09 08:14:29 -06:00
fromName: 'Super toto'
})
2021-07-06 08:53:25 -05:00
await command.send({
2019-01-09 08:14:29 -06:00
fromEmail: 'toto@example.com',
body: 'my super message',
fromName: 'Super toto',
subject: 'my subject',
expectedStatus: HttpStatusCode.FORBIDDEN_403
2019-01-09 08:14:29 -06:00
})
})
it('Should be able to send another contact form after a while', async function () {
await wait(1000)
2021-07-06 08:53:25 -05:00
await command.send({
2019-01-09 08:14:29 -06:00
fromEmail: 'toto@example.com',
fromName: 'Super toto',
subject: 'my subject',
body: 'my super message'
2019-01-09 08:14:29 -06:00
})
})
it('Should not have the manage preferences link in the email', async function () {
const email = emails[0]
expect(email['text']).to.not.contain('Manage your notification preferences')
})
2019-04-24 08:10:37 -05:00
after(async function () {
2019-01-09 08:14:29 -06:00
MockSmtpServer.Instance.kill()
2019-04-24 08:10:37 -05:00
await cleanupTests([ server ])
2019-01-09 08:14:29 -06:00
})
})