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