Implement contact form on server side
[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 { flushTests, killallServers, runServer, ServerInfo, setAccessTokensToServers, wait } from '../../../../shared/utils'
6 import { MockSmtpServer } from '../../../../shared/utils/miscs/email'
7 import { waitJobs } from '../../../../shared/utils/server/jobs'
8 import { sendContactForm } from '../../../../shared/utils/server/contact-form'
9
10 const expect = chai.expect
11
12 describe('Test contact form', function () {
13   let server: ServerInfo
14   const emails: object[] = []
15
16   before(async function () {
17     this.timeout(30000)
18
19     await MockSmtpServer.Instance.collectEmails(emails)
20
21     await flushTests()
22
23     const overrideConfig = {
24       smtp: {
25         hostname: 'localhost'
26       }
27     }
28     server = await runServer(1, overrideConfig)
29     await setAccessTokensToServers([ server ])
30   })
31
32   it('Should send a contact form', async function () {
33     await sendContactForm({
34       url: server.url,
35       fromEmail: 'toto@example.com',
36       body: 'my super message',
37       fromName: 'Super toto'
38     })
39
40     await waitJobs(server)
41
42     expect(emails).to.have.lengthOf(1)
43
44     const email = emails[0]
45
46     expect(email['from'][0]['address']).equal('toto@example.com')
47     expect(email['to'][0]['address']).equal('admin1@example.com')
48     expect(email['subject']).contains('Contact form')
49     expect(email['text']).contains('my super message')
50   })
51
52   it('Should not be able to send another contact form because of the anti spam checker', async function () {
53     await sendContactForm({
54       url: server.url,
55       fromEmail: 'toto@example.com',
56       body: 'my super message',
57       fromName: 'Super toto'
58     })
59
60     await sendContactForm({
61       url: server.url,
62       fromEmail: 'toto@example.com',
63       body: 'my super message',
64       fromName: 'Super toto',
65       expectedStatus: 403
66     })
67   })
68
69   it('Should be able to send another contact form after a while', async function () {
70     await wait(1000)
71
72     await sendContactForm({
73       url: server.url,
74       fromEmail: 'toto@example.com',
75       body: 'my super message',
76       fromName: 'Super toto'
77     })
78   })
79
80   after(async function () {
81     MockSmtpServer.Instance.kill()
82     killallServers([ server ])
83   })
84 })