Add auto follow back support for instances
[oweals/peertube.git] / server / tests / api / server / contact-form.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
5 import {
6   flushTests,
7   killallServers,
8   flushAndRunServer,
9   ServerInfo,
10   setAccessTokensToServers,
11   wait,
12   cleanupTests
13 } from '../../../../shared/extra-utils'
14 import { MockSmtpServer } from '../../../../shared/extra-utils/miscs/email'
15 import { waitJobs } from '../../../../shared/extra-utils/server/jobs'
16 import { sendContactForm } from '../../../../shared/extra-utils/server/contact-form'
17
18 const expect = chai.expect
19
20 describe('Test contact form', function () {
21   let server: ServerInfo
22   const emails: object[] = []
23
24   before(async function () {
25     this.timeout(30000)
26
27     const port = await MockSmtpServer.Instance.collectEmails(emails)
28
29     const overrideConfig = {
30       smtp: {
31         hostname: 'localhost',
32         port
33       }
34     }
35     server = await flushAndRunServer(1, overrideConfig)
36     await setAccessTokensToServers([ server ])
37   })
38
39   it('Should send a contact form', async function () {
40     this.timeout(10000)
41
42     await sendContactForm({
43       url: server.url,
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['from'][0]['name']).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 be able to send another contact form because of the anti spam checker', async function () {
64     await sendContactForm({
65       url: server.url,
66       fromEmail: 'toto@example.com',
67       body: 'my super message',
68       subject: 'my subject',
69       fromName: 'Super toto'
70     })
71
72     await sendContactForm({
73       url: server.url,
74       fromEmail: 'toto@example.com',
75       body: 'my super message',
76       fromName: 'Super toto',
77       subject: 'my subject',
78       expectedStatus: 403
79     })
80   })
81
82   it('Should be able to send another contact form after a while', async function () {
83     await wait(1000)
84
85     await sendContactForm({
86       url: server.url,
87       fromEmail: 'toto@example.com',
88       fromName: 'Super toto',
89       subject: 'my subject',
90       body: 'my super message'
91     })
92   })
93
94   after(async function () {
95     MockSmtpServer.Instance.kill()
96
97     await cleanupTests([ server ])
98   })
99 })