All API tests in parallel
[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       fromName: 'Super toto'
47     })
48
49     await waitJobs(server)
50
51     expect(emails).to.have.lengthOf(1)
52
53     const email = emails[0]
54
55     expect(email['from'][0]['address']).equal('test-admin@localhost')
56     expect(email['from'][0]['name']).equal('toto@example.com')
57     expect(email['to'][0]['address']).equal('admin' + server.internalServerNumber + '@example.com')
58     expect(email['subject']).contains('Contact form')
59     expect(email['text']).contains('my super message')
60   })
61
62   it('Should not be able to send another contact form because of the anti spam checker', async function () {
63     await sendContactForm({
64       url: server.url,
65       fromEmail: 'toto@example.com',
66       body: 'my super message',
67       fromName: 'Super toto'
68     })
69
70     await sendContactForm({
71       url: server.url,
72       fromEmail: 'toto@example.com',
73       body: 'my super message',
74       fromName: 'Super toto',
75       expectedStatus: 403
76     })
77   })
78
79   it('Should be able to send another contact form after a while', async function () {
80     await wait(1000)
81
82     await sendContactForm({
83       url: server.url,
84       fromEmail: 'toto@example.com',
85       body: 'my super message',
86       fromName: 'Super toto'
87     })
88   })
89
90   after(async function () {
91     MockSmtpServer.Instance.kill()
92
93     await cleanupTests([ server ])
94   })
95 })