]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tests/api/server/contact-form.ts
fcd7f3c4c07df47d75eab4c73aaed3c793010360
[github/Chocobozzz/PeerTube.git] / server / tests / api / server / contact-form.ts
1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3 import { expect } from 'chai'
4 import { MockSmtpServer } from '@server/tests/shared'
5 import { wait } from '@shared/core-utils'
6 import { HttpStatusCode } from '@shared/models'
7 import {
8 cleanupTests,
9 ContactFormCommand,
10 createSingleServer,
11 PeerTubeServer,
12 setAccessTokensToServers,
13 waitJobs
14 } from '@shared/server-commands'
15
16 describe('Test contact form', function () {
17 let server: PeerTubeServer
18 const emails: object[] = []
19 let command: ContactFormCommand
20
21 before(async function () {
22 this.timeout(30000)
23
24 const port = await MockSmtpServer.Instance.collectEmails(emails)
25
26 const overrideConfig = {
27 smtp: {
28 hostname: 'localhost',
29 port
30 }
31 }
32 server = await createSingleServer(1, overrideConfig)
33 await setAccessTokensToServers([ server ])
34
35 command = server.contactForm
36 })
37
38 it('Should send a contact form', async function () {
39 this.timeout(10000)
40
41 await command.send({
42 fromEmail: 'toto@example.com',
43 body: 'my super message',
44 subject: 'my subject',
45 fromName: 'Super toto'
46 })
47
48 await waitJobs(server)
49
50 expect(emails).to.have.lengthOf(1)
51
52 const email = emails[0]
53
54 expect(email['from'][0]['address']).equal('test-admin@localhost')
55 expect(email['replyTo'][0]['address']).equal('toto@example.com')
56 expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
57 expect(email['subject']).contains('my subject')
58 expect(email['text']).contains('my super message')
59 })
60
61 it('Should not have duplicated email address in text message', async function () {
62 const text = emails[0]['text'] as string
63
64 const matches = text.match(/toto@example.com/g)
65 expect(matches).to.have.lengthOf(1)
66 })
67
68 it('Should not be able to send another contact form because of the anti spam checker', async function () {
69 this.timeout(10000)
70
71 await wait(1000)
72
73 await command.send({
74 fromEmail: 'toto@example.com',
75 body: 'my super message',
76 subject: 'my subject',
77 fromName: 'Super toto'
78 })
79
80 await command.send({
81 fromEmail: 'toto@example.com',
82 body: 'my super message',
83 fromName: 'Super toto',
84 subject: 'my subject',
85 expectedStatus: HttpStatusCode.FORBIDDEN_403
86 })
87 })
88
89 it('Should be able to send another contact form after a while', async function () {
90 await wait(1000)
91
92 await command.send({
93 fromEmail: 'toto@example.com',
94 fromName: 'Super toto',
95 subject: 'my subject',
96 body: 'my super message'
97 })
98 })
99
100 it('Should not have the manage preferences link in the email', async function () {
101 const email = emails[0]
102 expect(email['text']).to.not.contain('Manage your notification preferences')
103 })
104
105 after(async function () {
106 MockSmtpServer.Instance.kill()
107
108 await cleanupTests([ server ])
109 })
110 })